Example usage for org.apache.http.client RedirectStrategy RedirectStrategy

List of usage examples for org.apache.http.client RedirectStrategy RedirectStrategy

Introduction

In this page you can find the example usage for org.apache.http.client RedirectStrategy RedirectStrategy.

Prototype

RedirectStrategy

Source Link

Usage

From source file:org.carrot2.util.httpclient.HttpRedirectStrategy.java

public RedirectStrategy value() {
    switch (this) {
    case NO_REDIRECTS:
        return new RedirectStrategy() {
            public HttpUriRequest getRedirect(HttpRequest req, HttpResponse resp, HttpContext ctx)
                    throws ProtocolException {
                throw new UnsupportedOperationException();
            }//  w  w  w.  j av a 2s  . c o  m

            public boolean isRedirected(HttpRequest req, HttpResponse resp, HttpContext ctx)
                    throws ProtocolException {
                return false;
            }
        };

    case FOLLOW:
        return new DefaultRedirectStrategy();
    }

    throw new RuntimeException();
}

From source file:org.ocpsoft.rewrite.servlet.config.SchemeChangeTest.java

@Test
public void testRedirectToHttps() {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new RedirectStrategy() {

        @Override//w  w  w  . j  a va 2  s  .c om
        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)
                throws ProtocolException {
            if (response.getFirstHeader("Location").getValue().contains("https"))
                throw new RuntimeException("Success!");
            return false;
        }

        @Override
        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context)
                throws ProtocolException {
            throw new IllegalStateException("Not implemented.");
        }

    });
    try {
        get(client, "/login");
    } catch (Exception e) {
        if (!"Success!".equals(e.getMessage()))
            Assert.fail();
    }
}

From source file:com.abc.turkey.service.unfreeze.UnfreezeProxy.java

public UnfreezeProxy() {
    logger.info("create proxy==");
    smsService = SpringContextUtil.getBean(SmsService.class);
    ArrayList<Header> headers = new ArrayList<Header>();
    // headers.add(new BasicHeader("Referer", referer));
    headers.add(new BasicHeader("User-Agent", agent));
    // forbid redirect
    RedirectStrategy redirectStrategy = new RedirectStrategy() {

        @Override/*  w ww.j a  v  a2s  .c o m*/
        public boolean isRedirected(HttpRequest arg0, HttpResponse arg1, HttpContext arg2)
                throws ProtocolException {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public HttpUriRequest getRedirect(HttpRequest arg0, HttpResponse arg1, HttpContext arg2)
                throws ProtocolException {
            // TODO Auto-generated method stub
            return null;
        }
    };
    httpclient = HttpClients.custom().setDefaultHeaders(headers).setRedirectStrategy(redirectStrategy).build();
}

From source file:org.frontcache.agent.FrontCacheAgent.java

public FrontCacheAgent(String frontcacheURL) {
    final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(3000)
            .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();

    ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() {
        @Override/*from  w  w w . j  a v  a  2 s  .co  m*/
        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            HeaderElementIterator it = new BasicHeaderElementIterator(
                    response.headerIterator(HTTP.CONN_KEEP_ALIVE));
            while (it.hasNext()) {
                HeaderElement he = it.nextElement();
                String param = he.getName();
                String value = he.getValue();
                if (value != null && param.equalsIgnoreCase("timeout")) {
                    return Long.parseLong(value) * 1000;
                }
            }
            return 10 * 1000;
        }
    };

    client = HttpClients.custom().setDefaultRequestConfig(requestConfig)
            .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))
            .setKeepAliveStrategy(keepAliveStrategy).setRedirectStrategy(new RedirectStrategy() {
                @Override
                public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)
                        throws ProtocolException {
                    return false;
                }

                @Override
                public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response,
                        HttpContext context) throws ProtocolException {
                    return null;
                }
            }).build();

    this.frontCacheURL = frontcacheURL;

    if (frontcacheURL.endsWith("/"))
        this.frontCacheURI = frontcacheURL + IO_URI;
    else
        this.frontCacheURI = frontcacheURL + "/" + IO_URI;
}

From source file:org.eclipse.lyo.client.oslc.OslcClient.java

/**
 * Initialize a new OslcClient using an Apache Http Components 4 Http client and configuration.
 * Use the provided TrustManagers and X509HostnameVerifiers instead of the defaults which do no verification;
 *//*w ww  .j  a  v a  2 s .com*/
public OslcClient(TrustManager[] trustManagers, X509HostnameVerifier hostnameVerifier) {
    httpClient = new DefaultHttpClient();
    httpClient.setRedirectStrategy(new RedirectStrategy() {
        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) {
            return null;
        }

        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
            return false;
        }
    });
    setupSSLSupport(trustManagers, hostnameVerifier);
    clientPool = new OAuthHttpPool();
    clientConfig = new ApacheHttpClientConfig(httpClient);
    javax.ws.rs.core.Application app = new javax.ws.rs.core.Application() {
        public Set<Class<?>> getClasses() {
            Set<Class<?>> classes = new HashSet<Class<?>>();
            classes.addAll(JenaProvidersRegistry.getProviders());
            classes.addAll(Json4JProvidersRegistry.getProviders());
            return classes;
        }
    };
    clientConfig = clientConfig.applications(app);

}

From source file:org.frontcache.client.FrontCacheClient.java

private FrontCacheClient(String frontcacheURL) {
    final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(CONNECTION_TIMEOUT)
            .setConnectTimeout(CONNECTION_TIMEOUT).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();

    ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() {
        @Override/*from  w  ww  .  jav  a  2 s .c o m*/
        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            HeaderElementIterator it = new BasicHeaderElementIterator(
                    response.headerIterator(HTTP.CONN_KEEP_ALIVE));
            while (it.hasNext()) {
                HeaderElement he = it.nextElement();
                String param = he.getName();
                String value = he.getValue();
                if (value != null && param.equalsIgnoreCase("timeout")) {
                    return Long.parseLong(value) * 1000;
                }
            }
            return 10 * 1000;
        }
    };

    client = HttpClients.custom().setDefaultRequestConfig(requestConfig)
            .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))
            .setKeepAliveStrategy(keepAliveStrategy).setRedirectStrategy(new RedirectStrategy() {
                @Override
                public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)
                        throws ProtocolException {
                    return false;
                }

                @Override
                public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response,
                        HttpContext context) throws ProtocolException {
                    return null;
                }
            }).build();

    this.frontCacheURL = frontcacheURL;

    if (frontcacheURL.endsWith("/"))
        this.frontCacheURI = frontcacheURL + IO_URI;
    else
        this.frontCacheURI = frontcacheURL + "/" + IO_URI;
}

From source file:net.officefloor.plugin.socket.server.http.HttpTestUtil.java

/**
 * Configures no redirects for the {@link HttpClientBuilder}.
 * //w  w w  .  jav  a 2s . c  om
 * @param builder
 *            {@link HttpClientBuilder}.
 */
public static void configureNoRedirects(HttpClientBuilder builder) {
    builder.setRedirectStrategy(new RedirectStrategy() {
        @Override
        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)
                throws ProtocolException {
            // No redirection
            return false;
        }

        @Override
        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context)
                throws ProtocolException {
            Assert.fail("Should not need redirect request");
            return null;
        }
    });
}

From source file:com.lp.alm.lyo.client.oslc.OslcClient.java

/**
 * Initialize a new OslcClient using an Apache Http Components 4 Http client and configuration.
 * Use the provided TrustManagers and X509HostnameVerifiers instead of the defaults which do no verification;
 *///w  w  w . ja  va2  s  .c o  m
public OslcClient(TrustManager[] trustManagers, X509HostnameVerifier hostnameVerifier) {
    httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    httpClient.setRedirectStrategy(new RedirectStrategy() {
        @Override
        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) {
            return null;
        }

        @Override
        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
            return false;
        }
    });
    this.trustManagers = trustManagers;
    this.hostnameVerifier = hostnameVerifier;
    setupSSLSupport();
    clientPool = new OAuthHttpPool();
    clientConfig = new ApacheHttpClientConfig(httpClient);
    javax.ws.rs.core.Application app = new javax.ws.rs.core.Application() {
        @Override
        public Set<Class<?>> getClasses() {
            Set<Class<?>> classes = new HashSet<Class<?>>();
            classes.addAll(JenaProvidersRegistry.getProviders());
            classes.addAll(Json4JProvidersRegistry.getProviders());
            return classes;
        }
    };
    clientConfig = clientConfig.applications(app);

}

From source file:de.codecentric.boot.admin.zuul.filters.route.SimpleHostRoutingFilter.java

protected CloseableHttpClient newClient() {
    final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIMEOUT.get())
            .setConnectTimeout(CONNECTION_TIMEOUT.get()).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();

    HttpClientBuilder httpClientBuilder = HttpClients.custom();
    if (!this.sslHostnameValidationEnabled) {
        httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
    }/*from  ww  w.j  a v  a 2  s . c o  m*/
    return httpClientBuilder.setConnectionManager(newConnectionManager()).useSystemProperties()
            .setDefaultRequestConfig(requestConfig)
            .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))
            .setRedirectStrategy(new RedirectStrategy() {
                @Override
                public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)
                        throws ProtocolException {
                    return false;
                }

                @Override
                public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response,
                        HttpContext context) throws ProtocolException {
                    return null;
                }
            }).build();
}

From source file:eionet.webq.web.interceptor.CdrAuthorizationInterceptor.java

/**
 * Calls a resource in CDR with redirect disabled. Then it is possible to catch if the user is redirected to login page.
 *
 * @param url CDR url to fetch./*from   w w  w .j ava2  s  .  com*/
 * @param headers HTTP headers to send.
 * @return HTTP response object
 * @throws IOException if network error occurs
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.security.KeyManagementException
 */

protected CloseableHttpResponse fetchUrlWithoutRedirection(String url, HttpHeaders headers)
        throws IOException, NoSuchAlgorithmException, KeyManagementException {
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    httpClientBuilder.setSSLContext(SSLContexts.custom().useProtocol("TLSv1.2").build())
            .setRedirectStrategy(new RedirectStrategy() {
                @Override
                public boolean isRedirected(HttpRequest httpRequest, HttpResponse httpResponse,
                        HttpContext httpContext) throws ProtocolException {
                    return false;
                }

                @Override
                public HttpUriRequest getRedirect(HttpRequest httpRequest, HttpResponse httpResponse,
                        HttpContext httpContext) throws ProtocolException {
                    return null;
                }
            });
    HttpGet httpget = new HttpGet(url);

    for (Map.Entry<String, List<String>> header : headers.entrySet()) {
        for (String value : header.getValue()) {
            httpget.addHeader(header.getKey(), value);
        }
    }
    CloseableHttpClient client = httpClientBuilder.build();
    CloseableHttpResponse httpResponse = client.execute(httpget);
    return httpResponse;
}