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

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

Introduction

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

Prototype

public abstract String getQueryString();

Source Link

Usage

From source file:com.zenkey.net.prowser.Tab.java

/**************************************************************************
 * Writes tracing information that traces the request/response activity.
 * //from  w  w  w  . j  a va2s.co  m
 * @param traceLevel
 *        Indicates how much trace info to produce.
 * @param traceStream
 *        An output stream where trace statements will be written.
 * @param httpMethod
 *        The HttpMethod object of the request.
 */
private static void writeTrace(int traceLevel, PrintStream traceStream, HttpMethod httpMethod) {

    try {
        if (traceLevel >= TRACE_URI) {
            // Show trace output of the request URI
            traceStream
                    .println("-------------------------------------------------------------------------------");
            traceStream.println(httpMethod.getURI() + "\n");
        }

        if (traceLevel >= TRACE_REQUEST_RESPONSE_LINES) {
            // Show trace output of the HTTP request line
            traceStream.println(httpMethod.getName() + " " + httpMethod.getPath()
                    + (httpMethod.getQueryString() == null ? "" : "?" + httpMethod.getQueryString()) + " "
                    + httpMethod.getParams().getVersion().toString());
        }

        if (traceLevel >= TRACE_HEADERS) {
            // Show trace output of the HTTP request headers
            for (Header header : httpMethod.getRequestHeaders()) {
                traceStream.println(header.getName() + ": " + header.getValue());
            }
            // Show trace of request entity body
            if (httpMethod instanceof PostMethod) {
                NameValuePair[] parameters = ((PostMethod) httpMethod).getParameters();
                if (parameters != null) {
                    // StringBuffer parameterString = new StringBuffer();
                    // for (NameValuePair parameter : parameters) {
                    //       parameterString.append(parameter.getName() + "=" + parameter.getValue() + "&");
                    // }
                    // parameterString.deleteCharAt(parameterString.length() - 1);
                    String parameterString = new String(
                            ((ByteArrayRequestEntity) ((PostMethod) httpMethod).getRequestEntity())
                                    .getContent(),
                            "UTF-8");
                    traceStream.println("    |");
                    traceStream.println("    +-- " + parameterString);
                }
            }
            traceStream.println();
        }

        if (traceLevel >= TRACE_REQUEST_RESPONSE_LINES) {
            // Show trace output of the HTTP status line
            traceStream.println(httpMethod.getStatusLine().toString());
        }

        if (traceLevel >= TRACE_HEADERS) {
            // Show trace output of the HTTP response headers
            for (Header header : httpMethod.getResponseHeaders()) {
                traceStream.println(header.getName() + ": " + header.getValue());
            }
            traceStream.println();
        }

        if (traceLevel >= TRACE_BODY) {
            // Show trace output of the HTTP response body
            traceStream.println(httpMethod.getResponseBodyAsString());
            traceStream.println();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ehu.ned.DBpediaSpotlightClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;/*from  w  ww . ja v a  2s  .co m*/

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        // // Deal with the response.
        // // Use caution: ensure correct character encoding and is not binary data
        InputStream responseBody = method.getResponseBodyAsStream();
        response = IOUtils.toString(responseBody, "UTF-8");

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:br.com.edu.dbpediaspotlight.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {
    String response = null;//from   www.j a  va 2 s  .  c  o m
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        response = new String(responseBody);

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:ixa.pipe.ned.DBpediaSpotlightClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;// w ww . j  ava 2  s .  c o  m

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        // // Deal with the response.
        // // Use caution: ensure correct character encoding and is not binary data
        InputStream responseBody = method.getResponseBodyAsStream();
        response = StreamUtils.copyToString(responseBody, Charset.forName("UTF-8"));

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;/*from  w ww  .j  a va2s  .c  o  m*/

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        response = new String(responseBody);

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:com.mythesis.userbehaviouranalysis.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;//from  w  ww  . j a  v  a  2s.  c o m

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        //byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
        InputStream responseBodyAsStream = method.getResponseBodyAsStream();
        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        StringWriter writer = new StringWriter();
        IOUtils.copy(responseBodyAsStream, writer, "utf-8");
        response = writer.toString();

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:com.groupon.odo.Proxy.java

/**
 * Log modified request/*  www.j a v  a 2 s .  co  m*/
 *
 * @param httpMethodProxyRequest
 * @param httpServletResponse
 * @param history
 */
private void logRequestHistory(HttpMethod httpMethodProxyRequest, HttpServletResponse httpServletResponse,
        History history) {
    try {
        if (requestInformation.get().handle && requestInformation.get().client.getIsActive()) {
            logger.info("Storing history");
            String createdDate;
            SimpleDateFormat sdf = new SimpleDateFormat();
            sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
            sdf.applyPattern("dd MMM yyyy HH:mm:ss");
            createdDate = sdf.format(new Date()) + " GMT";

            history.setCreatedAt(createdDate);
            history.setRequestURL(HttpUtilities.getURL(httpMethodProxyRequest.getURI().toString()));
            history.setRequestParams(httpMethodProxyRequest.getQueryString() == null ? ""
                    : httpMethodProxyRequest.getQueryString());
            history.setRequestHeaders(HttpUtilities.getHeaders(httpMethodProxyRequest));
            history.setResponseHeaders(HttpUtilities.getHeaders(httpServletResponse));
            history.setResponseCode(Integer.toString(httpServletResponse.getStatus()));
            history.setResponseContentType(httpServletResponse.getContentType());
            history.setResponseData(requestInformation.get().outputString);
            HistoryService.getInstance().addHistory(history);
            logger.info("Done storing");
        }
    } catch (URIException e) {
        e.printStackTrace();
    }
}

From source file:ensen.controler.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;//from   w  w  w.  j  a  v a2 s .c  om

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        client.setHttpConnectionManager(new MultiThreadedHttpConnectionManager());
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            System.out.println("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        InputStream responseBodyStream = method.getResponseBodyAsStream(); //Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

        int b = responseBodyStream.read();
        ArrayList<Integer> bytes = new ArrayList<Integer>();
        while (b != -1) {
            bytes.add(b);
            b = responseBodyStream.read();
        }
        byte[] responseBody = new byte[bytes.size()];

        for (int i = 0; i < bytes.size(); i++) {
            responseBody[i] = bytes.get(i).byteValue();
        }
        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        response = new String(responseBody);

    } catch (HttpException e) {
        System.out.println("Fatal protocol violation: " + e.getMessage());
        try {
            System.err.println(method.getURI());
        } catch (URIException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        System.out.println("Fatal transport error: " + e.getMessage());
        System.out.println(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:davmail.exchange.ExchangeSession.java

protected void checkFormLoginQueryString(HttpMethod logonMethod) throws DavMailAuthenticationException {
    String queryString = logonMethod.getQueryString();
    if (queryString != null && (queryString.contains("reason=2") || queryString.contains("reason=4"))) {
        logonMethod.releaseConnection();
        throwAuthenticationFailed();//from w  w  w.  jav a2 s . c  o  m
    }
}

From source file:org.alfresco.wcm.client.impl.WebScriptCallerImpl.java

private void executeRequest(WebscriptResponseHandler handler, HttpMethod httpMethod,
        boolean ignoreUnauthorized) {
    long startTime = 0L;
    if (log.isDebugEnabled()) {
        startTime = System.currentTimeMillis();
    }/*w  ww.  ja  v a2 s.co  m*/
    try {
        httpClient.executeMethod(httpMethod);

        if ((httpMethod.getStatusCode() == 401 || httpMethod.getStatusCode() == 403) && !ignoreUnauthorized) {
            discardResponse(httpMethod);

            this.getTicket(username, password);
            httpClient.executeMethod(httpMethod);
        }

        if (httpMethod.getStatusCode() == 200) {
            handler.handleResponse(httpMethod.getResponseBodyAsStream());
        } else {
            // Must read the response, even though we don't use it
            discardResponse(httpMethod);
        }
    } catch (RuntimeException ex) {
        log.error("Rethrowing runtime exception.", ex);
        throw ex;
    } catch (Exception ex) {
        log.error("Failed to make request to Alfresco web script", ex);
    } finally {
        if (log.isDebugEnabled()) {
            log.debug(httpMethod.getName() + " request to " + httpMethod.getPath() + "?"
                    + httpMethod.getQueryString() + " completed in " + (System.currentTimeMillis() - startTime)
                    + "ms");
        }
        httpMethod.releaseConnection();
    }
}