Example usage for org.apache.http.client.utils URIUtils createURI

List of usage examples for org.apache.http.client.utils URIUtils createURI

Introduction

In this page you can find the example usage for org.apache.http.client.utils URIUtils createURI.

Prototype

@Deprecated
public static URI createURI(final String scheme, final String host, final int port, final String path,
        final String query, final String fragment) throws URISyntaxException 

Source Link

Document

Constructs a URI using all the parameters.

Usage

From source file:org.craftercms.profile.impl.ProfileRestClientImpl.java

@SuppressWarnings("unchecked")
@Override// w w  w  .  j a va  2  s  . c om
/*
 * (non-Javadoc)
 * 
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#getProfiles(java.
 * lang.String, java.util.List)
 */
public List<Profile> getProfiles(String appToken, List<String> profileIds) {
    if (log.isDebugEnabled()) {
        log.debug("Getting a list of profiles base on  profile ids: " + profileIds);
    }
    HttpEntity entity = null;
    List<Profile> profiles = new ArrayList<Profile>();

    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));

    if (profileIds != null) {
        for (String profileId : profileIds) {
            qparams.add(new BasicNameValuePair("profileIdList", profileId));
        }
    }

    try {
        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/profile/ids.json",
                URLEncodedUtils.format(qparams, HTTP.UTF_8), null);
        HttpGet httpget = new HttpGet(uri);

        HttpResponse response = clientService.getHttpClient().execute(httpget);
        entity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == 200) {
            profiles = (List<Profile>) objectMapper.readValue(entity.getContent(), PROFILE_LIST_TYPE);
        } else {
            handleErrorStatus(response.getStatusLine(), entity);
        }
    } catch (URISyntaxException e) {
        log.error(e.getMessage(), e);
    } catch (ClientProtocolException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (RestException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            log.error("Could not consume entity", e);
        }
    }

    return profiles;
}

From source file:org.craftercms.profile.impl.ProfileRestClientImpl.java

@SuppressWarnings("unchecked")
@Override/*from w  w w .ja va  2 s  .co m*/
/*
 * (non-Javadoc)
 * 
 * @see org.craftercms.profile.httpclient.ProfileRestClient#
 * getProfilesWithAllAttributes(java.lang.String, java.util.List)
 */
public List<Profile> getProfilesWithAllAttributes(String appToken, List<String> profileIds) {
    if (log.isDebugEnabled()) {
        log.debug("Getting a list of profiles with all the attributes based on  profile ids: " + profileIds);
    }
    HttpEntity entity = null;
    List<Profile> profiles = new ArrayList<Profile>();

    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));

    if (profileIds != null) {
        for (String profileId : profileIds) {
            qparams.add(new BasicNameValuePair("profileIdList", profileId));
        }
    }

    try {
        URI uri = URIUtils.createURI(scheme, host, port,
                profileAppPath + "/api/2/profile/ids/with_attributes" + ".json",
                URLEncodedUtils.format(qparams, HTTP.UTF_8), null);
        HttpGet httpget = new HttpGet(uri);

        HttpResponse response = clientService.getHttpClient().execute(httpget);
        entity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == 200) {
            profiles = (List<Profile>) objectMapper.readValue(entity.getContent(), PROFILE_LIST_TYPE);
        } else {
            handleErrorStatus(response.getStatusLine(), entity);
        }
    } catch (URISyntaxException e) {
        log.error(e.getMessage(), e);
    } catch (ClientProtocolException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (RestException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            log.error("Could not consume entity", e);
        }
    }

    return profiles;
}

From source file:org.craftercms.profile.impl.ProfileRestClientImpl.java

public String getAppToken(String appUsername, String appPassword) throws AppAuthenticationFailedException {
    if (log.isDebugEnabled()) {
        log.debug("Getting a appToken for the app username : " + appUsername);
    }/*  www  .jav  a2s. co m*/
    String appToken = null;
    HttpResponse response = null;
    HttpEntity entity = null;

    try {
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair("username", appUsername));
        qparams.add(new BasicNameValuePair(ProfileConstants.PASSWORD, appPassword));

        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/auth/app_token.json",
                URLEncodedUtils.format(qparams, HTTP.UTF_8), null);

        HttpGet httpget = new HttpGet(uri);

        response = clientService.getHttpClient().execute(httpget);

        entity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == 200) {
            appToken = (String) objectMapper.readValue(entity.getContent(), String.class);
        } else {
            handleErrorStatus(response.getStatusLine(), entity);
        }
    } catch (AppAuthenticationFailedException e) {
        log.error("", e);
        throw e;
    } catch (URISyntaxException e) {
        log.error(e.getMessage(), e);
    } catch (ClientProtocolException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (RestException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            log.error("Could not consume entity", e);
        }
    }
    return appToken;
}

From source file:org.craftercms.profile.impl.ProfileRestClientImpl.java

@Override
/*//from  w  w  w  .  j a  v  a 2s .  c  om
 * (non-Javadoc)
 * 
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#getTicket(java.lang
 * .String, java.lang.String, java.lang.String)
 */
public String getTicket(String appToken, String username, String password, String tenantName)
        throws UserAuthenticationFailedException {
    if (log.isDebugEnabled()) {
        log.debug("Getting a ticket for the username: " + username);
    }
    String ticket = null;
    HttpResponse response = null;
    HttpEntity entity = null;

    try {
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
        qparams.add(new BasicNameValuePair("username", username));
        qparams.add(new BasicNameValuePair(ProfileConstants.PASSWORD, password));
        qparams.add(new BasicNameValuePair(ProfileConstants.TENANT_NAME, tenantName));

        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/auth/ticket.json",
                URLEncodedUtils.format(qparams, HTTP.UTF_8), null);

        HttpGet httpget = new HttpGet(uri);

        response = clientService.getHttpClient().execute(httpget);
        entity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == 200) {
            ticket = (String) objectMapper.readValue(entity.getContent(), String.class);
        } else {
            handleErrorStatus(response.getStatusLine(), entity);
        }
    } catch (URISyntaxException e) {
        log.error(e.getMessage(), e);
    } catch (ClientProtocolException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (RestException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            log.error("Could not consume entity", e);
        }
    }

    return ticket;
}

From source file:org.craftercms.profile.impl.ProfileRestClientImpl.java

@Override
/*//from ww  w .  jav a2 s .c o m
 * (non-Javadoc)
 *
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#getTicket(java.lang
 * .String, java.lang.String, java.lang.String, boolean)
 */
public String getTicket(String appToken, String username, String password, String tenantName, boolean sso)
        throws UserAuthenticationFailedException {
    if (log.isDebugEnabled()) {
        log.debug("Getting a ticket for the username: " + username);
    }
    String ticket = null;
    HttpResponse response = null;
    HttpEntity entity = null;

    try {
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
        qparams.add(new BasicNameValuePair("username", username));
        qparams.add(new BasicNameValuePair(ProfileConstants.PASSWORD, password));
        qparams.add(new BasicNameValuePair(ProfileConstants.TENANT_NAME, tenantName));
        qparams.add(new BasicNameValuePair(ProfileConstants.SSO, String.valueOf(sso)));

        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/auth/ticket.json",
                URLEncodedUtils.format(qparams, HTTP.UTF_8), null);

        HttpGet httpget = new HttpGet(uri);

        response = clientService.getHttpClient().execute(httpget);
        entity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == 200) {
            ticket = (String) objectMapper.readValue(entity.getContent(), String.class);
        } else {
            handleErrorStatus(response.getStatusLine(), entity);
        }
    } catch (URISyntaxException e) {
        log.error(e.getMessage(), e);
    } catch (ClientProtocolException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (RestException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            log.error("Could not consume entity", e);
        }
    }

    return ticket;
}

From source file:org.craftercms.profile.impl.ProfileRestClientImpl.java

@Override
/*//from w w  w . j  av  a 2 s .  c o  m
 * (non-Javadoc)
 * 
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#isTicketValid(java
 * .lang.String, java.lang.String)
 */
public boolean isTicketValid(String appToken, String ticket) {
    if (log.isDebugEnabled()) {
        log.debug("Getting if a ticket is valid : " + ticket);
    }
    boolean validTicket = false;
    HttpResponse response = null;
    HttpEntity entity = null;

    try {
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
        qparams.add(new BasicNameValuePair(ProfileConstants.TICKET, ticket));

        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/auth/ticket/validate.json",
                URLEncodedUtils.format(qparams, HTTP.UTF_8), null);

        HttpGet httpget = new HttpGet(uri);

        response = clientService.getHttpClient().execute(httpget);
        entity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == 200) {
            validTicket = (Boolean) objectMapper.readValue(entity.getContent(), Boolean.class);
        } else if (response.getStatusLine().getStatusCode() == 500) {
            // ticket format was wrong
            log.debug(String.format("500 error : %s", response.getStatusLine().getReasonPhrase()));
        } else {
            handleErrorStatus(response.getStatusLine(), entity);
        }
    } catch (URISyntaxException e) {
        log.error(e.getMessage(), e);
    } catch (ClientProtocolException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (RestException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            log.error("Could not consume entity", e);
        }
    }

    return validTicket;
}

From source file:org.craftercms.profile.impl.ProfileRestClientImpl.java

@Override
/*/* w  w w . j  a v  a 2s. co  m*/
 * (non-Javadoc)
 * 
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#invalidateTicket(
 * java.lang.String, java.lang.String)
 */
public void invalidateTicket(String appToken, String ticket) {
    if (log.isDebugEnabled()) {
        log.debug("Invalidating the ticket: " + ticket);
    }
    HttpResponse response = null;
    HttpEntity entity = null;

    try {
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
        qparams.add(new BasicNameValuePair(ProfileConstants.TICKET, ticket));

        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/auth/ticket/invalidate.json",
                null, null);

        HttpPost httppost = new HttpPost(uri);
        httppost.setEntity(new UrlEncodedFormEntity(qparams, HTTP.UTF_8));

        response = clientService.getHttpClient().execute(httppost);
        entity = response.getEntity();
        handleErrorStatus(response.getStatusLine(), entity);
    } catch (URISyntaxException e) {
        log.error(e.getMessage(), e);
    } catch (ClientProtocolException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (RestException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            log.error("Could not consume entity", e);
        }
    }

}

From source file:org.craftercms.profile.impl.ProfileRestClientImpl.java

@Override
/*//from   w  w  w .jav  a 2s. c  o  m
 * (non-Javadoc)
 * @see org.craftercms.profile.api.ProfileClient#createTenant(java.lang.String, java.lang.String, java.util.List,
 * java.util.List, boolean)
 */
public Tenant createTenant(String appToken, String tenantName, List<String> roles, List<String> domains,
        boolean createDefaultRoles, boolean emailNewProfile) {
    if (log.isDebugEnabled()) {
        log.debug("Creating a tenant: " + tenantName);
    }
    HttpEntity entity = null;
    Tenant tenant = new Tenant();
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
    qparams.add(new BasicNameValuePair(ProfileConstants.TENANT_NAME, tenantName));
    qparams.add(new BasicNameValuePair("createDefaultRoles", Boolean.toString(createDefaultRoles)));
    qparams.add(new BasicNameValuePair("emailNewProfile", Boolean.toString(emailNewProfile)));

    if (roles != null && !roles.isEmpty()) {
        for (String role : roles) {
            qparams.add(new BasicNameValuePair(ProfileConstants.ROLES, role));
        }
    }

    if (domains != null && !domains.isEmpty()) {
        for (String domain : domains) {
            qparams.add(new BasicNameValuePair(ProfileConstants.DOMAINS, domain));
        }
    }

    try {
        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/tenant/" + "create.json",
                URLEncodedUtils.format(qparams, HTTP.UTF_8), null);
        HttpPost httppost = new HttpPost(uri);

        HttpResponse response = clientService.getHttpClient().execute(httppost);
        entity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == 200) {
            tenant = (Tenant) objectMapper.readValue(entity.getContent(), Tenant.class);
        } else {
            handleErrorStatus(response.getStatusLine(), entity);
        }
    } catch (URISyntaxException e) {
        log.error(e.getMessage(), e);
    } catch (ClientProtocolException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (RestException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            log.error("Could not consume entity", e);
        }
    }

    return tenant;
}

From source file:org.craftercms.profile.impl.ProfileRestClientImpl.java

@Override
/*/*from ww w. ja  va2s.c o  m*/
 * (non-Javadoc)
 * @see org.craftercms.profile.api.ProfileClient#updateTenant(java.lang.String, java.lang.String, java.lang.String,
 * java.util.List, java.util.List)
 */
public Tenant updateTenant(String appToken, String id, String tenantName, List<String> roles,
        List<String> domains, boolean emailNewProfile) {
    if (log.isDebugEnabled()) {
        log.debug("Updating a tenant the tenantId : " + id);
    }
    HttpEntity entity = null;
    Tenant tenant = new Tenant();
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("_id", id));
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
    qparams.add(new BasicNameValuePair(ProfileConstants.TENANT_NAME, tenantName));
    qparams.add(new BasicNameValuePair("emailNewProfile", Boolean.toString(emailNewProfile)));

    if (roles != null && !roles.isEmpty()) {
        for (String role : roles) {
            qparams.add(new BasicNameValuePair(ProfileConstants.ROLES, role));
        }
    }

    if (domains != null && !domains.isEmpty()) {
        for (String domain : domains) {
            qparams.add(new BasicNameValuePair(ProfileConstants.DOMAINS, domain));
        }
    }

    try {
        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/tenant/" + "update.json",
                URLEncodedUtils.format(qparams, HTTP.UTF_8), null);
        HttpPost httppost = new HttpPost(uri);

        HttpResponse response = clientService.getHttpClient().execute(httppost);
        entity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == 200) {
            tenant = (Tenant) objectMapper.readValue(entity.getContent(), Tenant.class);
        } else {
            handleErrorStatus(response.getStatusLine(), entity);
        }
    } catch (URISyntaxException e) {
        log.error(e.getMessage(), e);
    } catch (ClientProtocolException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (RestException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            log.error("Could not consume entity", e);
        }
    }

    return tenant;
}

From source file:org.craftercms.profile.impl.ProfileRestClientImpl.java

@Override
public void deleteTenant(String appToken, String tenantName) {
    if (log.isDebugEnabled()) {
        log.debug("Deliting the tenant: " + tenantName);
    }//w  ww  .j a  v  a 2s .  c o m
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
    HttpEntity entity = null;

    try {
        URI uri = URIUtils.createURI(scheme, host, port,
                profileAppPath + "/api/2/tenant/" + "delete/" + tenantName + ".json",
                URLEncodedUtils.format(qparams, HTTP.UTF_8), null);
        HttpGet httpget = new HttpGet(uri);

        HttpResponse response = clientService.getHttpClient().execute(httpget);

        entity = response.getEntity();

        if (response.getStatusLine().getStatusCode() != 200) {
            handleErrorStatus(response.getStatusLine(), response.getEntity());
        } else {
            handleErrorStatus(response.getStatusLine(), entity);
        }
    } catch (URISyntaxException e) {
        log.error(e.getMessage(), e);
    } catch (ClientProtocolException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (RestException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            log.error("Could not consume entity", e);
        }
    }
}