Search in sources :

Example 21 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project aries by apache.

the class HttpTestCase method testSessionBean.

public void testSessionBean() throws Exception {
    Bundle tb5Bundle = installBundle("tb6.jar");
    try {
        String path = "/foo";
        RequestInfoDTO requestInfoDTO = waitFor(path);
        assertEquals("foo", requestInfoDTO.servletDTO.name);
        HttpClientBuilder clientBuilder = hcbf.newBuilder();
        CloseableHttpClient httpclient = clientBuilder.build();
        CookieStore cookieStore = new BasicCookieStore();
        HttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
        URI uri = new URIBuilder(getEndpoint()).setPath(path).setParameter("name", "test").build();
        HttpGet httpget = new HttpGet(uri);
        try (CloseableHttpResponse response = httpclient.execute(httpget, httpContext)) {
            HttpEntity entity = response.getEntity();
            assertEquals("test", read(entity));
        }
        for (int i = 0; i < 10; i++) {
            uri = new URIBuilder(getEndpoint()).setPath(path).build();
            httpget = new HttpGet(uri);
            try (CloseableHttpResponse response = httpclient.execute(httpget, httpContext)) {
                HttpEntity entity = response.getEntity();
                assertEquals("test", read(entity));
            }
        }
        uri = new URIBuilder(getEndpoint()).setPath(path).build();
        httpget = new HttpGet(uri);
        try (CloseableHttpResponse response = httpclient.execute(httpget)) {
            HttpEntity entity = response.getEntity();
            assertEquals("", read(entity));
        }
    } finally {
        tb5Bundle.uninstall();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) Bundle(org.osgi.framework.Bundle) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder) CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RequestInfoDTO(org.osgi.service.http.runtime.dto.RequestInfoDTO)

Example 22 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project geode by apache.

the class HttpClientRule method buildHttpGet.

private HttpGet buildHttpGet(String uri, String... params) throws Exception {
    URIBuilder builder = new URIBuilder();
    builder.setPath(uri);
    for (int i = 0; i < params.length; i += 2) {
        builder.setParameter(params[i], params[i + 1]);
    }
    return new HttpGet(builder.build());
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 23 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project lucene-solr by apache.

the class SolrCLI method getJson.

/**
   * Utility function for sending HTTP GET request to Solr and then doing some
   * validation of the response.
   */
@SuppressWarnings({ "unchecked" })
public static Map<String, Object> getJson(HttpClient httpClient, String getUrl) throws Exception {
    try {
        // ensure we're requesting JSON back from Solr
        HttpGet httpGet = new HttpGet(new URIBuilder(getUrl).setParameter(CommonParams.WT, CommonParams.JSON).build());
        // make the request and get back a parsed JSON object
        Map<String, Object> json = httpClient.execute(httpGet, new SolrResponseHandler(), HttpClientUtil.createNewHttpClientRequestContext());
        // check the response JSON from Solr to see if it is an error
        Long statusCode = asLong("/responseHeader/status", json);
        if (statusCode == -1) {
            throw new SolrServerException("Unable to determine outcome of GET request to: " + getUrl + "! Response: " + json);
        } else if (statusCode != 0) {
            String errMsg = asString("/error/msg", json);
            if (errMsg == null)
                errMsg = String.valueOf(json);
            throw new SolrServerException(errMsg);
        } else {
            // make sure no "failure" object in there either
            Object failureObj = json.get("failure");
            if (failureObj != null) {
                if (failureObj instanceof Map) {
                    Object err = ((Map) failureObj).get("");
                    if (err != null)
                        throw new SolrServerException(err.toString());
                }
                throw new SolrServerException(failureObj.toString());
            }
        }
        return json;
    } catch (ClientProtocolException cpe) {
        // Perhaps SolrClient should have thrown an exception itself??
        if (cpe.getMessage().contains("HTTP ERROR 401") || cpe.getMessage().contentEquals("HTTP ERROR 403")) {
            int code = cpe.getMessage().contains("HTTP ERROR 401") ? 401 : 403;
            throw new SolrException(SolrException.ErrorCode.getErrorCode(code), "Solr requires authentication for " + getUrl + ". Please supply valid credentials. HTTP code=" + code);
        } else {
            throw cpe;
        }
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SolrException(org.apache.solr.common.SolrException) URIBuilder(org.apache.http.client.utils.URIBuilder) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 24 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project wechat-mp-sdk by usc.

the class MediaUtil method getMedia.

public static File getMedia(License license, String mediaId, String path) {
    if (StringUtils.isEmpty(mediaId) || StringUtils.isEmpty(path)) {
        return null;
    }
    String accessToken = AccessTokenUtil.getAccessToken(license);
    String url = WechatRequest.GET_MEDIA.getUrl();
    try {
        URI uri = new URIBuilder(url).setParameter("access_token", accessToken).setParameter("media_id", mediaId).build();
        HttpResponse response = Request.Get(uri).connectTimeout(HttpUtil.CONNECT_TIMEOUT).socketTimeout(HttpUtil.SOCKET_TIMEOUT).execute().returnResponse();
        return downloadFile(response, mediaId, path, uri);
    } catch (Exception e) {
        String msg = "get media failed:\n " + "url=" + url + "?access_token=" + accessToken + "&media_id=" + mediaId;
        log.error(msg, e);
        return null;
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 25 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project wechat-mp-sdk by usc.

the class MenuUtil method getMenu.

public static Menu getMenu(License license) {
    String accessToken = AccessTokenUtil.getAccessToken(license);
    String url = WechatRequest.GET_MENU.getUrl();
    try {
        URI uri = new URIBuilder(url).setParameter("access_token", accessToken).build();
        String json = Request.Get(uri).connectTimeout(HttpUtil.CONNECT_TIMEOUT).socketTimeout(HttpUtil.SOCKET_TIMEOUT).execute().handleResponse(HttpUtil.UTF8_CONTENT_HANDLER);
        Menu menu = buildMenu(json);
        log.info("get menu:\n url={},\n rtn={},{}", uri, json, menu);
        return menu;
    } catch (Exception e) {
        String msg = "get menu failed: url=" + url + "?access_token=" + accessToken;
        log.error(msg, e);
        return null;
    }
}
Also used : Menu(org.usc.wechat.mp.sdk.vo.menu.Menu) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

URIBuilder (org.apache.http.client.utils.URIBuilder)95 URISyntaxException (java.net.URISyntaxException)36 URI (java.net.URI)35 HttpGet (org.apache.http.client.methods.HttpGet)21 IOException (java.io.IOException)19 NameValuePair (org.apache.http.NameValuePair)12 HttpEntity (org.apache.http.HttpEntity)9 NotNull (org.jetbrains.annotations.NotNull)9 Map (java.util.Map)8 HashMap (java.util.HashMap)7 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)7 HttpResponse (org.apache.http.HttpResponse)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 HttpClient (org.apache.http.client.HttpClient)5 Gson (com.google.gson.Gson)4 URL (java.net.URL)4 HttpPost (org.apache.http.client.methods.HttpPost)4 StringEntity (org.apache.http.entity.StringEntity)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4