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 2012-2016 ForgeRock AS.
15   */
16  
17  package org.forgerock.json.resource;
18  
19  import static org.forgerock.util.promise.Promises.newExceptionPromise;
20  import static org.forgerock.util.promise.Promises.newResultPromise;
21  
22  import java.util.Collection;
23  import java.util.LinkedList;
24  
25  import org.forgerock.services.context.Context;
26  import org.forgerock.util.promise.Promise;
27  
28  /**
29   * Implementation class for {@link Resources#asRequestHandler}.
30   */
31  class SynchronousRequestHandlerAdapter implements RequestHandler {
32      private final SynchronousRequestHandler syncHandler;
33  
34      SynchronousRequestHandlerAdapter(final SynchronousRequestHandler syncHandler) {
35          this.syncHandler = syncHandler;
36      }
37  
38      @Override
39      public Promise<ResourceResponse, ResourceException> handleUpdate(final Context context,
40              final UpdateRequest request) {
41          try {
42              return newResultPromise(syncHandler.handleUpdate(context, request));
43          } catch (final ResourceException e) {
44              return newExceptionPromise(e);
45          }
46      }
47  
48      @Override
49      public Promise<ResourceResponse, ResourceException> handleRead(final Context context,
50              final ReadRequest request) {
51          try {
52              return newResultPromise(syncHandler.handleRead(context, request));
53          } catch (final ResourceException e) {
54              return newExceptionPromise(e);
55          }
56      }
57  
58      @Override
59      public Promise<QueryResponse, ResourceException> handleQuery(final Context context,
60              final QueryRequest request,
61              final QueryResourceHandler handler) {
62          try {
63              final Collection<ResourceResponse> resources = new LinkedList<>();
64              final QueryResponse result = syncHandler.handleQuery(context, request, resources);
65              for (final ResourceResponse resource : resources) {
66                  handler.handleResource(resource);
67              }
68              return newResultPromise(result);
69          } catch (final ResourceException e) {
70              return newExceptionPromise(e);
71          }
72      }
73  
74      @Override
75      public Promise<ResourceResponse, ResourceException> handlePatch(final Context context,
76              final PatchRequest request) {
77          try {
78              return newResultPromise(syncHandler.handlePatch(context, request));
79          } catch (final ResourceException e) {
80              return newExceptionPromise(e);
81          }
82      }
83  
84      @Override
85      public Promise<ResourceResponse, ResourceException> handleDelete(final Context context,
86              final DeleteRequest request) {
87          try {
88              return newResultPromise(syncHandler.handleDelete(context, request));
89          } catch (final ResourceException e) {
90              return newExceptionPromise(e);
91          }
92      }
93  
94      @Override
95      public Promise<ResourceResponse, ResourceException> handleCreate(final Context context,
96              final CreateRequest request) {
97          try {
98              return newResultPromise(syncHandler.handleCreate(context, request));
99          } catch (final ResourceException e) {
100             return newExceptionPromise(e);
101         }
102     }
103 
104     @Override
105     public Promise<ActionResponse, ResourceException> handleAction(final Context context,
106             final ActionRequest request) {
107         try {
108             return newResultPromise(syncHandler.handleAction(context, request));
109         } catch (final ResourceException e) {
110             return newExceptionPromise(e);
111         }
112     }
113 }