@Override
  public User provideUserByScreenName(String screenName) throws Exception {
    // Add AuthCache to the execution context
    BasicHttpContext ctx = new BasicHttpContext();
    ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);

    HttpPost post = new HttpPost("/api/secure/jsonws/user/get-user-by-screen-name");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("companyId", companyId));
    params.add(new BasicNameValuePair("screenName", screenName));

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);

    HttpResponse resp = httpclient.execute(targetHost, post, ctx);
    System.out.println("provideUserByScreenName Status:[" + resp.getStatusLine() + "]");

    String response = null;
    if (resp.getEntity() != null) {
      response = EntityUtils.toString(resp.getEntity());
    }

    User user = null;
    if (!StringUtil.contains(response, "NoSuch", "")
        || !StringUtil.contains(response, "Exception", "")) {
      JSONDeserializer<User> deserializer = new JSONDeserializer<User>();
      user = deserializer.deserialize(response, User.class);
    }

    EntityUtils.consume(resp.getEntity());

    return user;
  }
  private Role getRoleByName(String name) throws ParseException, IOException {
    // Add AuthCache to the execution context
    BasicHttpContext ctx = new BasicHttpContext();
    ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);

    HttpPost post = new HttpPost("/api/secure/jsonws//role/get-role");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("companyId", companyId));
    params.add(new BasicNameValuePair("name", name));

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);

    HttpResponse resp = httpclient.execute(targetHost, post, ctx);
    System.out.println("getRoleById Status:[" + resp.getStatusLine() + "]");

    String response = null;
    if (resp.getEntity() != null) {
      response = EntityUtils.toString(resp.getEntity());
    }

    Role role = null;
    if (!StringUtil.contains(response, "NoSuch", "")
        && !StringUtil.contains(response, "exception", "")) {
      JSONDeserializer<Role> deserializer = new JSONDeserializer<Role>();
      role = deserializer.deserialize(response, Role.class);
    }

    EntityUtils.consume(resp.getEntity());

    return role;
  }
  public String getOrganizationIdByName(String organizationName) throws Exception {
    // Add AuthCache to the execution context
    BasicHttpContext ctx = new BasicHttpContext();
    ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);

    HttpPost post =
        new HttpPost(
            "/com.conx.bi.portal.liferay.common-portlet/api/secure/jsonws/conxbiorganizationutils/get-organization-id");
    List<NameValuePair> params = new ArrayList<NameValuePair>();

    params.add(new BasicNameValuePair("companyId", companyId));
    params.add(new BasicNameValuePair("name", organizationName));

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);

    HttpResponse resp = httpclient.execute(targetHost, post, ctx);
    System.out.println("getOrganizationByName Status:[" + resp.getStatusLine() + "]");

    String response = null;
    if (resp.getEntity() != null) {
      response = EntityUtils.toString(resp.getEntity());
    }

    String id = null;
    System.out.println("getOrganizationByName Resp:[" + response + "]");
    if (!StringUtil.contains(response, "Exception", "") && Validator.isNumber(response)) {
      if (!Validator.isNull(Long.valueOf(response))) id = response;
    }

    EntityUtils.consume(resp.getEntity());

    return id;
  }
  @Override
  public String generateUnencryptedTemporaryPassword(String userEmailAddress) throws Exception {
    // Add AuthCache to the execution context
    BasicHttpContext ctx = new BasicHttpContext();
    ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);
    HttpPost post =
        new HttpPost(
            "/com.conx.bi.portal.liferay.common-portlet/api/secure/jsonws/conxbiregistration/generate-unencrypted-temporary-password");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("companyId", companyId));
    params.add(new BasicNameValuePair("userEmailAddress", userEmailAddress));

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);

    HttpResponse resp = httpclient.execute(targetHost, post, ctx);
    System.out.println(
        "generateUnencryptedTemporaryPassword Status:[" + resp.getStatusLine() + "]");

    String response = null;
    if (resp.getEntity() != null) {
      response = EntityUtils.toString(resp.getEntity());
    }
    System.out.println("generateUnencryptedTemporaryPassword Status:[" + response + "]");

    String upwd = null;
    if (!StringUtil.contains(response, "Exception", "")) {
      JSONDeserializer<String> deserializer = new JSONDeserializer<String>();
      upwd = deserializer.deserialize(response, String.class);
    }

    EntityUtils.consume(resp.getEntity());

    return upwd;
  }
  @Override
  public User updatePassword(Long userId, String password1, String password2, Boolean passwordReset)
      throws Exception {
    // Add AuthCache to the execution context
    BasicHttpContext ctx = new BasicHttpContext();
    ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);

    HttpPost post = new HttpPost("/api/secure/jsonws/user/update-password");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("userId", Long.toString(userId)));
    params.add(new BasicNameValuePair("password1", password1));
    params.add(new BasicNameValuePair("password2", password2));
    params.add(new BasicNameValuePair("passwordReset", Boolean.toString(passwordReset)));

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);

    HttpResponse resp = httpclient.execute(targetHost, post, ctx);
    System.out.println("updatePassword Status:[" + resp.getStatusLine() + "]");

    String response = null;
    if (resp.getEntity() != null) {
      response = EntityUtils.toString(resp.getEntity());
    }

    User user = null;
    if (!StringUtil.contains(response, "NoSuch", "")
        || !StringUtil.contains(response, "Exception", "")) {
      JSONDeserializer<User> deserializer = new JSONDeserializer<User>();
      user = deserializer.deserialize(response, User.class);
    }

    EntityUtils.consume(resp.getEntity());

    return user;
  }
  public Organization addOrganization(String organizationName, String parentPortalOrganizationId)
      throws Exception {
    String type = "regular-organization";

    BasicHttpContext ctx = new BasicHttpContext();
    ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);

    HttpPost post = new HttpPost("/api/secure/jsonws/organization/add-organization");
    List<NameValuePair> params = new ArrayList<NameValuePair>();

    params.add(new BasicNameValuePair("companyId", companyId));
    params.add(new BasicNameValuePair("parentOrganizationId", parentPortalOrganizationId));
    params.add(new BasicNameValuePair("name", organizationName));
    params.add(new BasicNameValuePair("type", type));
    params.add(new BasicNameValuePair("recursable", "false"));
    params.add(new BasicNameValuePair("regionId", "0"));
    params.add(new BasicNameValuePair("countryId", "0"));
    params.add(new BasicNameValuePair("statusId", "12017"));
    params.add(new BasicNameValuePair("comments", null));
    params.add(new BasicNameValuePair("site", "false"));
    params.add(new BasicNameValuePair("serviceContext", null));

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);

    HttpResponse resp = httpclient.execute(targetHost, post, ctx);
    System.out.println("addOrganization Status:[" + resp.getStatusLine() + "]");

    String response = null;
    if (resp.getEntity() != null) {
      response = EntityUtils.toString(resp.getEntity());
    }

    System.out.println("addOrganization Resp:[" + response + "]");
    Organization org = null;
    if (!StringUtil.contains(response, "Exception", "")) {
      JSONDeserializer<Organization> deserializer = new JSONDeserializer<Organization>();
      org = deserializer.deserialize(response, Organization.class);
    }

    EntityUtils.consume(resp.getEntity());

    return org;
  }
  private User addUser(String firstName, String lastName, String emailAddress) throws Exception {
    // Add AuthCache to the execution context
    BasicHttpContext ctx = new BasicHttpContext();
    ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);

    HttpPost post = new HttpPost("/api/secure/jsonws/user/add-user");
    List<NameValuePair> params = new ArrayList<NameValuePair>();

    params.add(new BasicNameValuePair("companyId", companyId));
    params.add(new BasicNameValuePair("autoPassword", "true"));
    params.add(new BasicNameValuePair("password1", "password1"));
    params.add(new BasicNameValuePair("password2", "password2"));
    params.add(new BasicNameValuePair("autoScreenName", "true"));
    params.add(new BasicNameValuePair("screenName", null));
    params.add(new BasicNameValuePair("emailAddress", emailAddress));
    params.add(new BasicNameValuePair("facebookId", "0"));
    params.add(new BasicNameValuePair("openId", "0"));
    params.add(new BasicNameValuePair("locale", null));
    params.add(new BasicNameValuePair("firstName", firstName));
    params.add(new BasicNameValuePair("middleName", null));
    params.add(new BasicNameValuePair("lastName", lastName));
    params.add(new BasicNameValuePair("prefixId", "0"));
    params.add(new BasicNameValuePair("suffixId", "0"));
    params.add(new BasicNameValuePair("male", "true"));
    params.add(new BasicNameValuePair("birthdayMonth", "0"));
    params.add(new BasicNameValuePair("birthdayDay", "1"));
    params.add(new BasicNameValuePair("birthdayYear", "1971"));
    params.add(new BasicNameValuePair("jobTitle", "Tenant"));
    params.add(new BasicNameValuePair("groupIds", null)); // long[]
    params.add(new BasicNameValuePair("organizationIds", null));
    params.add(new BasicNameValuePair("roleIds", null));
    params.add(new BasicNameValuePair("userGroupIds", null));
    params.add(new BasicNameValuePair("addresses", "[]"));
    params.add(new BasicNameValuePair("emailAddresses", "[]"));
    params.add(new BasicNameValuePair("phones", "[]"));
    params.add(new BasicNameValuePair("websites", "[]"));
    params.add(new BasicNameValuePair("announcementsDelivers", "[]"));
    params.add(new BasicNameValuePair("sendEmail", "false"));
    params.add(new BasicNameValuePair("serviceContext", null));

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);

    HttpResponse resp = httpclient.execute(targetHost, post, ctx);
    System.out.println("addUser Status:[" + resp.getStatusLine() + "]");

    String response = null;
    if (resp.getEntity() != null) {
      response = EntityUtils.toString(resp.getEntity());
    }

    User user = null;
    if (!StringUtil.contains(response, "Exception", "")) {
      JSONDeserializer<User> deserializer = new JSONDeserializer<User>();
      user = deserializer.deserialize(response, User.class);
    }

    EntityUtils.consume(resp.getEntity());

    return user;
  }