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

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

Introduction

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

Prototype

public abstract StatusLine getStatusLine();

Source Link

Usage

From source file:org.apache.oodt.cas.filemgr.catalog.solr.SolrClient.java

/**
 * Common functionality for HTTP GET and POST requests.
 * @param method/*  w  w w.j a  v a  2 s.c o  m*/
 * @return
 * @throws Exception
 */
private String doHttp(HttpMethod method) throws IOException, CatalogException {

    StringBuilder response = new StringBuilder();
    BufferedReader br = null;
    try {

        // send request
        HttpClient httpClient = new HttpClient();
        // OODT-719 Prevent httpclient from spawning closewait tcp connections
        method.setRequestHeader("Connection", "close");

        int statusCode = httpClient.executeMethod(method);

        // read response
        if (statusCode != HttpStatus.SC_OK) {

            // still consume the response
            method.getResponseBodyAsString();
            throw new CatalogException("HTTP method failed: " + method.getStatusLine());

        } else {

            // read the response body.
            br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
            String readLine;
            while (((readLine = br.readLine()) != null)) {
                response.append(readLine);
            }

        }

    } finally {
        // must release the connection even if an exception occurred
        method.releaseConnection();
        if (br != null) {
            try {
                br.close();
            } catch (Exception ignored) {
            }
        }
    }

    return response.toString();

}

From source file:org.apache.solr.client.solrj.impl.CommonsHttpSolrServer.java

public NamedList<Object> request(final SolrRequest request, ResponseParser processor)
        throws SolrServerException, IOException {
    HttpMethod method = null;
    InputStream is = null;/*ww w .j  a  va  2 s .c  o  m*/
    SolrParams params = request.getParams();
    Collection<ContentStream> streams = requestWriter.getContentStreams(request);
    String path = requestWriter.getPath(request);
    if (path == null || !path.startsWith("/")) {
        path = "/select";
    }

    ResponseParser parser = request.getResponseParser();
    if (parser == null) {
        parser = _parser;
    }

    // The parser 'wt=' and 'version=' params are used instead of the original params
    ModifiableSolrParams wparams = new ModifiableSolrParams();
    wparams.set(CommonParams.WT, parser.getWriterType());
    wparams.set(CommonParams.VERSION, parser.getVersion());
    if (params == null) {
        params = wparams;
    } else {
        params = new DefaultSolrParams(wparams, params);
    }

    if (_invariantParams != null) {
        params = new DefaultSolrParams(_invariantParams, params);
    }

    int tries = _maxRetries + 1;
    try {
        while (tries-- > 0) {
            // Note: since we aren't do intermittent time keeping
            // ourselves, the potential non-timeout latency could be as
            // much as tries-times (plus scheduling effects) the given
            // timeAllowed.
            try {
                if (SolrRequest.METHOD.GET == request.getMethod()) {
                    if (streams != null) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!");
                    }
                    method = new GetMethod(_baseURL + path + ClientUtils.toQueryString(params, false));
                } else if (SolrRequest.METHOD.POST == request.getMethod()) {

                    String url = _baseURL + path;
                    boolean isMultipart = (streams != null && streams.size() > 1);

                    if (streams == null || isMultipart) {
                        PostMethod post = new PostMethod(url);
                        post.getParams().setContentCharset("UTF-8");
                        if (!this.useMultiPartPost && !isMultipart) {
                            post.addRequestHeader("Content-Type",
                                    "application/x-www-form-urlencoded; charset=UTF-8");
                        }

                        List<Part> parts = new LinkedList<Part>();
                        Iterator<String> iter = params.getParameterNamesIterator();
                        while (iter.hasNext()) {
                            String p = iter.next();
                            String[] vals = params.getParams(p);
                            if (vals != null) {
                                for (String v : vals) {
                                    if (this.useMultiPartPost || isMultipart) {
                                        parts.add(new StringPart(p, v, "UTF-8"));
                                    } else {
                                        post.addParameter(p, v);
                                    }
                                }
                            }
                        }

                        if (isMultipart) {
                            int i = 0;
                            for (ContentStream content : streams) {
                                final ContentStream c = content;

                                String charSet = null;
                                PartSource source = new PartSource() {
                                    public long getLength() {
                                        return c.getSize();
                                    }

                                    public String getFileName() {
                                        return c.getName();
                                    }

                                    public InputStream createInputStream() throws IOException {
                                        return c.getStream();
                                    }
                                };

                                parts.add(new FilePart(c.getName(), source, c.getContentType(), charSet));
                            }
                        }
                        if (parts.size() > 0) {
                            post.setRequestEntity(new MultipartRequestEntity(
                                    parts.toArray(new Part[parts.size()]), post.getParams()));
                        }

                        method = post;
                    }
                    // It is has one stream, it is the post body, put the params in the URL
                    else {
                        String pstr = ClientUtils.toQueryString(params, false);
                        PostMethod post = new PostMethod(url + pstr);
                        //              post.setRequestHeader("connection", "close");

                        // Single stream as body
                        // Using a loop just to get the first one
                        final ContentStream[] contentStream = new ContentStream[1];
                        for (ContentStream content : streams) {
                            contentStream[0] = content;
                            break;
                        }
                        if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
                            post.setRequestEntity(new RequestEntity() {
                                public long getContentLength() {
                                    return -1;
                                }

                                public String getContentType() {
                                    return contentStream[0].getContentType();
                                }

                                public boolean isRepeatable() {
                                    return false;
                                }

                                public void writeRequest(OutputStream outputStream) throws IOException {
                                    ((RequestWriter.LazyContentStream) contentStream[0]).writeTo(outputStream);
                                }
                            });

                        } else {
                            is = contentStream[0].getStream();
                            post.setRequestEntity(
                                    new InputStreamRequestEntity(is, contentStream[0].getContentType()));
                        }
                        method = post;
                    }
                } else {
                    throw new SolrServerException("Unsupported method: " + request.getMethod());
                }
            } catch (NoHttpResponseException r) {
                // This is generally safe to retry on
                method.releaseConnection();
                method = null;
                if (is != null) {
                    is.close();
                }
                // If out of tries then just rethrow (as normal error).
                if ((tries < 1)) {
                    throw r;
                }
                //log.warn( "Caught: " + r + ". Retrying..." );
            }
        }
    } catch (IOException ex) {
        log.error("####request####", ex);
        throw new SolrServerException("error reading streams", ex);
    }

    method.setFollowRedirects(_followRedirects);
    method.addRequestHeader("User-Agent", AGENT);
    if (_allowCompression) {
        method.setRequestHeader(new Header("Accept-Encoding", "gzip,deflate"));
    }
    //    method.setRequestHeader("connection", "close");

    try {
        // Execute the method.
        //System.out.println( "EXECUTE:"+method.getURI() );

        int statusCode = _httpClient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            StringBuilder msg = new StringBuilder();
            msg.append(method.getStatusLine().getReasonPhrase());
            msg.append("\n\n");
            msg.append(method.getStatusText());
            msg.append("\n\n");
            msg.append("request: " + method.getURI());
            throw new SolrException(statusCode, java.net.URLDecoder.decode(msg.toString(), "UTF-8"));
        }

        // Read the contents
        String charset = "UTF-8";
        if (method instanceof HttpMethodBase) {
            charset = ((HttpMethodBase) method).getResponseCharSet();
        }
        InputStream respBody = method.getResponseBodyAsStream();
        // Jakarta Commons HTTPClient doesn't handle any
        // compression natively.  Handle gzip or deflate
        // here if applicable.
        if (_allowCompression) {
            Header contentEncodingHeader = method.getResponseHeader("Content-Encoding");
            if (contentEncodingHeader != null) {
                String contentEncoding = contentEncodingHeader.getValue();
                if (contentEncoding.contains("gzip")) {
                    //log.debug( "wrapping response in GZIPInputStream" );
                    respBody = new GZIPInputStream(respBody);
                } else if (contentEncoding.contains("deflate")) {
                    //log.debug( "wrapping response in InflaterInputStream" );
                    respBody = new InflaterInputStream(respBody);
                }
            } else {
                Header contentTypeHeader = method.getResponseHeader("Content-Type");
                if (contentTypeHeader != null) {
                    String contentType = contentTypeHeader.getValue();
                    if (contentType != null) {
                        if (contentType.startsWith("application/x-gzip-compressed")) {
                            //log.debug( "wrapping response in GZIPInputStream" );
                            respBody = new GZIPInputStream(respBody);
                        } else if (contentType.startsWith("application/x-deflate")) {
                            //log.debug( "wrapping response in InflaterInputStream" );
                            respBody = new InflaterInputStream(respBody);
                        }
                    }
                }
            }
        }
        return processor.processResponse(respBody, charset);
    } catch (HttpException e) {
        throw new SolrServerException(e);
    } catch (IOException e) {
        throw new SolrServerException(e);
    } finally {
        method.releaseConnection();
        if (is != null) {
            is.close();
        }
    }
}

From source file:org.apache.wookie.proxy.ProxyClient.java

private String executeMethod(HttpMethod method, Configuration properties)
        throws Exception, AuthenticationException {
    // Execute the method.
    try {//from   w  ww .  jav a 2s  .c  o m
        HttpClient client = new HttpClient();

        // set the clients proxy values if needed
        ConnectionsPrefsManager.setProxySettings(client, properties);

        if (fUseProxyAuthentication) {
            if (fBase64Auth != null) {
                method.setRequestHeader("Authorization", fBase64Auth);
            } else {
                List<String> authPrefs = new ArrayList<String>(2);
                authPrefs.add(AuthPolicy.DIGEST);
                authPrefs.add(AuthPolicy.BASIC);
                client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
                // send the basic authentication response even before the server gives an unauthorized response
                client.getParams().setAuthenticationPreemptive(true);
                // Pass our credentials to HttpClient
                client.getState().setCredentials(
                        new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                        new UsernamePasswordCredentials(fProxyUsername, fProxyPassword));
            }
        }

        // Add user language to http request in order to notify server of user's language
        Locale locale = Locale.getDefault();

        method.setRequestHeader("Accept-Language", locale.getLanguage()); //$NON-NLS-1$
        method.removeRequestHeader("Content-Type");
        //method.setRequestHeader("Content-Type","application/json");
        //method.setRequestHeader("Referer", "");
        //method.removeRequestHeader("Referer");
        method.setRequestHeader("Accept", "*/*");

        int statusCode = client.executeMethod(method);

        //System.out.println("response="+method.getResponseBodyAsString());
        //System.out.println("method="+method.toString());
        //System.out.println("response="+method.getResponseBodyAsStream());

        if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
            Header hType = method.getResponseHeader("Content-Type");
            if (hType != null) {
                fContentType = hType.getValue();
            }
            // for now we are only expecting Strings
            //return method.getResponseBodyAsString();
            return readFully(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        } else if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED
                || statusCode == HttpStatus.SC_UNAUTHORIZED) {
            throw new AuthenticationException("Authentication failed:" + method.getStatusLine() + ' '
                    + method.getURI() + ' ' + method.getStatusText());
        } else {
            throw new Exception("Method failed: " + method.getStatusLine() + ' ' + method.getURI() + ' ' //$NON-NLS-1$
                    + method.getStatusText());
        }
    } catch (IOException e) {
        throw e;
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:org.apache.wookie.util.gadgets.GadgetUtils.java

/**
 * Call a remote service//from   ww w.  jav a 2 s  .  c  om
 * @param method the method to invoke
 * @return the response from the remote service
 * @throws Exception
 */
private static String executeMethod(HttpMethod method) throws Exception {
    // Execute the method.
    System.out.println("executeMethod() start");
    try {
        HttpClient client = new HttpClient();

        // Add user language to http request in order to notify server of user's language
        Locale locale = Locale.getDefault();

        method.setRequestHeader("Accept-Language", locale.getLanguage()); //$NON-NLS-1$ 
        int statusCode = client.executeMethod(method);
        System.out.println("HTTP client returned status:" + statusCode);
        if (statusCode == HttpStatus.SC_OK) {
            // for now we are only expecting Strings               
            return method.getResponseBodyAsString();
        } else {
            throw new Exception("Method failed: " + method.getStatusLine() + ' ' + method.getURI() + ' ' //$NON-NLS-1$
                    + method.getStatusText() + method.getResponseBodyAsString());
        }
    } catch (IOException e) {
        System.out.println("CaughtIOException");
        throw new Exception("ERROR_CONNECT", e);
    } finally {
        // Release the connection.
        System.out.println("executeMethod() end");
        method.releaseConnection();
    }
}

From source file:org.araqne.pkg.HttpWagon.java

public static InputStream openDownloadStream(URL url, boolean useAuth, String username, String password)
        throws IOException {
    Logger logger = LoggerFactory.getLogger(HttpWagon.class.getName());
    logger.trace("http wagon: downloading {}", url);

    int socketTimeout = getSocketTimeout();
    int connectionTimeout = getConnectTimeout();

    HttpClient client = new HttpClient();

    client.getParams().setParameter("http.socket.timeout", socketTimeout);
    client.getParams().setParameter("http.connection.timeout", connectionTimeout);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, null);

    String realm = "";
    if (useAuth) {
        client.getParams().setAuthenticationPreemptive(true);
        if (realmCache.containsKey(getRealmCacheKey(url)))
            realm = realmCache.get(getRealmCacheKey(url));
        setClientAuth(url, username, password, realm, client);
    }/*from   w w w.j  a  va 2 s. c  o  m*/

    HttpMethod method = new GetMethod(url.toString());

    int statusCode = client.executeMethod(method);

    if (useAuth && statusCode == HttpStatus.SC_UNAUTHORIZED) {
        realm = getRealm(method);
        setClientAuth(url, username, password, realm, client);
        method = new GetMethod(url.toString());
        statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new IOException("digest auth failed: " + method.getStatusLine());
        } else {
            realmCache.put(getRealmCacheKey(url), realm);
        }
    }

    if (statusCode != HttpStatus.SC_OK) {
        throw new IOException("method failed: " + method.getStatusLine());
    }

    return method.getResponseBodyAsStream();
}

From source file:org.biomart.martservice.MartServiceUtils.java

private static MartServiceException constructException(HttpMethod method, String martServiceLocation,
        Exception cause) {//w ww. j a v a2 s .  c  o  m
    StringBuffer errorMessage = new StringBuffer();
    errorMessage.append("Error posting to " + martServiceLocation + lineSeparator);
    if (cause == null) {
        errorMessage.append(" " + method.getStatusLine() + lineSeparator);
    }
    if (method instanceof PostMethod) {
        PostMethod postMethod = (PostMethod) method;
        NameValuePair[] data = postMethod.getParameters();
        for (int i = 0; i < data.length; i++) {
            errorMessage.append(" " + data[i].getName() + " = " + data[i].getValue() + lineSeparator);
        }

    } else {
        errorMessage.append(method.getQueryString());
    }
    return new MartServiceException(errorMessage.toString(), cause);
}

From source file:org.cancergrid.ws.util.HttpContentReader.java

public static String getHttpContent(String httpUrl, String query, Method method) {
    LOG.debug("getHttpContent(httpUrl): " + httpUrl);
    LOG.debug("getHttpContent(query): " + query);
    LOG.debug("getHttpContent(method): " + method);

    HttpMethod httpMethod = null;
    if (httpUrl.contains("&amp;")) {
        httpUrl = httpUrl.replace("&amp;", "&");
    }/*from w w  w .java 2 s  . c o  m*/

    if (query != null && query.length() > 0 && query.startsWith("?") && query.contains("&amp;")) {
        query = query.replace("&amp;", "&");
    }

    try {
        //LOG.debug("Querying: " + httpUrl);
        if (method == Method.GET) {
            httpMethod = new GetMethod(httpUrl);
            if (query != null && query.length() > 0) {
                httpMethod.setQueryString(query);
            }
        } else if (method == Method.POST) {
            httpMethod = new PostMethod(httpUrl);
            if (query != null && query.length() > 0) {
                RequestEntity entity = new StringRequestEntity(query, "text/xml", "UTF-8");
                ((PostMethod) httpMethod).setRequestEntity(entity);
            }
        }

        httpMethod.setFollowRedirects(true);

        httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        Protocol.registerProtocol("https", new Protocol("https",
                new org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory(), 443));
        HttpClient client = new HttpClient();
        int statusCode = client.executeMethod(httpMethod);
        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + httpMethod.getStatusLine());
            LOG.error("Error querying: " + httpMethod.getURI().toString());
            throw new Exception("Method failed: " + httpMethod.getStatusLine());
        }

        byte[] responseBody = httpMethod.getResponseBody();
        return new String(responseBody, "UTF-8");
    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
    } catch (Exception e) {
        LOG.error(e.getMessage());
    } finally {
        httpMethod.releaseConnection();
    }
    return null;
}

From source file:org.codehaus.httpcache4j.client.HTTPClientResponseResolver.java

private HTTPResponse convertResponse(HttpMethod method) {
    Headers headers = new Headers();
    for (Header header : method.getResponseHeaders()) {
        headers = headers.add(header.getName(), header.getValue());
    }// w w  w.  j a  v a2 s  .c  o m
    InputStream stream = null;
    HTTPResponse response;
    try {
        stream = getInputStream(method);
        StatusLine line = new StatusLine(HTTPVersion.get(method.getStatusLine().getHttpVersion()),
                Status.valueOf(method.getStatusCode()), method.getStatusText());
        response = getResponseCreator().createResponse(line, headers, stream);
    } finally {
        if (stream == null) {
            method.releaseConnection();
        }
    }
    return response;
}

From source file:org.codehaus.httpcache4j.client.HTTPClientResponseResolverTest.java

private HTTPClientResponseResolver createResponseResolver(final HttpMethod httpMethod, final Status status,
        final Header[] headers) {
    try {//  w  w w. ja va2 s. c o  m
        when(httpMethod.getStatusLine()).thenReturn(new org.apache.commons.httpclient.StatusLine(
                String.format("HTTP/1.1 %s %s\r\n", status.getCode(), status.getName())));
    } catch (HttpException e) {
        throw new RuntimeException(e);
    }
    when(httpMethod.getStatusCode()).thenReturn(status.getCode());
    when(httpMethod.getResponseHeaders()).thenReturn(headers);
    return new TestableHTTPClientResponseResolver(httpMethod);
}

From source file:org.colombbus.tangara.net.TConnection.java

private String executeMethod(HttpMethod method) throws CommandException {
    int statusCode = 0;
    try {//w ww .  j a  v a2 s.co m
        statusCode = getClient().executeMethod(method);
        if (statusCode == HttpStatus.SC_REQUEST_TIMEOUT) {
            LOG.warn("Request timeout");
        }
        if (statusCode != HttpStatus.SC_OK) {
            String msg = "Method failed: " + method.getStatusLine(); //$NON-NLS-1$
            LOG.error(msg);
            throw new IOException(msg);
        }

        InputStream in = method.getResponseBodyAsStream();
        byte[] buffer = IOUtils.toByteArray(in);

        String response = new String(buffer, encodingCharset);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Response content:\n" + response);
        }
        return response;
    } catch (HttpException httpEx) {
        String msg = String.format("An HTTP error occurs during the " + //$NON-NLS-1$
                "execution of the method %s. The returned HTTP code is %d", //$NON-NLS-1$
                method.getPath(), statusCode);
        LOG.error(msg, httpEx);
        throw CommandExceptionFactory.createCommunicationException(method, CommandException.HTTP_ERR, msg,
                httpEx);
    } catch (IOException ioEx) {
        String msg = String.format("An IO communication error occurs during the execution of the method %s.",
                method.getPath());
        LOG.error(msg, ioEx);
        throw CommandExceptionFactory.createCommunicationException(method, CommandException.HTTP_ERR, msg,
                ioEx);
    } catch (Throwable th) {
        String msg = String.format("An unknown error occurs during the execution of the method %s",
                method.getPath());
        LOG.error(msg, th);
        throw CommandExceptionFactory.createCommunicationException(method, CommandException.HTTP_ERR, msg, th);

    }
}