Ejemplo n.º 1
0
  /**
   * Deletes all of a user's sessions. Requires authentication by the session owner or a ROLE_ADMIN
   * user.
   *
   * @param sessionHeader The authentication information of the caller.
   * @param username The username of the session owner.
   * @return 200 OK.
   */
  @DELETE
  @Produces(MediaType.APPLICATION_JSON)
  public Response deleteAllSessions(
      @HeaderParam(UserSession.HEADER) String sessionHeader, @PathParam("username") String username)
      throws ApiException {

    SQLConnection sql = SQLConnection.connectDefault();

    try {
      UserSession session = UserSession.resume(sql, sessionHeader);

      if (!session.getUser().getUsername().equals(username)
          && !session.getUser().isRole("ROLE_ADMIN")) {
        throw new ApiException(ApiStatus.ACCESS_DENIED);
      }

      UserSession.endAll(sql, username);
    } finally {
      sql.close();
    }

    // TODO: we should probably return the sessions that were killed.
    ApiResponse response = new ApiResponse(ApiStatus.OK);
    return Response.ok(response.serialize()).build();
  }
 public static <T> ApiResponse<T> NotOk(String errorMessage) {
   ApiResponse<T> response = new ApiResponse<>();
   response.success = false;
   response.data = null;
   response.errorMessage = errorMessage;
   return response;
 }
Ejemplo n.º 3
0
  @RequireSystemAccess
  @POST
  @Path(RootResource.APPLICATION_ID_PATH)
  @JSONP
  @Produces({MediaType.APPLICATION_JSON, "application/javascript"})
  public ApiResponse addIndex(
      @Context UriInfo ui,
      @PathParam("applicationId") final String applicationIdStr,
      Map<String, Object> config,
      @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    Preconditions.checkNotNull(
        config, "Payload for config is null, please pass {replicas:int, shards:int} in body");

    ApiResponse response = createApiResponse();

    if (!config.containsKey("replicas")
        || !config.containsKey("shards")
        || !(config.get("replicas") instanceof Integer)
        || !(config.get("shards") instanceof Integer)) {
      throw new IllegalArgumentException(
          "body must contains 'replicas' of type int and 'shards' of type int");
    }

    if (!config.containsKey("indexSuffix")) {
      throw new IllegalArgumentException("Please add an indexSuffix to your post");
    }
    final UUID appId = UUIDUtils.tryExtractUUID(applicationIdStr);

    if (appId == null) {
      throw new IllegalArgumentException("app id was not parsed");
    }

    EntityManager em = emf.getEntityManager(appId);
    em.addIndex(
        config.get("indexSuffix").toString(),
        (int) config.get("shards"),
        (int) config.get("replicas"),
        (String) config.get("writeConsistency"));
    response.setAction("Add index to alias");

    return response;
  }
Ejemplo n.º 4
0
  @RequireSystemAccess
  @GET
  @Path("rebuild/{jobId}")
  @JSONP
  @Produces({MediaType.APPLICATION_JSON, "application/javascript"})
  public ApiResponse rebuildIndexesGet(
      @PathParam("jobId") String jobId,
      @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {
    logger.info("Getting status for index jobs");

    Preconditions.checkNotNull(jobId, "query param jobId must not be null");

    ReIndexService.ReIndexStatus status = getReIndexService().getStatus(jobId);

    final ApiResponse response = createApiResponse();

    response.setAction("rebuild indexes");
    response.setProperty("jobId", status.getJobId());
    response.setProperty("status", status.getStatus());
    response.setProperty("lastUpdatedEpoch", status.getLastUpdated());
    response.setProperty("numberQueued", status.getNumberProcessed());
    response.setSuccess();

    return response;
  }
Ejemplo n.º 5
0
  /** Execute the request and return the response. */
  private ApiResponse executeAndCreateResponse(
      final ReIndexRequestBuilder request, final String callback) {

    final ReIndexService.ReIndexStatus status = getReIndexService().rebuildIndex(request);

    final ApiResponse response = createApiResponse();

    response.setAction("rebuild indexes");
    response.setProperty("jobId", status.getJobId());
    response.setProperty("status", status.getStatus());
    response.setProperty("lastUpdatedEpoch", status.getLastUpdated());
    response.setProperty("numberQueued", status.getNumberProcessed());
    response.setSuccess();

    return response;
  }
 /*
  * Returns successful API call result.
  * */
 public static <T> ApiResponse<T> Ok(T data) {
   ApiResponse<T> response = new ApiResponse<>();
   response.success = true;
   response.data = data;
   return response;
 }