Example usage for org.apache.http.client.config RequestConfig getMaxRedirects

List of usage examples for org.apache.http.client.config RequestConfig getMaxRedirects

Introduction

In this page you can find the example usage for org.apache.http.client.config RequestConfig getMaxRedirects.

Prototype

public int getMaxRedirects() 

Source Link

Document

Returns the maximum number of redirects to be followed.

Usage

From source file:org.apache.maven.wagon.providers.http.HttpClientWagonTest.java

public void testSetMaxRedirectsParamViaConfig() {
    HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
    int maxRedirects = 2;
    methodConfig.addParam("http.protocol.max-redirects", "%i," + maxRedirects);

    HttpConfiguration config = new HttpConfiguration();
    config.setAll(methodConfig);/*from w  w w. j  a v  a2s .  c o  m*/

    HttpHead method = new HttpHead();
    RequestConfig.Builder builder = RequestConfig.custom();
    ConfigurationUtils.copyConfig(config.getMethodConfiguration(method), builder);
    RequestConfig requestConfig = builder.build();

    assertEquals(2, requestConfig.getMaxRedirects());
}

From source file:com.lehman.ic9.net.httpClient.java

/**
 * Gets an integer with the max number of redirects allowed.
 * @return A int with max redirects./*from   w  w  w. j a va  2 s.co  m*/
 */
public int getMaxRedirects() {
    RequestConfig rc = this.rcb.build();
    return rc.getMaxRedirects();
}

From source file:org.apache.http.impl.execchain.RedirectExec.java

public CloseableHttpResponse execute(final HttpRoute route, final HttpRequestWrapper request,
        final HttpClientContext context, final HttpExecutionAware execAware) throws IOException, HttpException {
    Args.notNull(route, "HTTP route");
    Args.notNull(request, "HTTP request");
    Args.notNull(context, "HTTP context");

    final List<URI> redirectLocations = context.getRedirectLocations();
    if (redirectLocations != null) {
        redirectLocations.clear();/*w  w w .j a  va 2 s  .  c  om*/
    }

    final RequestConfig config = context.getRequestConfig();
    final int maxRedirects = config.getMaxRedirects() > 0 ? config.getMaxRedirects() : 50;
    HttpRoute currentRoute = route;
    HttpRequestWrapper currentRequest = request;
    for (int redirectCount = 0;;) {
        final CloseableHttpResponse response = requestExecutor.execute(currentRoute, currentRequest, context,
                execAware);
        try {
            if (config.isRedirectsEnabled()
                    && this.redirectStrategy.isRedirected(currentRequest, response, context)) {

                if (redirectCount >= maxRedirects) {
                    throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
                }
                redirectCount++;

                final HttpRequest redirect = this.redirectStrategy.getRedirect(currentRequest, response,
                        context);
                if (!redirect.headerIterator().hasNext()) {
                    final HttpRequest original = request.getOriginal();
                    redirect.setHeaders(original.getAllHeaders());
                }
                currentRequest = HttpRequestWrapper.wrap(redirect);

                if (currentRequest instanceof HttpEntityEnclosingRequest) {
                    Proxies.enhanceEntity((HttpEntityEnclosingRequest) currentRequest);
                }

                final URI uri = currentRequest.getURI();
                final HttpHost newTarget = URIUtils.extractHost(uri);
                if (newTarget == null) {
                    throw new ProtocolException("Redirect URI does not specify a valid host name: " + uri);
                }

                // Reset virtual host and auth states if redirecting to another host
                if (!currentRoute.getTargetHost().equals(newTarget)) {
                    final AuthState targetAuthState = context.getTargetAuthState();
                    if (targetAuthState != null) {
                        this.log.debug("Resetting target auth state");
                        targetAuthState.reset();
                    }
                    final AuthState proxyAuthState = context.getProxyAuthState();
                    if (proxyAuthState != null) {
                        final AuthScheme authScheme = proxyAuthState.getAuthScheme();
                        if (authScheme != null && authScheme.isConnectionBased()) {
                            this.log.debug("Resetting proxy auth state");
                            proxyAuthState.reset();
                        }
                    }
                }

                currentRoute = this.routePlanner.determineRoute(newTarget, currentRequest, context);
                if (this.log.isDebugEnabled()) {
                    this.log.debug("Redirecting to '" + uri + "' via " + currentRoute);
                }
                EntityUtils.consume(response.getEntity());
                response.close();
            } else {
                return response;
            }
        } catch (final RuntimeException ex) {
            response.close();
            throw ex;
        } catch (final IOException ex) {
            response.close();
            throw ex;
        } catch (final HttpException ex) {
            // Protocol exception related to a direct.
            // The underlying connection may still be salvaged.
            try {
                EntityUtils.consume(response.getEntity());
            } catch (final IOException ioex) {
                this.log.debug("I/O error while releasing connection", ioex);
            } finally {
                response.close();
            }
            throw ex;
        }
    }
}

From source file:org.apache.http.impl.nio.client.MainClientExec.java

private boolean handleResponse(final InternalState state, final AbstractClientExchangeHandler<?> handler)
        throws HttpException {
    final HttpClientContext localContext = state.getLocalContext();
    final RequestConfig config = localContext.getRequestConfig();
    if (config.isAuthenticationEnabled()) {
        if (needAuthentication(state, handler)) {
            // discard previous auth headers
            final HttpRequestWrapper currentRequest = handler.getCurrentRequest();
            final HttpRequest original = currentRequest.getOriginal();
            if (!original.containsHeader(AUTH.WWW_AUTH_RESP)) {
                currentRequest.removeHeaders(AUTH.WWW_AUTH_RESP);
            }//from  w  w w .  jav  a 2  s  . c  o  m
            if (!original.containsHeader(AUTH.PROXY_AUTH_RESP)) {
                currentRequest.removeHeaders(AUTH.PROXY_AUTH_RESP);
            }
            return true;
        }
    }
    if (config.isRedirectsEnabled()) {
        final HttpRequest currentRequest = handler.getCurrentRequest();
        final HttpResponse currentResponse = handler.getCurrentResponse();
        if (this.redirectStrategy.isRedirected(currentRequest, currentResponse, localContext)) {
            final int maxRedirects = config.getMaxRedirects() >= 0 ? config.getMaxRedirects() : 100;
            if (state.getRedirectCount() >= maxRedirects) {
                throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded");
            }
            state.incrementRedirectCount();
            final HttpUriRequest redirect = this.redirectStrategy.getRedirect(currentRequest, currentResponse,
                    localContext);
            state.setRedirect(redirect);
            return true;
        }
    }
    return false;
}