Example usage for org.apache.commons.httpclient.params HttpMethodParams getCredentialCharset

List of usage examples for org.apache.commons.httpclient.params HttpMethodParams getCredentialCharset

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpMethodParams getCredentialCharset.

Prototype

public String getCredentialCharset() 

Source Link

Usage

From source file:com.cordys.coe.ac.httpconnector.execution.MethodExecutor.java

/**
 * Send the HTTP request to the web server and returns the response.
 * //from   ww  w. j  a v  a  2s  .  c  o  m
 * @param reqNode
 *            Request XML node.
 * @param serverConnection
 *            Server connection information.
 * @param methodInfo
 *            Method information information.
 * @param counters
 *            JMX performance counters
 * @return Response XML node.
 * @throws ConnectorException
 * @throws HandlerException
 */
public static int sendRequest(int reqNode, IServerConnection serverConnection, IMethodConfiguration methodInfo,
        PerformanceCounters counters) throws ConnectorException, HandlerException {
    boolean setJMXInfo = counters == null ? false : true;
    // Get request and response handlers for converting the data.
    IRequestHandler requestHandler = methodInfo.getRequestHandler();
    IResponseHandler responseHandler = methodInfo.getResponseHandler();

    // Create the connection
    HttpClient client = serverConnection.getHttpClient();

    client.getParams().setContentCharset("UTF-8");
    client.getParams().setCredentialCharset("UTF-8");
    client.getParams().setHttpElementCharset("UTF-8");

    HttpMethod httpMethod;
    int timeout = serverConnection.getTimeout();

    // Convert the SOAP request XML into an HTTP request.
    long startTime = 0;
    if (setJMXInfo) {
        startTime = counters.getStartTime();
    }
    httpMethod = requestHandler.process(reqNode, serverConnection, client);
    if (setJMXInfo) {
        counters.finishRequestTransformation(startTime);
    }

    // Set additional HTTP parameters.
    HttpMethodParams hmpMethodParams = httpMethod.getParams();

    if (hmpMethodParams != null) {
        // Set the authentication character set to UTF-8, if not set.
        String sCredCharset = hmpMethodParams.getCredentialCharset();

        if ((sCredCharset == null) || (sCredCharset.length() == 0)) {
            hmpMethodParams.setCredentialCharset("UTF-8");
        }

        if (timeout > 0) {
            hmpMethodParams.setSoTimeout(timeout);
        }
    }

    // Send the request and handle the response.
    try {
        if (setJMXInfo) {
            startTime = counters.getStartTime();
        }
        int statusCode = client.executeMethod(httpMethod);
        if (setJMXInfo) {
            counters.finishHTTP(startTime);
        }
        int validStatusCode = methodInfo.getValidResponseCode();

        if (statusCode != HttpStatus.SC_OK) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Received HTTP error code: " + statusCode);
            }
        }

        if (setJMXInfo) {
            startTime = counters.getStartTime();
        }
        // Convert the response into XML.
        int responseNode = responseHandler.convertResponseToXml(httpMethod, serverConnection,
                Node.getDocument(reqNode));
        if (setJMXInfo) {
            counters.finishResponseTransformation(startTime);
        }
        if ((validStatusCode >= 0) && (statusCode != validStatusCode)) {
            if (responseNode != 0) {
                Node.delete(responseNode);
                responseNode = 0;
            }

            throw new ConnectorException(ConnectorExceptionMessages.INVALID_STATUS_CODE_RECEIVED_0_EXPECTED_1,
                    statusCode, validStatusCode);
        }

        return responseNode;
    } catch (ConnectorException e) {
        throw e;
    } catch (HttpException e) {
        throw new ConnectorException(e, ConnectorExceptionMessages.HTTP_REQUEST_FAILED_0, e.getMessage());
    } catch (IOException e) {
        throw new ConnectorException(e, ConnectorExceptionMessages.HTTP_CONNECTION_FAILED_0, e.getMessage());
    } catch (XMLException e) {
        throw new ConnectorException((Throwable) e, ConnectorExceptionMessages.INVALID_RESPONSE_XML_RECEIVED);
    } finally {
        // Release the connection.
        httpMethod.releaseConnection();
    }
}