Example usage for org.apache.commons.httpclient.params HttpClientParams ALLOW_CIRCULAR_REDIRECTS

List of usage examples for org.apache.commons.httpclient.params HttpClientParams ALLOW_CIRCULAR_REDIRECTS

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpClientParams ALLOW_CIRCULAR_REDIRECTS.

Prototype

String ALLOW_CIRCULAR_REDIRECTS

To view the source code for org.apache.commons.httpclient.params HttpClientParams ALLOW_CIRCULAR_REDIRECTS.

Click Source Link

Usage

From source file:io.hops.hopsworks.api.jobs.JobService.java

/**
 * Get the job ui for the specified job.
 * This act as a proxy to get the job ui from yarn
 * <p>//from ww w.j  a  va2 s .  c  o m
 * @param appId
 * @param param
 * @param sc
 * @param req
 * @return
 */
@GET
@Path("/{appId}/prox/{path: .+}")
@Produces(MediaType.WILDCARD)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST })
public Response getProxy(@PathParam("appId") final String appId, @PathParam("path") final String param,
        @Context SecurityContext sc, @Context HttpServletRequest req) {

    Response response = checkAccessRight(appId);
    if (response != null) {
        return response;
    }
    try {
        String trackingUrl;
        if (param.matches("http([a-zA-Z,:,/,.,0-9,-])+:([0-9])+(.)+")) {
            trackingUrl = param;
        } else {
            trackingUrl = "http://" + param;
        }
        trackingUrl = trackingUrl.replace("@hwqm", "?");
        if (!hasAppAccessRight(trackingUrl)) {
            LOGGER.log(Level.SEVERE, "A user is trying to access an app outside their project!");
            return Response.status(Response.Status.FORBIDDEN).build();
        }
        org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(trackingUrl, false);

        HttpClientParams params = new HttpClientParams();
        params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        HttpClient client = new HttpClient(params);

        final HttpMethod method = new GetMethod(uri.getEscapedURI());
        Enumeration<String> names = req.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = req.getHeader(name);
            if (PASS_THROUGH_HEADERS.contains(name)) {
                //yarn does not send back the js if encoding is not accepted
                //but we don't want to accept encoding for the html because we
                //need to be able to parse it
                if (!name.toLowerCase().equals("accept-encoding") || trackingUrl.contains(".js")) {
                    method.setRequestHeader(name, value);
                }
            }
        }
        String user = req.getRemoteUser();
        if (user != null && !user.isEmpty()) {
            method.setRequestHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII"));
        }

        client.executeMethod(method);
        Response.ResponseBuilder responseBuilder = noCacheResponse
                .getNoCacheResponseBuilder(Response.Status.OK);
        for (Header header : method.getResponseHeaders()) {
            responseBuilder.header(header.getName(), header.getValue());
        }
        //method.getPath().contains("/allexecutors") is needed to replace the links under Executors tab
        //which are in a json response object
        if (method.getResponseHeader("Content-Type") == null
                || method.getResponseHeader("Content-Type").getValue().contains("html")
                || method.getPath().contains("/allexecutors")) {
            final String source = "http://" + method.getURI().getHost() + ":" + method.getURI().getPort();
            if (method.getResponseHeader("Content-Length") == null) {
                responseBuilder.entity(new StreamingOutput() {
                    @Override
                    public void write(OutputStream out) throws IOException, WebApplicationException {
                        Writer writer = new BufferedWriter(new OutputStreamWriter(out));
                        InputStream stream = method.getResponseBodyAsStream();
                        Reader in = new InputStreamReader(stream, "UTF-8");
                        char[] buffer = new char[4 * 1024];
                        String remaining = "";
                        int n;
                        while ((n = in.read(buffer)) != -1) {
                            StringBuilder strb = new StringBuilder();
                            strb.append(buffer, 0, n);
                            String s = remaining + strb.toString();
                            remaining = s.substring(s.lastIndexOf(">") + 1, s.length());
                            s = hopify(s.substring(0, s.lastIndexOf(">") + 1), param, appId, source);
                            writer.write(s);
                        }
                        writer.flush();
                    }
                });
            } else {
                String s = hopify(method.getResponseBodyAsString(), param, appId, source);
                responseBuilder.entity(s);
                responseBuilder.header("Content-Length", s.length());
            }

        } else {
            responseBuilder.entity(new StreamingOutput() {
                @Override
                public void write(OutputStream out) throws IOException, WebApplicationException {
                    InputStream stream = method.getResponseBodyAsStream();
                    org.apache.hadoop.io.IOUtils.copyBytes(stream, out, 4096, true);
                    out.flush();
                }
            });
        }
        return responseBuilder.build();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "exception while geting job ui " + e.getLocalizedMessage(), e);
        return noCacheResponse.getNoCacheResponseBuilder(Response.Status.NOT_FOUND).build();
    }

}

From source file:nu.validator.xml.PrudentHttpEntityResolver.java

/**
 * Sets the timeouts of the HTTP client.
 * //w w w  .  j a  v a2s .  co m
 * @param connectionTimeout
 *            timeout until connection established in milliseconds. Zero
 *            means no timeout.
 * @param socketTimeout
 *            timeout for waiting for data in milliseconds. Zero means no
 *            timeout.
 */
public static void setParams(int connectionTimeout, int socketTimeout, int maxRequests) {
    HttpConnectionManagerParams hcmp = client.getHttpConnectionManager().getParams();
    hcmp.setConnectionTimeout(connectionTimeout);
    hcmp.setSoTimeout(socketTimeout);
    hcmp.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxRequests);
    hcmp.setMaxTotalConnections(200); // XXX take this from a property
    PrudentHttpEntityResolver.maxRequests = maxRequests;
    HttpClientParams hcp = client.getParams();
    hcp.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
    hcp.setIntParameter(HttpClientParams.MAX_REDIRECTS, 20); // Gecko
    // default
}

From source file:org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet.java

/**
 * Download link and have it be the response.
 * @param req the http request/*from   w  w  w . j ava2 s  .c  o  m*/
 * @param resp the http response
 * @param link the link to download
 * @param c the cookie to set if any
 * @throws IOException on any error.
 */
private static void proxyLink(HttpServletRequest req, HttpServletResponse resp, URI link, Cookie c,
        String proxyHost) throws IOException {
    org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(link.toString(), false);
    HttpClientParams params = new HttpClientParams();
    params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
    HttpClient client = new HttpClient(params);
    // Make sure we send the request from the proxy address in the config
    // since that is what the AM filter checks against. IP aliasing or
    // similar could cause issues otherwise.
    HostConfiguration config = new HostConfiguration();
    InetAddress localAddress = InetAddress.getByName(proxyHost);
    if (LOG.isDebugEnabled()) {
        LOG.debug("local InetAddress for proxy host: " + localAddress.toString());
    }
    config.setLocalAddress(localAddress);
    HttpMethod method = new GetMethod(uri.getEscapedURI());
    @SuppressWarnings("unchecked")
    Enumeration<String> names = req.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = names.nextElement();
        if (passThroughHeaders.contains(name)) {
            String value = req.getHeader(name);
            LOG.debug("REQ HEADER: " + name + " : " + value);
            method.setRequestHeader(name, value);
        }
    }

    String user = req.getRemoteUser();
    if (user != null && !user.isEmpty()) {
        method.setRequestHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII"));
    }
    OutputStream out = resp.getOutputStream();
    try {
        resp.setStatus(client.executeMethod(config, method));
        for (Header header : method.getResponseHeaders()) {
            resp.setHeader(header.getName(), header.getValue());
        }
        if (c != null) {
            resp.addCookie(c);
        }
        InputStream in = method.getResponseBodyAsStream();
        if (in != null) {
            IOUtils.copyBytes(in, out, 4096, true);
        }
    } finally {
        method.releaseConnection();
    }
}

From source file:org.apache.maven.doxia.linkcheck.validation.OnlineHTTPLinkValidator.java

/** Initialize the HttpClient. */
private void initHttpClient() {
    LOG.debug("A new HttpClient instance is needed ...");

    this.cl = new HttpClient(new MultiThreadedHttpConnectionManager());

    // Default params
    if (this.http.getTimeout() != 0) {
        this.cl.getHttpConnectionManager().getParams().setConnectionTimeout(this.http.getTimeout());
        this.cl.getHttpConnectionManager().getParams().setSoTimeout(this.http.getTimeout());
    }/*from w  ww  . j  av a2  s. c o  m*/
    this.cl.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);

    HostConfiguration hc = new HostConfiguration();

    HttpState state = new HttpState();
    if (StringUtils.isNotEmpty(this.http.getProxyHost())) {
        hc.setProxy(this.http.getProxyHost(), this.http.getProxyPort());

        if (LOG.isDebugEnabled()) {
            LOG.debug("Proxy Host:" + this.http.getProxyHost());
            LOG.debug("Proxy Port:" + this.http.getProxyPort());
        }

        if (StringUtils.isNotEmpty(this.http.getProxyUser()) && this.http.getProxyPassword() != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Proxy User:" + this.http.getProxyUser());
            }

            Credentials credentials;
            if (StringUtils.isNotEmpty(this.http.getProxyNtlmHost())) {
                credentials = new NTCredentials(this.http.getProxyUser(), this.http.getProxyPassword(),
                        this.http.getProxyNtlmHost(), this.http.getProxyNtlmDomain());
            } else {
                credentials = new UsernamePasswordCredentials(this.http.getProxyUser(),
                        this.http.getProxyPassword());
            }

            state.setProxyCredentials(AuthScope.ANY, credentials);
        }
    } else {
        LOG.debug("Not using a proxy");
    }

    this.cl.setHostConfiguration(hc);
    this.cl.setState(state);

    LOG.debug("New HttpClient instance created.");
}

From source file:org.apache.maven.plugin.doap.DoapUtil.java

/**
 * Fetch an URL//  w w  w. jav a 2s .  c  om
 *
 * @param settings the user settings used to fetch the url with an active proxy, if defined.
 * @param url the url to fetch
 * @throws IOException if any
 * @see #DEFAULT_TIMEOUT
 * @since 1.1
 */
public static void fetchURL(Settings settings, URL url) throws IOException {
    if (url == null) {
        throw new IllegalArgumentException("The url is null");
    }

    if ("file".equals(url.getProtocol())) {
        InputStream in = null;
        try {
            in = url.openStream();
        } finally {
            IOUtil.close(in);
        }

        return;
    }

    // http, https...
    HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_TIMEOUT);
    httpClient.getHttpConnectionManager().getParams().setSoTimeout(DEFAULT_TIMEOUT);
    httpClient.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);

    // Some web servers don't allow the default user-agent sent by httpClient
    httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT,
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");

    if (settings != null && settings.getActiveProxy() != null) {
        Proxy activeProxy = settings.getActiveProxy();

        ProxyInfo proxyInfo = new ProxyInfo();
        proxyInfo.setNonProxyHosts(activeProxy.getNonProxyHosts());

        if (StringUtils.isNotEmpty(activeProxy.getHost())
                && !ProxyUtils.validateNonProxyHosts(proxyInfo, url.getHost())) {
            httpClient.getHostConfiguration().setProxy(activeProxy.getHost(), activeProxy.getPort());

            if (StringUtils.isNotEmpty(activeProxy.getUsername()) && activeProxy.getPassword() != null) {
                Credentials credentials = new UsernamePasswordCredentials(activeProxy.getUsername(),
                        activeProxy.getPassword());

                httpClient.getState().setProxyCredentials(AuthScope.ANY, credentials);
            }
        }
    }

    GetMethod getMethod = new GetMethod(url.toString());
    try {
        int status;
        try {
            status = httpClient.executeMethod(getMethod);
        } catch (SocketTimeoutException e) {
            // could be a sporadic failure, one more retry before we give up
            status = httpClient.executeMethod(getMethod);
        }

        if (status != HttpStatus.SC_OK) {
            throw new FileNotFoundException(url.toString());
        }
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.maven.plugin.jira.AbstractJiraDownloader.java

/**
 * Execute the query on the JIRA server.
 *
 * @throws Exception on error/*from   w w w .  jav  a  2  s. c om*/
 */
public void doExecute() throws Exception {
    try {
        HttpClient client = new HttpClient();

        // MCHANGES-89 Allow circular redirects
        HttpClientParams clientParams = client.getParams();
        clientParams.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);

        HttpState state = new HttpState();

        HostConfiguration hc = new HostConfiguration();

        client.setHostConfiguration(hc);

        client.setState(state);

        Map<String, String> urlMap = JiraHelper.getJiraUrlAndProjectId(project.getIssueManagement().getUrl());

        String jiraUrl = urlMap.get("url");
        getLog().debug("JIRA lives at: " + jiraUrl);

        String jiraId = urlMap.get("id");

        determineProxy(jiraUrl, client);

        prepareBasicAuthentication(client);

        boolean jiraAuthenticationSuccessful = false;
        if (isJiraAuthenticationConfigured()) {
            jiraAuthenticationSuccessful = doJiraAuthentication(client, jiraUrl);
        }

        if ((isJiraAuthenticationConfigured() && jiraAuthenticationSuccessful)
                || !isJiraAuthenticationConfigured()) {
            if (jiraId == null || jiraId.length() == 0) {
                log.debug("The JIRA URL " + project.getIssueManagement().getUrl()
                        + " doesn't include a pid, trying to extract it from JIRA.");
                jiraId = JiraHelper.getPidFromJira(log, project.getIssueManagement().getUrl(), client);
            }

            if (jiraId == null) {
                getLog().error("The issue management URL in the POM does not include a pid,"
                        + " and it was not possible to extract it from the page at that URL.");
            } else {
                // create the URL for getting the proper issues from JIRA
                String fullURL = jiraUrl + "/secure/IssueNavigator.jspa?view=rss&pid=" + jiraId;

                if (getFixFor() != null) {
                    fullURL += "&fixfor=" + getFixFor();
                }

                String createdFilter = createFilter();
                if (createdFilter.charAt(0) != '&') {
                    fullURL += "&";
                }
                fullURL += createdFilter;

                fullURL += ("&tempMax=" + nbEntriesMax + "&reset=true&decorator=none");

                if (log.isDebugEnabled()) {
                    log.debug("download jira issues from url " + fullURL);
                }

                // execute the GET
                download(client, fullURL);
            }
        }
    } catch (Exception e) {
        if (project.getIssueManagement() != null) {
            getLog().error("Error accessing " + project.getIssueManagement().getUrl(), e);
        } else {
            getLog().error("Error accessing mock project issues", e);
        }
    }
}

From source file:org.apache.maven.plugin.jira.ClassicJiraDownloader.java

/**
 * Execute the query on the JIRA server.
 *
 * @throws Exception on error//from  w  ww .  j  a v  a2s.c  o m
 */
public void doExecute() throws Exception {
    try {
        HttpClient client = new HttpClient();

        // MCHANGES-89 Allow circular redirects
        HttpClientParams clientParams = client.getParams();
        clientParams.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        clientParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); //MCHANGES-237

        HttpState state = new HttpState();

        HostConfiguration hc = new HostConfiguration();

        client.setHostConfiguration(hc);

        client.setState(state);

        String baseUrl = JiraHelper.getBaseUrl(project.getIssueManagement().getUrl());

        getLog().debug("JIRA lives at: " + baseUrl);
        // Here we only need the host part of the URL
        determineProxy(baseUrl, client);

        prepareBasicAuthentication(client);

        boolean jiraAuthenticationSuccessful = false;
        if (isJiraAuthenticationConfigured()) {
            // Here we only need the parts up to and including the host part of the URL
            jiraAuthenticationSuccessful = doJiraAuthentication(client, baseUrl);
        }

        if ((isJiraAuthenticationConfigured() && jiraAuthenticationSuccessful)
                || !isJiraAuthenticationConfigured()) {
            String fullUrl;

            if (useJql) {
                fullUrl = getJqlQueryURL();
            } else {
                fullUrl = getParameterBasedQueryURL(client);
            }
            if (log.isDebugEnabled()) {
                log.debug("download jira issues from url " + fullUrl);
            }

            // execute the GET
            download(client, fullUrl);
        }
    } catch (Exception e) {
        if (project.getIssueManagement() != null) {
            getLog().error("Error accessing " + project.getIssueManagement().getUrl(), e);
        } else {
            getLog().error("Error accessing mock project issues", e);
        }
    }
}

From source file:org.eclipse.ecf.remoteservice.rest.client.RestClientService.java

protected HttpMethod createAndPrepareHttpMethod(String uri, IRemoteCall call, IRemoteCallable callable)
        throws RestException {
    HttpMethod httpMethod = null;//from  ww  w.jav  a2  s .c o  m

    IRemoteCallableRequestType requestType = callable.getRequestType();
    if (requestType == null)
        throw new RestException("Request type for call cannot be null"); //$NON-NLS-1$
    try {
        if (requestType instanceof HttpGetRequestType) {
            httpMethod = prepareGetMethod(uri, call, callable);
        } else if (requestType instanceof HttpPostRequestType) {
            httpMethod = preparePostMethod(uri, call, callable);
        } else if (requestType instanceof HttpPutRequestType) {
            httpMethod = preparePutMethod(uri, call, callable);
        } else if (requestType instanceof HttpDeleteRequestType) {
            httpMethod = prepareDeleteMethod(uri, call, callable);
        } else {
            throw new RestException("HTTP method " + requestType + " not supported"); //$NON-NLS-1$ //$NON-NLS-2$
        }
    } catch (NotSerializableException e) {
        String message = "Could not serialize parameters for uri=" + uri + " call=" + call + " callable=" //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
                + callable;
        logException(message, e);
        throw new RestException(message);
    }
    // add additional request headers
    addRequestHeaders(httpMethod, call, callable);
    // handle authentication
    setupAuthenticaton(httpClient, httpMethod);
    // needed because a resource can link to another resource
    httpClient.getParams().setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, new Boolean(true));
    httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, DEFAULT_HTTP_CONTENT_CHARSET);
    setupTimeouts(httpClient, call, callable);
    return httpMethod;
}

From source file:org.eclipse.mylyn.commons.net.WebUtil.java

/**
 * @since 3.0/*from  w  ww  .  j a v a2  s.c o m*/
 */
public static void configureHttpClient(HttpClient client, String userAgent) {
    client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
    client.getParams().setParameter(HttpMethodParams.USER_AGENT, getUserAgent(userAgent));
    client.getParams().setConnectionManagerTimeout(CONNECTION_TIMEOUT_INTERVAL);
    // TODO consider setting this as the default
    //client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    configureHttpClientConnectionManager(client);
}

From source file:org.infoscoop.request.ProxyRequest.java

private HttpClient newHttpClient() {
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    if (this.timeout > 0) {
        params.setConnectionTimeout(this.timeout);
    } else {/*w  w  w . j av  a  2s . c o m*/
        params.setConnectionTimeout(SOCKET_CONNECTION_TIMEOUT);
    }
    HttpConnectionManager manager = new SimpleHttpConnectionManager();
    manager.setParams(params);
    HttpClient client = new HttpClient(manager);
    client.getParams().setVersion(new HttpVersion(1, 1));

    if (proxy != null && proxy.isUseProxy()) {
        if (log.isInfoEnabled())
            log.info("Proxy=" + proxy.getHost() + ":" + proxy.getPort() + ", authentication="
                    + proxy.needsProxyAuth());

        client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort());

        if (proxy.needsProxyAuth()) {
            client.getParams().setAuthenticationPreemptive(true);
            client.getState().setProxyCredentials(new AuthScope(proxy.getHost(), proxy.getPort()),
                    proxy.getProxyCredentials());
        }

    }
    client.getParams().setParameter("http.socket.timeout", new Integer(this.timeout));

    String allowCircularRedirect = this.getRequestHeader(ALLOW_CIRCULAR_REDIRECT) == null ? "false"
            : this.getRequestHeader(ALLOW_CIRCULAR_REDIRECT);

    if (Boolean.valueOf(allowCircularRedirect).booleanValue()) {
        if (log.isInfoEnabled())
            log.info("Circular redirect on");
        client.getParams().setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
    }
    return client;
}