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

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

Introduction

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

Prototype

public abstract String getStatusText();

Source Link

Usage

From source file:net.sf.sail.webapp.domain.webservice.http.AbstractHttpRequest.java

/**
 * Checks the status code returned from an HTTP request against the status
 * code expected to be returned and logs the response information if the
 * status code is not the expected one. Override this method to handle
 * specific status codes.//from ww w . j  a  va 2  s  . c  om
 * 
 * @param method
 *            The request sent.
 * @param actualStatusCode
 *            The status code returned from the request
 * @return true if the status code matches the expected status code
 * @throws IOException
 *             when the http response body cannot be obtained.
 * @throws HttpStatusCodeException
 *             when the returned status code is not as expected.
 * 
 */

public boolean isValidResponseStatus(HttpMethod method, int actualStatusCode)
        throws IOException, HttpStatusCodeException {
    if (actualStatusCode == this.expectedResponseStatusCode)
        return true;

    String statusText = method.getStatusText();
    logMethodInfo(method, actualStatusCode);
    throw new HttpStatusCodeException(statusText);
}

From source file:com.thoughtworks.go.agent.service.AgentUpgradeService.java

void checkForUpgrade(String md5, String launcherMd5, String agentPluginsMd5) throws Exception {
    HttpMethod method = getAgentLatestStatusGetMethod();
    try {//  w  w  w  .  j a  va2 s .  c  o  m
        final int status = httpClient.executeMethod(method);
        if (status != 200) {
            LOGGER.error(
                    String.format("[Agent Upgrade] Got status %d %s from Go", status, method.getStatusText()));
            return;
        }
        validateIfLatestAgent(md5, method);
        validateIfLatestLauncher(launcherMd5, method);
        validateIfLatestPluginZipAvailable(agentPluginsMd5, method);
    } catch (IOException ioe) {
        String message = String.format("[Agent Upgrade] Couldn't connect to: %s: %s",
                urlService.getAgentLatestStatusUrl(), ioe.toString());
        LOGGER.error(message);
        LOGGER.debug(message, ioe);
        throw ioe;
    } finally {
        method.releaseConnection();
    }
}

From source file:com.marvelution.hudson.plugins.apiv2.client.connectors.HttpClient3Connector.java

/**
 * Internal method to convert a {@link HttpMethod} into a {@link ConnectorResponse}
 * /*w w  w  . j ava 2s  .  c  om*/
 * @param method the {@link HttpMethod} to convert
 * @return the {@link ConnectorResponse}
 * @throws ConnectionException in case of errors while creating the {@link ConnectorResponse}
 */
private ConnectorResponse getConnectorResponseFromMethod(HttpMethod method) throws ConnectionException {
    ConnectorResponse response = new ConnectorResponse();
    response.setStatusCode(method.getStatusCode());
    response.setReasonPhrase(method.getStatusText());
    try {
        response.setResponseAsStream(method.getResponseBodyAsStream());
    } catch (IOException e) {
        throw new ConnectionException("Failed to copy the response stream", e);
    }
    return response;
}

From source file:net.sf.ufsc.http.HttpFile.java

protected void execute(HttpMethod method) throws java.io.IOException {
    method.setFollowRedirects(true);/*from w  w  w  .  j  a v a 2  s .c  o m*/
    method.setDoAuthentication(true);

    int status = this.client.executeMethod(method);

    if (status != HttpStatus.SC_OK) {
        throw new java.io.IOException(method.getStatusText());
    }
}

From source file:io.fabric8.gateway.apiman.HTTPGatewayApiManTest.java

@Test
public void testGoldClientBadPathRequest() throws Exception {
    /** Tests obtaining the mapping info as JSON */
    int httpPort = httpGatewayServer.getPort();
    HttpClient httpClient = new HttpClient();
    HttpMethod method = new GetMethod("http://127.0.0.1:" + httpPort + "/mapping/notexist?apikey=gold-key");
    assertEquals(404, httpClient.executeMethod(method));
    String message = method.getStatusText();
    assertEquals("User error Service Not Found in API Manager.", message);
}

From source file:edu.ucsd.xmlrpc.xmlrpc.client.XmlRpcCommonsTransport.java

/**
 * Check the status of the HTTP request and throw an XmlRpcHttpTransportException if it
 * indicates that there is an error./*from   ww w .  ja  v  a 2  s  .c o  m*/
 * @param pMethod the method that has been executed
 * @throws XmlRpcHttpTransportException if the status of the method indicates that there is an error.
 */
private void checkStatus(HttpMethod pMethod) throws XmlRpcHttpTransportException {
    final int status = pMethod.getStatusCode();

    // All status codes except SC_OK are handled as errors. Perhaps some should require special handling (e.g., SC_UNAUTHORIZED)
    if (status < 200 || status > 299) {
        throw new XmlRpcHttpTransportException(status, pMethod.getStatusText());
    }
}

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

private String doRequest(HttpMethod method) throws OpenAmplifyClientException {
    try {// ww w  .  j  av a2 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.thoughtworks.go.agent.launcher.ServerBinaryDownloader.java

private boolean download() throws Exception {
    HttpClient httpClient = new HttpClient();
    HttpMethod method = new GetMethod(checkUrl());
    InputStream body = null;/*from   ww w  .  ja  va 2 s  . c  o  m*/
    OutputStream outputFile = null;
    httpClient.setConnectionTimeout(ServerCall.HTTP_TIMEOUT_IN_MILLISECONDS);
    try {
        LOG.info("download started at " + new Date());
        final int status = httpClient.executeMethod(method);
        if (status != 200) {
            throw new Exception("Got status " + status + " " + method.getStatusText() + " from server");
        }
        body = new BufferedInputStream(method.getResponseBodyAsStream());
        LOG.info("got server response at " + new Date());
        outputFile = new BufferedOutputStream(
                new FileOutputStream(new File(downloadableFile.getLocalFileName())));
        IOUtils.copy(body, outputFile);
        LOG.info("pipe the stream to " + downloadableFile + " at " + new Date());
        return true;
    } catch (Exception e) {
        String message = "Couldn't access Go Server with base url: " + downloadableFile.url(urlGenerator) + ": "
                + e.toString();
        LOG.error(message);
        throw new Exception(message, e);
    } finally {
        IOUtils.closeQuietly(body);
        IOUtils.closeQuietly(outputFile);
        method.releaseConnection();
    }
}

From source file:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClient.java

private void checkStreamStatus(int status, HttpMethod httpMethod) throws HttpClientException {
    LOG.debug(String.format("Got status: %s %s", status, httpMethod.getStatusText()));
    if (status != 200 && status != 201) {
        try {//  www. ja v  a  2s . c  o  m
            String responseBodyAsString = httpMethod.getResponseBodyAsString();
            if (!StringUtils.isEmpty(responseBodyAsString)) {
                responseBodyAsString = new String(responseBodyAsString.getBytes(), "UTF-8");
                throw new HttpClientException(
                        "Bad status : " + httpMethod.getStatusText() + ":\n" + responseBodyAsString);
            } else {
                throw new HttpClientException("Bad status : " + httpMethod.getStatusLine());
            }
        } catch (Exception e) {
            throw new HttpClientException("Bad status : " + status + ". Could not read response body.");
        }
    }
}

From source file:com.springsource.insight.plugin.apache.http.hc3.HttpClientExecutionCollectionAspectTest.java

private void assertResponseDetails(String uri, OperationMap details, HttpMethod method, int statusCode,
        boolean checkHeaders) {
    assertEquals("Mismatched status code", statusCode, details.getInt("statusCode", (-1)));
    assertEquals("Mismatched reason phrase", method.getStatusText(), details.get("reasonPhrase", String.class));
    if (checkHeaders) {
        assertHeadersContents(uri, "response", details, method, false);
    }//from w  ww  . j  a v a2 s  .c o m
}