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:org.openo.nfvo.vnfmadapter.service.csm.connect.ConnectMgrVnfm.java

/**
 * Make connection/*from   w  w  w  .  j a v  a2  s. c  o  m*/
 * <br>
 *
 * @param vnfmObj
 * @return
 * @since  NFVO 0.5
 */
public int connect(JSONObject vnfmObj, String authModel) {
    LOG.info("function=connect, msg=enter connect function.");

    ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString("userName"),
            vnfmObj.getString("password"), authModel);
    HttpMethod httpMethod = null;
    int statusCode = Constant.INTERNAL_EXCEPTION;

    try {
        httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
                .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
                .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
                .post().execute();
        statusCode = httpMethod.getStatusCode();

        String result = httpMethod.getResponseBodyAsString();
        LOG.info("connect result:" + result);
        if (statusCode == HttpStatus.SC_CREATED) {
            JSONObject accessObj = JSONObject.fromObject(result);
            JSONObject tokenObj = accessObj.getJSONObject("token");
            Header header = httpMethod.getResponseHeader("accessSession");
            setAccessSession(header.getValue());
            setRoaRand(tokenObj.getString("roa_rand"));
            statusCode = HttpStatus.SC_OK;
        } else {
            LOG.error("connect fail, code:" + statusCode + " re:" + result);
        }

    } catch (JSONException e) {
        LOG.error("function=connect, msg=connect JSONException e={}.", e);
    } catch (VnfmException e) {
        LOG.error("function=connect, msg=connect VnfmException e={}.", e);
    } catch (IOException e) {
        LOG.error("function=connect, msg=connect IOException e={}.", e);
    } finally {
        clearCSMPwd(info);
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }
    return statusCode;

}

From source file:org.openo.nfvo.vnfmadapter.service.csm.connect.ConnectMgrVnfm.java

/**
 * Make connection//from   w w  w .jav a 2 s .  c o m
 * <br>
 *
 * @param vnfmObj
 * @return
 * @since  NFVO 0.5
 */
public int connect(JSONObject vnfmObj) {
    LOG.info("function=connect, msg=enter connect function.");

    ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString("userName"),
            vnfmObj.getString("password"), Constant.ANONYMOUS);
    HttpMethod httpMethod = null;
    int statusCode = Constant.INTERNAL_EXCEPTION;

    try {
        httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
                .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
                .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
                .post().execute();
        statusCode = httpMethod.getStatusCode();

        String result = httpMethod.getResponseBodyAsString();

        if (statusCode == HttpStatus.SC_CREATED) {
            JSONObject accessObj = JSONObject.fromObject(result);
            JSONObject tokenObj = accessObj.getJSONObject("token");
            Header header = httpMethod.getResponseHeader("accessSession");
            setAccessSession(header.getValue());
            setRoaRand(tokenObj.getString("roa_rand"));
            statusCode = HttpStatus.SC_OK;
        } else {
            LOG.error("connect fail, code:" + statusCode + " re:" + result);
        }

    } catch (JSONException e) {
        LOG.error("function=connect, msg=connect JSONException e={}.", e);
    } catch (VnfmException e) {
        LOG.error("function=connect, msg=connect VnfmException e={}.", e);
    } catch (IOException e) {
        LOG.error("function=connect, msg=connect IOException e={}.", e);
    } finally {
        clearCSMPwd(info);
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }
    return statusCode;

}

From source file:org.openqa.selenium.remote.HttpCommandExecutor.java

private Response createResponse(HttpMethod httpMethod) throws Exception {
    Response response;/*from  w  w  w . j  a  va  2  s.co  m*/

    Header header = httpMethod.getResponseHeader("Content-Type");

    if (header != null && header.getValue().startsWith("application/json")) {
        response = new JsonToBeanConverter().convert(Response.class, httpMethod.getResponseBodyAsString());
    } else {
        response = new Response();

        if (header != null && header.getValue().startsWith("image/png")) {
            response.setValue(httpMethod.getResponseBody());
        } else {
            response.setValue(httpMethod.getResponseBodyAsString());
        }

        String uri = httpMethod.getURI().toString();
        int sessionIndex = uri.indexOf("/session/");
        if (sessionIndex != -1) {
            sessionIndex += "/session/".length();
            int nextSlash = uri.indexOf("/", sessionIndex);
            if (nextSlash != -1) {
                response.setSessionId(uri.substring(sessionIndex, nextSlash));
                response.setContext("foo");
            }
        }
    }
    response.setError(!(httpMethod.getStatusCode() > 199 && httpMethod.getStatusCode() < 300));

    if (response.getValue() instanceof String) {
        //We normalise to \n because Java will translate this to \r\n
        //if this is suitable on our platform, and if we have \r\n, java will
        //turn this into \r\r\n, which would be Bad!
        response.setValue(((String) response.getValue()).replace("\r\n", "\n"));
    }

    return response;
}

From source file:org.openqa.selenium.remote.HttpCommandExecutor.java

private boolean isRedirect(HttpMethod httpMethod) {
    int code = httpMethod.getStatusCode();
    return (code == 301 || code == 302 || code == 303 || code == 307)
            && httpMethod.getResponseHeader("location") != null;
}

From source file:org.osaf.caldav4j.exceptions.BadStatusException.java

public BadStatusException(HttpMethod method) {
    super(String.format(MESSAGE, method.getStatusCode(), method.getName(), method.getPath()));
}

From source file:org.red5.server.net.rtmpt.RTMPTClientConnector.java

private void checkResponseCode(HttpMethod method) {
    if (method.getStatusCode() != HttpStatus.SC_OK) {
        try {//from  w  w  w .j a  v  a  2  s  . c o m
            String body = method.getResponseBodyAsString();
            throw new RuntimeException(
                    "Bad HTTP status returned, line: " + method.getStatusLine() + "; body: " + body);
        } catch (IOException e) {
            throw new RuntimeException("Bad HTTP status returned, line: " + method.getStatusLine()
                    + "; in addition IOException occured on reading body", e);
        }
    }
}

From source file:org.roda.wui.filter.CasClient.java

/**
 * Returns an error message for invalid response from CAS server.
 * /*from   w  ww. j  av  a2s .  com*/
 * @param method
 *          the HTTP method
 * @return a String with the error message.
 */
private String invalidResponseMessage(final HttpMethod method) {
    return String.format("Invalid response from CAS server: %s - %s", method.getStatusCode(),
            method.getStatusText());
}

From source file:org.smartfrog.projects.alpine.transport.http.HttpTransportFault.java

private void bind(HttpMethod method) {
    status = method.getStatusCode();
    statusLine = method.getStatusText();
    try {//from  w  w w  .  java  2  s.  c  om
        response = method.getResponseBodyAsString();
    } catch (IOException e) {
        log.warn("Could not read response of fault", e);
    }
    addDetail(FaultConstants.QNAME_FAULTDETAIL_HTTPERRORCODE, Integer.toString(status));
}

From source file:org.sonatype.nexus.bundle.launcher.support.RequestUtils.java

public static boolean isNexusRESTStarted(final String nexusBaseURI) throws IOException, HttpException {
    Preconditions.checkNotNull(nexusBaseURI);
    final String serviceStatusURI = nexusBaseURI.endsWith("/") ? nexusBaseURI + "service/local/status"
            : nexusBaseURI + "/service/local/status";
    org.apache.commons.httpclient.HttpMethod method = null;
    try {//from w  w w  .  ja v  a2  s. c  om
        method = new GetMethod(serviceStatusURI);
        // only try once makes sense by default
        DefaultHttpMethodRetryHandler oneRetry = new DefaultHttpMethodRetryHandler(1, true);
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, oneRetry);

        method = executeHTTPClientMethodAsAdmin(method);
        final int statusCode = method.getStatusCode();
        if (statusCode != 200) {
            LOG.debug("Status check returned status " + statusCode);
            return false;
        }

        final String entityText = method.getResponseBodyAsString();
        if (entityText == null || !entityText.contains("<state>STARTED</state>")) {
            LOG.debug("Status check returned invalid system state. Status: " + entityText);
            return false;
        }

        return true;
    } finally {
        if (method != null) {
            method.releaseConnection(); // request facade does this but just making sure
        }
    }
}

From source file:org.sonatype.nexus.integrationtests.nexus3233.Nexus3233IndexPomSha1IT.java

@Test
@Category(INDEX.class)
public void restDeploy() throws Exception {
    final File pom = getTestFile("rest.pom");
    HttpMethod r = getDeployUtils().deployPomWithRest(REPO_TEST_HARNESS_REPO, pom);
    Assert.assertTrue("Unable to deploy artifact " + r.getStatusCode() + ": " + r.getStatusText(),
            Status.isSuccess(r.getStatusCode()));
    searchFor(pom);// w ww .java 2  s  .  com
}