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-2015 ForgeRock AS.
15   */
16  
17  package org.forgerock.json.resource;
18  
19  import java.util.Collection;
20  
21  import org.forgerock.services.context.Context;
22  
23  /**
24   * An abstract connection whose synchronous methods are implemented in terms of
25   * asynchronous methods.
26   */
27  public abstract class AbstractAsynchronousConnection implements Connection {
28  
29      /**
30       * Creates a new abstract asynchronous connection.
31       */
32      protected AbstractAsynchronousConnection() {
33          // No implementation required.
34      }
35  
36      @Override
37      public ActionResponse action(final Context context, final ActionRequest request)
38              throws ResourceException {
39          try {
40              return actionAsync(context, request).getOrThrow();
41          } catch (InterruptedException e) {
42              throw newTimeoutException(e);
43          }
44      }
45  
46      @Override
47      public ResourceResponse create(final Context context, final CreateRequest request)
48              throws ResourceException {
49          try {
50              return createAsync(context, request).getOrThrow();
51          } catch (InterruptedException e) {
52              throw newTimeoutException(e);
53          }
54      }
55  
56      @Override
57      public ResourceResponse delete(final Context context, final DeleteRequest request)
58              throws ResourceException {
59          try {
60              return deleteAsync(context, request).getOrThrow();
61          } catch (InterruptedException e) {
62              throw newTimeoutException(e);
63          }
64      }
65  
66      @Override
67      public ResourceResponse patch(final Context context, final PatchRequest request)
68              throws ResourceException {
69          try {
70              return patchAsync(context, request).getOrThrow();
71          } catch (InterruptedException e) {
72              throw newTimeoutException(e);
73          }
74      }
75  
76      @Override
77      public QueryResponse query(final Context context, final QueryRequest request,
78              final QueryResourceHandler handler) throws ResourceException {
79          try {
80              return queryAsync(context, request, handler).getOrThrow();
81          } catch (InterruptedException e) {
82              throw newTimeoutException(e);
83          }
84      }
85  
86      @Override
87      public QueryResponse query(final Context context, final QueryRequest request,
88              final Collection<? super ResourceResponse> results) throws ResourceException {
89          return query(context, request, new QueryResourceHandler() {
90              @Override
91              public boolean handleResource(final ResourceResponse resource) {
92                  results.add(resource);
93                  return true;
94              }
95          });
96      }
97  
98      @Override
99      public ResourceResponse read(final Context context, final ReadRequest request) throws ResourceException {
100         try {
101             return readAsync(context, request).getOrThrow();
102         } catch (InterruptedException e) {
103             throw newTimeoutException(e);
104         }
105     }
106 
107     @Override
108     public ResourceResponse update(final Context context, final UpdateRequest request)
109             throws ResourceException {
110         try {
111             return updateAsync(context, request).getOrThrow();
112         } catch (InterruptedException e) {
113             throw newTimeoutException(e);
114         }
115     }
116 
117     private ResourceException newTimeoutException(Exception cause) {
118         return ResourceException.newResourceException(503, "Request was interrupted", cause);
119     }
120 }