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

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

Introduction

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

Prototype

@Deprecated
public URIBuilder setQuery(final String query) 

Source Link

Document

Sets URI query.

Usage

From source file:org.megam.api.http.TransportMachinery.java

public static TransportResponse get(TransportTools nuts)
        throws URISyntaxException, ClientProtocolException, IOException {

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(nuts.urlString());
    if (nuts.headers() != null) {
        for (Map.Entry<String, String> headerEntry : nuts.headers().entrySet()) {

            //this if condition is set for twilio Rest API to add credentials to DefaultHTTPClient, conditions met twilio work.
            if (headerEntry.getKey().equalsIgnoreCase("provider")
                    & headerEntry.getValue().equalsIgnoreCase(nuts.headers().get("provider"))) {
                httpclient.getCredentialsProvider().setCredentials(
                        new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(
                                nuts.headers().get("account_sid"), nuts.headers().get("oauth_token")));
            }/*from   ww  w.j ava2 s . com*/
            //this else part statements for other providers
            else {
                httpget.addHeader(headerEntry.getKey(), headerEntry.getValue());
            }
        }
    }

    if (nuts.isQuery()) {
        URIBuilder uribuilder = new URIBuilder(nuts.urlString());
        uribuilder.setQuery(URLEncodedUtils.format(nuts.pairs(), nuts.encoding()));
        httpget.setURI(uribuilder.build());
    }

    TransportResponse transportResp = null;
    try {
        HttpResponse httpResp = httpclient.execute(httpget);
        transportResp = new TransportResponse(httpResp.getStatusLine(), httpResp.getEntity(),
                httpResp.getLocale());
    } finally {
        httpget.releaseConnection();
    }

    return transportResp;

}

From source file:org.hl7.fhir.client.ResourceAddress.java

public static URI appendQueryStringToUri(URI uri, String parameterName, String parameterValue) {
    URI modifiedUri = null;//  ww w . j  av  a  2 s.  c om
    try {
        URIBuilder uriBuilder = new URIBuilder(uri);
        uriBuilder.setQuery(parameterName + "=" + parameterValue);
        modifiedUri = uriBuilder.build();
    } catch (Exception e) {
        throw new EFhirClientException(
                "Unable to append query parameter '" + parameterName + "=" + parameterValue + " to URI " + uri,
                e);
    }
    return modifiedUri;
}

From source file:com.griddynamics.jagger.invoker.http.ApacheHttpInvoker.java

@Override
protected HttpRequestBase getHttpMethod(HttpRequestBase query, String endpoint) {
    try {//  w  w  w . j av a  2 s. c  om
        if (query.getURI() == null) {
            query.setURI(URI.create(endpoint));
            return query;
        } else {
            URIBuilder uriBuilder = new URIBuilder(URI.create(endpoint));
            uriBuilder.setQuery(query.getURI().getQuery());
            uriBuilder.setFragment(query.getURI().getFragment());
            uriBuilder.setUserInfo(query.getURI().getUserInfo());
            if (!query.getURI().getPath().isEmpty()) {
                uriBuilder.setPath(query.getURI().getPath());
            }
            query.setURI(uriBuilder.build());
            return query;
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.jvoicexml.documentserver.schemestrategy.HttpSchemeStrategy.java

/**
 * Adds the parameters to the HTTP method.
 * /*from  w  w  w.  jav a2  s . c  o m*/
 * @param parameters
 *            parameters to add
 * @param uri
 *            the given URI
 * @return URI with the given parameters
 * @throws URISyntaxException
 *             error creating a URI
 * @throws SemanticError
 *             error evaluating a parameter
 */
private URI addParameters(final Collection<KeyValuePair> parameters, final URI uri)
        throws URISyntaxException, SemanticError {
    if ((parameters == null) || parameters.isEmpty()) {
        return uri;
    }
    final ArrayList<NameValuePair> queryParameters = new ArrayList<NameValuePair>();
    for (KeyValuePair current : parameters) {
        final Object value = current.getValue();
        if (!(value instanceof File)) {
            final String name = current.getKey();
            final NameValuePair pair = new BasicNameValuePair(name, value.toString());
            queryParameters.add(pair);
        }
    }

    final Collection<NameValuePair> parameterList = URLEncodedUtils.parse(uri, encoding);
    queryParameters.addAll(parameterList);

    final String query = URLEncodedUtils.format(queryParameters, encoding);
    final URIBuilder builder = new URIBuilder(uri);
    builder.setQuery(query);
    return builder.build();
}

From source file:net.datacrow.onlinesearch.bol.BolClient.java

/**
 * Searches./*from  w  ww  .j a v a2  s . c  o m*/
 * 
 * @param term The search term (required).
 * @param categoryID The category id and refinements, separated by spaces (optional).
 */
public String search(String term, String categoryID) throws IOException, URISyntaxException {
    List<NameValuePair> queryParams = new ArrayList<NameValuePair>();
    queryParams.add(new BasicNameValuePair("term", term));

    if (categoryID != null)
        queryParams.add(new BasicNameValuePair("categoryId", categoryID));

    queryParams.add(new BasicNameValuePair("nrProducts", "10"));
    queryParams.add(new BasicNameValuePair("includeProducts", "TRUE"));
    queryParams.add(new BasicNameValuePair("includeCategories", "TRUE"));
    queryParams.add(new BasicNameValuePair("includeRefinements", "FALSE"));

    URIBuilder builder = new URIBuilder();
    builder.setScheme("https");
    builder.setHost("openapi.bol.com");
    builder.setPath("/openapi/services/rest/catalog/v3/searchresults/");
    builder.setQuery(URLEncodedUtils.format(queryParams, "UTF-8"));

    HttpGet httpGet = new HttpGet(builder.build());

    HttpOAuthHelper au = new HttpOAuthHelper("application/xml");
    au.handleRequest(httpGet, accessKeyId, secretAccessKey, null, queryParams);

    HttpResponse httpResponse = httpClient.execute(httpGet);
    String xml = getXML(httpResponse);
    httpClient.getConnectionManager().shutdown();
    return xml;
}

From source file:org.berlin.crawl.bean.BotLink.java

public URIBuilder toBuilder() {
    final URIBuilder b = new URIBuilder();
    b.setPath(path);//from w ww.  j  av a  2 s.  c  om
    b.setHost(host);
    b.setScheme(scheme);
    b.setQuery(query);
    return b;
}

From source file:org.n52.sos.service.it.SosITBase.java

/**
 * Get URI for the relative sos path and query using test host, port, and
 * basepath/*from ww w.j a  v  a  2 s .  com*/
 * 
 * @param path
 *            The relative test endpoint
 * @param query
 *            Query parameters to add to the request
 * @return Constructed URI
 * @throws URISyntaxException
 */
protected URI getURI(String path, String query) throws URISyntaxException {
    URIBuilder b = new URIBuilder();
    b.setScheme("http");
    b.setHost(host);
    b.setPort(port);
    b.setPath(getPath(path));
    b.setQuery(query);
    b.setFragment(null);

    return b.build();
}

From source file:net.datacrow.onlinesearch.discogs.task.DiscogsMusicSearch.java

@Override
protected Collection<Object> getItemKeys() throws Exception {
    HttpClient httpClient = getHttpClient();

    List<NameValuePair> queryParams = new ArrayList<NameValuePair>();
    queryParams.add(new BasicNameValuePair("q", getQuery()));

    URIBuilder builder = new URIBuilder();
    builder.setScheme("http");
    builder.setHost("api.discogs.com");
    builder.setPath("/database/search");
    builder.setQuery(URLEncodedUtils.format(queryParams, "UTF-8"));
    URI uri = builder.build();//www .  j a va 2 s .  c  om
    HttpGet httpGet = new HttpGet(uri);

    HttpOAuthHelper au = new HttpOAuthHelper("application/json");
    au.handleRequest(httpGet, _CONSUMER_KEY, _CONSUMER_SECRET, null, queryParams);

    HttpResponse httpResponse = httpClient.execute(httpGet);
    String response = getReponseText(httpResponse);

    httpClient.getConnectionManager().shutdown();

    Collection<Object> keys = new ArrayList<Object>();

    int counter = 0;
    for (String key : StringUtils.getValuesBetween("\"id\":", "}", response)) {
        keys.add(key.trim());

        if (counter++ >= maxQuerySize)
            break;
    }
    return keys;
}

From source file:com.net.plus.common.http.transport.AbstractHttpClientTransport.java

public URI getSendUrl() {
    try {//from ww  w. ja va 2  s.com
        if (StringUtils.hasText(url)) {
            return new URIBuilder(url).build();
        }
        URIBuilder builder = new URIBuilder();
        if (StringUtils.hasText(protocol)) {
            builder.setScheme(protocol);
        }
        if (StringUtils.hasText(host)) {
            builder.setHost(host);
        }
        if (port != -1) {
            builder.setPort(port);
        }
        if (StringUtils.hasText(target)) {
            builder.setPath(target);
        }
        if (StringUtils.hasText(queryString)) {
            builder.setQuery(queryString);
        }
        return builder.build();
    } catch (Exception e) {
        return null;
    }
}