Example usage for org.apache.http.client.utils URIBuilder addParameters

List of usage examples for org.apache.http.client.utils URIBuilder addParameters

Introduction

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

Prototype

public URIBuilder addParameters(final List<NameValuePair> nvps) 

Source Link

Document

Adds URI query parameters.

Usage

From source file:com.vmware.identity.openidconnect.protocol.URIUtils.java

public static URI appendQueryParameters(URI uri, Map<String, String> parameters) {
    Validate.notNull(uri, "uri");
    Validate.notNull(parameters, "parameters");

    List<NameValuePair> pairs = new ArrayList<NameValuePair>(parameters.size());
    for (Map.Entry<String, String> entry : parameters.entrySet()) {
        pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }/*from ww  w  . j  av a 2 s .com*/

    URIBuilder uriBuilder = new URIBuilder(uri);
    uriBuilder.addParameters(pairs);
    try {
        return uriBuilder.build();
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("failed to add parameters to uri", e);
    }
}

From source file:org.wso2.carbon.appmgt.mdm.restconnector.utils.RestUtils.java

/**
 * If not exists generate new access key or return existing one.
 *
 * @param remoteServer bean that holds information about remote server
 * @param generateNewKey whether generate new access key or not
 * @return generated access key/*from ww w  . j a va 2  s  . c  om*/
 */
public static String getAPIToken(RemoteServer remoteServer, boolean generateNewKey) {

    if (!generateNewKey) {
        if (!(AuthHandler.authKey == null || "null".equals(AuthHandler.authKey))) {
            return AuthHandler.authKey;
        }
    }

    HttpClient httpClient = AppManagerUtil.getHttpClient(remoteServer.getTokenApiURL());
    HttpPost postMethod = null;
    HttpResponse response = null;
    String responseString = "";
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<>();
        nameValuePairs.add(
                new BasicNameValuePair(Constants.RestConstants.GRANT_TYPE, Constants.RestConstants.PASSWORD));
        nameValuePairs
                .add(new BasicNameValuePair(Constants.RestConstants.USERNAME, remoteServer.getAuthUser()));
        nameValuePairs
                .add(new BasicNameValuePair(Constants.RestConstants.PASSWORD, remoteServer.getAuthPass()));
        URIBuilder uriBuilder = new URIBuilder(remoteServer.getTokenApiURL());
        uriBuilder.addParameters(nameValuePairs);
        postMethod = new HttpPost(uriBuilder.build());

        postMethod.setHeader(Constants.RestConstants.AUTHORIZATION,
                Constants.RestConstants.BASIC + new String(Base64.encodeBase64((remoteServer.getClientKey()
                        + Constants.RestConstants.COLON + remoteServer.getClientSecret()).getBytes())));
        postMethod.setHeader(Constants.RestConstants.CONTENT_TYPE,
                Constants.RestConstants.APPLICATION_FORM_URL_ENCODED);
    } catch (URISyntaxException e) {
        String errorMessage = "Cannot construct the Httppost. Url Encoded error.";
        log.error(errorMessage, e);
        return null;
    }
    try {
        if (log.isDebugEnabled()) {
            log.debug("Sending POST request to API Token endpoint. Request path:  "
                    + remoteServer.getTokenApiURL());
        }

        response = httpClient.execute(postMethod);
        int statusCode = response.getStatusLine().getStatusCode();

        if (log.isDebugEnabled()) {
            log.debug("Status code " + statusCode + " received while accessing the API Token endpoint.");
        }

    } catch (IOException e) {
        String errorMessage = "Cannot connect to Token API Endpoint.";
        log.error(errorMessage, e);
        return null;
    }

    try {
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            responseString = EntityUtils.toString(entity, "UTF-8");
            EntityUtils.consume(entity);
        }

    } catch (IOException e) {
        String errorMessage = "Cannot get response body for auth.";
        log.error(errorMessage, e);
        return null;
    }
    JSONObject token = (JSONObject) new JSONValue().parse(responseString);

    AuthHandler.authKey = String.valueOf(token.get(Constants.RestConstants.ACCESS_TOKEN));
    return AuthHandler.authKey;
}

From source file:org.apache.rya.indexing.smarturi.SmartUriAdapter.java

private static URI createTypeMapUri(final List<RyaURI> types) throws SmartUriException {
    final List<NameValuePair> nameValuePairs = new ArrayList<>();
    for (final RyaURI type : types) {
        final String shortName = getShortNameForType(type);
        nameValuePairs.add(new BasicNameValuePair(type.getData(), shortName));
    }/*  ww w  .ja va  2  s .  c  o  m*/

    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.addParameters(nameValuePairs);

    String uriString;
    try {
        final java.net.URI uri = uriBuilder.build();
        final String queryString = uri.getRawSchemeSpecificPart();
        uriString = ENTITY_TYPE_MAP_URN + queryString;
    } catch (final URISyntaxException e) {
        throw new SmartUriException("Unable to create type properties for the Smart URI", e);
    }

    return new URIImpl(uriString);
}

From source file:org.apache.rya.indexing.smarturi.SmartUriAdapter.java

private static URI createTypePropertiesUri(
        final ImmutableMap<RyaURI, ImmutableMap<RyaURI, Property>> typeProperties) throws SmartUriException {
    final List<NameValuePair> nameValuePairs = new ArrayList<>();
    for (final Entry<RyaURI, ImmutableMap<RyaURI, Property>> typeProperty : typeProperties.entrySet()) {
        final RyaURI type = typeProperty.getKey();
        final Map<RyaURI, Property> propertyMap = typeProperty.getValue();
        final URI typeUri = createIndividualTypeWithPropertiesUri(type, propertyMap);
        final String keyString = type.getDataType().getLocalName();
        final String valueString = typeUri.getLocalName();
        nameValuePairs.add(new BasicNameValuePair(keyString, valueString));
    }// w w  w.  j ava 2  s .com

    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.addParameters(nameValuePairs);

    String uriString;
    try {
        final java.net.URI uri = uriBuilder.build();
        final String queryString = uri.getRawSchemeSpecificPart();
        uriString = "urn:test" + queryString;
    } catch (final URISyntaxException e) {
        throw new SmartUriException("Unable to create type properties for the Smart URI", e);
    }

    return new URIImpl(uriString);
}

From source file:org.apache.rya.indexing.smarturi.SmartUriAdapter.java

private static URI createIndividualTypeWithPropertiesUri(final RyaURI type, final Map<RyaURI, Property> map)
        throws SmartUriException {
    final List<NameValuePair> nameValuePairs = new ArrayList<>();
    for (final Entry<RyaURI, Property> entry : map.entrySet()) {
        final RyaURI key = entry.getKey();
        final Property property = entry.getValue();

        final RyaType ryaType = property.getValue();
        final String keyString = (new URIImpl(key.getData())).getLocalName();
        final Value value = RyaToRdfConversions.convertValue(ryaType);
        final String valueString = value.stringValue();
        nameValuePairs.add(new BasicNameValuePair(keyString, valueString));
    }/*  w w w . j  ava 2 s  .com*/

    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.addParameters(nameValuePairs);

    String uriString;
    try {
        final java.net.URI uri = uriBuilder.build();
        final String queryString = uri.getRawSchemeSpecificPart();
        uriString = type.getData()/*new URIImpl(type.getData()).getLocalName()*/ + queryString;
    } catch (final URISyntaxException e) {
        throw new SmartUriException("Unable to create type URI with all its properties for the Smart URI", e);
    }

    return new URIImpl(uriString);
}

From source file:com.urhola.vehicletracker.connection.mattersoft.MatterSoftLiveHelsinki.java

private HttpURLConnection getOpenedConnection(List<NameValuePair> params, String responseMethod)
        throws ConnectionException {
    HttpURLConnection urlConnection;
    try {/* ww w  .j  av  a 2s.  c  o m*/
        URIBuilder b = new URIBuilder(BASE_URL);
        b.addParameters(params);
        URL url = b.build().toURL();
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod(responseMethod);
        urlConnection.setReadTimeout(TIME_OUT_LENGTH);
        urlConnection.setConnectTimeout(TIME_OUT_LENGTH);
        urlConnection.connect();
        int responseCode = urlConnection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK)
            throw new ConnectionException(urlConnection.getResponseMessage());
        return urlConnection;
    } catch (URISyntaxException | IOException ex) {
        throw new ConnectionException(ex);
    }
}

From source file:api.BaiduFolkMusicApi.java

private URI getRequestUri(List<NameValuePair> parameters) {

    try {/*from ww w.  j av a 2s  .c o  m*/

        parameters.add(new BasicNameValuePair("errordetails", "1"));
        URIBuilder uriBuilder = new URIBuilder(this.apiInvokeAddress);
        uriBuilder.addParameters(parameters);

        URI uri = uriBuilder.build();
        String uriStr = uri.toString();

        System.out.println(uriStr);

        return uri;

    } catch (URISyntaxException ex) {
        Logger.getLogger(BaiduFolkMusicApi.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:co.tuzza.swipehq.transport.ManagedHttpTransport.java

@Override
public <T> T doGet(String url, Map<String, String> params, Class<T> c) throws Exception {
    URIBuilder uriBuilder = new URIBuilder(url);
    uriBuilder.addParameters(parseParams(params));

    HttpUriRequest method;/* ww w. j a v a2  s  .  c  o  m*/
    HttpGet httpGet = new HttpGet(uriBuilder.build());
    method = httpGet;

    HttpClient client = getHttpClient();

    HttpResponse httpResponse = client.execute(method);
    String response = new BasicResponseHandler().handleResponse(httpResponse);

    return ResponseParser.parseResponse(response, c);
}

From source file:com.ny.apps.executor.TencentWeiboOAuth2.java

private URI prepareGetUrl() {
    URI uri = null;// ww  w.  j  av  a 2  s  .c o  m

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    NameValuePair clientId = new BasicNameValuePair("client_id", APPKEY);
    NameValuePair responseType = new BasicNameValuePair("response_type", "code");
    NameValuePair redirectURI = new BasicNameValuePair("redirect_uri", "http://www.smiletony.com");
    nvps.add(clientId);
    nvps.add(responseType);
    nvps.add(redirectURI);

    URIBuilder uriBuilder;
    try {
        uriBuilder = new URIBuilder("https://open.t.qq.com/cgi-bin/oauth2/authorize");
        uriBuilder.addParameters(nvps);
        uri = uriBuilder.build();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    return uri;
}

From source file:com.joyent.manta.http.MantaHttpRequestFactory.java

/**
 * Derives Manta URI for a given path with the passed query
 * parameters.//from   w ww  . j ava  2  s.  co m
 *
 * @param path full path
 * @param params query parameters to add to the URI
 * @return full URI as string of resource
 */
protected String uriForPath(final String path, final List<NameValuePair> params) {
    Validate.notNull(path, "Path must not be null");
    Validate.notNull(params, "Params must not be null");

    try {
        final URIBuilder uriBuilder = new URIBuilder(uriForPath(path));
        uriBuilder.addParameters(params);
        return uriBuilder.build().toString();
    } catch (final URISyntaxException e) {
        throw new ConfigurationException(String.format("Invalid path in URI: %s", path));
    }
}