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-2017 ForgeRock AS.
15   */
16  
17  package org.forgerock.json.resource;
18  
19  import static org.forgerock.util.promise.Promises.*;
20  
21  import org.forgerock.services.context.Context;
22  import org.forgerock.util.promise.Promise;
23  
24  class InterfaceSingletonHandler implements RequestHandler {
25      private final SingletonResourceProvider provider;
26  
27      InterfaceSingletonHandler(final SingletonResourceProvider provider) {
28          this.provider = provider;
29      }
30  
31      @Override
32      public Promise<ActionResponse, ResourceException> handleAction(final Context context,
33              final ActionRequest request) {
34          return provider.actionInstance(context, request);
35      }
36  
37      @Override
38      public final Promise<ResourceResponse, ResourceException> handleCreate(final Context context,
39              final CreateRequest request) {
40          // TODO: i18n
41          return new CreateNotSupportedException(String.format("The singleton resource %s cannot be created",
42                  request.getResourcePath())).asPromise();
43      }
44  
45      @Override
46      public final Promise<ResourceResponse, ResourceException> handleDelete(final Context context,
47              final DeleteRequest request) {
48          // TODO: i18n
49          return newExceptionPromise(Resources.newBadRequestException(
50                  "The singleton resource %s cannot be deleted", request.getResourcePath()));
51      }
52  
53      @Override
54      public Promise<ResourceResponse, ResourceException> handlePatch(final Context context,
55              final PatchRequest request) {
56          return provider.patchInstance(context, request);
57      }
58  
59      @Override
60      public final Promise<QueryResponse, ResourceException> handleQuery(final Context context,
61              final QueryRequest request, final QueryResourceHandler handler) {
62          // TODO: i18n
63          return newExceptionPromise(Resources.newBadRequestException(
64                  "The singleton resource %s cannot be queried", request.getResourcePath()));
65      }
66  
67      @Override
68      public Promise<ResourceResponse, ResourceException> handleRead(final Context context,
69              final ReadRequest request) {
70          return provider.readInstance(context, request);
71      }
72  
73      @Override
74      public Promise<ResourceResponse, ResourceException> handleUpdate(final Context context,
75              final UpdateRequest request) {
76          return provider.updateInstance(context, request);
77      }
78  }