use of org.apache.http.protocol.HttpContext in project wildfly-camel by wildfly-extras.
the class Olingo4IntegrationTest method getRealServiceUrl.
/*
* Every request to the demo OData 4.0
* (http://services.odata.org/TripPinRESTierService) generates unique
* service URL with postfix like (S(tuivu3up5ygvjzo5fszvnwfv)) for each
* session This method makes reuest to the base URL and return URL with
* generated postfix
*/
private String getRealServiceUrl(String baseUrl) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(baseUrl);
HttpContext httpContext = new BasicHttpContext();
httpclient.execute(httpGet, httpContext);
HttpUriRequest currentReq = (HttpUriRequest) httpContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
HttpHost currentHost = (HttpHost) httpContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq.getURI());
return currentUrl;
}
use of org.apache.http.protocol.HttpContext in project crnk-framework by crnk-project.
the class CharsetTest method testUTF8isDefault.
public void testUTF8isDefault(boolean okHttp) throws InstantiationException, IllegalAccessException {
requestContentType = null;
responseContentType = null;
if (okHttp) {
OkHttpAdapter adapter = OkHttpAdapter.newInstance();
adapter.addListener(new OkHttpAdapterListener() {
@Override
public void onBuild(OkHttpClient.Builder builder) {
builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
requestContentType = chain.request().header(HttpHeaders.HTTP_CONTENT_TYPE);
Response response = chain.proceed(chain.request());
responseContentType = response.header(HttpHeaders.HTTP_CONTENT_TYPE);
return response;
}
});
}
});
client.setHttpAdapter(adapter);
} else {
HttpClientAdapter adapter = HttpClientAdapter.newInstance();
adapter.addListener(new HttpClientAdapterListener() {
@Override
public void onBuild(HttpClientBuilder builder) {
builder.addInterceptorFirst(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
Header header = httpRequest.getFirstHeader(HttpHeaders.HTTP_CONTENT_TYPE);
requestContentType = header != null ? header.getValue() : null;
}
});
builder.addInterceptorFirst(new HttpResponseInterceptor() {
@Override
public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
Header header = httpResponse.getFirstHeader(HttpHeaders.HTTP_CONTENT_TYPE);
responseContentType = header != null ? header.getValue() : null;
}
});
}
});
client.setHttpAdapter(adapter);
}
ResourceRepositoryV2<Task, Long> testRepo = client.getRepositoryForType(Task.class);
Task entity = new Task();
entity.setId(1L);
entity.setName("äöüé@¢€");
testRepo.create(entity);
Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, requestContentType);
Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, responseContentType);
Task savedEntity = testRepo.findOne(1L, new QuerySpec(Task.class));
Assert.assertEquals(entity.getName(), savedEntity.getName());
Assert.assertNull(requestContentType);
Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, responseContentType);
}
use of org.apache.http.protocol.HttpContext in project androidquery by androidquery.
the class AbstractAjaxCallback method httpDo.
private void httpDo(HttpUriRequest hr, String url, AjaxStatus status) throws ClientProtocolException, IOException {
DefaultHttpClient client = getClient();
if (proxyHandle != null) {
proxyHandle.applyProxy(this, hr, client);
}
if (AGENT != null) {
hr.addHeader("User-Agent", AGENT);
} else if (AGENT == null && GZIP) {
hr.addHeader("User-Agent", "gzip");
}
if (headers != null) {
for (String name : headers.keySet()) {
hr.addHeader(name, headers.get(name));
}
}
if (GZIP && (headers == null || !headers.containsKey("Accept-Encoding"))) {
hr.addHeader("Accept-Encoding", "gzip");
}
if (ah != null) {
ah.applyToken(this, hr);
}
String cookie = makeCookie();
if (cookie != null) {
hr.addHeader("Cookie", cookie);
}
HttpParams hp = hr.getParams();
if (proxy != null)
hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
if (timeout > 0) {
hp.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
hp.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
}
if (!redirect) {
hp.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
}
HttpContext context = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
request = hr;
if (abort) {
throw new IOException("Aborted");
}
if (SIMULATE_ERROR) {
throw new IOException("Simulated Error");
}
HttpResponse response = null;
try {
// response = client.execute(hr, context);
response = execute(hr, client, context);
} catch (HttpHostConnectException e) {
// if proxy is used, automatically retry without proxy
if (proxy != null) {
AQUtility.debug("proxy failed, retrying without proxy");
hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
// response = client.execute(hr, context);
response = execute(hr, client, context);
} else {
throw e;
}
}
byte[] data = null;
String redirect = url;
int code = response.getStatusLine().getStatusCode();
String message = response.getStatusLine().getReasonPhrase();
String error = null;
HttpEntity entity = response.getEntity();
File file = null;
File tempFile = null;
if (code < 200 || code >= 300) {
InputStream is = null;
try {
if (entity != null) {
is = entity.getContent();
byte[] s = toData(getEncoding(entity), is);
error = new String(s, "UTF-8");
AQUtility.debug("error", error);
}
} catch (Exception e) {
AQUtility.debug(e);
} finally {
AQUtility.close(is);
}
} else {
HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
redirect = currentHost.toURI() + currentReq.getURI();
int size = Math.max(32, Math.min(1024 * 64, (int) entity.getContentLength()));
OutputStream os = null;
InputStream is = null;
try {
file = getPreFile();
if (file == null) {
os = new PredefinedBAOS(size);
} else {
// file.createNewFile();
tempFile = makeTempFile(file);
os = new BufferedOutputStream(new FileOutputStream(tempFile));
}
is = entity.getContent();
boolean gzip = "gzip".equalsIgnoreCase(getEncoding(entity));
if (gzip) {
is = new GZIPInputStream(is);
}
int contentLength = (int) entity.getContentLength();
// AQUtility.debug("gzip response", entity.getContentEncoding());
copy(is, os, contentLength, tempFile, file);
if (file == null) {
data = ((PredefinedBAOS) os).toByteArray();
} else {
if (!file.exists() || file.length() == 0) {
file = null;
}
}
} finally {
AQUtility.close(is);
AQUtility.close(os);
}
}
AQUtility.debug("response", code);
if (data != null) {
AQUtility.debug(data.length, url);
}
status.code(code).message(message).error(error).redirect(redirect).time(new Date()).data(data).file(file).client(client).context(context).headers(response.getAllHeaders());
}
use of org.apache.http.protocol.HttpContext in project csb-sdk by aliyun.
the class HttpClientFactory method createKeepAliveStrategy.
private static ConnectionKeepAliveStrategy createKeepAliveStrategy() {
return new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
return 5 * 1000;
}
};
}
use of org.apache.http.protocol.HttpContext in project xian by happyyangyuan.
the class ConnKeepAliveStrategy method create.
public static ConnectionKeepAliveStrategy create(long keepTimeMill) {
if (keepTimeMill < 0)
throw new IllegalArgumentException("apache_httpclient连接持久时间不能小于0");
return new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
// Honor 'keep-alive' header
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch (NumberFormatException ignore) {
}
}
}
return keepTimeMill;
}
};
}
Aggregations