Example usage for org.apache.commons.httpclient.methods PostMethod setFollowRedirects

List of usage examples for org.apache.commons.httpclient.methods PostMethod setFollowRedirects

Introduction

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

Prototype

public void setFollowRedirects(boolean paramBoolean) 

Source Link

Usage

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

/**
 * Returns an executed PostMethod object with the given URL
 *
 * @param url URL for HTTP POST request/*w w w. j a va  2 s .c om*/
 * @return executed PostMethod object
 */
public PostMethod executePostMethod(String url) {
    try {
        url = encodeURL(url);
        PostMethod postMethod = new PostMethod(url);
        postMethod.setFollowRedirects(true);
        postMethod = (PostMethod) executeMethod(postMethod);
        return postMethod;
    } catch (Exception e) {
        logger.severe("Error while generating POST method: " + e);
        return null;
    }
}

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

public PostMethod executePostMethod(String url, NameValuePair[] nameValuePairs) {
    try {/*from ww w  .  j a v  a 2 s  . co m*/
        url = encodeURL(url);
        PostMethod postMethod = new PostMethod(url);
        postMethod.setFollowRedirects(false);
        postMethod.setRequestBody(nameValuePairs);
        postMethod = (PostMethod) executeMethod(postMethod);
        return postMethod;
    } catch (Exception e) {
        logger.severe("Error while generating POST method: " + e);
        return null;
    }
}

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

/**
 * Returns an executed PostMethod object with the given URL and the given
 * RequestEntity object in the request body
 *
 * @param url URL for HTTP POST request// w  ww  .jav a2s .co m
 * @param requestEntity RequestEntity for request body
 * @return executed PostMethod object
 */
public PostMethod executePostMethod(String url, RequestEntity requestEntity) {
    try {
        url = encodeURL(url);
        PostMethod postMethod = new PostMethod(url);
        if (requestEntity != null) {
            postMethod.setFollowRedirects(false);
            postMethod.setRequestEntity(requestEntity);
        } else {
            postMethod.setFollowRedirects(true);
        }
        postMethod = (PostMethod) executeMethod(postMethod);
        return postMethod;
    } catch (Exception e) {
        logger.severe("Error while generating POST method: " + e);
        return null;
    }
}

From source file:autohit.call.modules.SimpleHttpModule.java

/**
 * Post method. It will set the target address for the client, as well as
 * clearing any state./*from  w  ww.j  av a2s.  c om*/
 * 
 * @param url
 *            the Url path, not to include protocol, address, and port (ie.
 *            "/goats/index.html").
 * @param nv
 *            set of name/value pairs for the post. it can be empty.
 * @return the data from the page as a String
 * @throws CallException
 */
private String post(String url, Hashtable nv) throws CallException {

    if (started == false) {
        throw buildException("Tried to post when a session wasn't started.", CallException.CODE_MODULE_FAULT);
    }

    String result = null;
    String name;
    Object value;

    // Construct our method.
    PostMethod method = new PostMethod(url);
    method.setFollowRedirects(true);
    method.setStrictMode(false);

    //build the rest of the method
    try {
        // Construct the headers
        Enumeration eNV = nv.keys();
        while (eNV.hasMoreElements()) {
            name = (String) eNV.nextElement();
            value = nv.get(name);
            if (value instanceof String) {
                // Only take it if it is a string
                method.addParameter(name, (String) value);
                debug("ADD POST - name=" + name + " value=" + (String) value);
            }
        }
        //DEBUG
        debug("DUMP POST-------------------------------");
        debug(method.toString());
        debug("DUMP POST-------------------------------");

        // Do it
        debug("(post)post=" + url);
        httpClient.executeMethod(method);

        // Process result
        result = method.getResponseBodyAsString();
        log("(post)" + method.getStatusLine().toString() + " size=" + result.length());

    } catch (HttpException he) {
        // Bad but not fatal
        error("(post)Error on connect to url " + url + ".  Error=" + he.getMessage());
    } catch (IOException ioe) {
        // Fatal
        throw buildException("(post)Unable to connect.  Session is invalid.", CallException.CODE_MODULE_FAULT,
                ioe);

    } catch (Exception ex) {
        // Fatal
        throw buildException("(post)Serious general error.", CallException.CODE_MODULE_FAULT, ex);
    } finally {
        try {
            method.releaseConnection();
            method.recycle();
        } catch (Exception e) {
            // Already FUBAR
        }
    }
    return result;
}

From source file:JiraWebSession.java

private HostConfiguration login(HttpClient httpClient, IProgressMonitor monitor) throws JiraException {
    RedirectTracker tracker = new RedirectTracker();

    final String restLogin = "/rest/gadget/1.0/login"; //$NON-NLS-1$ // JIRA 4.x has additional endpoint for login that tells if CAPTCHA limit was hit
    final String loginAction = "/secure/Dashboard.jspa"; //$NON-NLS-1$;
    boolean jira4x = true;

    for (int i = 0; i <= MAX_REDIRECTS; i++) {
        AuthenticationCredentials credentials = location.getCredentials(AuthenticationType.REPOSITORY);
        if (credentials == null) {
            // TODO prompt user?
            credentials = new AuthenticationCredentials("", ""); //$NON-NLS-1$ //$NON-NLS-2$
        }//from  w ww.jav  a2 s  . c  om

        String url = baseUrl + (jira4x ? restLogin : loginAction);

        PostMethod login = new PostMethod(url);
        login.setFollowRedirects(false);
        login.setRequestHeader("Content-Type", getContentType()); //$NON-NLS-1$
        login.addParameter("os_username", credentials.getUserName()); //$NON-NLS-1$
        login.addParameter("os_password", credentials.getPassword()); //$NON-NLS-1$
        login.addParameter("os_destination", "/success"); //$NON-NLS-1$ //$NON-NLS-2$

        tracker.addUrl(url);

        try {
            HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(httpClient, location,
                    monitor);
            int statusCode = WebUtil.execute(httpClient, hostConfiguration, login, monitor);

            if (statusCode == HttpStatus.SC_NOT_FOUND) {
                jira4x = false;
                continue;
            }

            if (needsReauthentication(httpClient, login, monitor)) {
                continue;
            } else if (statusCode != HttpStatus.SC_MOVED_TEMPORARILY
                    && statusCode != HttpStatus.SC_MOVED_PERMANENTLY) {
                throw new JiraServiceUnavailableException("Unexpected status code during login: " + statusCode); //$NON-NLS-1$
            }

            tracker.addRedirect(url, login, statusCode);

            this.characterEncoding = login.getResponseCharSet();

            Header locationHeader = login.getResponseHeader("location"); //$NON-NLS-1$
            if (locationHeader == null) {
                throw new JiraServiceUnavailableException("Invalid redirect, missing location"); //$NON-NLS-1$
            }
            url = locationHeader.getValue();
            tracker.checkForCircle(url);
            if (!insecureRedirect && isSecure() && url.startsWith("http://")) { //$NON-NLS-1$
                tracker.log("Redirect to insecure location during login to repository: " + client.getBaseUrl()); //$NON-NLS-1$
                insecureRedirect = true;
            }
            if (url.endsWith("/success")) { //$NON-NLS-1$
                String newBaseUrl = url.substring(0, url.lastIndexOf("/success")); //$NON-NLS-1$
                if (baseUrl.equals(newBaseUrl) || !client.getLocalConfiguration().getFollowRedirects()) {
                    // success
                    addAuthenticationCookie(httpClient, login);
                    return hostConfiguration;
                } else {
                    // need to login to make sure HttpClient picks up the session cookie
                    baseUrl = newBaseUrl;
                }
            }
        } catch (IOException e) {
            throw new JiraServiceUnavailableException(e);
        } finally {
            login.releaseConnection();
        }
    }

    tracker.log(
            "Exceeded maximum number of allowed redirects during login to repository: " + client.getBaseUrl()); //$NON-NLS-1$

    throw new JiraServiceUnavailableException("Exceeded maximum number of allowed redirects during login"); //$NON-NLS-1$
}

From source file:com.dtolabs.client.utils.BaseFormAuthenticator.java

/**
 * Authenticate the client http state so that the colony requests can be made.
 *
 * @param baseURL URL requested for colony
 * @param client  HttpClient instance//w  w  w .j  a  v  a 2s.  co  m
 *
 * @return true if authentication succeeded.
 *
 * @throws com.dtolabs.client.utils.HttpClientException
 *
 */
public boolean authenticate(final URL baseURL, final HttpClient client) throws HttpClientException {
    final HttpState state = client.getState();
    if (hasSessionCookie(baseURL, state, basePath)) {
        return true;
    }
    final byte[] buffer = new byte[1024];

    boolean doPostLogin = false;
    boolean isLoginFormContent = false;
    logger.debug("No session found, must login...");
    try {
        final URL newUrl = new URL(baseURL.getProtocol(), baseURL.getHost(), baseURL.getPort(),
                basePath + getInitialPath());

        //load welcome page, which should forward to form based logon page.
        final GetMethod get = new GetMethod(newUrl.toExternalForm());
        get.setDoAuthentication(false);
        get.setFollowRedirects(false);
        logger.debug("Requesting: " + newUrl);
        int res = client.executeMethod(get);
        logger.debug("Result is: " + res);

        /*
          Tomcat container auth behaves differently than Jetty.  Tomcat will respond 200 OK and include the login form
          when auth is required, as well as on auth failure, it will also require complete GET of original URL after
          successful auth.
          Jetty will redirect to login page when auth is required, and will redirect to error page on failure.
         */

        String body = get.getResponseBodyAsString();
        if (null != body && body.contains(J_SECURITY_CHECK) && body.contains(JAVA_USER_PARAM)
                && body.contains(JAVA_PASS_PARAM)) {
            isLoginFormContent = true;
        }
        get.releaseConnection();

        if ((res == HttpStatus.SC_UNAUTHORIZED)) {
            if (get.getResponseHeader("WWW-Authenticate") != null
                    && get.getResponseHeader("WWW-Authenticate").getValue().matches("^Basic.*")) {
                logger.warn("Form-based login received UNAUTHORIZED, trying to use Basic authentication");
                final BasicAuthenticator auth = new BasicAuthenticator(username, password);
                return auth.authenticate(baseURL, client);
            } else {
                throw new HttpClientException(
                        "Form-based login received UNAUTHORIZED, but didn't recognize it as Basic authentication: unable to get a session");
            }

        }
        //should now have the proper session cookie
        if (!hasSessionCookie(baseURL, state, basePath)) {
            throw new HttpClientException("Unable to get a session from URL : " + newUrl);
        }
        if (res == HttpStatus.SC_OK && isLoginFormContent) {
            doPostLogin = true;
        } else if ((res == HttpStatus.SC_MOVED_TEMPORARILY) || (res == HttpStatus.SC_MOVED_PERMANENTLY)
                || (res == HttpStatus.SC_SEE_OTHER) || (res == HttpStatus.SC_TEMPORARY_REDIRECT)) {
            Header locHeader = get.getResponseHeader("Location");
            if (locHeader == null) {
                throw new HttpClientException("Redirect with no Location header, request URL: " + newUrl);
            }
            String location = locHeader.getValue();
            if (!isValidLoginRedirect(get)) {
                //unexpected response
                throw new HttpClientException("Unexpected redirection when getting session: " + location);
            }
            logger.debug("Follow redirect: " + res + ": " + location);

            final GetMethod redir = new GetMethod(location);
            redir.setFollowRedirects(true);
            res = client.executeMethod(redir);
            InputStream ins = redir.getResponseBodyAsStream();
            while (ins.available() > 0) {
                //read and discard response body
                ins.read(buffer);
            }
            redir.releaseConnection();

            if (res != HttpStatus.SC_OK) {
                throw new HttpClientException("Login page status was not OK: " + res);
            }
            logger.debug("Result: " + res);

            doPostLogin = true;
        } else if (res != HttpStatus.SC_OK) {
            //if request to welcome page was OK, we figure that the session is already set
            throw new HttpClientException("Request to welcome page returned error: " + res + ": " + get);
        }
        if (doPostLogin) {
            //now post login
            final URL loginUrl = new URL(baseURL.getProtocol(), baseURL.getHost(), baseURL.getPort(),
                    basePath + JAVA_AUTH_PATH);

            final PostMethod login = new PostMethod(loginUrl.toExternalForm());
            login.setRequestBody(new NameValuePair[] { new NameValuePair(JAVA_USER_PARAM, getUsername()),
                    new NameValuePair(JAVA_PASS_PARAM, getPassword()) });

            login.setFollowRedirects(false);
            logger.debug("Post login info to URL: " + loginUrl);

            res = client.executeMethod(login);

            final InputStream ins = login.getResponseBodyAsStream();
            while (ins.available() > 0) {
                //read and discard response body
                ins.read(buffer);
            }
            login.releaseConnection();

            Header locHeader = login.getResponseHeader("Location");
            String location = null != locHeader ? locHeader.getValue() : null;
            if (isLoginError(login)) {
                logger.error("Form-based auth failed");
                return false;
            } else if (null != location && !location.equals(newUrl.toExternalForm())) {

                logger.warn("Form-based auth succeeded, but last URL was unexpected");
            }
            if (isFollowLoginRedirect()
                    && ((res == HttpStatus.SC_MOVED_TEMPORARILY) || (res == HttpStatus.SC_MOVED_PERMANENTLY)
                            || (res == HttpStatus.SC_SEE_OTHER) || (res == HttpStatus.SC_TEMPORARY_REDIRECT))) {

                if (location == null) {
                    throw new HttpClientException("Redirect with no Location header, request URL: " + newUrl);
                }
                final GetMethod get2 = new GetMethod(location);
                //                    logger.debug("Result: " + res + ": " + location + ", following redirect");
                res = client.executeMethod(get2);
            } else if (res != HttpStatus.SC_OK) {
                throw new HttpClientException(
                        "Login didn't seem to work: " + res + ": " + login.getResponseBodyAsString());
            }
            logger.debug("Result: " + res);
        }
    } catch (MalformedURLException e) {
        throw new HttpClientException("Bad URL", e);
    } catch (HttpException e) {
        throw new HttpClientException("HTTP Error: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new HttpClientException(
                "Error occurred while trying to authenticate to server: " + e.getMessage(), e);
    }

    return true;
}

From source file:com.tacitknowledge.maven.plugin.crx.CRXPackageInstallerPlugin.java

private void checkin(final Cookie[] cookies, String path, HttpClient client)
        throws HttpException, IOException, MojoExecutionException {
    getLog().info("Checking in " + path);
    getLog().info(crxPath + "/browser/content.jsp?Path=" + path + "&action_ops=checkin");

    PostMethod checkinCall = new PostMethod(crxPath + "/browser/content.jsp");
    checkinCall.setFollowRedirects(false);

    checkinCall.addParameter("Path", path);
    checkinCall.addParameter("action_ops", "checkin");

    int status = client.executeMethod(checkinCall);

    if (status == HttpStatus.SC_OK) {
        getLog().info("Successfully checked in.\r\n");
    } else {//  w w  w  .  j av  a2s .  c  om
        logResponseDetails(checkinCall);
        throw new MojoExecutionException(
                "Removing node " + path + " failed, response=" + HttpStatus.getStatusText(status));
    }
}

From source file:com.tacitknowledge.maven.plugin.crx.CRXPackageInstallerPlugin.java

private void checkout(final Cookie[] cookies, String path, HttpClient client)
        throws HttpException, IOException, MojoExecutionException {
    getLog().info("Checking out " + path);
    getLog().info(crxPath + "/browser/content.jsp?Path=" + path + "&action_ops=checkout");

    PostMethod checkoutCall = new PostMethod(crxPath + "/browser/content.jsp");
    checkoutCall.setFollowRedirects(false);

    checkoutCall.addParameter("Path", path);
    checkoutCall.addParameter("action_ops", "checkout");

    int status = client.executeMethod(checkoutCall);

    if (status == HttpStatus.SC_OK) {
        getLog().info("Successfully checked out.\r\n");
    } else {//  w w w. j  av  a2s .c o  m
        logResponseDetails(checkoutCall);
        throw new MojoExecutionException(
                "Removing node " + path + " failed, response=" + HttpStatus.getStatusText(status));
    }
}

From source file:edu.harvard.hmdc.dvnplugin.DVNOAIUrlCacher.java

private boolean processTermsOfUseResponse() throws IOException {
    //get the location header to find out where to redirect to
    String location = conn.getResponseHeaderValue("location");
    releaseConnection();//from ww w.j a  va  2  s. c  o  m

    GetMethod method = null;
    int status = 200;
    String jsessionid = null;

    try {

        logger.debug3("making Get method " + location + "&clicker=downloadServlet");
        method = new GetMethod(location + "&clicker=downloadServlet");
        // instruct method not to follow redirects:
        method.setFollowRedirects(false);
        logger.debug3("executing method");
        status = getClient().executeMethod(method);

        InputStream in = method.getResponseBodyAsStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(in));

        String line = null;

        String vdcid = null;
        String viewstate = null;
        String studyid = null;
        String remotefileid = null;

        String regexpJsession = "jsessionid=([^\"?&]*)";
        String regexpViewState = "ViewState\" value=\"([^\"]*)\"";
        String regexpStudyId = "studyId\" value=\"([0-9]*)\"";
        String regexpRemoteFileId = "fileId=([0-9]*)";

        Pattern patternJsession = Pattern.compile(regexpJsession);
        Pattern patternViewState = Pattern.compile(regexpViewState);
        Pattern patternStudyId = Pattern.compile(regexpStudyId);
        Pattern patternRemoteFileId = Pattern.compile(regexpRemoteFileId);

        Matcher matcher = null;

        matcher = patternRemoteFileId.matcher(fetchUrl);
        if (matcher.find()) {
            remotefileid = matcher.group(1);
            logger.debug3("TermsOfUse: found fileid " + remotefileid);
        }

        while ((line = rd.readLine()) != null) {
            matcher = patternJsession.matcher(line);
            if (matcher.find()) {
                jsessionid = matcher.group(1);
                logger.debug3("TermsOfUse: found jsessionid " + jsessionid);
            }
            matcher = patternViewState.matcher(line);
            if (matcher.find()) {
                viewstate = matcher.group(1);
                logger.debug3("TermsOfUse: found viewstate " + viewstate);
            }
            matcher = patternStudyId.matcher(line);
            if (matcher.find()) {
                studyid = matcher.group(1);
                logger.debug3("TermsOfUse: found studyid " + studyid);
            }
        }

        rd.close();
        method.releaseConnection();

        logger.debug3("released TermsOfUse GET connection");

        //boolean notTrue = false; 
        //if ( notTrue ) {
        if (jsessionid != null) {

            logger.debug3("TermsOfUse: obtained authorization information");
            location = location.substring(0, location.indexOf("?"));
            logger.debug3("Connecting again " + location + ";jsessionid=" + jsessionid);

            PostMethod TOUpostMethod = new PostMethod(location + ";jsessionid=" + jsessionid);
            // instruct method not to follow redirects:
            TOUpostMethod.setFollowRedirects(false);

            Part[] parts = { new StringPart("pageName", "TermsOfUsePage"),
                    new StringPart("content:termsOfUsePageView:form1:vdcId", ""),
                    new StringPart("content:termsOfUsePageView:form1:studyId", studyid),
                    new StringPart("content:termsOfUsePageView:form1:redirectPage",
                            "/FileDownload/?fileId=" + remotefileid),
                    new StringPart("content:termsOfUsePageView:form1:tou", "download"),
                    new StringPart("content:termsOfUsePageView:form1:termsAccepted", "on"),
                    new StringPart("content:termsOfUsePageView:form1:termsButton", "Continue"),
                    new StringPart("content:termsOfUsePageView:form1_hidden",
                            "content:termsOfUsePageView:form1_hidden'"),
                    new StringPart("javax.faces.ViewState", viewstate) };

            TOUpostMethod.addRequestHeader("Cookie", "JSESSIONID=" + jsessionid);
            TOUpostMethod.setRequestEntity(new MultipartRequestEntity(parts, TOUpostMethod.getParams()));

            status = getClient().executeMethod(TOUpostMethod);

            // TODO -- more diagnostics needed here! 
            logger.debug3("TermsOfUse POST: received code " + status);
            if (status == 302) {
                String newLocation = null;
                for (int i = 0; i < TOUpostMethod.getResponseHeaders().length; i++) {
                    String headerName = TOUpostMethod.getResponseHeaders()[i].getName();
                    if (headerName.equals("Location")) {
                        newLocation = TOUpostMethod.getResponseHeaders()[i].getValue();
                    }
                }
                logger.debug3("TermsOfUse POST: redirected to " + newLocation);
            }
            TOUpostMethod.releaseConnection();
            logger.debug3("released TermsOfUse POST connection");
        }

        //   } catch (IOException e) {
        //logger.debug2("failed to access TermsOfUse page", e);
        //return false;
    } catch (MalformedURLException ex) {
        logger.debug2("failed to acquire TermsOfUse authorization", ex);
        return false;
    } catch (IOException ex) {
        logger.debug2("failed to acquire TermsOfUse authorization", ex);
        return false;
    } catch (RuntimeException e) {
        logger.warning("failed to acquire TermsOfUse authorization", e);
        return false;
    }

    // OK, we can try again now!

    logger.debug3("trying to access the file again " + fetchUrl);

    try {
        conn = makeConnection(fetchUrl, connectionPool);
        if (localAddr != null) {
            conn.setLocalAddress(localAddr);
        }
        if (reqProps != null) {
            for (Iterator iter = reqProps.keySet().iterator(); iter.hasNext();) {
                String key = (String) iter.next();
                conn.setRequestProperty(key, reqProps.getProperty(key));
            }
        }

        conn.setRequestProperty("user-agent", LockssDaemon.getUserAgent());
        String myCookie = "JSESSIONID=" + jsessionid;
        logger.debug3("setting cookie: " + myCookie);
        conn.addRequestProperty("Cookie", myCookie);

        logger.debug3("executing request");
        conn.execute();
    } catch (MalformedURLException ex) {
        logger.debug2("openConnection", ex);
        throw resultMap.getMalformedURLException(ex);
    } catch (IOException ex) {
        logger.debug2("openConnection", ex);
        throw resultMap.getHostException(ex);
    } catch (RuntimeException e) {
        logger.warning("openConnection: unexpected exception", e);
        throw e;
    }

    return true;
}

From source file:com.sun.faban.driver.transport.hc3.ApacheHC3Transport.java

/**
 * Makes a POST request to the URL. Reads data back and returns the data
 * read. Note that this method only works with text data as it does the
 * byte-to-char conversion. This method will return null for responses
 * with binary MIME types. The addTextType(String) method is used to
 * register additional MIME types as text types. Use getContentSize()
 * to obtain the bytes of binary data read.
 *
 * @param url The URL to read from//w  w  w .j av  a 2 s .  com
 * @param postRequest The post request string
 * @param headers The request headers
 * @return The StringBuilder buffer containing the resulting document
 * @throws java.io.IOException
 * @see #addTextType(String)
 * @see #getContentSize()
 */
public StringBuilder fetchURL(String url, String postRequest, Map<String, String> headers) throws IOException {
    PostMethod method = new PostMethod(url);
    method.setFollowRedirects(followRedirects);
    setHeaders(method, headers);
    setParameters(method, postRequest);
    try {
        responseCode = hc.executeMethod(method);
        buildResponseHeaders(method);
        return fetchResponse(method);
    } finally {
        method.releaseConnection();
    }
}