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();
}
}
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());
}
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;
}
}
}
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;
}
}
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;
}
}
Aggregations