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:org.greencheek.spring.rest.SSLCachingHttpComponentsClientHttpResponse.java
public String getFinalUri() { String url = finalUri.get();//from w w w . ja v a2 s .c o m if (url != null) return url; HttpHost host = (HttpHost) this.httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); HttpUriRequest finalRequest = (HttpUriRequest) this.httpContext.getAttribute(ExecutionContext.HTTP_REQUEST); StringBuilder finalUriString = new StringBuilder(); finalUriString.append(host.getSchemeName()).append("://"); finalUriString.append(host.getHostName()); if (host.getPort() != -1) { finalUriString.append(':').append(host.getPort()); } finalUriString.append(finalRequest.getURI().normalize().toString()); finalUri.compareAndSet(null, finalUriString.toString()); return finalUri.get(); }
From source file:org.dataconservancy.archive.impl.fcrepo.ri.MultiThreadedHttpClient.java
public MultiThreadedHttpClient(HttpClientConfig config) { this.config = config; if (config.getPreemptiveAuthN()) { HttpRequestInterceptor interceptor = new HttpRequestInterceptor() { @Override//ww w. j a va 2 s. c o m public void process(HttpRequest request, 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 no auth scheme has been initialized yet if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); // Obtain credentials matching the target host Credentials creds = credsProvider.getCredentials(authScope); // If found, generate BasicScheme preemptively if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); } } } }; addRequestInterceptor(interceptor, 0); } }
From source file:org.ocpsoft.redoculous.tests.HttpAction.java
/** * Return the URL up to and including the host and port. *///from ww w . jav a2 s . co m public String getHost() { HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); return currentHost.toURI(); }
From source file:net.oauth.client.httpclient4.PreemptiveAuthorizer.java
/** * If no auth scheme has been selected for the given context, consider each * of the preferred auth schemes and select the first one for which an * AuthScheme and matching Credentials are available. *//*ww w . j a va2 s.co m*/ public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState != null && authState.getAuthScheme() != null) { return; } HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); CredentialsProvider creds = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER); AuthSchemeRegistry schemes = (AuthSchemeRegistry) context.getAttribute(ClientContext.AUTHSCHEME_REGISTRY); for (Object schemeName : (Iterable) context.getAttribute(ClientContext.AUTH_SCHEME_PREF)) { AuthScheme scheme = schemes.getAuthScheme(schemeName.toString(), request.getParams()); if (scheme != null) { AuthScope targetScope = new AuthScope(target.getHostName(), target.getPort(), scheme.getRealm(), scheme.getSchemeName()); Credentials cred = creds.getCredentials(targetScope); if (cred != null) { authState.setAuthScheme(scheme); authState.setCredentials(cred); return; } } } }
From source file:com.subgraph.vega.internal.http.requests.RequestTask.java
@Override public IHttpResponse call() throws Exception { if (config.getForceIdentityEncoding()) request.setHeader(HTTP.CONTENT_ENCODING, HTTP.IDENTITY_CODING); if (rateLimit != null) rateLimit.maybeDelayRequest();/*from w ww . j a v a 2 s . c om*/ final long start = System.currentTimeMillis(); final HttpResponse httpResponse = client.execute(request, context); final long elapsed = System.currentTimeMillis() - start; final HttpEntity entity = httpResponse.getEntity(); if (entity != null) { if (config.getMaximumResponseKilobytes() > 0 && entity.getContentLength() > (config.getMaximumResponseKilobytes() * 1024)) { logger.warning("Aborting request " + request.getURI().toString() + " because response length " + entity.getContentLength() + " exceeds maximum length of " + config.getMaximumResponseKilobytes() + " kb."); request.abort(); httpResponse.setEntity(createEmptyEntity()); } final HttpEntity newEntity = processEntity(httpResponse, entity); httpResponse.setEntity(newEntity); } final HttpHost host = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); final HttpRequest sentRequest = (HttpRequest) context.getAttribute(HttpRequestEngine.VEGA_SENT_REQUEST); final IHttpResponse response = new EngineHttpResponse(request.getURI(), host, (sentRequest == null) ? (request) : (sentRequest), httpResponse, elapsed, htmlParser); for (IHttpResponseProcessor p : config.getResponseProcessors()) { p.processResponse(response.getOriginalRequest(), response, context); } return response; }
From source file:com.soulgalore.crawler.core.impl.HTTPClientResponseFetcher.java
public HTMLPageResponse get(CrawlerURL url, boolean getPage, Map<String, String> requestHeaders, boolean followRedirectsToNewDomain) { if (url.isWrongSyntax()) { return new HTMLPageResponse(url, StatusCode.SC_MALFORMED_URI.getCode(), Collections.<String, String>emptyMap(), "", "", 0, "", 0); }//from w w w.j a va 2s.c o m final HttpGet get = new HttpGet(url.getUri()); for (String key : requestHeaders.keySet()) { get.setHeader(key, requestHeaders.get(key)); } HttpEntity entity = null; final long start = System.currentTimeMillis(); try { HttpContext localContext = new BasicHttpContext(); final HttpResponse resp = httpClient.execute(get, localContext); final long fetchTime = System.currentTimeMillis() - start; // Get the last URL in the redirect chain final HttpHost target = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); final HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST); // Fix when using proxy, relative URI (no proxy used) String newURL; if (req.getURI().toString().startsWith("http")) { newURL = req.getURI().toString(); } else { newURL = target + req.getURI().toString(); } entity = resp.getEntity(); // this is a hack to minimize the amount of memory used // should make this configurable maybe // don't fetch headers for request that don't fetch the body and // response isn't 200 // these headers will not be shown in the results final Map<String, String> headersAndValues = getPage || !StatusCode.isResponseCodeOk(resp.getStatusLine().getStatusCode()) ? getHeaders(resp) : Collections.<String, String>emptyMap(); final String encoding = entity.getContentEncoding() != null ? entity.getContentEncoding().getValue() : ""; final String body = getPage ? getBody(entity, "".equals(encoding) ? "UTF-8" : encoding) : ""; final long size = entity.getContentLength(); // TODO add log when null final String type = (entity.getContentType() != null) ? entity.getContentType().getValue() : ""; final int sc = resp.getStatusLine().getStatusCode(); EntityUtils.consume(entity); // If we want to only collect only URLS that don't redirect to a new domain // This solves the problem with local links like: // http://www.peterhedenskog.com/facebook that redirects to http://www.facebook.com/u/peter.hedenskog // TODO the host check can be done better :) if (!followRedirectsToNewDomain && !newURL.contains(url.getHost())) { return new HTMLPageResponse(url, StatusCode.SC_SERVER_REDIRECT_TO_NEW_DOMAIN.getCode(), Collections.<String, String>emptyMap(), "", "", 0, "", fetchTime); } return new HTMLPageResponse( !url.getUrl().equals(newURL) ? new CrawlerURL(newURL, url.getReferer()) : url, sc, headersAndValues, body, encoding, size, type, fetchTime); } catch (SocketTimeoutException e) { System.err.println(e); return new HTMLPageResponse(url, StatusCode.SC_SERVER_RESPONSE_TIMEOUT.getCode(), Collections.<String, String>emptyMap(), "", "", 0, "", System.currentTimeMillis() - start); } catch (ConnectTimeoutException e) { System.err.println(e); return new HTMLPageResponse(url, StatusCode.SC_SERVER_RESPONSE_TIMEOUT.getCode(), Collections.<String, String>emptyMap(), "", "", 0, "", System.currentTimeMillis() - start); } catch (IOException e) { System.err.println(e); return new HTMLPageResponse(url, StatusCode.SC_SERVER_RESPONSE_UNKNOWN.getCode(), Collections.<String, String>emptyMap(), "", "", 0, "", -1); } finally { get.releaseConnection(); } }
From source file:com.mongolduu.android.ng.misc.RedirectHandler.java
public URI getLocationURI(final HttpResponse response, final HttpContext context) throws ProtocolException { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); }/* w w w. j av a 2 s .c o m*/ // get the location header to find out where to redirect to Header locationHeader = response.getFirstHeader("location"); if (locationHeader == null) { // got a redirect response, but no location header throw new ProtocolException( "Received redirect response " + response.getStatusLine() + " but no location header"); } // HERE IS THE MODIFIED LINE OF CODE String location = locationHeader.getValue().replaceAll(" ", "%20"); URI uri; try { uri = new URI(location); } catch (URISyntaxException ex) { throw new ProtocolException("Invalid redirect URI: " + location, ex); } HttpParams params = response.getParams(); // rfc2616 demands the location value be a complete URI // Location = "Location" ":" absoluteURI if (!uri.isAbsolute()) { if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) { throw new ProtocolException("Relative redirect location '" + uri + "' not allowed"); } // Adjust location URI HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (target == null) { throw new IllegalStateException("Target host not available " + "in the HTTP context"); } HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); try { URI requestURI = new URI(request.getRequestLine().getUri()); URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true); uri = URIUtils.resolve(absoluteRequestURI, uri); } catch (URISyntaxException ex) { throw new ProtocolException(ex.getMessage(), ex); } } if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) { RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS); if (redirectLocations == null) { redirectLocations = new RedirectLocations(); context.setAttribute(REDIRECT_LOCATIONS, redirectLocations); } URI redirectURI; if (uri.getFragment() != null) { try { HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); redirectURI = URIUtils.rewriteURI(uri, target, true); } catch (URISyntaxException ex) { throw new ProtocolException(ex.getMessage(), ex); } } else { redirectURI = uri; } if (redirectLocations.contains(redirectURI)) { throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'"); } else { redirectLocations.add(redirectURI); } } return uri; }
From source file:com.doculibre.constellio.utils.ConnectorManagerRequestUtils.java
public static Element sendGet(ConnectorManager connectorManager, String servletPath, Map<String, String> paramsMap) { if (paramsMap == null) { paramsMap = new HashMap<String, String>(); }/* w w w . j a va 2 s . co m*/ try { HttpParams params = new BasicHttpParams(); for (Iterator<String> it = paramsMap.keySet().iterator(); it.hasNext();) { String paramName = (String) it.next(); String paramValue = (String) paramsMap.get(paramName); params.setParameter(paramName, paramValue); } HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1"); HttpProtocolParams.setUseExpectContinue(params, true); BasicHttpProcessor httpproc = new BasicHttpProcessor(); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTargetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); URL connectorManagerURL = new URL(connectorManager.getUrl()); HttpHost host = new HttpHost(connectorManagerURL.getHost(), connectorManagerURL.getPort()); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); try { String target = connectorManager.getUrl() + servletPath; boolean firstParam = true; for (Iterator<String> it = paramsMap.keySet().iterator(); it.hasNext();) { String paramName = (String) it.next(); String paramValue = (String) paramsMap.get(paramName); if (firstParam) { target += "?"; firstParam = false; } else { target += "&"; } target += paramName + "=" + paramValue; } if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket, params); } BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("GET", target); LOGGER.fine(">> Request URI: " + request.getRequestLine().getUri()); request.setParams(params); httpexecutor.preProcess(request, httpproc, context); HttpResponse response = httpexecutor.execute(request, conn, context); response.setParams(params); httpexecutor.postProcess(response, httpproc, context); LOGGER.fine("<< Response: " + response.getStatusLine()); String entityText = EntityUtils.toString(response.getEntity()); LOGGER.fine(entityText); LOGGER.fine("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { LOGGER.fine("Connection kept alive..."); } try { Document xml = DocumentHelper.parseText(entityText); return xml.getRootElement(); } catch (Exception e) { LOGGER.severe("Error caused by text : " + entityText); throw e; } } finally { conn.close(); } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.devoteam.srit.xmlloader.http.test.HttpLoaderServer.java
public void sendRequest(String method, String uri, HttpParams params) throws IOException { // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTargetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestConnControl()); httpproc.addInterceptor(new RequestUserAgent()); context.setAttribute(ExecutionContext.HTTP_CONNECTION, Clientconn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, hostname); try {/*from w ww. j a va2 s .co m*/ testConnection(); BasicHttpRequest br = new BasicHttpRequest(method, uri); context.setAttribute(ExecutionContext.HTTP_REQUEST, br); br.setParams(params); //myclass.doSendRequest(br, Clientconn, context); } catch (Exception e) { } }