List of usage examples for org.apache.http.protocol ExecutionContext HTTP_TARGET_HOST
String HTTP_TARGET_HOST
To view the source code for org.apache.http.protocol ExecutionContext HTTP_TARGET_HOST.
Click Source Link
From source file:com.rightscale.provider.rest.DashboardSession.java
private HttpRequestInterceptor createBasicAuthInterceptor() { return new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); }/*w w w. ja va 2 s . c o m*/ } } }; }
From source file:wuit.crawler.CrawlerHtml.java
public String doGetHttp(String url) { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 12000); HttpConnectionParams.setSoTimeout(params, 9000); HttpClient httpclient = new DefaultHttpClient(params); String rs = ""; try {/*from w ww . j a v a 2s .co m*/ HttpGet httpget = new HttpGet(url); System.out.println("executing request " + url); HttpContext httpContext = new BasicHttpContext(); // httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)"); HttpResponse response = httpclient.execute(httpget, httpContext); HttpUriRequest realRequest = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); url = targetHost.toString() + realRequest.getURI(); int resStatu = response.getStatusLine().getStatusCode();//? if (resStatu == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { rs = EntityUtils.toString(entity, "iso-8859-1"); String in_code = getEncoding(rs); String encode = getHtmlEncode(rs); if (encode.isEmpty()) { httpclient.getConnectionManager().shutdown(); return ""; } else { if (!in_code.toLowerCase().equals("utf-8") && !in_code.toLowerCase().equals(encode.toLowerCase())) { if (!in_code.toLowerCase().equals("iso-8859-1")) rs = new String(rs.getBytes("iso-8859-1"), in_code); if (!encode.toLowerCase().equals(in_code.toLowerCase())) rs = new String(rs.getBytes(in_code), encode); } } try { } catch (RuntimeException ex) { httpget.abort(); throw ex; } finally { // Closing the input stream will trigger connection release // try { instream.close(); } catch (Exception ignore) {} } } } } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); return rs; } }
From source file:org.callimachusproject.client.HttpClientFactoryTest.java
@Test public void test302RedirectTarget() throws Exception { HttpContext localContext = new BasicHttpContext(); HttpGet get = new HttpGet("http://example.com/302"); get.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true); BasicHttpResponse redirect = new BasicHttpResponse(_302); redirect.setHeader("Location", "http://example.com/200"); responses.add(redirect);/*w ww .ja v a2 s.c om*/ responses.add(new BasicHttpResponse(_200)); client.execute(get, new ResponseHandler<Void>() { public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException { assertEquals(_200.getStatusCode(), response.getStatusLine().getStatusCode()); return null; } }, localContext); HttpHost host = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST); URI root = new URI(host.getSchemeName(), null, host.getHostName(), host.getPort(), "/", null, null); assertEquals("http://example.com/200", root.resolve(req.getURI()).toASCIIString()); }
From source file:com.grendelscan.commons.http.apache_overrides.client.CustomHttpClient.java
public String getRequestHeadString(final HttpHost originalTarget, final HttpRequest request, final HttpContext originalContext) throws IOException, HttpException { HttpContext context = originalContext; HttpHost target = originalTarget;//from ww w .j av a 2s . c om if (context == null) { context = createHttpContext(); } if (target == null) { URI uri = ((HttpUriRequest) request).getURI(); target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); } context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target); prepareRequest(request); getHttpProcessor().process(request, context); String requestString = request.getRequestLine().toString() + "\n"; for (Header header : request.getAllHeaders()) { requestString += header.toString() + "\n"; } return requestString; }
From source file:net.oddsoftware.android.html.HttpCache.java
private void download(CacheItem cacheItem) { try {/* www .j a va 2 s . c om*/ // check to see if file exist, if so check etag and last-modified if (cacheItem.mFilename.length() > 0) { File f = new File(cacheItem.mFilename); try { InputStream is = new FileInputStream(f); is.close(); } catch (IOException exc) { // no file, nuke the cache stats cacheItem.mETag = ""; cacheItem.mLastModified = 0; } } else { cacheItem.mFilename = mCacheDirectory + File.separator + UUID.randomUUID().toString() + ".html.gz"; } HttpContext httpContext = new BasicHttpContext(); HttpClient client = createHttpClient(); HttpUriRequest request = createHttpRequest(cacheItem.mUrl, cacheItem.mETag, cacheItem.mLastModified); if (request == null || request.getURI() == null || request.getURI().getHost() == null || request.getURI().getHost().length() == 0) { if (Globals.LOGGING) Log.e(Globals.LOG_TAG, "unable to create http request for url " + cacheItem.mUrl); return; // sadness } HttpResponse response = client.execute(request, httpContext); StatusLine status = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (status.getStatusCode() == 304) { if (Globals.LOGGING) Log.d(Globals.LOG_TAG, "received 304 not modified"); cacheItem.mHitTime = new Date().getTime(); cacheItem.update(mContentResolver); return; } if (status.getStatusCode() == 200) { InputStream inputStream = null; if (entity != null) { inputStream = entity.getContent(); } else { return; } long contentLength = entity.getContentLength(); if (contentLength > MAX_CONTENT_LENGTH) { if (Globals.LOGGING) Log.w(Globals.LOG_TAG, "HttpCache.download item " + cacheItem.mUrl + " content length is too big " + contentLength); return; } Header encodingHeader = entity.getContentEncoding(); boolean encoded = false; if (encodingHeader != null) { if (encodingHeader.getValue().equalsIgnoreCase("gzip")) { inputStream = new GZIPInputStream(inputStream); encoded = true; } else if (encodingHeader.getValue().equalsIgnoreCase("deflate")) { inputStream = new InflaterInputStream(inputStream); encoded = true; } } File tmpFile = File.createTempFile("httpcache", ".html.gz.tmp", mCacheDirectory); OutputStream os = new GZIPOutputStream(new FileOutputStream(tmpFile)); byte[] buffer = new byte[4096]; int count = 0; long fileSize = 0; while ((count = inputStream.read(buffer)) != -1) { os.write(buffer, 0, count); fileSize += count; } inputStream.close(); os.close(); if (!encoded && contentLength > 0 && fileSize != contentLength) { Log.e(Globals.LOG_TAG, "HttpCache.download: content-length: " + contentLength + " but file size: " + fileSize + " aborting"); tmpFile.delete(); return; } tmpFile.renameTo(new File(cacheItem.mFilename)); // if the parse was ok, update these attributes // ETag: "6050003-78e5-4981d775e87c0" Header etagHeader = response.getFirstHeader("ETag"); if (etagHeader != null) { if (etagHeader.getValue().length() < MAX_ETAG_LENGTH) { cacheItem.mETag = etagHeader.getValue(); } else { if (Globals.LOGGING) Log.e(Globals.LOG_TAG, "etag length was too big: " + etagHeader.getValue().length()); } } // Last-Modified: Fri, 24 Dec 2010 00:57:11 GMT Header lastModifiedHeader = response.getFirstHeader("Last-Modified"); if (lastModifiedHeader != null) { try { cacheItem.mLastModified = FeedManager.parseRFC822Date(lastModifiedHeader.getValue()) .getTime(); } catch (ParseException exc) { if (Globals.LOGGING) Log.e(Globals.LOG_TAG, "unable to parse date", exc); } } // Expires: Thu, 01 Dec 1994 16:00:00 GMT Header expiresHeader = response.getFirstHeader("Expires"); if (expiresHeader != null) { try { cacheItem.mExpiresAt = FeedManager.parseRFC822Date(expiresHeader.getValue()).getTime(); } catch (ParseException exc) { if (Globals.LOGGING) Log.e(Globals.LOG_TAG, "unable to parse expires", exc); } } long now = new Date().getTime() + DEFAULT_EXPIRES; if (cacheItem.mExpiresAt < now) { cacheItem.mExpiresAt = now; } HttpUriRequest currentReq = (HttpUriRequest) httpContext .getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost currentHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); String currentUrl = currentHost.toURI() + currentReq.getURI(); if (Globals.LOGGING) Log.w(Globals.LOG_TAG, "loaded redirect from " + request.getURI().toString() + " to " + currentUrl); cacheItem.mLastUrl = currentUrl; cacheItem.mHitTime = new Date().getTime(); cacheItem.update(mContentResolver); } } catch (IOException exc) { if (Globals.LOGGING) { Log.e(Globals.LOG_TAG, "error downloading file to cache", exc); } } }
From source file:com.seajas.search.contender.service.modifier.SourceElementModifierService.java
/** * Retrieve the result content for the given URI. * * @param encodingOverride//w w w .ja v a2 s .c o m * @param resultHeaders * @param userAgent * @return Content */ public Content getContent(final URI resultUri, final String encodingOverride, final Map<String, String> resultHeaders, final String userAgent) { URI uriAfterRedirects = null; // Retrieve the content Header contentType = null; try { InputStream inputStream; // Local file streams can only be read if the parent scheme is also local if (!resultUri.getScheme().equalsIgnoreCase("file")) { HttpGet method = new HttpGet(resultUri); if (resultHeaders != null) for (Entry<String, String> resultHeader : resultHeaders.entrySet()) method.setHeader(new BasicHeader(resultHeader.getKey(), resultHeader.getValue())); if (userAgent != null) method.setHeader(CoreProtocolPNames.USER_AGENT, userAgent); HttpContext context = new BasicHttpContext(); SizeRestrictedHttpResponse response = httpClient.execute(method, new SizeRestrictedResponseHandler(maximumContentLength, resultUri), context); if (response != null) { HttpUriRequest currentRequest = (HttpUriRequest) context .getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); try { uriAfterRedirects = new URI(currentHost.toURI()).resolve(currentRequest.getURI()); } catch (URISyntaxException e) { logger.error(String.format("Final URI '%s' is mysteriously invalid", currentHost.toURI()), e); } inputStream = new ByteArrayInputStream(response.getResponse()); contentType = response.getContentType(); } else return null; } else inputStream = new FileInputStream(resultUri.getPath()); // Convert the stream to a reset-able one ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); IOUtils.copy(inputStream, outputStream); inputStream.close(); inputStream = new ByteArrayInputStream(outputStream.toByteArray()); outputStream.close(); // Now determine the content type and create a reader in case of structured content Metadata metadata = new Metadata(); if (encodingOverride != null && contentType != null && StringUtils.hasText(contentType.getValue())) { MediaType type = MediaType.parse(contentType.getValue()); metadata.add(HttpHeaders.CONTENT_TYPE, type.getType() + "/" + type.getSubtype() + "; charset=" + encodingOverride); } else if (contentType != null && StringUtils.hasText(contentType.getValue())) metadata.add(HttpHeaders.CONTENT_TYPE, contentType.getValue()); else if (encodingOverride != null) metadata.add(HttpHeaders.CONTENT_ENCODING, encodingOverride); MediaType mediaType = autoDetectParser.getDetector().detect(inputStream, metadata); return new Content(new ByteArrayInputStream(outputStream.toByteArray()), mediaType.getBaseType() + "/" + mediaType.getSubtype(), contentType != null ? contentType.getValue() : null, uriAfterRedirects != null ? uriAfterRedirects : resultUri); } catch (IOException e) { logger.error("Could not retrieve the given URL", e); return null; } }
From source file:org.callimachusproject.client.HttpClientFactoryTest.java
@Test public void test302CachedRedirectTarget() throws Exception { do {//w w w. ja v a 2 s .com HttpGet get = new HttpGet("http://example.com/302"); get.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true); BasicHttpResponse redirect = new BasicHttpResponse(_302); redirect.setHeader("Location", "http://example.com/200"); redirect.setHeader("Cache-Control", "public,max-age=3600"); responses.add(redirect); BasicHttpResponse doc = new BasicHttpResponse(_200); doc.setHeader("Cache-Control", "public,max-age=3600"); responses.add(doc); client.execute(get, new ResponseHandler<Void>() { public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException { assertEquals(_200.getStatusCode(), response.getStatusLine().getStatusCode()); return null; } }); } while (false); do { HttpContext localContext = new BasicHttpContext(); HttpGet get = new HttpGet("http://example.com/302"); get.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); BasicHttpResponse redirect = new BasicHttpResponse(_302); redirect.setHeader("Location", "http://example.com/200"); redirect.setHeader("Cache-Control", "public,max-age=3600"); responses.add(redirect); client.execute(get, new ResponseHandler<Void>() { public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException { assertEquals(_302.getStatusCode(), response.getStatusLine().getStatusCode()); return null; } }, localContext); HttpHost host = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST); URI root = new URI(host.getSchemeName(), null, host.getHostName(), -1, "/", null, null); assertEquals("http://example.com/302", root.resolve(req.getURI()).toASCIIString()); } while (false); }
From source file:cn.openwatch.internal.http.loopj.AsyncHttpClient.java
/** * Creates a new AsyncHttpClient.//from ww w . jav a 2 s . c om * * @param schemeRegistry SchemeRegistry to be used */ public AsyncHttpClient(SchemeRegistry schemeRegistry) { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, connectTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); HttpConnectionParams.setSoTimeout(httpParams, responseTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, connectTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); ClientConnectionManager cm = createConnectionManager(schemeRegistry, httpParams); Utils.asserts(cm != null, "Custom implementation of #createConnectionManager(SchemeRegistry, BasicHttpParams) returned null"); threadPool = getDefaultThreadPool(); requestMap = Collections.synchronizedMap(new WeakHashMap<Context, List<RequestHandle>>()); clientHeaderMap = new HashMap<String, String>(); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { if (request.containsHeader(header)) { Header overwritten = request.getFirstHeader(header); // remove the overwritten header request.removeHeader(overwritten); } request.addHeader(header, clientHeaderMap.get(header)); } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(entity)); break; } } } } }); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } } }, 0); httpClient .setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES, DEFAULT_RETRY_SLEEP_TIME_MILLIS)); }
From source file:com.rae.core.http.async.AsyncHttpClient.java
/** * Creates a new AsyncHttpClient.//from ww w . j ava 2 s .c o m * * @param schemeRegistry SchemeRegistry to be used */ public AsyncHttpClient(SchemeRegistry schemeRegistry) { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, timeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); HttpConnectionParams.setSoTimeout(httpParams, timeout); HttpConnectionParams.setConnectionTimeout(httpParams, timeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); threadPool = getDefaultThreadPool(); requestMap = new WeakHashMap<Context, List<RequestHandle>>(); clientHeaderMap = new HashMap<String, String>(); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { if (request.containsHeader(header)) { Header overwritten = request.getFirstHeader(header); Log.d(LOG_TAG, String.format("Headers were overwritten! (%s | %s) overwrites (%s | %s)", header, clientHeaderMap.get(header), overwritten.getName(), overwritten.getValue())); } request.addHeader(header, clientHeaderMap.get(header)); } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(entity)); break; } } } } }); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } } }, 0); httpClient .setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES, DEFAULT_RETRY_SLEEP_TIME_MILLIS)); }