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

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

Introduction

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

Prototype

@Override
public int getStatusCode() 

Source Link

Document

Returns the response status code.

Usage

From source file:org.rssowl.core.internal.connection.DefaultProtocolHandler.java

private InputStream internalOpenStream(URI link, URI authLink, String authRealm, Map<Object, Object> properties)
        throws ConnectionException {

    /* Handle File Protocol at first */
    if (URIUtils.FILE_SCHEME.equals(link.getScheme()))
        return loadFileProtocol(link);

    /* SSL Support */
    if (URIUtils.HTTPS_SCHEME.equals(link.getScheme()))
        initSSLProtocol();//  www  .ja v a 2  s  .  c  o m

    /* Feed Support */
    if (URIUtils.FEED_SCHEME.equals(link.getScheme()))
        initFeedProtocol();

    /* Init Client */
    HttpClient client = initClient(properties);

    /* Init the connection */
    HttpMethodBase method = null;
    InputStream inS = null;
    try {

        /* Create Method (GET or POST) */
        method = initConnection(link, properties);

        /* Proxy if required */
        setupProxy(link, client);

        /* Authentication if required */
        setupAuthentication(authLink, authRealm, client, method);

        /* Open the connection */
        inS = openConnection(client, method);

        /* Try to pipe the resulting stream into a GZipInputStream */
        if (inS != null)
            inS = pipeStream(inS, method);
    } catch (IOException e) {
        abortAndRelease(method);
        throw new ConnectionException(Activator.getDefault().createErrorStatus(e.getMessage(), e));
    }

    /* In case authentication required */
    int statusCode = method.getStatusCode();
    if (statusCode == HTTP_ERROR_AUTH_REQUIRED) {
        AuthState hostAuthState = method.getHostAuthState();
        String realm = hostAuthState != null ? hostAuthState.getRealm() : null;
        abortAndRelease(method);

        throw new AuthenticationRequiredException(realm, Activator.getDefault()
                .createErrorStatus(Messages.DefaultProtocolHandler_ERROR_AUTHENTICATION_REQUIRED, null));
    }

    /* In case sync authentication failed (Forbidden) */
    else if (isSyncAuthenticationIssue(method, authLink)) {
        abortAndRelease(method);

        throw new AuthenticationRequiredException(null, Activator.getDefault()
                .createErrorStatus(Messages.DefaultProtocolHandler_GR_ERROR_BAD_AUTH, null));
    }

    /* In case of Forbidden Status with Error Code (Google Reader) */
    else if (statusCode == HTTP_ERROR_FORBIDDEN && method.getResponseHeader(HEADER_RESPONSE_ERROR) != null)
        handleForbidden(method);

    /* In case proxy-authentication required / failed */
    else if (statusCode == HTTP_ERROR_PROXY_AUTH_REQUIRED) {
        abortAndRelease(method);

        throw new ProxyAuthenticationRequiredException(Activator.getDefault()
                .createErrorStatus(Messages.DefaultProtocolHandler_ERROR_PROXY_AUTHENTICATION_REQUIRED, null));
    }

    /* If status code is 4xx, throw an IOException with the status code included */
    else if (statusCode >= HTTP_ERRORS) {
        String error = getError(statusCode);
        abortAndRelease(method);

        if (error != null)
            throw new ConnectionException(Activator.getDefault()
                    .createErrorStatus(NLS.bind(Messages.DefaultProtocolHandler_ERROR_HTTP_STATUS_MSG,
                            String.valueOf(statusCode), error), null));

        throw new ConnectionException(Activator.getDefault().createErrorStatus(
                NLS.bind(Messages.DefaultProtocolHandler_ERROR_HTTP_STATUS, String.valueOf(statusCode)), null));
    }

    /* In case the Feed has not been modified since */
    else if (statusCode == HTTP_STATUS_NOT_MODIFIED) {
        abortAndRelease(method);

        throw new NotModifiedException(Activator.getDefault()
                .createInfoStatus(Messages.DefaultProtocolHandler_INFO_NOT_MODIFIED_SINCE, null));
    }

    /* In case response body is not available */
    if (inS == null) {
        abortAndRelease(method);

        throw new ConnectionException(Activator.getDefault()
                .createErrorStatus(Messages.DefaultProtocolHandler_ERROR_STREAM_UNAVAILABLE, null));
    }

    /* Check wether a Progress Monitor is provided to support early cancelation */
    IProgressMonitor monitor = null;
    if (properties != null && properties.containsKey(IConnectionPropertyConstants.PROGRESS_MONITOR))
        monitor = (IProgressMonitor) properties.get(IConnectionPropertyConstants.PROGRESS_MONITOR);

    /* Return a Stream that releases the connection once closed */
    return new HttpConnectionInputStream(link, method, monitor, inS);
}

From source file:org.rssowl.core.internal.connection.DefaultProtocolHandler.java

private boolean isSyncAuthenticationIssue(HttpMethodBase method, URI link) {

    /* Handle Google Error Response "Forbidden" for synced connections */
    if (method.getStatusCode() == HTTP_ERROR_FORBIDDEN && SyncUtils.fromGoogle(link.toString())) {
        Header errorHeader = method.getResponseHeader(HEADER_RESPONSE_ERROR);
        if (errorHeader == null || ERROR_BAD_AUTH.equals(errorHeader.getValue()))
            return true;
    }/*from  w w  w .jav a 2 s .  c  o m*/

    return false;
}

From source file:org.sonar.wsclient.connectors.HttpClient3Connector.java

private String executeRequest(HttpMethodBase method) {
    String json = null;//from  ww  w. ja  v  a2s  .c  om
    try {
        httpClient.executeMethod(method);

        if (method.getStatusCode() == HttpStatus.SC_OK) {
            json = getResponseBodyAsString(method);

        } else if (method.getStatusCode() != HttpStatus.SC_NOT_FOUND) {
            throw new ConnectionException("HTTP error: " + method.getStatusCode() + ", msg: "
                    + method.getStatusText() + ", query: " + method);
        }

    } catch (HttpException e) {
        throw new ConnectionException("Query: " + method, e);

    } catch (IOException e) {
        throw new ConnectionException("Query: " + method, e);

    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
    return json;
}

From source file:org.sonarqube.ws.connectors.HttpClient3Connector.java

/**
 * Reauest execution/*w  ww.ja  v a 2  s  . c om*/
 * 
 * @param method
 *            method
 * @return String result
 */
private String executeRequest(HttpMethodBase method) throws ConnectionException {
    String json = null;
    try {
        httpClient.executeMethod(method);

        if (method.getStatusCode() == HttpStatus.SC_OK) {
            json = getResponseBodyAsString(method);

        } else if (method.getStatusCode() != HttpStatus.SC_NOT_FOUND) {
            throw new ConnectionException("HTTP error: " + method.getStatusCode() + ", msg: "
                    + method.getStatusText() + ", query: " + method);
        }

    } catch (IOException e) {
        throw new ConnectionException("Query: " + method, e);

    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
    return json;
}