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.Delete;
21  import org.forgerock.api.annotations.Patch;
22  import org.forgerock.api.annotations.Read;
23  import org.forgerock.api.annotations.Update;
24  import org.forgerock.services.context.Context;
25  import org.forgerock.util.promise.Promise;
26  
27  /**
28   * Exposes an annotated POJO as collection instance methods {@link org.forgerock.json.resource.RequestHandler} by
29   * looking for annotated and/or conventionally-named methods (as per {@link CollectionProvider}).
30   * <p>
31   * This class will handle the requests to the collection's instance-level endpoint, so only Read, Update, Delete,
32   * Patch and Action are implemented - the remaining methods delegate to the {@link InterfaceCollectionInstance} for
33   * reporting the erroneous request to the caller.
34   * {@see org.forgeock.json.resource.annotations}
35   */
36  class AnnotationCollectionInstance extends InterfaceCollectionInstance {
37  
38      private final AnnotatedMethod readMethod;
39      private final AnnotatedMethod updateMethod;
40      private final AnnotatedMethod deleteMethod;
41      private final AnnotatedMethod patchMethod;
42      private final AnnotatedActionMethods actionMethods;
43  
44      AnnotationCollectionInstance(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.readMethod = AnnotatedMethod.findMethod(requestHandler, Read.class, true);
51          this.updateMethod = AnnotatedMethod.findMethod(requestHandler, Update.class, true);
52          this.deleteMethod = AnnotatedMethod.findMethod(requestHandler, Delete.class, true);
53          this.patchMethod = AnnotatedMethod.findMethod(requestHandler, Patch.class, true);
54          this.actionMethods = AnnotatedActionMethods.findAll(requestHandler, true);
55      }
56  
57      @Override
58      public Promise<ResourceResponse, ResourceException> handleRead(Context context, ReadRequest request) {
59          return readMethod.invoke(context, request, Resources.idOf(context));
60      }
61  
62      @Override
63      public Promise<ResourceResponse, ResourceException> handleUpdate(Context context, UpdateRequest request) {
64          return updateMethod.invoke(context, request, Resources.idOf(context));
65      }
66  
67      @Override
68      public Promise<ResourceResponse, ResourceException> handleDelete(Context context, DeleteRequest request) {
69          return deleteMethod.invoke(context, request, Resources.idOf(context));
70      }
71  
72      @Override
73      public Promise<ResourceResponse, ResourceException> handlePatch(Context context, PatchRequest request) {
74          return patchMethod.invoke(context, request, Resources.idOf(context));
75      }
76  
77      @Override
78      public Promise<ActionResponse, ResourceException> handleAction(Context context, ActionRequest request) {
79          return actionMethods.invoke(context, request, Resources.idOf(context));
80      }
81  }