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

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

Introduction

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

Prototype

public abstract int getStatusCode();

Source Link

Usage

From source file:com.zimbra.cs.dav.client.CalDavClient.java

private String getCurrentUserPrincipal() {
    DavRequest propfind = DavRequest.PROPFIND("/.well-known/caldav");
    propfind.addRequestProp(DavElements.E_CURRENT_USER_PRINCIPAL);
    HttpMethod m = null;
    try {/*from  w  ww .j  a v  a2  s  .  c o  m*/
        m = executeFollowRedirect(propfind);
        int status = m.getStatusCode();
        if (status >= 400) {
            return null;
        }
        Document doc = W3cDomUtil.parseXMLToDom4jDocUsingSecureProcessing(m.getResponseBodyAsStream());
        Element top = doc.getRootElement();
        for (Object obj : top.elements(DavElements.E_RESPONSE)) {
            if (obj instanceof Element) {
                DavObject davObject = new DavObject((Element) obj);
                Element e = davObject.getProperty(DavElements.E_CURRENT_USER_PRINCIPAL);
                if (e != null) {
                    return e.getStringValue().trim();
                }
            }
        }
    } catch (Exception e) {
        ZimbraLog.dav.debug("Exception thrown getting Current User Principal", e);
        return null;
    } finally {
        if (m != null) {
            m.releaseConnection();
        }
    }
    return null;
}

From source file:com.sun.jersey.client.apache.ApacheHttpClientHandler.java

@Override
public ClientResponse handle(final ClientRequest cr) throws ClientHandlerException {

    final HttpMethod method = getHttpMethod(cr);

    methodExecutor.executeMethod(method, cr);

    try {/*w w w.ja va 2s.  c om*/
        ClientResponse r = new ClientResponse(method.getStatusCode(), getInBoundHeaders(method),
                new HttpClientResponseInputStream(method), workers);
        if (!r.hasEntity()) {
            r.bufferEntity();
            r.close();
        }
        return r;
    } catch (Exception e) {
        method.releaseConnection();
        throw new ClientHandlerException(e);
    }
}

From source file:com.zimbra.cs.dav.client.WebDavClient.java

public Collection<DavObject> sendMultiResponseRequest(DavRequest req) throws IOException, DavException {
    ArrayList<DavObject> ret = new ArrayList<DavObject>();

    HttpMethod m = null;
    try {/*from w  w  w .  j  a v  a2 s  .  com*/
        m = executeFollowRedirect(req);
        int status = m.getStatusCode();
        if (status >= 400) {
            throw new DavException("DAV server returned an error: " + status, status);
        }

        Document doc = W3cDomUtil.parseXMLToDom4jDocUsingSecureProcessing(m.getResponseBodyAsStream());
        Element top = doc.getRootElement();
        for (Object obj : top.elements(DavElements.E_RESPONSE)) {
            if (obj instanceof Element) {
                ret.add(new DavObject((Element) obj));
            }
        }
    } catch (XmlParseException e) {
        throw new DavException("can't parse response", e);
    } finally {
        if (m != null) {
            m.releaseConnection();
        }
    }
    return ret;
}

From source file:com.shelrick.openamplify.client.OpenAmplifyClientImpl.java

private String doRequest(HttpMethod method) throws OpenAmplifyClientException {
    try {/*www .j  a v  a 2  s .co m*/
        httpClient.executeMethod(method);
        if (method.getStatusCode() != 200) {
            throw new OpenAmplifyClientException(
                    "Error: code=" + method.getStatusCode() + " with message: " + method.getStatusText());
        } else {
            return method.getResponseBodyAsString();
        }
    } catch (HttpException httpe) {
        throw new OpenAmplifyClientException(httpe);
    } catch (IOException ioe) {
        throw new OpenAmplifyClientException(ioe);
    }
}

From source file:com.zimbra.qa.unittest.TestPreAuthServlet.java

void doPreAuthServletRequest(String preAuthUrl, boolean admin) throws Exception {
    Server localServer = Provisioning.getInstance().getLocalServer();

    String protoHostPort;/*from w w w  . j  ava 2s.c  o m*/
    if (admin)
        protoHostPort = "https://localhost:" + localServer.getIntAttr(Provisioning.A_zimbraAdminPort, 0);
    else
        protoHostPort = "http://localhost:" + localServer.getIntAttr(Provisioning.A_zimbraMailPort, 0);

    String url = protoHostPort + preAuthUrl;

    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);

    try {
        int respCode = HttpClientUtil.executeMethod(client, method);
        int statusCode = method.getStatusCode();
        String statusLine = method.getStatusLine().toString();

        System.out.println("respCode=" + respCode);
        System.out.println("statusCode=" + statusCode);
        System.out.println("statusLine=" + statusLine);
        /*
        System.out.println("Headers");
        Header[] respHeaders = method.getResponseHeaders();
        for (int i=0; i < respHeaders.length; i++) {
        String header = respHeaders[i].toString();
        System.out.println(header);
        }
                
        String respBody = method.getResponseBodyAsString();
        // System.out.println("respBody=" + respBody);
        */

    } catch (HttpException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        method.releaseConnection();
    }
}

From source file:com.zimbra.cs.dav.client.WebDavClient.java

protected HttpMethod executeFollowRedirect(DavRequest req) throws IOException {
    HttpMethod method = null;
    boolean done = false;
    while (!done) {
        method = execute(req);/*from ww  w .j av a2s .  c om*/
        int ret = method.getStatusCode();
        if (ret == HttpStatus.SC_MOVED_PERMANENTLY || ret == HttpStatus.SC_MOVED_TEMPORARILY) {
            Header newLocation = method.getResponseHeader("Location");
            if (newLocation != null) {
                String uri = newLocation.getValue();
                ZimbraLog.dav.debug("redirect to new url = " + uri);
                method.releaseConnection();
                req.setRedirectUrl(uri);
                continue;
            }
        }
        done = true;
    }
    return method;
}

From source file:com.zimbra.qa.unittest.TestPreAuthServlet.java

public void testPreAuthAccountNotActive() throws Exception {
    String user = "user1";
    Account acct = TestUtil.getAccount(user);

    Provisioning prov = Provisioning.getInstance();

    Map<String, Object> attrs = new HashMap<String, Object>();
    attrs.put(Provisioning.A_zimbraAccountStatus, "maintenance");
    prov.modifyAttrs(acct, attrs);/*w  w  w.  j  a  va2 s  . c o  m*/

    System.out.println("Before the test:");
    System.out.println(
            Provisioning.A_zimbraAccountStatus + ": " + acct.getAttr(Provisioning.A_zimbraAccountStatus));
    System.out.println();

    String preAuthKey = setUpDomain();
    String preAuthUrl = genPreAuthUrl(preAuthKey, user, false, false);

    System.out.println("preAuthKey=" + preAuthKey);
    System.out.println("preAuth=" + preAuthUrl);

    Server localServer = Provisioning.getInstance().getLocalServer();
    String protoHostPort = "http://localhost:" + localServer.getIntAttr(Provisioning.A_zimbraMailPort, 0);
    String url = protoHostPort + preAuthUrl;

    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    try {
        int respCode = HttpClientUtil.executeMethod(client, method);
        int statusCode = method.getStatusCode();
        String statusLine = method.getStatusLine().toString();
        System.out.println("respCode=" + respCode);
        System.out.println("statusCode=" + statusCode);
        System.out.println("statusLine=" + statusLine);
        assertEquals(400, statusCode);

    } catch (HttpException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        method.releaseConnection();
    }

    //revert account status back to active
    attrs = new HashMap<String, Object>();
    attrs.put(Provisioning.A_zimbraAccountStatus, "active");
    prov.modifyAttrs(acct, attrs);

    System.out.println("After the test:");
    System.out.println(
            Provisioning.A_zimbraAccountStatus + ": " + acct.getAttr(Provisioning.A_zimbraAccountStatus));
    System.out.println();
}

From source file:com.alibaba.intl.bcds.goldroom.remote.DoubanBookInfoFetcher.java

/**
 * @param isbn/*w  w w  . ja v  a 2  s.  c o  m*/
 * @param imageUrl
 * @return
 */
private String saveImage(String isbn, String imageUrl) {

    String largeImageUrl = StringUtils.replaceOnce(imageUrl, "s", "l");
    HttpMethod method = new GetMethod(largeImageUrl);
    HttpClient client = new HttpClient();
    try {
        // ??????api????douban????~~~
        client.executeMethod(method);
        if (method.getStatusCode() != 200) {
            method = new GetMethod(imageUrl);
            client.executeMethod(method);
        }
        byte[] imgBody = method.getResponseBody();
        return imageUtil.save(isbn, getImageSuffix(imageUrl), imgBody);
    } catch (Exception e) {
        logger.warn("error occurred while save image from url :" + imageUrl + " for isbn " + isbn, e);
    }
    return null;
}

From source file:com.cerema.cloud2.lib.common.OwnCloudClient.java

public RedirectionPath followRedirection(HttpMethod method) throws IOException {
    int redirectionsCount = 0;
    int status = method.getStatusCode();
    RedirectionPath result = new RedirectionPath(status, MAX_REDIRECTIONS_COUNT);
    while (redirectionsCount < MAX_REDIRECTIONS_COUNT && (status == HttpStatus.SC_MOVED_PERMANENTLY
            || status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_TEMPORARY_REDIRECT)) {

        Header location = method.getResponseHeader("Location");
        if (location == null) {
            location = method.getResponseHeader("location");
        }/*from  w ww. ja va 2s  .  c  o  m*/
        if (location != null) {
            Log_OC.d(TAG + " #" + mInstanceNumber, "Location to redirect: " + location.getValue());

            String locationStr = location.getValue();
            result.addLocation(locationStr);

            // Release the connection to avoid reach the max number of connections per host
            // due to it will be set a different url
            exhaustResponse(method.getResponseBodyAsStream());
            method.releaseConnection();

            method.setURI(new URI(locationStr, true));
            Header destination = method.getRequestHeader("Destination");
            if (destination == null) {
                destination = method.getRequestHeader("destination");
            }
            if (destination != null) {
                int suffixIndex = locationStr.lastIndexOf(
                        (mCredentials instanceof OwnCloudBearerCredentials) ? AccountUtils.ODAV_PATH
                                : AccountUtils.WEBDAV_PATH_4_0);
                String redirectionBase = locationStr.substring(0, suffixIndex);

                String destinationStr = destination.getValue();
                String destinationPath = destinationStr.substring(mBaseUri.toString().length());
                String redirectedDestination = redirectionBase + destinationPath;

                destination.setValue(redirectedDestination);
                method.setRequestHeader(destination);
            }
            status = super.executeMethod(method);
            result.addStatus(status);
            redirectionsCount++;

        } else {
            Log_OC.d(TAG + " #" + mInstanceNumber, "No location to redirect!");
            status = HttpStatus.SC_NOT_FOUND;
        }
    }
    return result;
}

From source file:com.tasktop.c2c.server.web.proxy.HttpProxy.java

void copyProxyReponse(HttpMethod proxyResponse, HttpServletResponse response) throws IOException {
    copyProxyHeaders(proxyResponse.getResponseHeaders(), response);
    response.setContentLength(getResponseContentLength(proxyResponse));
    copy(proxyResponse.getResponseBodyAsStream(), response.getOutputStream());
    if (proxyResponse.getStatusLine() != null) {
        int statCode = proxyResponse.getStatusCode();
        response.setStatus(statCode);/* ww  w . j  a  v  a2s.  co  m*/
    }
}