Example usage for org.apache.commons.httpclient HttpMethodBase getStatusLine

List of usage examples for org.apache.commons.httpclient HttpMethodBase getStatusLine

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethodBase getStatusLine.

Prototype

@Override
public StatusLine getStatusLine() 

Source Link

Document

Provides access to the response status line.

Usage

From source file:org.jboss.mod_cluster.Client.java

public int runit() throws Exception {

    PostMethod pm = null;//from   w ww.  ja  va 2 s  . co m
    GetMethod gm = null;
    HttpMethodBase bm = null;
    long starttime, endtime;
    if (httpClient == null)
        httpClient = new HttpClient();
    if (fd != null) {
        pm = new PostMethod(URL);
        // InputStreamRequestEntity buf = new InputStreamRequestEntity(fd);
        // XXX: Ugly hack to test...
        byte[] buffet = new byte[6144];
        for (int i = 0; i < buffet.length; i++)
            buffet[i] = 'a';
        ByteArrayRequestEntity buf = new ByteArrayRequestEntity(buffet);
        pm.setRequestEntity(buf);
        // pm.setRequestBody(fd);
        pm.setHttp11(true);
        pm.setContentChunked(true);
        // pm.setRequestContentLength(PostMethod.CONTENT_LENGTH_CHUNKED);
        bm = pm;
    } else if (post != null) {
        pm = new PostMethod(URL);
        pm.setRequestEntity(new StringRequestEntity(post, "application/x-www-form-urlencoded", "UTF8"));
        bm = pm;
    } else {
        gm = new GetMethod(URL);
        bm = gm;
    }
    if (user != null) {
        Credentials cred = new UsernamePasswordCredentials(user, pass);
        httpClient.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY, cred);
    }

    // System.out.println("Connecting to " + URL);

    Integer connectionTimeout = 40000;
    bm.getParams().setParameter("http.socket.timeout", connectionTimeout);
    bm.getParams().setParameter("http.connection.timeout", connectionTimeout);
    if (VirtualHost != null)
        bm.getParams().setVirtualHost(VirtualHost);
    httpClient.getParams().setParameter("http.socket.timeout", connectionTimeout);
    httpClient.getParams().setParameter("http.connection.timeout", connectionTimeout);
    if (jsessionid != null) {
        // System.out.println("jsessionid: " + jsessionid);
        bm.setRequestHeader("Cookie", "JSESSIONID=" + jsessionid);
    }

    try {
        if (gm == null) {
            pm.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
            starttime = System.currentTimeMillis();
            httpResponseCode = httpClient.executeMethod(pm);
            endtime = System.currentTimeMillis();
        } else {
            gm.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
            starttime = System.currentTimeMillis();
            httpResponseCode = httpClient.executeMethod(gm);
            endtime = System.currentTimeMillis();
        }

        if (httpResponseCode == 200) {
            response = bm.getResponseBodyAsString();
            Cookie[] cookies = httpClient.getState().getCookies();
            // System.out.println( "Cookies: " + cookies);
            if (cookies != null && cookies.length != 0) {
                for (int i = 0; i < cookies.length; i++) {
                    Cookie cookie = cookies[i];
                    // System.out.println( "Cookie: " + cookie.getName() + ", Value: " + cookie.getValue());
                    if (cookie.getName().equals("JSESSIONID")) {
                        if (jsessionid == null) {
                            jsessionid = cookie.getValue();
                            String nodes[] = jsessionid.split("\\.");
                            if (nodes.length == 2)
                                node = nodes[1];
                            System.out.println("cookie first time: " + jsessionid);
                            bm.releaseConnection();
                            return 0; // first time ok.
                        } else {
                            if (jsessionid.compareTo(cookie.getValue()) == 0) {
                                if (logok)
                                    if (bm.getResponseHeader("Date") != null)
                                        System.out.println("cookie ok: "
                                                + bm.getResponseHeader("Date").toString().replace('\r', ' ')
                                                        .replace('\n', ' ')
                                                + " response time: " + (endtime - starttime));
                                    else {
                                        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                                        Date date = new Date();
                                        System.out.println("cookie ok: " + dateFormat.format(date)
                                                + " response time: " + (endtime - starttime));
                                    }
                                bm.releaseConnection();
                                return 0;
                            } else {
                                System.out.println(
                                        "cookie \"second\" time: " + cookie.getValue() + " : " + jsessionid);
                                System.out.println("cookie changed");
                                bm.releaseConnection();
                                if (checkcookie)
                                    return -1;
                                else if (checknode) {
                                    String nodes[] = cookie.getValue().split("\\.");
                                    if (nodes.length != 2) {
                                        System.out.println("Can't find node in cookie");
                                        return -1;
                                    }
                                    if (nodes[1].compareTo(node) == 0) {
                                        return 0;
                                    } else {
                                        System.out.println("node " + nodes[1] + " changed too");
                                        return -1;
                                    }
                                } else
                                    return 0;
                            }
                        }
                    }
                }
            } else {
                // Look in the response to make sure that there is a cookie.
                int len = (int) bm.getResponseContentLength();

                if (jsessionid != null && bm.getResponseBodyAsString(len).indexOf(jsessionid) != -1) {
                    bm.releaseConnection();
                    return 0;
                }
                if (jsessionid == null && !checkcookie) {
                    return 0;
                }
                System.out.println("No cookies");
            }
            Header head = bm.getResponseHeader("getRequestedSessionId");
            if (head != null) {
                HeaderElement[] heade = head.getElements();
                requestedSessionId = heade[0].getValue();
            } else {
                requestedSessionId = null;
            }
        } else {
            System.out.println("response: " + httpResponseCode);
            System.out.println("response: " + bm.getStatusLine());
            response = bm.getResponseBodyAsString();
            System.out.println("response: " + response);
            success = false;
            httpClient = null;
        }
        // System.out.println("response:\n" + bm.getResponseBodyAsString(len)); 
    } catch (HttpException e) {
        e.printStackTrace();
        success = false;
        httpClient = null;
    }
    System.out.println("DONE: " + httpResponseCode);
    bm.releaseConnection();
    return httpResponseCode;
}

From source file:org.jboss.mod_cluster.ManagerClient.java

/**
  * Run the first test./*from   www. j a v a  2 s.  c  o m*/
  *
  * @param string a part of URL to connect to.
  *
  * @return ManagerClient object
  *
  * @throws IOException for any failures.
  */
public ManagerClient(String string) throws Exception {
    URL = "http://" + string + "/mod_cluster_manager/";
    GetMethod gm = null;
    HttpMethodBase bm = null;
    if (httpClient == null) {
        httpClient = new HttpClient();
        gm = new GetMethod(URL);
        bm = gm;
    }

    System.out.println("Connecting to " + URL);

    Integer connectionTimeout = 40000;
    bm.getParams().setParameter("http.socket.timeout", connectionTimeout);
    bm.getParams().setParameter("http.connection.timeout", connectionTimeout);
    httpClient.getParams().setParameter("http.socket.timeout", connectionTimeout);
    httpClient.getParams().setParameter("http.connection.timeout", connectionTimeout);

    try {
        httpResponseCode = httpClient.executeMethod(gm);

        if (httpResponseCode == 200) {
            // Read the nonce.
            String result = gm.getResponseBodyAsString();
            String[] records = result.split("\n");
            for (int i = 0; i < records.length; i++) {
                int j = records[i].indexOf("?nonce=");
                if (j < 0)
                    continue;
                j = j + 7;
                String nnonce = records[i].substring(j);
                int k = nnonce.indexOf('&');
                if (k > 0) {
                    nonce = nnonce.substring(0, k);
                    break;
                }
            }
        } else {
            System.out.println("response: " + httpResponseCode);
            System.out.println("response: " + bm.getStatusLine());
            throw (new Exception("Reponse notok"));
        }
        // System.out.println("response:\n" + bm.getResponseBodyAsString(len)); 
    } catch (HttpException e) {
        System.out.println("error: " + e);
        throw (e);
    }
    bm.releaseConnection();
}

From source file:org.moxie.proxy.connection.DownloadFailed.java

public DownloadFailed(HttpMethodBase get) {
    super("Download failed: " + get.getStatusLine().toString());
    statusLine = get.getStatusLine().toString();
}

From source file:org.tinygroup.httpvisit.impl.HttpVisitorImpl.java

String execute(HttpMethodBase method) {
    try {/*from  w w  w. j av a2 s .c  om*/
        if (client == null) {
            init();
        }
        LOGGER.logMessage(LogLevel.DEBUG, "?:{}", method.getURI().toString());
        if (!("ISO-8859-1").equals(requestCharset)) {
            method.addRequestHeader("Content-Type", "text/html; charset=" + requestCharset);
        }
        method.setDoAuthentication(authEnabled);
        int iGetResultCode = client.executeMethod(method);
        if (iGetResultCode == HttpStatus.SC_OK) {
            LOGGER.logMessage(LogLevel.DEBUG, "?");
            Header responseHeader = method.getResponseHeader("Content-Encoding");
            if (responseHeader != null) {
                String acceptEncoding = responseHeader.getValue();
                if (acceptEncoding != null && ("gzip").equals(acceptEncoding)) {
                    //gzip?
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                            method.getResponseBody());
                    GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);
                    return IOUtils.readFromInputStream(gzipInputStream, responseCharset);
                }
            }
            return new String(method.getResponseBody(), responseCharset);
        }
        LOGGER.logMessage(LogLevel.ERROR, "{}",
                method.getStatusLine().toString());
        throw new RuntimeException(method.getStatusLine().toString());
    } catch (Exception e) {
        LOGGER.logMessage(LogLevel.DEBUG, "{}", e.getMessage());
        throw new RuntimeException(e);
    } finally {
        method.releaseConnection();
    }
}