@RequireOrganizationAccess
  @POST
  @Path("credentials")
  public JSONWithPadding generateCredentials(
      @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

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

    ClientCredentialsInfo credentials =
        new ClientCredentialsInfo(
            management.getClientIdForOrganization(organization.getUuid()),
            management.newClientSecretForOrganization(organization.getUuid()));

    response.setCredentials(credentials);
    return new JSONWithPadding(response, callback);
  }
  @GET
  public JSONWithPadding getOrganizationDetails(
      @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    logger.info("Get details for organization: " + organization.getUuid());

    ApiResponse response = createApiResponse();
    response.setProperty("organization", management.getOrganizationData(organization));

    return new JSONWithPadding(response, callback);
  }
  @GET
  @Path("reactivate")
  public JSONWithPadding reactivate(
      @Context UriInfo ui, @QueryParam("callback") @DefaultValue("callback") String callback)
      throws Exception {

    logger.info("Send activation email for organization: " + organization.getUuid());

    ApiResponse response = createApiResponse();

    management.startOrganizationActivationFlow(organization);

    response.setAction("reactivate organization");
    return new JSONWithPadding(response, callback);
  }
  @GET
  @Path("activate")
  @Produces(MediaType.TEXT_HTML)
  public Viewable activate(@Context UriInfo ui, @QueryParam("token") String token) {

    try {
      management.handleActivationTokenForOrganization(organization.getUuid(), token);
      return handleViewable("activate", this);
    } catch (TokenException e) {
      return handleViewable("bad_activation_token", this);
    } catch (RedirectionException e) {
      throw e;
    } catch (Exception e) {
      return handleViewable("error", e);
    }
  }
  @GET
  @Path("confirm")
  @Produces(MediaType.TEXT_HTML)
  public Viewable confirm(@Context UriInfo ui, @QueryParam("token") String token) {

    try {
      ActivationState state =
          management.handleActivationTokenForOrganization(organization.getUuid(), token);
      if (state == ActivationState.CONFIRMED_AWAITING_ACTIVATION) {
        return handleViewable("confirm", this);
      }
      return handleViewable("activate", this);
    } catch (TokenException e) {
      return handleViewable("bad_activation_token", this);
    } catch (RedirectionException e) {
      throw e;
    } catch (Exception e) {
      return handleViewable("error", e);
    }
  }
  @Override
  public void runTool(CommandLine line) throws Exception {
    logger.info("Starting test...");
    startSpring();

    UserInfo user =
        managementService.createAdminUser(
            "admin", "admin", "*****@*****.**", "none", false, false, false);

    logger.info("Creating organization: sample-organization");
    // management
    // .createOrganization("sample-organization", "*****@*****.**", "1234");
    OrganizationInfo organization =
        managementService.createOrganization("sample-organization", user);

    logger.info("creating application: testEntityManagerTest");
    UUID applicationId =
        managementService.createApplication(organization.getUuid(), "sample-application");

    ServiceManager sm = smf.getServiceManager(applicationId);

    EntityManager em = emf.getEntityManager(applicationId);

    // Create user
    Map<String, Object> properties = new LinkedHashMap<String, Object>();
    properties.put("username", "edanuff");
    properties.put("email", "*****@*****.**");
    properties.put("name", "Ed Anuff");

    Entity user1 = em.create("user", properties);

    // Create activity
    properties =
        Activity.newActivity(
                Activity.VERB_POST, null, "I ate a sammich", null, user1, null, "tweet", null, null)
            .getProperties();

    @SuppressWarnings("unused")
    Entity activity =
        testRequest(sm, ServiceAction.POST, 1, properties, "users", user1.getUuid(), "activities")
            .getEntity();

    // Create another activity
    properties =
        Activity.newActivity(
                Activity.VERB_POST, null, "cool pic dude", null, user1, null, "tweet", null, null)
            .getProperties();

    activity =
        testRequest(sm, ServiceAction.POST, 1, properties, "users", user1.getUuid(), "activities")
            .getEntity();

    // Create another user
    properties = new LinkedHashMap<String, Object>();
    properties.put("username", "justin");
    properties.put("email", "*****@*****.**");
    properties.put("name", "Justin Clark");

    Entity user2 = em.create("user", properties);

    // Create activity
    properties =
        Activity.newActivity(
                Activity.VERB_POST,
                null,
                "ATT U-verse May payment",
                null,
                user2,
                null,
                "payment",
                null,
                null)
            .getProperties();

    activity =
        testRequest(sm, ServiceAction.POST, 1, properties, "users", user2.getUuid(), "activities")
            .getEntity();

    // Connections
    em.createConnection(user1, "workWith", user2);
  }