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.sonatype.nexus.integrationtests.nexus4548.Nexus4548RepoTargetPermissionMatchesPathInRepoIT.java

private HttpMethod put(final String gavPath, final int code) throws Exception {
    PutMethod putMethod = new PutMethod(getNexusTestRepoUrl() + gavPath);
    putMethod.setRequestEntity(new FileRequestEntity(getTestFile("pom-a.pom"), "text/xml"));

    final HttpMethod httpMethod = RequestFacade.executeHTTPClientMethod(putMethod);
    assertThat(httpMethod.getStatusCode(), Matchers.is(code));

    return httpMethod;
}

From source file:org.sonatype.nexus.integrationtests.nexus4548.Nexus4548RepoTargetPermissionMatchesPathInRepoIT.java

private HttpMethod putRest(final String artifactId, final int code) throws IOException {
    File testFile = getTestFile(String.format("pom-%s.pom", artifactId));
    final HttpMethod httpMethod = getDeployUtils().deployPomWithRest("releases", testFile);
    assertThat(httpMethod.getStatusCode(), Matchers.is(code));

    return httpMethod;
}

From source file:org.sonatype.nexus.integrationtests.nexus950.Nexus950CorruptPomIT.java

@Test
public void uploadCorruptPomTest() throws HttpException, IOException {

    File jarFile = this.getTestFile("bad-pom.jar");
    File badPomFile = this.getTestFile("pom.xml");

    HttpMethod resultMethod = getDeployUtils().deployUsingPomWithRestReturnResult(this.getTestRepositoryId(),
            jarFile, badPomFile, "", "jar");

    Assert.assertEquals("Expected a 400 error returned.", 400, resultMethod.getStatusCode());
}

From source file:org.sonatype.nexus.integrationtests.nxcm4389.NXCM4389FavIconIT.java

private void assertExists(final String url) throws IOException {
    final GetMethod get = new GetMethod(url);
    final HttpMethod result = RequestFacade.executeHTTPClientMethod(get);
    assertThat(result.getStatusCode(), is(200));
}

From source file:org.sonatype.nexus.test.utils.NexusStatusUtil.java

public boolean isNexusRESTStarted() throws NexusIllegalStateException {
    final String statusURI = AbstractNexusIntegrationTest.nexusBaseUrl + RequestFacade.SERVICE_LOCAL + "status";
    // by not using test context we are only checking anonymously - this may not be a good idea, not sure
    org.apache.commons.httpclient.HttpMethod method = null;
    try {/*from  w w w  . jav  a  2 s  . c  om*/
        try {
            method = RequestFacade.executeHTTPClientMethod(new GetMethod(statusURI), false);
        } catch (HttpException ex) {
            throw new NexusIllegalStateException("Problem executing status request: ", ex);
        } catch (IOException ex) {
            throw new NexusIllegalStateException("Problem executing status request: ", ex);
        }

        final int statusCode = method.getStatusCode();
        // 200 if anonymous access is enabled
        // 401 if nexus is running but anonymous access is disabled
        if (statusCode == 401) {
            return true;
        } else if (statusCode != 200) {
            log.debug("Status check returned status " + statusCode);
            return false;
        }

        String entityText;
        try {
            entityText = method.getResponseBodyAsString();
        } catch (IOException e) {
            throw new NexusIllegalStateException("Unable to retrieve nexus status body", e);
        }

        StatusResourceResponse status = (StatusResourceResponse) XStreamFactory.getXmlXStream()
                .fromXML(entityText);
        if (!SystemState.STARTED.toString().equals(status.getData().getState())) {
            log.debug("Status check returned system state " + status.getData().getState());
            return false;
        }

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

From source file:org.springbyexample.httpclient.AbstractHttpClientTemplate.java

/**
 * Validate response./*from   w w  w .  j  a  va 2 s. co  m*/
 * 
 * @param   httpMethod      <code>HttpMethod</code> to validate.
 */
protected void validateResponse(HttpMethod httpMethod) {
    if (httpMethod.getStatusCode() >= 300) {
        throw new HttpAccessException(
                "Did not receive successful HTTP response: status code = " + httpMethod.getStatusCode()
                        + ", status message = [" + httpMethod.getStatusText() + "]",
                httpMethod.getStatusCode());
    }
}

From source file:org.springbyexample.httpclient.HttpClientOxmTemplate.java

/**
 * Processes <code>HttpMethod</code> by executing the method, 
 * validating the response, and calling the callback.
 * //w  w w.j a v a2  s . c o  m
 * @param   httpMethod      <code>HttpMethod</code> to process.
 * @param   callback        Callback with HTTP method's response.
 */
@SuppressWarnings("unchecked")
protected void processHttpMethod(HttpMethod httpMethod, ResponseCallback callback) {
    try {
        client.executeMethod(httpMethod);

        validateResponse(httpMethod);

        if (callback != null) {
            Object value = unmarshaller.unmarshal(new StreamSource(httpMethod.getResponseBodyAsStream()));

            callback.doWithResponse((T) value);
        }
    } catch (HttpException e) {
        throw new HttpAccessException(e.getMessage(), e, httpMethod.getStatusCode());
    } catch (IOException e) {
        throw new HttpAccessException(e.getMessage(), e);
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:org.springbyexample.httpclient.HttpClientTemplate.java

/**
 * Processes <code>HttpMethod</code> by executing the method, 
 * validating the response, and calling the callback.
 * //from w ww  .ja  v  a 2  s .  com
 * @param   httpMethod      <code>HttpMethod</code> to process.
 * @param   callback        Callback with HTTP method's response.
 */
protected void processHttpMethod(HttpMethod httpMethod, ResponseCallback<?> callback) {
    try {
        client.executeMethod(httpMethod);

        validateResponse(httpMethod);

        if (callback instanceof ResponseByteCallback) {
            ((ResponseByteCallback) callback).doWithResponse(httpMethod.getResponseBody());
        } else if (callback instanceof ResponseStreamCallback) {
            ((ResponseStreamCallback) callback).doWithResponse(httpMethod.getResponseBodyAsStream());
        } else if (callback instanceof ResponseStringCallback) {
            ((ResponseStringCallback) callback).doWithResponse(httpMethod.getResponseBodyAsString());
        }
    } catch (HttpException e) {
        throw new HttpAccessException(e.getMessage(), e, httpMethod.getStatusCode());
    } catch (IOException e) {
        throw new HttpAccessException(e.getMessage(), e);
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:org.springside.fi.common.httpclient.HttpClientTemplate.java

/**
 * Processes <code>HttpMethod</code> by executing the method, 
 * validating the response, and calling the callback.
 * //from w w w.j a v a 2  s  . c  o  m
 * @param   httpMethod      <code>HttpMethod</code> to process.
 * @param   callback        Callback with HTTP method's response.
 */
protected byte[] processHttpMethod(HttpMethod httpMethod) {
    try {
        client.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
        client.getHttpConnectionManager().getParams().setSoTimeout(60000);
        client.executeMethod(httpMethod);

        validateResponse(httpMethod);

        return httpMethod.getResponseBody();
    } catch (HttpException e) {
        throw new HttpAccessException(e.getMessage(), e, httpMethod.getStatusCode());
    } catch (IOException e) {
        throw new HttpAccessException(e.getMessage(), e);
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:org.svenk.redmine.core.client.AbstractRedmineClient.java

/**
 * Ask user for name and password./*from ww  w. j a  v a  2s.com*/
 * 
 * @param authenticationType
 * @param method
 * @param monitor
 * @return
 * @throws RedmineException
 */
protected HostConfiguration refreshCredentials(AuthenticationType authenticationType, HttpMethod method,
        IProgressMonitor monitor) throws RedmineException {
    if (Policy.isBackgroundMonitor(monitor)) {
        throw new RedmineAuthenticationException(method.getStatusCode(),
                Messages.AbstractRedmineClient_MISSING_CREDENTIALS_MANUALLY_SYNC_REQUIRED);
    }

    try {
        String message = Messages.AbstractRedmineClient_AUTHENTICATION_REQUIRED;
        if (authenticationType.equals(AuthenticationType.HTTP)) {
            Header authHeader = method.getResponseHeader(HEADER_WWW_AUTHENTICATE);
            if (authHeader != null) {
                for (HeaderElement headerElem : authHeader.getElements()) {
                    if (headerElem.getName().contains(HEADER_WWW_AUTHENTICATE_REALM)) {
                        message += ": " + headerElem.getValue(); //$NON-NLS-1$
                        break;
                    }
                }
            }
        }
        location.requestCredentials(authenticationType, message, monitor);

        return WebUtil.createHostConfiguration(httpClient, location, monitor);
    } catch (UnsupportedRequestException e) {
        IStatus status = RedmineCorePlugin.toStatus(e, null,
                Messages.AbstractRedmineClient_CREDENTIALS_REQUEST_FAILED);
        StatusHandler.log(status);
        throw new RedmineStatusException(status);
    } catch (OperationCanceledException e) {
        monitor.setCanceled(true);
        throw new RedmineException(Messages.AbstractRedmineClient_AUTHENTICATION_CANCELED);
    }
}