View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2015-2016 ForgeRock AS.
15   */
16  
17  package org.forgerock.json.resource;
18  
19  import org.forgerock.api.annotations.CollectionProvider;
20  import org.forgerock.api.annotations.Create;
21  import org.forgerock.api.annotations.Query;
22  import org.forgerock.api.models.ApiDescription;
23  import org.forgerock.http.ApiProducer;
24  import org.forgerock.services.context.Context;
25  import org.forgerock.services.descriptor.Describable;
26  import org.forgerock.util.promise.Promise;
27  
28  /**
29   * Exposes an annotated POJO as collection methods {@link org.forgerock.json.resource.RequestHandler} by
30   * looking for annotated and/or conventionally-named methods (as per {@link CollectionProvider}).
31   * <p>
32   * This class will handle the requests to the collection-level endpoint, so only Create, Query and Action
33   * are implemented - the remaining methods delegate to the {@link InterfaceCollectionHandler} for
34   * reporting the erroneous request to the caller.
35   * {@see org.forgeock.json.resource.annotations}
36   */
37  class AnnotatedCollectionHandler extends InterfaceCollectionHandler implements Describable<ApiDescription, Request> {
38  
39      private final AnnotatedMethod createMethod;
40      private final AnnotatedMethod queryMethod;
41      private final AnnotatedActionMethods actionMethods;
42      private final Describable<ApiDescription, Request> describable;
43  
44      public AnnotatedCollectionHandler(Object requestHandler) {
45          super(null);
46          if (!requestHandler.getClass().isAnnotationPresent(CollectionProvider.class)) {
47              throw new IllegalArgumentException("CollectionProvider missing from class: "
48                      + requestHandler.getClass().getName());
49          }
50          this.createMethod = AnnotatedMethod.findMethod(requestHandler, Create.class, false);
51          this.queryMethod = AnnotatedMethod.findMethod(requestHandler, Query.class, false);
52          this.actionMethods = AnnotatedActionMethods.findAll(requestHandler, false);
53          this.describable = requestHandler instanceof Describable
54                  ? (Describable<ApiDescription, Request>) requestHandler
55                  : null;
56      }
57  
58      @Override
59      public Promise<ResourceResponse, ResourceException> handleCreate(Context context, CreateRequest request) {
60          return createMethod.invoke(context, request);
61      }
62  
63      @Override
64      public Promise<QueryResponse, ResourceException> handleQuery(Context context, QueryRequest request,
65              QueryResourceHandler handler) {
66          return queryMethod.invoke(context, request, handler);
67      }
68  
69      @Override
70      public Promise<ActionResponse, ResourceException> handleAction(Context context, ActionRequest request) {
71          return actionMethods.invoke(context, request);
72      }
73  
74      @Override
75      public ApiDescription api(ApiProducer<ApiDescription> producer) {
76          if (describable == null) {
77              throw new UnsupportedOperationException(
78                      "The provided request handler does not support API Descriptor methods");
79          }
80          return describable.api(producer);
81      }
82  
83      @Override
84      public ApiDescription handleApiRequest(Context context, Request request) {
85          if (describable == null) {
86              throw new UnsupportedOperationException(
87                      "The provided request handler does not support API Descriptor methods");
88          }
89          return describable.handleApiRequest(context, request);
90      }
91  
92      @Override
93      public void addDescriptorListener(Describable.Listener listener) {
94          if (describable != null) {
95              describable.addDescriptorListener(listener);
96          }
97      }
98  
99      @Override
100     public void removeDescriptorListener(Describable.Listener listener) {
101         if (describable != null) {
102             describable.removeDescriptorListener(listener);
103         }
104     }
105 }