List of usage examples for org.apache.http.protocol HttpContext setAttribute
void setAttribute(String str, Object obj);
From source file:com.grendelscan.commons.http.apache_overrides.client.CustomClientRequestDirector.java
/** * Creates a tunnel to the target server. * The connection must be established to the (last) proxy. * A CONNECT request for tunnelling through the proxy will * be created and sent, the response received and checked. * This method does <i>not</i> update the connection with * information about the tunnel, that is left to the caller. * // ww w .ja v a 2s. c om * @param route * the route to establish * @param context * the context for request execution * * @return <code>true</code> if the tunnelled route is secure, * <code>false</code> otherwise. * The implementation here always returns <code>false</code>, * but derived classes may override. * * @throws HttpException * in case of a problem * @throws IOException * in case of an IO problem */ private boolean createTunnelToTarget(HttpRoute route, HttpContext context) throws HttpException, IOException { HttpHost proxy = route.getProxyHost(); HttpHost target = route.getTargetHost(); HttpResponse response = null; boolean done = false; while (!done) { done = true; if (!managedConn.isOpen()) { managedConn.open(route, context, params); } HttpRequest connect = createConnectRequest(route); connect.setParams(params); // Populate the execution context context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target); context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, proxy); context.setAttribute(ExecutionContext.HTTP_CONNECTION, managedConn); context.setAttribute(ClientContext.TARGET_AUTH_STATE, targetAuthState); context.setAttribute(ClientContext.PROXY_AUTH_STATE, proxyAuthState); context.setAttribute(ExecutionContext.HTTP_REQUEST, connect); requestExec.preProcess(connect, httpProcessor, context); response = requestExec.execute(connect, managedConn, context); response.setParams(params); requestExec.postProcess(response, httpProcessor, context); int status = response.getStatusLine().getStatusCode(); if (status < 200) { throw new HttpException("Unexpected response to CONNECT request: " + response.getStatusLine()); } CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); if ((credsProvider != null) && HttpClientParams.isAuthenticating(params)) { if (proxyAuthHandler.isAuthenticationRequested(response, context)) { LOGGER.debug("Proxy requested authentication"); Map<String, Header> challenges = proxyAuthHandler.getChallenges(response, context); try { processChallenges(challenges, proxyAuthState, proxyAuthHandler, response, context); } catch (AuthenticationException ex) { LOGGER.warn("Authentication error: " + ex.getMessage()); } updateAuthState(proxyAuthState, proxy, credsProvider); if (proxyAuthState.getCredentials() != null) { done = false; // Retry request if (reuseStrategy.keepAlive(response, context)) { LOGGER.debug("Connection kept alive"); // Consume response content HttpEntity entity = response.getEntity(); if (entity != null) { entity.consumeContent(); } } else { managedConn.close(); } } } else { // Reset proxy auth scope proxyAuthState.setAuthScope(null); } } } int status = response.getStatusLine().getStatusCode(); // can't be null if (status > 299) { // Buffer response content HttpEntity entity = response.getEntity(); if (entity != null) { response.setEntity(new BufferedHttpEntity(entity)); } managedConn.close(); throw new TunnelRefusedException("CONNECT refused by proxy: " + response.getStatusLine(), response); } managedConn.markReusable(); // How to decide on security of the tunnelled connection? // The socket factory knows only about the segment to the proxy. // Even if that is secure, the hop to the target may be insecure. // Leave it to derived classes, consider insecure by default here. return false; }
From source file:org.apache.sling.testing.clients.interceptors.StickyCookieInterceptor.java
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException { final HttpClientContext clientContext = HttpClientContext.adapt(httpContext); List<Cookie> cookies = clientContext.getCookieStore().getCookies(); boolean set = (null != StickyCookieHolder.getTestStickySessionCookie()); boolean found = false; ListIterator<Cookie> it = cookies.listIterator(); while (it.hasNext()) { Cookie cookie = it.next();/*from ww w.j a v a2 s.c o m*/ if (cookie.getName().equals(StickyCookieHolder.COOKIE_NAME)) { found = true; if (set) { // set the cookie with the value saved for each thread using the rule it.set(StickyCookieHolder.getTestStickySessionCookie()); } else { // if the cookie is not set in TestStickySessionRule, remove it from here it.remove(); } } } // if the cookie needs to be set from TestStickySessionRule but did not exist in the client cookie list, add it here. if (!found && set) { cookies.add(StickyCookieHolder.getTestStickySessionCookie()); } BasicCookieStore cs = new BasicCookieStore(); cs.addCookies(cookies.toArray(new Cookie[cookies.size()])); httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cs); }
From source file:org.apache.synapse.transport.nhttp.ClientHandler.java
private void setServerContextAttribute(String key, Object value, NHttpClientConnection conn) { MessageContext msgCtx = getMessageContext(conn); if (msgCtx != null) { Object outTransport = msgCtx.getProperty(Constants.OUT_TRANSPORT_INFO); if (outTransport != null && outTransport instanceof ServerWorker) { HttpContext context = ((ServerWorker) outTransport).getConn().getContext(); context.setAttribute(key, value); }/* w ww . j a v a 2 s. co m*/ } }
From source file:crawlercommons.fetcher.http.SimpleHttpFetcher.java
private FetchedResult doRequest(HttpRequestBase request, String url, Payload payload) throws BaseFetchException { LOGGER.trace("Fetching " + url); HttpResponse response;/*ww w .ja v a 2s . co m*/ long readStartTime; Metadata headerMap = new Metadata(); String redirectedUrl = null; String newBaseUrl = null; int numRedirects = 0; boolean needAbort = true; String contentType = ""; String mimeType = ""; String hostAddress = null; int statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; String reasonPhrase = null; // Create a local instance of cookie store, and bind to local context // Without this we get killed w/lots of threads, due to sync() on single // cookie store. HttpContext localContext = new BasicHttpContext(); CookieStore cookieStore = localCookieStore.get(); localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); StringBuilder fetchTrace = null; if (LOGGER.isTraceEnabled()) { fetchTrace = new StringBuilder("Fetched url: " + url); } try { request.setURI(new URI(url)); readStartTime = System.currentTimeMillis(); response = _httpClient.execute(request, localContext); Header[] headers = response.getAllHeaders(); for (Header header : headers) { headerMap.add(header.getName(), header.getValue()); } statusCode = response.getStatusLine().getStatusCode(); reasonPhrase = response.getStatusLine().getReasonPhrase(); if (LOGGER.isTraceEnabled()) { fetchTrace.append("; status code: " + statusCode); if (headerMap.get(HttpHeaders.CONTENT_LENGTH) != null) { fetchTrace.append("; Content-Length: " + headerMap.get(HttpHeaders.CONTENT_LENGTH)); } if (headerMap.get(HttpHeaders.LOCATION) != null) { fetchTrace.append("; Location: " + headerMap.get(HttpHeaders.LOCATION)); } } if ((statusCode < 200) || (statusCode >= 300)) { // We can't just check against SC_OK, as some wackos return 201, // 202, etc throw new HttpFetchException(url, "Error fetching " + url + " due to \"" + reasonPhrase + "\"", statusCode, headerMap); } redirectedUrl = extractRedirectedUrl(url, localContext); URI permRedirectUri = (URI) localContext.getAttribute(PERM_REDIRECT_CONTEXT_KEY); if (permRedirectUri != null) { newBaseUrl = permRedirectUri.toURL().toExternalForm(); } Integer redirects = (Integer) localContext.getAttribute(REDIRECT_COUNT_CONTEXT_KEY); if (redirects != null) { numRedirects = redirects.intValue(); } hostAddress = (String) (localContext.getAttribute(HOST_ADDRESS)); if (hostAddress == null) { throw new UrlFetchException(url, "Host address not saved in context"); } Header cth = response.getFirstHeader(HttpHeaders.CONTENT_TYPE); if (cth != null) { contentType = cth.getValue(); } // Check if we should abort due to mime-type filtering. Note that // this will fail if the server // doesn't report a mime-type, but that's how we want it as this // configuration is typically // used when only a subset of parsers are installed/enabled, so we // don't want the auto-detect // code in Tika to get triggered & try to process an unsupported // type. If you want unknown // mime-types from the server to be processed, set "" as one of the // valid mime-types in // FetcherPolicy. mimeType = getMimeTypeFromContentType(contentType); Set<String> mimeTypes = getValidMimeTypes(); if ((mimeTypes != null) && (mimeTypes.size() > 0)) { if (!mimeTypes.contains(mimeType)) { throw new AbortedFetchException(url, "Invalid mime-type: " + mimeType, AbortedFetchReason.INVALID_MIMETYPE); } } needAbort = false; } catch (ClientProtocolException e) { // Oleg guarantees that no abort is needed in the case of an // IOException // (which is is a subclass of) needAbort = false; // If the root case was a "too many redirects" error, we want to map // this to a specific // exception that contains the final redirect. if (e.getCause() instanceof MyRedirectException) { MyRedirectException mre = (MyRedirectException) e.getCause(); String redirectUrl = url; try { redirectUrl = mre.getUri().toURL().toExternalForm(); } catch (MalformedURLException e2) { LOGGER.warn("Invalid URI saved during redirect handling: " + mre.getUri()); } throw new RedirectFetchException(url, redirectUrl, mre.getReason()); } else if (e.getCause() instanceof RedirectException) { LOGGER.error(e.getMessage()); throw new RedirectFetchException(url, extractRedirectedUrl(url, localContext), RedirectExceptionReason.TOO_MANY_REDIRECTS); } else { throw new IOFetchException(url, e); } } catch (IOException e) { // Oleg guarantees that no abort is needed in the case of an // IOException needAbort = false; throw new IOFetchException(url, e); } catch (URISyntaxException e) { throw new UrlFetchException(url, e.getMessage()); } catch (IllegalStateException e) { throw new UrlFetchException(url, e.getMessage()); } catch (BaseFetchException e) { throw e; } catch (Exception e) { // Map anything else to a generic IOFetchException // TODO KKr - create generic fetch exception throw new IOFetchException(url, new IOException(e)); } finally { safeAbort(needAbort, request); } // Figure out how much data we want to try to fetch. int maxContentSize = getMaxContentSize(mimeType); int targetLength = maxContentSize; boolean truncated = false; String contentLengthStr = headerMap.get(HttpHeaders.CONTENT_LENGTH); if (contentLengthStr != null) { try { int contentLength = Integer.parseInt(contentLengthStr); if (contentLength > targetLength) { truncated = true; } else { targetLength = contentLength; } } catch (NumberFormatException e) { // Ignore (and log) invalid content length values. LOGGER.warn("Invalid content length in header: " + contentLengthStr); } } // Now finally read in response body, up to targetLength bytes. // Note that entity might be null, for zero length responses. byte[] content = new byte[0]; long readRate = 0; HttpEntity entity = response.getEntity(); needAbort = true; if (entity != null) { InputStream in = null; try { in = entity.getContent(); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = 0; int totalRead = 0; ByteArrayOutputStream out = new ByteArrayOutputStream(DEFAULT_BYTEARRAY_SIZE); int readRequests = 0; int minResponseRate = getMinResponseRate(); // TODO KKr - we need to monitor the rate while reading a // single block. Look at HttpClient // metrics support for how to do this. Once we fix this, fix // the test to read a smaller (< 20K) // chuck of data. while ((totalRead < targetLength) && ((bytesRead = in.read(buffer, 0, Math.min(buffer.length, targetLength - totalRead))) != -1)) { readRequests += 1; totalRead += bytesRead; out.write(buffer, 0, bytesRead); // Assume read time is at least one millisecond, to avoid // DBZ exception. long totalReadTime = Math.max(1, System.currentTimeMillis() - readStartTime); readRate = (totalRead * 1000L) / totalReadTime; // Don't bail on the first read cycle, as we can get a // hiccup starting out. // Also don't bail if we've read everything we need. if ((readRequests > 1) && (totalRead < targetLength) && (readRate < minResponseRate)) { throw new AbortedFetchException(url, "Slow response rate of " + readRate + " bytes/sec", AbortedFetchReason.SLOW_RESPONSE_RATE); } // Check to see if we got interrupted, but don't clear the // interrupted flag. if (Thread.currentThread().isInterrupted()) { throw new AbortedFetchException(url, AbortedFetchReason.INTERRUPTED); } } content = out.toByteArray(); needAbort = truncated || (in.available() > 0); } catch (IOException e) { // We don't need to abort if there's an IOException throw new IOFetchException(url, e); } finally { safeAbort(needAbort, request); safeClose(in); } } // Toss truncated image content. if ((truncated) && (!isTextMimeType(mimeType))) { throw new AbortedFetchException(url, "Truncated image", AbortedFetchReason.CONTENT_SIZE); } // Now see if we need to uncompress the content. String contentEncoding = headerMap.get(HttpHeaders.CONTENT_ENCODING); if (contentEncoding != null) { if (LOGGER.isTraceEnabled()) { fetchTrace.append("; Content-Encoding: " + contentEncoding); } // TODO KKr We might want to just decompress a truncated gzip // containing text (since we have a max content size to save us // from any gzip corruption). We might want to break the following // out into a separate method, by the way (if not refactor this // entire monolithic method). // try { if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) { if (truncated) { throw new AbortedFetchException(url, "Truncated compressed data", AbortedFetchReason.CONTENT_SIZE); } else { ExpandedResult expandedResult = EncodingUtils.processGzipEncoded(content, maxContentSize); truncated = expandedResult.isTruncated(); if ((truncated) && (!isTextMimeType(mimeType))) { throw new AbortedFetchException(url, "Truncated decompressed image", AbortedFetchReason.CONTENT_SIZE); } else { content = expandedResult.getExpanded(); if (LOGGER.isTraceEnabled()) { fetchTrace.append("; unzipped to " + content.length + " bytes"); } } // } else if ("deflate".equals(contentEncoding)) { // content = // EncodingUtils.processDeflateEncoded(content); // if (LOGGER.isTraceEnabled()) { // fetchTrace.append("; inflated to " + content.length + // " bytes"); // } } } } catch (IOException e) { throw new IOFetchException(url, e); } } // Finally dump out the trace msg we've been building. if (LOGGER.isTraceEnabled()) { LOGGER.trace(fetchTrace.toString()); } // TODO KKr - Save truncated flag in FetchedResult/FetchedDatum. return new FetchedResult(url, redirectedUrl, System.currentTimeMillis(), headerMap, content, contentType, (int) readRate, payload, newBaseUrl, numRedirects, hostAddress, statusCode, reasonPhrase); }
From source file:io.milton.httpclient.Host.java
protected HttpContext newContext() { HttpContext context = new BasicHttpContext(); AuthScheme authScheme;//from w w w . j a v a 2 s .c o m if (useDigestForPreemptiveAuth) { authScheme = new DigestScheme(); } else { authScheme = new BasicScheme(); } context.setAttribute("preemptive-auth", authScheme); return context; }
From source file:org.apache.synapse.transport.nhttp.ClientHandler.java
/** * Invoked when the destination is connected * /*from w ww . ja v a 2 s .c o m*/ * @param conn the connection being processed * @param attachment the attachment set previously */ public void connected(final NHttpClientConnection conn, final Object attachment) { if (log.isDebugEnabled()) { log.debug(conn + ": ClientHandler connected : " + conn); } HttpContext context = conn.getContext(); metrics.connected(); // record connection creation time for debug logging context.setAttribute(CONNECTION_CREATION_TIME, System.currentTimeMillis()); if (countConnections) { recordConnection(conn); } Axis2HttpRequest axis2Req = (Axis2HttpRequest) attachment; HttpRoute route = axis2Req.getRoute(); if (route.isTunnelled()) { // Requires a proxy tunnel ProxyTunnelHandler tunnelHandler = new ProxyTunnelHandler(route, connFactory); context.setAttribute(TUNNEL_HANDLER, tunnelHandler); } context.setAttribute(ATTACHMENT_KEY, axis2Req); conn.requestOutput(); }
From source file:org.apache.synapse.transport.nhttp.ServerHandler.java
/** * Process ready output by writing into the channel * @param conn the connection being processed * @param encoder the content encoder in use *//* ww w. ja v a2s. c o m*/ public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) { HttpContext context = conn.getContext(); HttpResponse response = conn.getHttpResponse(); ContentOutputBuffer outBuf = (ContentOutputBuffer) context.getAttribute(RESPONSE_SOURCE_BUFFER); if (outBuf == null) { // fix for SYNAPSE 584. This is a temporaly fix becuase of HTTPCORE-208 shutdownConnection(conn, false, null); return; } try { int bytesWritten = outBuf.produceContent(encoder); if (metrics != null && bytesWritten > 0) { metrics.incrementBytesSent(bytesWritten); } if (encoder.isCompleted()) { long currentTime = System.currentTimeMillis(); context.setAttribute(NhttpConstants.RES_TO_CLIENT_WRITE_END_TIME, currentTime); context.setAttribute(NhttpConstants.RES_DEPARTURE_TIME, currentTime); updateLatencyView(context); context.removeAttribute(NhttpConstants.REQ_ARRIVAL_TIME); context.removeAttribute(NhttpConstants.REQ_DEPARTURE_TIME); context.removeAttribute(NhttpConstants.RES_ARRIVAL_TIME); ((ServerConnectionDebug) conn.getContext().getAttribute(SERVER_CONNECTION_DEBUG)) .recordResponseCompletionTime(); Boolean reqRead = (Boolean) conn.getContext().getAttribute(NhttpConstants.REQUEST_READ); Boolean forceConnectionClose = (Boolean) conn.getContext() .getAttribute(NhttpConstants.FORCE_CONNECTION_CLOSE); if (reqRead != null && !reqRead) { try { // this is a connection we should not re-use conn.close(); } catch (Exception ignore) { } } else if (!connStrategy.keepAlive(response, context)) { conn.close(); } else if (forceConnectionClose != null && forceConnectionClose) { conn.close(); } else { conn.requestInput(); } } } catch (IOException e) { if (metrics != null) { metrics.incrementFaultsSending(); } handleException("I/O Error at outputReady : " + e.getMessage(), e, conn); } }
From source file:org.apache.synapse.transport.nhttp.ClientHandler.java
/** * Handle IO errors while reading or writing to underlying channels * //from w w w .ja va2 s . c o m * @param conn the connection being processed * @param ex the exception encountered */ public void exception(final NHttpClientConnection conn, final Exception ex) { HttpContext context = conn.getContext(); Axis2HttpRequest axis2Req = (Axis2HttpRequest) context.getAttribute(ATTACHMENT_KEY); context.setAttribute(AXIS2_HTTP_REQUEST, axis2Req); if (ex instanceof HttpException) { String message = getErrorMessage("HTTP protocol violation : " + ex.getMessage(), conn); log.error(message, ex); checkAxisRequestComplete(conn, NhttpConstants.PROTOCOL_VIOLATION, message, ex); shutdownConnection(conn, true, "HTTP protocol violation : " + ex.getMessage()); } if (ex instanceof IOException) { String message = getErrorMessage("I/O error : " + ex.getMessage(), conn); if (message.toLowerCase().indexOf("reset") != -1) { log.warn(message); } else { log.error(message, ex); } checkAxisRequestComplete(conn, NhttpConstants.SND_IO_ERROR_SENDING, message, ex); shutdownConnection(conn, true, "I/O error : " + ex.getMessage()); } else { log.error(ex.getMessage(), ex); shutdownConnection(conn, true, "Error occurred : " + ex.getMessage()); } }