@Deprecated public DefaultRequestDirector( final Log log, final HttpRequestExecutor requestExec, final ClientConnectionManager conman, final ConnectionReuseStrategy reustrat, final ConnectionKeepAliveStrategy kastrat, final HttpRoutePlanner rouplan, final HttpProcessor httpProcessor, final HttpRequestRetryHandler retryHandler, final RedirectStrategy redirectStrategy, final AuthenticationHandler targetAuthHandler, final AuthenticationHandler proxyAuthHandler, final UserTokenHandler userTokenHandler, final HttpParams params) { this(LogFactory.getLog(DefaultRequestDirector.class), requestExec, conman, reustrat, kastrat, rouplan, httpProcessor, retryHandler, redirectStrategy, new AuthenticationStrategyAdaptor(targetAuthHandler), new AuthenticationStrategyAdaptor(proxyAuthHandler), userTokenHandler, params); }
/** * @param redirectStrategy redirect strategy, do not call * {@link HttpClientBuilder#disableRedirectHandling()} * @param redirectHandlingDisabled disable redirect strategy, do not call * {@link org.apache.http.impl.client.HttpClientBuilder#setRedirectStrategy(RedirectStrategy)} * @param tracer tracer instance * @param spanDecorators decorators */ public TracingHttpClientBuilder( RedirectStrategy redirectStrategy, boolean redirectHandlingDisabled, Tracer tracer, List<ApacheClientSpanDecorator> spanDecorators) { this.redirectStrategy = redirectStrategy; this.redirectHandlingDisabled = redirectHandlingDisabled; this.tracer = tracer; this.spanDecorators = new ArrayList<>(spanDecorators); super.setRedirectStrategy(redirectStrategy); if (redirectHandlingDisabled) { super.disableRedirectHandling(); } }
@Test public void doFollowCrossSiteRedirects() throws Exception { when(response.getStatusLine()).thenReturn(statusLine); final RedirectStrategy underTest = new NexusRedirectStrategy(); // simple cross redirect request = new HttpGet("http://hostA/dir"); when(statusLine.getStatusCode()).thenReturn(SC_MOVED_TEMPORARILY); when(response.getFirstHeader(argThat(equalToIgnoringCase(LOCATION)))) .thenReturn(new BasicHeader(LOCATION, "http://hostB/dir")); assertThat(underTest.isRedirected(request, response, new BasicHttpContext()), is(true)); // cross redirect to dir (failed coz NEXUS-5744) request = new HttpGet("http://hostA/dir/"); when(statusLine.getStatusCode()).thenReturn(SC_MOVED_TEMPORARILY); when(response.getFirstHeader(argThat(equalToIgnoringCase(LOCATION)))) .thenReturn(new BasicHeader(LOCATION, "http://hostB/dir/")); assertThat(underTest.isRedirected(request, response, new BasicHttpContext()), is(true)); }
/** * HTTP GET Without Handling HTTP redirects automatically * * @param url * @param headers * @return CloseableHttpResponse */ public static CloseableHttpResponse getWithoutAutoRedirect(String url, Map<String, String> headers) { if (url == null) { return null; } HttpGet get = new HttpGet(url); addHeaders(get, headers); CloseableHttpResponse response = null; try { client = HttpClients.custom().disableRedirectHandling().build(); response = visit(get); RedirectStrategy rs = DefaultRedirectStrategy.INSTANCE; client = HttpClients.custom().setRedirectStrategy(rs).build(); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); } return response; }
/** * @since 4.1 */ public synchronized final RedirectStrategy getRedirectStrategy() { if (redirectStrategy == null) { redirectStrategy = new DefaultRedirectStrategy(); } return redirectStrategy; }
/** * @deprecated (4.2) do not use */ @Deprecated protected RequestDirector createClientRequestDirector( final HttpRequestExecutor requestExec, final ClientConnectionManager conman, final ConnectionReuseStrategy reustrat, final ConnectionKeepAliveStrategy kastrat, final HttpRoutePlanner rouplan, final HttpProcessor httpProcessor, final HttpRequestRetryHandler retryHandler, final RedirectStrategy redirectStrategy, final AuthenticationHandler targetAuthHandler, final AuthenticationHandler proxyAuthHandler, final UserTokenHandler userTokenHandler, final HttpParams params) { return new DefaultRequestDirector( log, requestExec, conman, reustrat, kastrat, rouplan, httpProcessor, retryHandler, redirectStrategy, targetAuthHandler, proxyAuthHandler, userTokenHandler, params); }
/** * @since 4.2 */ protected RequestDirector createClientRequestDirector( final HttpRequestExecutor requestExec, final ClientConnectionManager conman, final ConnectionReuseStrategy reustrat, final ConnectionKeepAliveStrategy kastrat, final HttpRoutePlanner rouplan, final HttpProcessor httpProcessor, final HttpRequestRetryHandler retryHandler, final RedirectStrategy redirectStrategy, final AuthenticationStrategy targetAuthStrategy, final AuthenticationStrategy proxyAuthStrategy, final UserTokenHandler userTokenHandler, final HttpParams params) { return new DefaultRequestDirector( log, requestExec, conman, reustrat, kastrat, rouplan, httpProcessor, retryHandler, redirectStrategy, targetAuthStrategy, proxyAuthStrategy, userTokenHandler, params); }
public TracingClientExec( ClientExecChain clientExecChain, RedirectStrategy redirectStrategy, boolean redirectHandlingDisabled, Tracer tracer, List<ApacheClientSpanDecorator> spanDecorators) { this.requestExecutor = clientExecChain; this.redirectStrategy = redirectStrategy; this.redirectHandlingDisabled = redirectHandlingDisabled; this.tracer = tracer; this.spanDecorators = new ArrayList<>(spanDecorators); }
/** * @param redirectStrategy redirect strategy, do not call * {@link HttpClientBuilder#disableRedirectHandling()} * @param redirectHandlingDisabled disable redirect strategy, do not call * {@link org.apache.http.impl.client.HttpClientBuilder#setRedirectStrategy(RedirectStrategy)} */ public TracingHttpClientBuilder( RedirectStrategy redirectStrategy, boolean redirectHandlingDisabled) { this(redirectStrategy, redirectHandlingDisabled, GlobalTracer.get(), Collections.<ApacheClientSpanDecorator>singletonList(new StandardTags())); }
private void setRedirectStrategy(HttpClientBuilder httpClientBuilder) { RedirectStrategy redirectStrategy = config.getRedirectStrategy().getImplementation(); if (redirectStrategy == null) { httpClientBuilder.disableRedirectHandling(); } else { httpClientBuilder.setRedirectStrategy(redirectStrategy); } }
public RedirectExec( final ClientExecChain requestExecutor, final HttpRoutePlanner routePlanner, final RedirectStrategy redirectStrategy) { super(); Args.notNull(requestExecutor, "HTTP client request executor"); Args.notNull(routePlanner, "HTTP route planner"); Args.notNull(redirectStrategy, "HTTP redirect strategy"); this.requestExecutor = requestExecutor; this.routePlanner = routePlanner; this.redirectStrategy = redirectStrategy; }
@Test public void doNotFollowRedirectsToDirIndex() throws Exception { when(response.getStatusLine()).thenReturn(statusLine); final RedirectStrategy underTest = new NexusRedirectStrategy(); HttpContext httpContext; // no location header request = new HttpGet("http://localhost/dir/fileA"); httpContext = new BasicHttpContext(); httpContext.setAttribute(CONTENT_RETRIEVAL_MARKER_KEY, Boolean.TRUE); when(statusLine.getStatusCode()).thenReturn(SC_OK); assertThat(underTest.isRedirected(request, response, httpContext), is(false)); // redirect to file request = new HttpGet("http://localhost/dir/fileA"); httpContext = new BasicHttpContext(); httpContext.setAttribute(CONTENT_RETRIEVAL_MARKER_KEY, Boolean.TRUE); when(statusLine.getStatusCode()).thenReturn(SC_MOVED_TEMPORARILY); when(response.getFirstHeader(argThat(equalToIgnoringCase(LOCATION)))) .thenReturn(new BasicHeader(LOCATION, "http://localhost/dir/fileB")); assertThat(underTest.isRedirected(request, response, httpContext), is(true)); // redirect to dir request = new HttpGet("http://localhost/dir"); httpContext = new BasicHttpContext(); httpContext.setAttribute(CONTENT_RETRIEVAL_MARKER_KEY, Boolean.TRUE); when(statusLine.getStatusCode()).thenReturn(SC_MOVED_TEMPORARILY); when(response.getFirstHeader(argThat(equalToIgnoringCase(LOCATION)))) .thenReturn(new BasicHeader(LOCATION, "http://localhost/dir/")); assertThat(underTest.isRedirected(request, response, httpContext), is(false)); }
@Override protected RequestDirector createClientRequestDirector(HttpRequestExecutor requestExec, ClientConnectionManager conman, ConnectionReuseStrategy reustrat, ConnectionKeepAliveStrategy kastrat, HttpRoutePlanner rouplan, HttpProcessor httpProcessor, HttpRequestRetryHandler retryHandler, RedirectStrategy redirectStrategy, AuthenticationStrategy targetAuthStrategy, AuthenticationStrategy proxyAuthStrategy, UserTokenHandler userTokenHandler, HttpParams params) { return new RedirectRequestDirector(log, requestExec, conman, reustrat, kastrat, rouplan, httpProcessor, retryHandler, redirectStrategy, targetAuthStrategy, proxyAuthStrategy, userTokenHandler, params); }
public RedirectRequestDirector(Log log, HttpRequestExecutor requestExec, ClientConnectionManager conman, ConnectionReuseStrategy reustrat, ConnectionKeepAliveStrategy kastrat, HttpRoutePlanner rouplan, HttpProcessor httpProcessor, HttpRequestRetryHandler retryHandler, RedirectStrategy redirectStrategy, AuthenticationStrategy targetAuthStrategy, AuthenticationStrategy proxyAuthStrategy, UserTokenHandler userTokenHandler, HttpParams params) { super(log, requestExec, conman, reustrat, kastrat, rouplan, httpProcessor, retryHandler, redirectStrategy, targetAuthStrategy, proxyAuthStrategy, userTokenHandler, params); }
protected RedirectStrategy createRedirectStrategy() { return new FormatLocationRedirectStrategy(); }
public RedirectStrategy getRedirectionStrategy() { return this.redirectionStrategy; }
public void setRedirectionStrategy(final RedirectStrategy redirectionStrategy) { this.redirectionStrategy = redirectionStrategy; }
public DatarouterHttpClientBuilder setRedirectStrategy(RedirectStrategy redirectStrategy){ httpClientBuilder.setRedirectStrategy(redirectStrategy); return this; }
/** * @since 4.1 */ public synchronized void setRedirectStrategy(final RedirectStrategy strategy) { this.redirectStrategy = strategy; }
/** * Create a redirect and robots.txt strategy * @return strategy */ protected RedirectStrategy createRedirectAndRobotsStrategy() { return new DefaultRedirectHandler( getUserAgent(), new RobotDirectives( "*", getCrawlDelay())); }
/** * @since 4.2 */ public DefaultRequestDirector( final Log log, final HttpRequestExecutor requestExec, final ClientConnectionManager conman, final ConnectionReuseStrategy reustrat, final ConnectionKeepAliveStrategy kastrat, final HttpRoutePlanner rouplan, final HttpProcessor httpProcessor, final HttpRequestRetryHandler retryHandler, final RedirectStrategy redirectStrategy, final AuthenticationStrategy targetAuthStrategy, final AuthenticationStrategy proxyAuthStrategy, final UserTokenHandler userTokenHandler, final HttpParams params) { Args.notNull(log, "Log"); Args.notNull(requestExec, "Request executor"); Args.notNull(conman, "Client connection manager"); Args.notNull(reustrat, "Connection reuse strategy"); Args.notNull(kastrat, "Connection keep alive strategy"); Args.notNull(rouplan, "Route planner"); Args.notNull(httpProcessor, "HTTP protocol processor"); Args.notNull(retryHandler, "HTTP request retry handler"); Args.notNull(redirectStrategy, "Redirect strategy"); Args.notNull(targetAuthStrategy, "Target authentication strategy"); Args.notNull(proxyAuthStrategy, "Proxy authentication strategy"); Args.notNull(userTokenHandler, "User token handler"); Args.notNull(params, "HTTP parameters"); this.log = log; this.authenticator = new HttpAuthenticator(log); this.requestExec = requestExec; this.connManager = conman; this.reuseStrategy = reustrat; this.keepAliveStrategy = kastrat; this.routePlanner = rouplan; this.httpProcessor = httpProcessor; this.retryHandler = retryHandler; this.redirectStrategy = redirectStrategy; this.targetAuthStrategy = targetAuthStrategy; this.proxyAuthStrategy = proxyAuthStrategy; this.userTokenHandler = userTokenHandler; this.params = params; if (redirectStrategy instanceof DefaultRedirectStrategyAdaptor) { this.redirectHandler = ((DefaultRedirectStrategyAdaptor) redirectStrategy).getHandler(); } else { this.redirectHandler = null; } if (targetAuthStrategy instanceof AuthenticationStrategyAdaptor) { this.targetAuthHandler = ((AuthenticationStrategyAdaptor) targetAuthStrategy).getHandler(); } else { this.targetAuthHandler = null; } if (proxyAuthStrategy instanceof AuthenticationStrategyAdaptor) { this.proxyAuthHandler = ((AuthenticationStrategyAdaptor) proxyAuthStrategy).getHandler(); } else { this.proxyAuthHandler = null; } this.managedConn = null; this.execCount = 0; this.redirectCount = 0; this.targetAuthState = new AuthState(); this.proxyAuthState = new AuthState(); this.maxRedirects = this.params.getIntParameter(ClientPNames.MAX_REDIRECTS, 100); }
/** * Read the contents of the given {@link URL} as a * {@link ByteArrayInputStream} (i.e. a byte[] in memory wrapped in an * {@link InputStream}). If redirects are not being followed, then the * result will be null if the URL is redirected. * * @param url * the URL to read from * @param connectionTimeout * amount of time to wait for connection * @param readTimeout * amount of time to wait for reading * @param redirectStrategy * the redirection strategy * @param userAgent * the useragent string * @return the content referenced by the URL * @throws IOException * if an error occurs * @throws IllegalArgumentException * if the URL is not an HTTP(s) URL */ public static IndependentPair<HttpEntity, ByteArrayInputStream> readURLAsByteArrayInputStream(URL url, int connectionTimeout, int readTimeout, RedirectStrategy redirectStrategy, String userAgent) throws IOException { DefaultHttpClient c = null; try { final HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, connectionTimeout); HttpConnectionParams.setSoTimeout(params, readTimeout); HttpProtocolParams.setUserAgent(params, userAgent); HttpClientParams.setRedirecting(params, redirectStrategy != null); final boolean followRedirects = redirectStrategy != null; c = new DefaultHttpClient(params); if (followRedirects) c.setRedirectStrategy(redirectStrategy); HttpResponse resp = null; try { resp = c.execute(new HttpGet(url.toURI())); } catch (final URISyntaxException e) { throw new IOException(e); } final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); final InputStream stream = resp.getEntity().getContent(); final byte[] tempBuffer = new byte[1024]; // read the rest! while (true) { final int readThisTime = stream.read(tempBuffer); if (readThisTime == -1) { break; } // write to the outStream outStream.write(tempBuffer, 0, readThisTime); } final IndependentPair<HttpEntity, ByteArrayInputStream> toRet = IndependentPair.pair(resp.getEntity(), new ByteArrayInputStream(outStream.toByteArray())); ; return toRet; } finally { if (c != null) c.getConnectionManager().shutdown(); } }
public HttpClientBuilderFacade setRedirect( RedirectStrategy redirectStrategy ) { this.redirectStrategy = redirectStrategy; return this; }
/** * Assigns {@link RedirectStrategy} instance. * <p>Please note this value can be overridden by the {@link #disableRedirectHandling()} method.</p> * * @param redirectStrategy custom redirect strategy * @return this */ public final InternalBuilder<T> setRedirectStrategy(final RedirectStrategy redirectStrategy) { httpClientBuilder.setRedirectStrategy(redirectStrategy); return this; }