void doPost(final HttpServletRequest req, final HttpServletResponse resp) {
    try {
      // Parse out the required API versions.
      final AcceptAPIVersion acceptVersion = parseAcceptAPIVersion(req);

      // Prepare response.
      prepareResponse(req, resp);

      // Validate request.
      preprocessRequest(req);
      rejectIfNoneMatch(req);
      rejectIfMatch(req);

      final Map<String, String[]> parameters = req.getParameterMap();
      final String action = asSingleValue(PARAM_ACTION, getParameter(req, PARAM_ACTION));
      if (action.equalsIgnoreCase(ACTION_ID_CREATE)) {
        final JsonValue content = getJsonContent(req);
        final CreateRequest request = Requests.newCreateRequest(getResourceName(req), content);
        for (final Map.Entry<String, String[]> p : parameters.entrySet()) {
          final String name = p.getKey();
          final String[] values = p.getValue();
          if (parseCommonParameter(name, values, request)) {
            continue;
          } else if (name.equalsIgnoreCase(PARAM_ACTION)) {
            // Ignore - already handled.
          } else if (HttpUtils.isMultiPartRequest(req.getContentType())) {
            // Ignore - multipart content adds form parts to the parameter set
          } else {
            request.setAdditionalParameter(name, asSingleValue(name, values));
          }
        }
        doRequest(req, resp, acceptVersion, request);
      } else {
        // Action request.
        final JsonValue content = getJsonActionContent(req);
        final ActionRequest request =
            Requests.newActionRequest(getResourceName(req), action).setContent(content);
        for (final Map.Entry<String, String[]> p : parameters.entrySet()) {
          final String name = p.getKey();
          final String[] values = p.getValue();
          if (parseCommonParameter(name, values, request)) {
            continue;
          } else if (name.equalsIgnoreCase(PARAM_ACTION)) {
            // Ignore - already handled.
          } else if (HttpUtils.isMultiPartRequest(req.getContentType())) {
            // Ignore - multipart content adds form parts to the parameter set
          } else {
            request.setAdditionalParameter(name, asSingleValue(name, values));
          }
        }
        doRequest(req, resp, acceptVersion, request);
      }
    } catch (final Exception e) {
      fail(req, resp, e);
    }
  }
Exemplo n.º 2
0
  @Test
  public void actionCollectionShouldHandleUnsupportedAction() {

    // Given
    ServerContext context = mock(ServerContext.class);
    ActionRequest request = mock(ActionRequest.class);
    ResultHandler<JsonValue> handler = mock(ResultHandler.class);

    given(request.getAction()).willReturn("UNSUPPORTED_ACTION");

    // When
    resource.actionCollection(context, request, handler);

    // Then
    verify(handler).handleError(Matchers.<NotSupportedException>anyObject());
    verify(handler, never()).handleResult(Matchers.<JsonValue>anyObject());
  }
  /**
   * Execute a database command according to the details in the action request.
   *
   * @param request the ActionRequest
   * @return the number of affected rows/records.
   * @throws ResourceException on failure to resolved query
   */
  public Object command(ActionRequest request) throws ResourceException {

    ODatabaseDocumentTx db = getConnection();
    try {
      return commands.query(request.getResourcePath(), request, db);
    } finally {
      if (db != null) {
        db.close();
      }
    }
  }
Exemplo n.º 4
0
  @Test
  public void revokeAllUserPoliciesActionShouldHandleResourceException() {

    // Given
    ServerContext context = mock(ServerContext.class);
    ActionRequest request = mock(ActionRequest.class);
    ResultHandler<JsonValue> handler = mock(ResultHandler.class);

    given(contextHelper.getRealm(context)).willReturn("REALM");
    given(contextHelper.getUserId(context)).willReturn("RESOURCE_OWNER_ID");
    given(request.getAction()).willReturn("revokeAll");
    given(resourceSetService.revokeAllPolicies(context, "REALM", "RESOURCE_OWNER_ID"))
        .willReturn(Promises.<Void, ResourceException>newFailedPromise(new NotFoundException()));

    // When
    resource.actionCollection(context, request, handler);

    // Then
    verify(handler).handleError(Matchers.<ResourceException>anyObject());
    verify(handler, never()).handleResult(Matchers.<JsonValue>anyObject());
  }
 @Override
 public Promise<ActionResponse, ResourceException> handleAction(
     final Context context, final ActionRequest request) {
   try {
     Map<String, String> params = request.getAdditionalParameters();
     switch (request.getActionAsEnum(Action.class)) {
       case updateDbCredentials:
         String newUser = params.get("user");
         String newPassword = params.get("password");
         if (newUser == null || newPassword == null) {
           return adapt(new BadRequestException("Expecting 'user' and 'password' parameters"))
               .asPromise();
         }
         synchronized (dbLock) {
           DBHelper.updateDbCredentials(dbURL, user, password, newUser, newPassword);
           JsonValue config =
               connectionFactory
                   .getConnection()
                   .read(context, Requests.newReadRequest("config", PID))
                   .getContent();
           config.put("user", newUser);
           config.put("password", newPassword);
           UpdateRequest updateRequest = Requests.newUpdateRequest("config/" + PID, config);
           connectionFactory.getConnection().update(context, updateRequest);
           return newActionResponse(new JsonValue(params)).asPromise();
         }
       case command:
         return newActionResponse(new JsonValue(command(request))).asPromise();
       default:
         return adapt(new BadRequestException("Unknown action: " + request.getAction()))
             .asPromise();
     }
   } catch (IllegalArgumentException e) {
     return adapt(new BadRequestException("Unknown action: " + request.getAction())).asPromise();
   } catch (ResourceException e) {
     return e.asPromise();
   }
 }
Exemplo n.º 6
0
  @Test
  public void shouldRevokeAllUserPolicies() {

    // Given
    ServerContext context = mock(ServerContext.class);
    ActionRequest request = mock(ActionRequest.class);
    ResultHandler<JsonValue> handler = mock(ResultHandler.class);

    given(contextHelper.getRealm(context)).willReturn("REALM");
    given(contextHelper.getUserId(context)).willReturn("RESOURCE_OWNER_ID");
    given(request.getAction()).willReturn("revokeAll");
    given(resourceSetService.revokeAllPolicies(context, "REALM", "RESOURCE_OWNER_ID"))
        .willReturn(Promises.<Void, ResourceException>newSuccessfulPromise(null));

    // When
    resource.actionCollection(context, request, handler);

    // Then
    ArgumentCaptor<JsonValue> jsonCaptor = ArgumentCaptor.forClass(JsonValue.class);
    verify(handler).handleResult(jsonCaptor.capture());
    verify(handler, never()).handleError(Matchers.<ResourceException>anyObject());
    assertThat(jsonCaptor.getValue().asMap()).isEmpty();
  }
Exemplo n.º 7
0
  @Override
  public void handleAction(
      final Context context, final ActionRequest request, final Bindings handler)
      throws ResourceException {
    super.handleAction(context, request, handler);
    for (Map.Entry<String, String> entry : request.getAdditionalParameters().entrySet()) {
      if (handler.containsKey(entry.getKey())) {
        continue;
      }
      if (bindings != null) {
        bindings.put(entry.getKey(), entry.getValue());
      }
    }

    handler.put("request", request);
    handler.put("resources", configuration.get("resources").copy().getObject());
  }