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

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

Introduction

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

Prototype

public URIBuilder setCustomQuery(final String query) 

Source Link

Document

Sets custom URI query.

Usage

From source file:pl.raszkowski.sporttrackersconnector.rest.RESTUriBuilder.java

public URI build(String resourcePath, GetParameters getParameters) {
    try {//w w w.ja  v a 2s .co  m
        URIBuilder uriBuilder = new URIBuilder(resourcePath);
        if (getParameters.hasParameters()) {
            uriBuilder.setCustomQuery(getParameters.getCustomQuery());
        }
        return uriBuilder.build();
    } catch (URISyntaxException e) {
        LOG.error("Cannot build URI for resourcePath = {}.", resourcePath);
        throw new ConnectorException(String.format(CANNOT_BUILD_URI_ERROR_MESSAGE, resourcePath), e);
    }
}

From source file:org.duniter.core.client.service.HttpServiceImpl.java

public URIBuilder getURIBuilder(URI baseUri, String... path) {
    String pathToAppend = pathJoiner.skipNulls().join(path);

    int customQueryStartIndex = pathToAppend.indexOf('?');
    String customQuery = null;// w w  w .ja v a2s.c o m
    if (customQueryStartIndex != -1) {
        customQuery = pathToAppend.substring(customQueryStartIndex + 1);
        pathToAppend = pathToAppend.substring(0, customQueryStartIndex);
    }

    URIBuilder builder = new URIBuilder(baseUri);

    builder.setPath(baseUri.getPath() + pathToAppend);
    if (StringUtils.isNotBlank(customQuery)) {
        builder.setCustomQuery(customQuery);
    }
    return builder;
}

From source file:org.olat.modules.tu.TunnelMapper.java

@Override
public MediaResource handle(String relPath, HttpServletRequest hreq) {
    String method = hreq.getMethod();
    String uri = relPath;// w  w  w . j  a  v a2s  .  c  o  m
    HttpUriRequest meth = null;

    try {
        URIBuilder builder = new URIBuilder();
        builder.setScheme(proto).setHost(host).setPort(port.intValue());
        if (uri == null) {
            uri = (startUri == null) ? "" : startUri;
        }
        if (uri.length() > 0 && uri.charAt(0) != '/') {
            uri = "/" + uri;
        }
        if (StringHelper.containsNonWhitespace(uri)) {
            builder.setPath(uri);
        }

        if (method.equals("GET")) {
            String queryString = hreq.getQueryString();
            if (StringHelper.containsNonWhitespace(queryString)) {
                builder.setCustomQuery(queryString);
            }
            meth = new HttpGet(builder.build());
        } else if (method.equals("POST")) {
            Map<String, String[]> params = hreq.getParameterMap();
            HttpPost pmeth = new HttpPost(builder.build());
            List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
            for (String key : params.keySet()) {
                String vals[] = params.get(key);
                for (String val : vals) {
                    pairs.add(new BasicNameValuePair(key, val));
                }
            }

            HttpEntity entity = new UrlEncodedFormEntity(pairs, "UTF-8");
            pmeth.setEntity(entity);
            meth = pmeth;
        }

        // Add olat specific headers to the request, can be used by external
        // applications to identify user and to get other params
        // test page e.g. http://cgi.algonet.se/htbin/cgiwrap/ug/test.py
        if ("enabled".equals(
                CoreSpringFactory.getImpl(BaseSecurityModule.class).getUserInfosTunnelCourseBuildingBlock())) {
            User u = ident.getUser();
            meth.addHeader("X-OLAT-USERNAME", ident.getName());
            meth.addHeader("X-OLAT-LASTNAME", u.getProperty(UserConstants.LASTNAME, null));
            meth.addHeader("X-OLAT-FIRSTNAME", u.getProperty(UserConstants.FIRSTNAME, null));
            meth.addHeader("X-OLAT-EMAIL", u.getProperty(UserConstants.EMAIL, null));
            meth.addHeader("X-OLAT-USERIP", ipAddress);
        }

        HttpResponse response = httpClient.execute(meth);
        if (response == null) {
            // error
            return new NotFoundMediaResource(relPath);
        }

        // get or post successfully
        Header responseHeader = response.getFirstHeader("Content-Type");
        if (responseHeader == null) {
            // error
            EntityUtils.consumeQuietly(response.getEntity());
            return new NotFoundMediaResource(relPath);
        }
        return new HttpRequestMediaResource(response);
    } catch (ClientProtocolException e) {
        log.error("", e);
        return null;
    } catch (URISyntaxException e) {
        log.error("", e);
        return null;
    } catch (IOException e) {
        log.error("Error loading URI: " + (meth == null ? "???" : meth.getURI()), e);
        return null;
    }
}

From source file:com.oDesk.api.OAuthClient.java

/**
 * Send signed GET OAuth request/* www  .ja  va2s  .co  m*/
 * 
 * @param   url Relative URL
 * @param   type Type of HTTP request (HTTP method)
 * @param   params Hash of parameters
 * @throws   JSONException If JSON object is invalid or request was abnormal
 * @return   {@link JSONObject} JSON Object that contains data from response
 * */
private JSONObject sendGetRequest(String url, Integer type, HashMap<String, String> params)
        throws JSONException {
    String fullUrl = getFullUrl(url);
    HttpGet request = new HttpGet(fullUrl);

    if (params != null) {
        URI uri;
        String query = "";
        try {
            URIBuilder uriBuilder = new URIBuilder(request.getURI());

            // encode values and add them to the request
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                // to prevent double encoding, we need to create query string ourself
                // uriBuilder.addParameter(key, URLEncoder.encode(value).replace("%3B", ";"));
                query = query + key + "=" + value + "&";
            }
            uriBuilder.setCustomQuery(query);
            uri = uriBuilder.build();

            ((HttpRequestBase) request).setURI(uri);
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    try {
        mOAuthConsumer.sign(request);
    } catch (OAuthException e) {
        e.printStackTrace();
    }

    return oDeskRestClient.getJSONObject(request, type);
}

From source file:com.github.brandtg.pantopod.crawler.CrawlingEventHandler.java

private URI getNextUri(URI url, String href, String chroot) throws Exception {
    //    if (href.contains("..")) {
    ///*from w  ww  .j av a2 s . c om*/
    //      throw new IllegalArgumentException("Relative URI not allowed: " + href);
    //    }

    URI hrefUri = URI.create(href.trim().replaceAll(" ", "+"));

    URIBuilder builder = new URIBuilder();

    builder.setScheme(hrefUri.getScheme() == null ? url.getScheme() : hrefUri.getScheme());
    builder.setHost(hrefUri.getHost() == null ? url.getHost() : hrefUri.getHost());
    builder.setPort(hrefUri.getPort() == -1 ? url.getPort() : hrefUri.getPort());

    if (hrefUri.getPath() != null) {
        StringBuilder path = new StringBuilder();
        if (hrefUri.getHost() == null && chroot != null) {
            path.append(chroot);
        }

        // Ensure no two slashes
        if (hrefUri.getPath() != null && hrefUri.getPath().length() > 0 && hrefUri.getPath().charAt(0) == '/'
                && path.length() > 0 && path.charAt(path.length() - 1) == '/') {
            path.setLength(path.length() - 1);
        }

        path.append(hrefUri.getPath());

        builder.setPath(chroot == null ? "" : chroot + hrefUri.getPath());
    }
    if (hrefUri.getQuery() != null) {
        builder.setCustomQuery(hrefUri.getQuery());
    }
    if (hrefUri.getFragment() != null) {
        builder.setFragment(hrefUri.getFragment());
    }

    return builder.build();
}

From source file:com.Upwork.api.OAuthClient.java

/**
 * Send signed GET OAuth request/*from ww w.  j  a v  a  2  s .com*/
 * 
 * @param   url Relative URL
 * @param   type Type of HTTP request (HTTP method)
 * @param   params Hash of parameters
 * @throws   JSONException If JSON object is invalid or request was abnormal
 * @return   {@link JSONObject} JSON Object that contains data from response
 * */
private JSONObject sendGetRequest(String url, Integer type, HashMap<String, String> params)
        throws JSONException {
    String fullUrl = getFullUrl(url);
    HttpGet request = new HttpGet(fullUrl);

    if (params != null) {
        URI uri;
        String query = "";
        try {
            URIBuilder uriBuilder = new URIBuilder(request.getURI());

            // encode values and add them to the request
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                // to prevent double encoding, we need to create query string ourself
                // uriBuilder.addParameter(key, URLEncoder.encode(value).replace("%3B", ";"));
                query = query + key + "=" + value.replace("&", "&amp;") + "&";
                // what the hell is going on in java - no adequate way to encode query string
                // lets temporary replace "&" in the value, to encode it manually later
            }
            // this routine will encode query string
            uriBuilder.setCustomQuery(query);
            uri = uriBuilder.build();

            // re-create request to have validly encoded ampersand
            request = new HttpGet(fullUrl + "?" + uri.getRawQuery().replace("&amp;", "%26"));
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    try {
        mOAuthConsumer.sign(request);
    } catch (OAuthException e) {
        e.printStackTrace();
    }

    return UpworkRestClient.getJSONObject(request, type);
}