Example usage for org.apache.commons.httpclient.cookie CookiePolicy COMPATIBILITY

List of usage examples for org.apache.commons.httpclient.cookie CookiePolicy COMPATIBILITY

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.cookie CookiePolicy COMPATIBILITY.

Prototype

int COMPATIBILITY

To view the source code for org.apache.commons.httpclient.cookie CookiePolicy COMPATIBILITY.

Click Source Link

Usage

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

private void checkin(Cookie[] cookies) throws MojoExecutionException {
    if (deleteNodePaths != null && deleteNodePaths.startsWith("/")) {
        HttpClient client = new HttpClient();
        client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
        client.getState().addCookies(cookies);
        client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_DEFAULT_TIMEOUT);

        StringTokenizer paths = new StringTokenizer(deleteNodePaths, ";");
        while (paths.hasMoreTokens()) {
            String[] pathes = paths.nextToken().split(",");

            for (String path : pathes) {
                try {
                    if (isVersionable(cookies, path, client)) {
                        getLog().info("Node at : " + path + " is mix:versionable.");
                        checkin(cookies, path, client);
                    }/*from   www.  ja  v a  2 s .  com*/
                } catch (Exception e) {
                    getLog().error("ERROR: " + e.getClass().getName() + " " + e.getMessage());
                }
            }
        }
        saveAll(cookies);
    }
}

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;//from   w w w  . j  a va 2  s  .  c o m

    try {
        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:autohit.call.modules.SimpleHttpModule.java

/**
 * Start method for an HTTP session. It will set the target address for the client, as well as
 * clearing any state.//from  w  ww . ja va 2 s. com
 * 
 * @param addr
 *            the address. Do not include protocol, but you may add port
 *            (ie. "www.goatland.com:80").
 * @throws CallException
 */
private void start(String addr, int port) throws CallException {

    try {
        httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        creds = null;
        HttpState initialState = new HttpState();
        initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY);
        httpClient.setState(initialState);
        httpClient.setConnectionTimeout(DEFAULT_TIMEOUT);
        httpClient.getHostConfiguration().setHost(addr, port, "http");

    } catch (Exception ex) {
        throw new CallException(
                "Serious fault while creating session with start method.  Session is not valid.  error="
                        + ex.getMessage(),
                CallException.CODE_MODULE_FAULT, ex);
    }

    // NO CODE AFTER THIS!
    started = true;
}

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

/**
 * @param cookies// w w  w .jav a 2  s .c  o  m
 *            get a session using the same existing previously requested
 *            response cookies.
 * @throws MojoExecutionException
 *             if any error occurred during this process.
 */
@SuppressWarnings("deprecation")
private void getSession(final Cookie[] cookies) throws MojoExecutionException {
    PostMethod loginPost = new PostMethod(crxPath + "/login.jsp");
    loginPost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
    try {
        getLog().info("login to " + loginPost.getPath());
        loginPost.setParameter("Workspace", workspace);
        loginPost.setParameter("UserId", login);
        loginPost.setParameter("Password", password);
        HttpClient client = new HttpClient();
        client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
        client.getState().addCookies(cookies);
        client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_DEFAULT_TIMEOUT);
        int status = client.executeMethod(loginPost);
        // log the status
        getLog().info(
                "Response status: " + status + ", statusText: " + HttpStatus.getStatusText(status) + "\r\n");
        if (status == HttpStatus.SC_MOVED_TEMPORARILY) {
            getLog().info("Login successful");
        } else {
            logResponseDetails(loginPost);
            throw new MojoExecutionException("Login failed, response=" + HttpStatus.getStatusText(status));
        }
    } catch (Exception ex) {
        getLog().error("ERROR: " + ex.getClass().getName() + " " + ex.getMessage());
        throw new MojoExecutionException(ex.getMessage());
    } finally {
        loginPost.releaseConnection();
    }
}

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

/**
 * Start method for an HTTPS session. It will set the target address for the client, as well as
 * clearing any state./*from w ww  . j a va2 s. c  o  m*/
 * 
 * @param addr
 *            the address. Do not include protocol, but you may add port
 *            (ie. "www.goatland.com:443").
 * @throws CallException
 */
private void starthttps(String addr, int port) throws CallException {

    try {
        // buidl protocol
        Protocol myhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), port);

        httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        creds = null;
        HttpState initialState = new HttpState();
        initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY);
        httpClient.setState(initialState);
        httpClient.setConnectionTimeout(DEFAULT_TIMEOUT);
        httpClient.getHostConfiguration().setHost(addr, port, myhttps);

    } catch (Exception ex) {
        throw new CallException(
                "Serious fault while creating session with start method.  Session is not valid.  error="
                        + ex.getMessage(),
                CallException.CODE_MODULE_FAULT, ex);
    }

    // NO CODE AFTER THIS!
    started = true;
}

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

/**
 * @param cookies//from   w w w . j  a  v  a 2  s . c  o m
 *            the previous requests cookies to keep in the same session.
 * @throws MojoExecutionException
 *             if any error occurs during this process.
 */
@SuppressWarnings("deprecation")
private void uploadPackage(final Cookie[] cookies) throws MojoExecutionException {
    PostMethod filePost = new PostMethod(crxPath + "/packmgr/list.jsp");
    filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
    try {
        getLog().info("Uploading " + jarfile + " to " + filePost.getPath());
        File jarFile = new File(jarfile);
        Part[] parts = { new FilePart("file", jarFile) };
        filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
        HttpClient client = new HttpClient();
        client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
        client.getState().addCookies(cookies);
        client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_DEFAULT_TIMEOUT);
        int status = client.executeMethod(filePost);
        // log the status
        getLog().info(
                "Response status: " + status + ", statusText: " + HttpStatus.getStatusText(status) + "\r\n");
        if (status == HttpStatus.SC_MOVED_TEMPORARILY) {
            getLog().info("Upload complete");
        } else {
            logResponseDetails(filePost);
            throw new MojoExecutionException(
                    "Package upload failed, response=" + HttpStatus.getStatusText(status));
        }
    } catch (Exception ex) {
        getLog().error("ERROR: " + ex.getClass().getName() + " " + ex.getMessage());
        throw new MojoExecutionException(ex.getMessage());
    } finally {
        filePost.releaseConnection();
    }
}

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

/**
 * @param cookies/*w  w w  . ja v  a  2  s  . c  om*/
 *            the previous request response existing cookies to keep the
 *            session information.
 * @throws MojoExecutionException
 *             in case of any errors during this process.
 */
@SuppressWarnings("deprecation")
private void installPackage(final Cookie[] cookies) throws MojoExecutionException {
    File file = new File(jarfile);
    String url = crxPath + "/packmgr/unpack.jsp?Path=" + getPackagePath(file)
            + (aclIgnore ? "" : "&acHandling=overwrite");

    GetMethod loginPost = new GetMethod(url);
    loginPost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
    try {
        getLog().info("installing: " + url);
        HttpClient client = new HttpClient();
        client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
        client.getState().addCookies(cookies);
        client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_DEFAULT_TIMEOUT);
        int status = client.executeMethod(loginPost);
        // log the status
        getLog().info(
                "Response status: " + status + ", statusText: " + HttpStatus.getStatusText(status) + "\r\n");
        // if it's ok, proceed
        if (status == HttpStatus.SC_OK) {
            InputStream response = loginPost.getResponseBodyAsStream();
            if (response != null) {
                String responseBody = IOUtils.toString(response);
                if (responseBody.contains("Package installed in")) {
                    getLog().info("Install successful");
                } else {
                    logResponseDetails(loginPost);
                    throw new MojoExecutionException("Error installing package on crx");
                }
            } else {
                throw new MojoExecutionException("Null response when installing package on crx");
            }
        } else {
            logResponseDetails(loginPost);
            throw new MojoExecutionException("Installation failed");
        }
    } catch (Exception ex) {
        getLog().error("ERROR: " + ex.getClass().getName() + " " + ex.getMessage());
        throw new MojoExecutionException(ex.getMessage());
    } finally {
        loginPost.releaseConnection();
    }
}

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

/**
 * @param cookies//  w w w  . j  a  va 2 s .c  o  m
 *            the previous request response existing cookies to keep the
 *            session information.
 * @param path
 *            the node path to delete.
 * @throws MojoExecutionException
 *             in case of any errors during this process.
 */
@SuppressWarnings("deprecation")
private void deleteNode(final Cookie[] cookies, final String pathInput) throws MojoExecutionException {
    String[] pathes = pathInput.split(",");

    for (String path : pathes) {
        GetMethod removeCall = new GetMethod(
                crxPath + "/browser/delete_recursive.jsp?Path=" + path + "&action=delete");
        removeCall.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
        try {
            getLog().info("removing " + path);
            getLog().info(crxPath + "/browser/delete_recursive.jsp?Path=" + path + "&action=delete");

            HttpClient client = new HttpClient();
            client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
            client.getState().addCookies(cookies);
            client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_DEFAULT_TIMEOUT);
            removeCall.setFollowRedirects(false);

            if (isVersionable(cookies, path, client)) {
                getLog().info("Node at : " + path + " is mix:versionable.");
                checkout(cookies, path, client);
            }

            else {

                getLog().info("removing " + path);
                getLog().info(crxPath + "/browser/delete_recursive.jsp?Path=" + path + "&action=delete");

                int status = client.executeMethod(removeCall);

                if (status == HttpStatus.SC_OK) {
                    getLog().info("Node deleted");
                    // log the status
                    getLog().info("Response status: " + status + ", statusText: "
                            + HttpStatus.getStatusText(status) + "\r\n");
                } else {
                    logResponseDetails(removeCall);
                    throw new MojoExecutionException(
                            "Removing node " + path + " failed, response=" + HttpStatus.getStatusText(status));
                }
                getLog().info("Response status: " + status + ", statusText: " + HttpStatus.getStatusText(status)
                        + "\r\n");
            }
        } catch (Exception ex) {
            getLog().error("ERROR: " + ex.getClass().getName() + " " + ex.getMessage());
            throw new MojoExecutionException(ex.getMessage());
        } finally {
            removeCall.releaseConnection();
        }
    }

}

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

/**
 * @param cookies//from   w  w  w .j a  v  a  2  s . c  om
 *            the previous request response existing cookies to keep the
 *            session information.
 * @throws MojoExecutionException
 *             in case of any errors during this process.
 */
@SuppressWarnings("deprecation")
private void saveAll(final Cookie[] cookies) throws MojoExecutionException {
    GetMethod savePost = new GetMethod(crxPath + "/browser/content.jsp?Path=/&action_ops=saveAll");
    savePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
    try {
        getLog().info("save all changes");
        HttpClient client = new HttpClient();
        client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
        client.getState().addCookies(cookies);
        client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_DEFAULT_TIMEOUT);
        int status = client.executeMethod(savePost);
        // log the status
        getLog().info(
                "Response status: " + status + ", statusText: " + HttpStatus.getStatusText(status) + "\r\n");
        if (status == HttpStatus.SC_OK) {
            getLog().info("All changes saved");
        } else {
            logResponseDetails(savePost);
            throw new MojoExecutionException(
                    "save all changes failed, response=" + HttpStatus.getStatusText(status));
        }
    } catch (Exception ex) {
        getLog().error("ERROR: " + ex.getClass().getName() + " " + ex.getMessage());
        throw new MojoExecutionException(ex.getMessage());
    } finally {
        savePost.releaseConnection();
    }
}

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

@SuppressWarnings("deprecation")
private void backUp(final Cookie[] cookies) throws MojoExecutionException {
    checkBackupFolder();//from ww  w . jav a  2s.c  om
    File file = new File(jarfile);
    GetMethod backupPost = new GetMethod(crxPath + "/packmgr/service.jsp?cmd=get&_charset_=utf8&name="
            + FilenameUtils.getBaseName(file.getName()));
    backupPost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
    try {
        getLog().info("backing up /etc/packages/" + file.getName());
        HttpClient client = new HttpClient();
        client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
        client.getState().addCookies(cookies);
        client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_DEFAULT_TIMEOUT);
        int status = client.executeMethod(backupPost);
        // log the status
        getLog().info(
                "Response status: " + status + ", statusText: " + HttpStatus.getStatusText(status) + "\r\n");

        String backUpFileName = formatbackUpFileName(file);

        File backupFile = new File(backUpFileName);

        if (status == HttpStatus.SC_OK) {
            copyStreamToFile(backupPost.getResponseBodyAsStream(), backupFile);
            getLog().info("Back-up succesfull. The backup is " + backupFile.getAbsolutePath());
        } else {
            logResponseDetails(backupPost);
            throw new MojoExecutionException("Back-up failed, response=" + HttpStatus.getStatusText(status));
        }
    } catch (Exception ex) {
        getLog().error("ERROR: " + ex.getClass().getName() + " " + ex.getMessage());
        throw new MojoExecutionException(ex.getMessage());
    } finally {
        backupPost.releaseConnection();
    }
}