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

@Override
/*//  ww w .j  a  va 2s  .  c  o m
 * (non-Javadoc)
 * 
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#createProfile(java
 * .lang.String, java.util.Map)
 */
public Profile createProfile(String appToken, Map<String, Serializable> queryParams) {
    if (log.isDebugEnabled()) {
        log.debug("Creating a new profile  " + queryParams);
    }
    HttpEntity entity = null;
    Profile profile = new Profile();
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
    if (queryParams != null && !queryParams.isEmpty() && queryParams.keySet() != null) {
        @SuppressWarnings("rawtypes")
        Iterator it = queryParams.keySet().iterator();
        boolean findRole = false;
        while (it.hasNext()) {
            String key = (String) it.next();
            if (key.equals(ProfileConstants.ROLES)) {
                ArrayList<String> list = (ArrayList<String>) queryParams.get(key);
                if (list != null) {
                    for (String s : list) {
                        qparams.add(new BasicNameValuePair(ProfileConstants.ROLES, s));
                    }
                } else {
                    qparams.add(new BasicNameValuePair(ProfileConstants.ROLES, ProfileConstants.DEFAULT_ROLE));
                }
            } else {
                qparams.add(new BasicNameValuePair(key, (String) queryParams.get(key)));
            }
        }

    }

    try {
        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/profile/" + "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) {
            profile = (Profile) objectMapper.readValue(entity.getContent(), Profile.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 profile;
}

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

@Override
/*//from   w w w.  j  a v a2  s .c om
 * (non-Javadoc)
 * 
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#updateProfile(java
 * .lang.String, java.util.Map)
 */
public Profile updateProfile(String appToken, Map<String, Serializable> queryParams) {
    if (log.isDebugEnabled()) {
        log.debug("Updating a profile  with the data " + queryParams);
    }
    HttpEntity entity = null;
    Profile profile = new Profile();
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
    if (queryParams != null && !queryParams.isEmpty() && queryParams.keySet() != null) {
        @SuppressWarnings("rawtypes")
        Iterator it = queryParams.keySet().iterator();
        boolean findRole = false;
        while (it.hasNext()) {
            String key = (String) it.next();
            if (key.equals(ProfileConstants.ROLES)) {
                ArrayList<String> list = (ArrayList<String>) queryParams.get(key);
                for (String s : list) {
                    qparams.add(new BasicNameValuePair("roles", s));
                }
            } else {
                qparams.add(new BasicNameValuePair(key, (String) queryParams.get(key)));
            }

        }
    }

    try {
        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/profile/" + "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) {
            profile = (Profile) objectMapper.readValue(entity.getContent(), Profile.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 profile;
}

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

@Override
/*/*from w  w w .  j a v a 2s . c  o m*/
 * (non-Javadoc)
 * 
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#setAttributesForProfile
 * (java.lang.String, java.lang.String, java.util.Map)
 */
public void setAttributesForProfile(String appToken, String profileId, Map<String, Serializable> queryParams) {
    if (log.isDebugEnabled()) {
        log.debug("Setting attributes for the profileId " + profileId);
    }
    HttpEntity entity = null;
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
    if (queryParams != null && !queryParams.isEmpty() && queryParams.keySet() != null) {
        @SuppressWarnings("rawtypes")
        Iterator it = queryParams.keySet().iterator();
        Object value;
        while (it.hasNext()) {
            String key = (String) it.next();
            value = queryParams.get(key);
            if (value instanceof String) {
                qparams.add(new BasicNameValuePair(key, (String) value));
            }
        }
    }

    try {
        URI uri = URIUtils.createURI(scheme, host, port,
                profileAppPath + "/api/2/profile/" + "set_attributes/" + profileId + ".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) {
            handleErrorStatus(response.getStatusLine(), response.getEntity());
        }
    } 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  .  ja  va 2s. c  o  m*/
 * (non-Javadoc)
 * 
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#deleteProfile(java
 * .lang.String, java.lang.String)
 */
public void activeProfile(String appToken, String profileId, boolean active) {
    if (log.isDebugEnabled()) {
        log.debug("Actives a profile using the  profileId " + profileId);
    }
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
    qparams.add(new BasicNameValuePair(ProfileConstants.ACTIVE, String.valueOf(active)));
    HttpEntity entity = null;

    try {
        URI uri = URIUtils.createURI(scheme, host, port,
                profileAppPath + "/api/2/profile/active/" + profileId + ".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);
        }
    }
}

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

@Override
/*/*from   w  w  w  .j  a  va2 s  .c  o  m*/
 * (non-Javadoc)
 * 
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#deleteProfile(java
 * .lang.String, java.lang.String)
 */
public Profile verifyProfile(String appToken, String token) {
    if (log.isDebugEnabled()) {
        log.debug("Verify a profile using the  token " + token);
    }
    Profile profile = new Profile();
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
    qparams.add(new BasicNameValuePair(ProfileConstants.TOKEN, token));
    HttpEntity entity = null;

    try {
        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/profile/verify.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) {
            profile = (Profile) objectMapper.readValue(entity.getContent(), Profile.class);

        } else {
            handleVerifyError(entity, response);
        }
    } catch (VerifyAccountException e) {
        log.error(e.getMessage(), e);
        throw e;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            log.error("Could not consume entity", e);
        }
    }
    return profile;
}

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

@Override
/*//from w w w  .  j a v a  2 s  .  c  om
 * (non-Javadoc)
 * 
 * @see org.craftercms.profile.httpclient.ProfileRestClient#
 * deleteAllAttributesForProfile(java.lang.String, java.lang.String)
 */
public void deleteAllAttributesForProfile(String appToken, String profileId) {
    if (log.isDebugEnabled()) {
        log.debug("Delete all attributes in the  profileId " + profileId);
    }
    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/profile/" + profileId + "/delete_all_attributes.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) {
            handleErrorStatus(response.getStatusLine(), response.getEntity());
        }
    } 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   www . jav a 2s .co m*/
 * (non-Javadoc)
 * 
 * @see org.craftercms.profile.httpclient.ProfileRestClient#
 * deleteAttributesForProfile(java.lang.String, java.lang.String,
 * java.util.List)
 */
public void deleteAttributesForProfile(String appToken, String profileId, List<String> attributes) {
    if (log.isDebugEnabled()) {
        log.debug("Delete certains attributes in the  profileId " + profileId);
    }
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
    HttpEntity entity = null;

    if (attributes != null) {
        for (String attribute : attributes) {
            qparams.add(new BasicNameValuePair("attributes", attribute));
        }
    }

    try {
        URI uri = URIUtils.createURI(scheme, host, port,
                profileAppPath + "/api/2/profile/" + profileId + "/delete_attributes.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) {
            handleErrorStatus(response.getStatusLine(), response.getEntity());
        }
    } 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

@SuppressWarnings("unchecked")
@Override/*from   w  ww.j  a  v a  2  s . c om*/
/*
 * (non-Javadoc)
 * 
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#getAttributesForProfile
 * (java.lang.String, java.lang.String, java.util.List)
 */
public Map<String, Serializable> getAttributesForProfile(String appToken, String profileId,
        List<String> attributes) {
    if (log.isDebugEnabled()) {
        log.debug("Getting attributes for the  profileId " + profileId);
    }
    Map<String, Serializable> result = new HashMap<String, Serializable>();
    HttpEntity entity = null;

    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
    if (attributes != null) {
        for (String attribute : attributes) {
            qparams.add(new BasicNameValuePair("attributes", attribute));
        }
    }

    try {
        URI uri = URIUtils.createURI(scheme, host, port,
                profileAppPath + "/api/2/profile/" + profileId + "/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) {
            result = (Map<String, Serializable>) objectMapper.readValue(entity.getContent(),
                    MAP_STRING_SERIALIZABLE_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 result;
}

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

@SuppressWarnings("unchecked")
@Override//w w  w.  ja v a  2s. co  m
/*
 * (non-Javadoc)
 * 
 * @see org.craftercms.profile.httpclient.ProfileRestClient#
 * getAllAttributesForProfile(java.lang.String, java.lang.String)
 */
public Map<String, Serializable> getAllAttributesForProfile(String appToken, String profileId) {
    if (log.isDebugEnabled()) {
        log.debug("Getting all attributes for the  profileId " + profileId);
    }
    Map<String, Serializable> result = new HashMap<String, Serializable>();
    HttpEntity entity = null;

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

    try {
        URI uri = URIUtils.createURI(scheme, host, port,
                profileAppPath + "/api/2/profile/" + profileId + "/all_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) {
            //            inputStreamToString(response.getEntity().getContent());
            result = (Map<String, Serializable>) objectMapper.readValue(entity.getContent(),
                    MAP_STRING_SERIALIZABLE_TYPE);
            //            result = (HashMap<String, Serializable>) objectMapper.readValue(
            //                  entity.getContent(), MAP_STRING_SERIALIZABLE_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 result;
}

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

@SuppressWarnings("unchecked")
@Override//from w  w w  .j  a v a 2s .  co  m
/*
 * (non-Javadoc)
 * 
 * @see
 * org.craftercms.profile.httpclient.ProfileRestClient#getProfileRange(java
 * .lang.String, int, int, java.lang.String, java.lang.String,
 * java.util.List)
 */
public List<Profile> getProfileRange(String appToken, String tenantName, int start, int end, String sortBy,
        String sortOrder, List<String> attributes) {
    if (log.isDebugEnabled()) {
        log.debug("Getting a list of profile for the tenant name " + tenantName + " starting " + start
                + " ending" + " " + end + " sort by: " + sortBy + " sort order:" + sortOrder);
    }

    List<Profile> profiles = new ArrayList<Profile>();
    HttpEntity entity = null;

    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair(ProfileConstants.TENANT_NAME, tenantName));
    qparams.add(new BasicNameValuePair(ProfileConstants.APP_TOKEN, appToken));
    qparams.add(new BasicNameValuePair("sortBy", sortBy));
    qparams.add(new BasicNameValuePair("sortOrder", sortOrder));
    qparams.add(new BasicNameValuePair("start", String.valueOf(start)));
    qparams.add(new BasicNameValuePair("end", String.valueOf(end)));
    if (attributes != null && attributes.size() > 0) {
        for (String attribute : attributes) {
            qparams.add(new BasicNameValuePair("attributes", attribute));
        }
    }

    try {
        URI uri = URIUtils.createURI(scheme, host, port, profileAppPath + "/api/2/profile/range.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;
}