Example usage for org.apache.commons.httpclient HttpMethod setFollowRedirects

List of usage examples for org.apache.commons.httpclient HttpMethod setFollowRedirects

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod setFollowRedirects.

Prototype

public abstract void setFollowRedirects(boolean paramBoolean);

Source Link

Usage

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Execute method with httpClient, follow 30x redirects.
 *
 * @param httpClient Http client instance
 * @param method     Http method//from www .  j a va 2 s  .c om
 * @return last http method after redirects
 * @throws IOException on error
 */
public static HttpMethod executeFollowRedirects(HttpClient httpClient, HttpMethod method) throws IOException {
    HttpMethod currentMethod = method;
    try {
        DavGatewayTray.debug(new BundleMessage("LOG_EXECUTE_FOLLOW_REDIRECTS", currentMethod.getURI()));
        httpClient.executeMethod(currentMethod);
        checkNTLM(httpClient, currentMethod);

        String locationValue = getLocationValue(currentMethod);
        // check javascript redirect (multiple authentication pages)
        if (locationValue == null) {
            locationValue = getJavascriptRedirectUrl(currentMethod);
        }

        int redirectCount = 0;
        while (redirectCount++ < 10 && locationValue != null) {
            currentMethod.releaseConnection();
            currentMethod = new GetMethod(locationValue);
            currentMethod.setFollowRedirects(false);
            DavGatewayTray.debug(new BundleMessage("LOG_EXECUTE_FOLLOW_REDIRECTS_COUNT", currentMethod.getURI(),
                    redirectCount));
            httpClient.executeMethod(currentMethod);
            checkNTLM(httpClient, currentMethod);
            locationValue = getLocationValue(currentMethod);
        }
        if (locationValue != null) {
            currentMethod.releaseConnection();
            throw new HttpException("Maximum redirections reached");
        }
    } catch (IOException e) {
        currentMethod.releaseConnection();
        throw e;
    }
    // caller will need to release connection
    return currentMethod;
}

From source file:com.sun.syndication.fetcher.impl.HttpClientFeedFetcher.java

/**
 * @see com.sun.syndication.fetcher.FeedFetcher#retrieveFeed(java.net.URL)
 */// w w  w.  j  a va2  s.  com
public SyndFeed retrieveFeed(URL feedUrl)
        throws IllegalArgumentException, IOException, FeedException, FetcherException {
    if (feedUrl == null) {
        throw new IllegalArgumentException("null is not a valid URL");
    }
    // TODO Fix this
    //System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    HttpClient client = new HttpClient(httpClientParams);

    if (getCredentialSupplier() != null) {
        client.getState().setAuthenticationPreemptive(true);
        // TODO what should realm be here?
        Credentials credentials = getCredentialSupplier().getCredentials(null, feedUrl.getHost());
        if (credentials != null) {
            client.getState().setCredentials(null, feedUrl.getHost(), credentials);
        }
    }

    System.setProperty("httpclient.useragent", getUserAgent());
    String urlStr = feedUrl.toString();

    HttpMethod method = new GetMethod(urlStr);
    method.addRequestHeader("Accept-Encoding", "gzip");
    method.addRequestHeader("User-Agent", getUserAgent());
    method.setFollowRedirects(true);

    if (httpClientMethodCallback != null) {
        synchronized (httpClientMethodCallback) {
            httpClientMethodCallback.afterHttpClientMethodCreate(method);
        }
    }

    FeedFetcherCache cache = getFeedInfoCache();
    if (cache != null) {
        // retrieve feed

        try {
            if (isUsingDeltaEncoding()) {
                method.setRequestHeader("A-IM", "feed");
            }

            // get the feed info from the cache
            // Note that syndFeedInfo will be null if it is not in the cache
            SyndFeedInfo syndFeedInfo = cache.getFeedInfo(feedUrl);
            if (syndFeedInfo != null) {
                method.setRequestHeader("If-None-Match", syndFeedInfo.getETag());

                if (syndFeedInfo.getLastModified() instanceof String) {
                    method.setRequestHeader("If-Modified-Since", (String) syndFeedInfo.getLastModified());
                }
            }

            int statusCode = client.executeMethod(method);
            fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr);
            handleErrorCodes(statusCode);

            SyndFeed feed = getFeed(syndFeedInfo, urlStr, method, statusCode);

            syndFeedInfo = buildSyndFeedInfo(feedUrl, urlStr, method, feed, statusCode);

            cache.setFeedInfo(new URL(urlStr), syndFeedInfo);

            // the feed may have been modified to pick up cached values
            // (eg - for delta encoding)
            feed = syndFeedInfo.getSyndFeed();

            return feed;
        } finally {
            method.releaseConnection();
            method.recycle();
        }

    } else {
        // cache is not in use          
        try {
            int statusCode = client.executeMethod(method);
            fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr);
            handleErrorCodes(statusCode);

            return getFeed(null, urlStr, method, statusCode);
        } finally {
            method.releaseConnection();
            method.recycle();
        }
    }
}

From source file:net.sourceforge.eclipsetrader.core.CurrencyConverter.java

private Double downloadQuote(String symbol) {
    Double result = null;/*from   w  ww  . j  a  va2  s .co m*/

    String host = "quote.yahoo.com"; //$NON-NLS-1$
    StringBuffer url = new StringBuffer("http://" + host + "/download/javasoft.beans?symbols="); //$NON-NLS-1$ //$NON-NLS-2$
    url = url.append(symbol + "=X"); //$NON-NLS-1$
    url.append("&format=sl1d1t1c1ohgvbap"); //$NON-NLS-1$

    String line = ""; //$NON-NLS-1$
    try {
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

        BundleContext context = CorePlugin.getDefault().getBundle().getBundleContext();
        ServiceReference reference = context.getServiceReference(IProxyService.class.getName());
        if (reference != null) {
            IProxyService proxy = (IProxyService) context.getService(reference);
            IProxyData data = proxy.getProxyDataForHost(host, IProxyData.HTTP_PROXY_TYPE);
            if (data != null) {
                if (data.getHost() != null)
                    client.getHostConfiguration().setProxy(data.getHost(), data.getPort());
                if (data.isRequiresAuthentication())
                    client.getState().setProxyCredentials(AuthScope.ANY,
                            new UsernamePasswordCredentials(data.getUserId(), data.getPassword()));
            }
        }

        HttpMethod method = new GetMethod(url.toString());
        method.setFollowRedirects(true);
        client.executeMethod(method);

        BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        while ((line = in.readLine()) != null) {
            String[] item = line.split(","); //$NON-NLS-1$
            if (line.indexOf(";") != -1) //$NON-NLS-1$
                item = line.split(";"); //$NON-NLS-1$

            // 1 = Last price or N/A
            if (item[1].equalsIgnoreCase(Messages.CurrencyConverter_NA) == false)
                result = new Double(numberFormat.parse(item[1]).doubleValue());
            // 2 = Date
            // 3 = Time
            // 4 = Change
            // 5 = Open
            // 6 = Maximum
            // 7 = Minimum
            // 8 = Volume
            // 9 = Bid Price
            // 10 = Ask Price
            // 11 = Close Price

            // 0 = Code
        }
        in.close();
    } catch (Exception e) {
        logger.error(e, e);
    }

    return result;
}

From source file:de.innovationgate.webgate.api.query.rss.WGDatabaseImpl.java

private Document retrievePage(String url) {

    try {/*from   w  w w  .  j  ava 2  s .c o m*/

        // Try to retrieve from cache
        CachedPage page = (CachedPage) this.cachedPages.get(url);
        if (page != null) {
            return page.getDocument();
        }

        SAXReader reader = new SAXReader();

        // Retrieve from web
        HttpClient client = WGFactory.getHttpClientFactory().createHttpClient();
        client.setConnectionTimeout(5000);
        HttpMethod method = new GetMethod(url);
        method.setFollowRedirects(true);
        method.setStrictMode(false);
        client.executeMethod(method);

        // Read response. Wrap content decoder if necessary.
        InputStream inStream = method.getResponseBodyAsStream();

        // Not necessary - HttpClient decodes automatically
        /*Header contentEncoding = method.getResponseHeader("Content-Encoding");
        if (contentEncoding != null) {
           if (contentEncoding.getValue().equals("gzip")) {
              inStream = new GZIPInputStream(inStream);
           }
        }*/
        Document doc = reader.read(inStream);

        /*HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
        Document doc = reader.read(conn.getInputStream());*/

        // Put into cache, if cache latency is set
        if (cacheLatency > 0) {
            this.cachedPages.put(url, new CachedPage(doc));
        }

        return doc;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (DocumentException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.owncloud.android.lib.common.OwnCloudClient.java

@Override
public int executeMethod(HttpMethod method) throws IOException, HttpException {
    try { // just to log
        boolean customRedirectionNeeded = false;

        try {//w  w w. j  a v  a  2s .  c  om
            method.setFollowRedirects(mFollowRedirects);
        } catch (Exception e) {
            /*
             if (mFollowRedirects)
               Log_OC.d(TAG, "setFollowRedirects failed for " + method.getName()
            + " method, custom redirection will be used if needed");
            */
            customRedirectionNeeded = mFollowRedirects;
        }

        Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " + method.getName() + " " + method.getPath());

        //           logCookiesAtRequest(method.getRequestHeaders(), "before");
        //           logCookiesAtState("before");

        int status = super.executeMethod(method);

        if (customRedirectionNeeded) {
            status = patchRedirection(status, method);
        }

        //           logCookiesAtRequest(method.getRequestHeaders(), "after");
        //           logCookiesAtState("after");
        //           logSetCookiesAtResponse(method.getResponseHeaders());

        return status;

    } catch (IOException e) {
        Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occured", e);
        throw e;
    }
}

From source file:ir.keloud.android.lib.common.KeloudClient.java

@Override
public int executeMethod(HttpMethod method) throws IOException, HttpException {
    try { // just to log 
        boolean customRedirectionNeeded = false;

        try {//from   w ww .  ja v  a 2 s .  c  o  m
            method.setFollowRedirects(mFollowRedirects);
        } catch (Exception e) {
            /*
             if (mFollowRedirects) 
               Log_OC.d(TAG, "setFollowRedirects failed for " + method.getName() 
            + " method, custom redirection will be used if needed");
            */
            customRedirectionNeeded = mFollowRedirects;
        }

        // Update User Agent
        HttpParams params = method.getParams();
        String userAgent = KeloudClientManagerFactory.getUserAgent();
        params.setParameter(HttpMethodParams.USER_AGENT, userAgent);

        Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " + method.getName() + " " + method.getPath());

        //           logCookiesAtRequest(method.getRequestHeaders(), "before");
        //           logCookiesAtState("before");

        int status = super.executeMethod(method);

        if (customRedirectionNeeded) {
            status = patchRedirection(status, method);
        }

        //           logCookiesAtRequest(method.getRequestHeaders(), "after");
        //           logCookiesAtState("after");
        //           logSetCookiesAtResponse(method.getResponseHeaders());

        return status;

    } catch (IOException e) {
        Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occurred", e);
        throw e;
    }
}

From source file:ch.ksfx.web.services.spidering.http.HttpClientHelper.java

private HttpMethod createNewHttpMethod(HttpMethod oldMethod) throws URIException {
    HttpMethod httpMethod;
    if (oldMethod instanceof GetMethod) {
        httpMethod = new GetMethod();
    } else {/*from ww  w .j ava  2  s.c  o  m*/
        httpMethod = new PostMethod();
        ((PostMethod) httpMethod).setRequestEntity(((PostMethod) oldMethod).getRequestEntity());
        httpMethod.setParams(oldMethod.getParams());
    }
    httpMethod.setURI(oldMethod.getURI());
    httpMethod.setFollowRedirects(oldMethod.getFollowRedirects());
    return httpMethod;
}

From source file:de.innovationgate.webgate.api.rss2.SimpleRSS.java

private InputStream retrievePage(String url, NativeQueryOptions nativeOptions) throws WGQueryException {

    try {//  www  .j  a  va  2 s .  com

        // Retrieve from web
        HttpClient client = WGFactory.getHttpClientFactory().createHttpClient();
        client.setConnectionTimeout(10000);
        if (_useProxy) {
            client.getHostConfiguration().setProxy(_proxyHost, _proxyPort);
            if (_proxyCredentials != null) {
                Credentials credentials;
                if (_proxyDomain != null) {
                    List elements = WGUtils.deserializeCollection(_proxyCredentials, ":");
                    credentials = new NTCredentials((String) elements.get(0), (String) elements.get(1),
                            _proxyHost, _proxyDomain);
                } else {
                    credentials = new UsernamePasswordCredentials(_proxyCredentials);

                }
                client.getState().setProxyCredentials(null, _proxyHost, credentials);

            }
        }
        HttpMethod method = new GetMethod(url);
        method.setFollowRedirects(true);
        method.setStrictMode(false);

        if (nativeOptions.containsKey(QUERYOPTION_USER) && nativeOptions.containsKey(QUERYOPTION_PWD)) {
            method.setDoAuthentication(true);
            client.getParams().setAuthenticationPreemptive(true);
            client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
                    nativeOptions.get(QUERYOPTION_USER), nativeOptions.get(QUERYOPTION_PWD)));
        }

        client.executeMethod(method);

        // Read response. Wrap content decoder if necessary.
        InputStream inStream = method.getResponseBodyAsStream();

        // Return InputStream from given URL
        return inStream;
    } catch (MalformedURLException e) {
        throw new WGQueryException("Malformed feed URL", url, e);
    } catch (IOException e) {
        throw new WGQueryException("IO Exception retrieving feed", url, e);
    }
}

From source file:com.liferay.util.Http.java

public static byte[] URLtoByteArray(String location, Cookie[] cookies, boolean post) throws IOException {

    byte[] byteArray = null;

    HttpMethod method = null;

    try {/*from  w  w  w . jav  a2s . c  om*/
        HttpClient client = new HttpClient(new SimpleHttpConnectionManager());

        if (location == null) {
            return byteArray;
        } else if (!location.startsWith(HTTP_WITH_SLASH) && !location.startsWith(HTTPS_WITH_SLASH)) {

            location = HTTP_WITH_SLASH + location;
        }

        HostConfiguration hostConfig = new HostConfiguration();

        hostConfig.setHost(new URI(location));

        if (Validator.isNotNull(PROXY_HOST) && PROXY_PORT > 0) {
            hostConfig.setProxy(PROXY_HOST, PROXY_PORT);
        }

        client.setHostConfiguration(hostConfig);
        client.setConnectionTimeout(5000);
        client.setTimeout(5000);

        if (cookies != null && cookies.length > 0) {
            HttpState state = new HttpState();

            state.addCookies(cookies);
            state.setCookiePolicy(CookiePolicy.COMPATIBILITY);

            client.setState(state);
        }

        if (post) {
            method = new PostMethod(location);
        } else {
            method = new GetMethod(location);
        }

        method.setFollowRedirects(true);

        client.executeMethod(method);

        Header locationHeader = method.getResponseHeader("location");
        if (locationHeader != null) {
            return URLtoByteArray(locationHeader.getValue(), cookies, post);
        }

        InputStream is = method.getResponseBodyAsStream();

        if (is != null) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            byte[] bytes = new byte[512];

            for (int i = is.read(bytes, 0, 512); i != -1; i = is.read(bytes, 0, 512)) {

                buffer.write(bytes, 0, i);
            }

            byteArray = buffer.toByteArray();

            is.close();
            buffer.close();
        }

        return byteArray;
    } finally {
        try {
            if (method != null) {
                method.releaseConnection();
            }
        } catch (Exception e) {
            Logger.error(Http.class, e.getMessage(), e);
        }
    }
}

From source file:ar.com.zauber.commons.web.proxy.HttpClientRequestProxy.java

/**
 * @see AbstractController#handleRequestInternal(HttpServletRequest,
 *      HttpServletResponse)/*from www .  ja  v a2 s .  c o  m*/
 *      @throws Exception .
 */
public final void handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response)
        throws Exception {

    final URLResult r = urlRequestMapper.getProxiedURLFromRequest(request);
    if (r.hasResult()) {
        final HttpMethod method = buildRequest(request, r);
        InputStream is = null;
        try {
            // Entity enclosing requests cannot be redirected 
            // without user intervention according to RFC 2616
            if (!method.getName().equalsIgnoreCase("post") && !method.getName().equalsIgnoreCase("put")) {
                method.setFollowRedirects(followRedirects);
            }
            httpClient.executeMethod(method);
            updateResponseCode(request, response, method);
            proxyHeaders(response, method);
            addOtherHeaders(response, method);

            is = method.getResponseBodyAsStream();

            if (is != null) {
                if (contentTransformer.getContentType() != null) {
                    response.setContentType(contentTransformer.getContentType());
                }

                try {
                    String uri = request.getPathInfo();
                    // When using this component as a Filter,  the path info
                    // can be null, but the same information is available at
                    // getServletPath
                    if (uri == null) {
                        uri = request.getServletPath();
                    }

                    contentTransformer.transform(is, response.getOutputStream(),
                            new InmutableContentMetadata(uri, getContentType(method), method.getStatusCode()));
                } finally {
                    is.close();
                }
            }
        } catch (final ConnectException e) {
            onConnectionException(request, response, method, e);
        } finally {
            if (is != null) {
                is.close();
            }
            method.releaseConnection();
        }
    } else {
        onNoMapping(request, response);
    }
}