Example #1
0
  @POST
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  public JSONWithPadding createUser(
      @Context UriInfo ui,
      @FormParam("username") String username,
      @FormParam("name") String name,
      @FormParam("email") String email,
      @FormParam("password") String password,
      @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    logger.info("Create user: "******"create user");

    UserInfo user = management.createAdminUser(username, name, email, password, false, false);
    Map<String, Object> result = new LinkedHashMap<String, Object>();
    if (user != null) {
      result.put("user", user);
      response.setData(result);
      response.setSuccess();
    } else {
      throw mappableSecurityException(AuthErrorInfo.BAD_CREDENTIALS_SYNTAX_ERROR);
    }

    return new JSONWithPadding(response, callback);
  }
  public Response toResponse(int status, E e) {

    if (status >= 500) {
      // only log real errors as errors
      logger.error(e.getClass().getCanonicalName() + " 5XX Uncaught Exception (" + status + ")", e);

    } else {
      if (logger.isDebugEnabled()) {
        logger.debug(e.getClass().getCanonicalName() + " Uncaught Exception (" + status + ")", e);
      }
    }

    ApiResponse response = new ApiResponse();

    AuthErrorInfo authError = AuthErrorInfo.getForException(e);

    if (authError != null) {
      response.setError(authError.getType(), authError.getMessage(), e);
    } else {
      response.setError(e);
    }

    String jsonResponse = mapToJsonString(response);

    return toResponse(status, jsonResponse);
  }
  @RequireOrganizationAccess
  @GET
  @JSONP
  @Produces({MediaType.APPLICATION_JSON, "application/javascript"})
  public ApiResponse getApplication(
      @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    ApiResponse response = createApiResponse();
    ServiceManager sm = smf.getServiceManager(applicationId);
    response.setAction("get");
    response.setApplication(sm.getApplication());
    response.setParams(ui.getQueryParameters());
    response.setResults(management.getApplicationMetadata(applicationId));
    return response;
  }
 @RequireOrganizationAccess
 @GET
 @JSONP
 @Path("collections/_size")
 public ApiResponse getEachCollectionSize(
     @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
     throws Exception {
   ApiResponse response = createApiResponse();
   response.setAction("get collection size for all entities");
   Map<String, Long> sizes = management.getEachCollectionSize(this.applicationId);
   Map<String, Object> map = new HashMap<>();
   Map<String, Object> sumMap = new HashMap<>();
   sumMap.put("size", sizes);
   map.put("aggregation", sumMap);
   response.setMetadata(map);
   return response;
 }
  /**
   * Return queue depth of this Usergrid instance in JSON format.
   *
   * <p>By Default this end-point will ignore errors but if you call it with ignore_status=false
   * then it will return HTTP 500 if either the Entity store or the Index for the management
   * application are in a bad state.
   */
  @GET
  @RequireSystemAccess
  @Path("size")
  public ApiResponse getQueueDepth(
      @QueryParam("callback") @DefaultValue("callback") String callback) {

    ApiResponse response = createApiResponse();
    response.setAction("get queue depth");

    AsyncEventService eventService = injector.getInstance(AsyncEventService.class);
    ObjectNode node = JsonNodeFactory.instance.objectNode();

    node.put("queueDepth", eventService.getQueueDepth());

    response.setProperty("data", node);

    return response;
  }
  @DELETE
  @RequireOrganizationAccess
  @JSONP
  @Produces({MediaType.APPLICATION_JSON, "application/javascript"})
  public ApiResponse executeDelete(
      @Context UriInfo ui,
      @QueryParam("callback") @DefaultValue("callback") String callback,
      @QueryParam("app_delete_confirm") String confirmDelete)
      throws Exception {

    if (!"confirm_delete_of_application_and_data".equals(confirmDelete)) {
      throw new IllegalArgumentException(
          "Cannot delete application without app_delete_confirm parameter");
    }

    Properties props = management.getProperties();

    // for now, only works in test mode
    String testProp = (String) props.get("usergrid.test");
    if (testProp == null || !Boolean.parseBoolean(testProp)) {
      throw new UnsupportedOperationException();
    }

    if (applicationId == null) {
      throw new IllegalArgumentException("Application ID not specified in request");
    }

    management.deleteApplication(applicationId);

    if (logger.isDebugEnabled()) {
      logger.debug("ApplicationResource.delete() deleted appId = {}", applicationId);
    }

    ApiResponse response = createApiResponse();
    response.setAction("delete");
    response.setApplication(emf.getEntityManager(applicationId).getApplication());
    response.setParams(ui.getQueryParameters());

    if (logger.isDebugEnabled()) {
      logger.debug("ApplicationResource.delete() sending response ");
    }

    return response;
  }
  @RequireOrganizationAccess
  @POST
  @Path("credentials")
  @JSONP
  @Produces({MediaType.APPLICATION_JSON, "application/javascript"})
  public ApiResponse generateCredentials(
      @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    ApiResponse response = createApiResponse();
    response.setAction("generate application client credentials");

    ClientCredentialsInfo credentials =
        new ClientCredentialsInfo(
            management.getClientIdForApplication(applicationId),
            management.newClientSecretForApplication(applicationId));

    response.setCredentials(credentials);
    return response;
  }
  /** Put on application URL will restore application if it was deleted. */
  @PUT
  @RequireOrganizationAccess
  @JSONP
  @Produces({MediaType.APPLICATION_JSON, "application/javascript"})
  public ApiResponse executePut(
      @Context UriInfo ui,
      String body,
      @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    if (applicationId == null) {
      throw new IllegalArgumentException("Application ID not specified in request");
    }

    management.restoreApplication(applicationId);

    ApiResponse response = createApiResponse();
    response.setAction("restore");
    response.setApplication(emf.getEntityManager(applicationId).getApplication());
    response.setParams(ui.getQueryParameters());

    return response;
  }
  @POST
  @Path("sia-provider")
  @Consumes(APPLICATION_JSON)
  @RequireOrganizationAccess
  @JSONP
  @Produces({MediaType.APPLICATION_JSON, "application/javascript"})
  public ApiResponse configureProvider(
      @Context UriInfo ui,
      @QueryParam("provider_key") String siaProvider,
      Map<String, Object> json,
      @QueryParam("callback") @DefaultValue("") String callback)
      throws Exception {

    ApiResponse response = createApiResponse();
    response.setAction("post signin provider configuration");

    Preconditions.checkArgument(siaProvider != null, "Sign in provider required");

    SignInAsProvider signInAsProvider = null;
    if (StringUtils.equalsIgnoreCase(siaProvider, "facebook")) {
      signInAsProvider =
          signInProviderFactory.facebook(smf.getServiceManager(applicationId).getApplication());
    } else if (StringUtils.equalsIgnoreCase(siaProvider, "pingident")) {
      signInAsProvider =
          signInProviderFactory.pingident(smf.getServiceManager(applicationId).getApplication());
    } else if (StringUtils.equalsIgnoreCase(siaProvider, "foursquare")) {
      signInAsProvider =
          signInProviderFactory.foursquare(smf.getServiceManager(applicationId).getApplication());
    }

    Preconditions.checkArgument(
        signInAsProvider != null, "No signin provider found by that name: " + siaProvider);

    signInAsProvider.saveToConfiguration(json);

    return response;
  }