Example usage for org.apache.commons.httpclient.params HttpParams getBooleanParameter

List of usage examples for org.apache.commons.httpclient.params HttpParams getBooleanParameter

Introduction

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

Prototype

public abstract boolean getBooleanParameter(String paramString, boolean paramBoolean);

Source Link

Usage

From source file:org.apache.ode.axis2.httpbinding.HttpMethodConverter.java

/**
 * create and initialize the http method.
 * Http Headers that may been passed in the params are not set in this method.
 * Headers will be automatically set by HttpClient.
 * See usages of HostParams.DEFAULT_HEADERS
 * See org.apache.commons.httpclient.HttpMethodDirector#executeMethod(org.apache.commons.httpclient.HttpMethod)
 *///w ww.  j av  a2s  .c o  m
protected HttpMethod prepareHttpMethod(BindingOperation opBinding, String verb, Map<String, Element> partValues,
        Map<String, Node> headers, final String rootUri, HttpParams params)
        throws UnsupportedEncodingException {
    if (log.isDebugEnabled())
        log.debug("Preparing http request...");
    // convenience variables...
    BindingInput bindingInput = opBinding.getBindingInput();
    HTTPOperation httpOperation = (HTTPOperation) WsdlUtils.getOperationExtension(opBinding);
    MIMEContent content = WsdlUtils.getMimeContent(bindingInput.getExtensibilityElements());
    String contentType = content == null ? null : content.getType();
    boolean useUrlEncoded = WsdlUtils.useUrlEncoded(bindingInput)
            || PostMethod.FORM_URL_ENCODED_CONTENT_TYPE.equalsIgnoreCase(contentType);
    boolean useUrlReplacement = WsdlUtils.useUrlReplacement(bindingInput);

    // the http method to be built and returned
    HttpMethod method = null;

    // the 4 elements the http method may be made of
    String relativeUri = httpOperation.getLocationURI();
    String queryPath = null;
    RequestEntity requestEntity;
    String encodedParams = null;

    // ODE supports uri template in both port and operation location.
    // so assemble the final url *before* replacement
    String completeUri = rootUri;
    if (StringUtils.isNotEmpty(relativeUri)) {
        completeUri = completeUri + (completeUri.endsWith("/") || relativeUri.startsWith("/") ? "" : "/")
                + relativeUri;
    }

    if (useUrlReplacement) {
        // insert part values in the url
        completeUri = new UrlReplacementTransformer().transform(completeUri, partValues);
    } else if (useUrlEncoded) {
        // encode part values
        encodedParams = new URLEncodedTransformer().transform(partValues);
    }

    // http-client api is not really neat
    // something similar to the following would save some if/else manipulations.
    // But we have to deal with it as-is.
    //
    //  method = new Method(verb);
    //  method.setRequestEnity(..)
    //  etc...
    if ("GET".equalsIgnoreCase(verb) || "DELETE".equalsIgnoreCase(verb)) {
        if ("GET".equalsIgnoreCase(verb)) {
            method = new GetMethod();
        } else if ("DELETE".equalsIgnoreCase(verb)) {
            method = new DeleteMethod();
        }
        method.getParams().setDefaults(params);
        if (useUrlEncoded) {
            queryPath = encodedParams;
        }

        // Let http-client manage the redirection
        // see org.apache.commons.httpclient.params.HttpClientParams.MAX_REDIRECTS
        // default is 100
        method.setFollowRedirects(true);
    } else if ("POST".equalsIgnoreCase(verb) || "PUT".equalsIgnoreCase(verb)) {

        if ("POST".equalsIgnoreCase(verb)) {
            method = new PostMethod();
        } else if ("PUT".equalsIgnoreCase(verb)) {
            method = new PutMethod();
        }
        method.getParams().setDefaults(params);
        // some body-building...
        final String contentCharset = method.getParams().getContentCharset();
        if (log.isDebugEnabled())
            log.debug("Content-Type [" + contentType + "] Charset [" + contentCharset + "]");
        if (useUrlEncoded) {
            requestEntity = new StringRequestEntity(encodedParams, PostMethod.FORM_URL_ENCODED_CONTENT_TYPE,
                    contentCharset);
        } else {
            // get the part to be put in the body
            Part part = opBinding.getOperation().getInput().getMessage().getPart(content.getPart());
            Element partValue = partValues.get(part.getName());

            if (part.getElementName() == null) {
                String errMsg = "XML Types are not supported. Parts must use elements.";
                if (log.isErrorEnabled())
                    log.error(errMsg);
                throw new RuntimeException(errMsg);
            } else if (HttpUtils.isXml(contentType)) {
                if (log.isDebugEnabled())
                    log.debug("Content-Type [" + contentType + "] equivalent to 'text/xml'");
                // stringify the first element
                String xmlString = DOMUtils.domToString(DOMUtils.getFirstChildElement(partValue));
                requestEntity = new StringRequestEntity(xmlString, contentType, contentCharset);
            } else {
                if (log.isDebugEnabled())
                    log.debug("Content-Type [" + contentType
                            + "] NOT equivalent to 'text/xml'. The text content of part value will be sent as text");
                // encoding conversion is managed by StringRequestEntity if necessary
                requestEntity = new StringRequestEntity(DOMUtils.getTextContent(partValue), contentType,
                        contentCharset);
            }
        }

        // cast safely, PUT and POST are subclasses of EntityEnclosingMethod
        final EntityEnclosingMethod enclosingMethod = (EntityEnclosingMethod) method;
        enclosingMethod.setRequestEntity(requestEntity);
        enclosingMethod
                .setContentChunked(params.getBooleanParameter(Properties.PROP_HTTP_REQUEST_CHUNK, false));

    } else {
        // should not happen because of HttpBindingValidator, but never say never
        throw new IllegalArgumentException("Unsupported HTTP method: " + verb);
    }

    method.setPath(completeUri); // assumes that the path is properly encoded (URL safe).
    method.setQueryString(queryPath);

    // set headers
    setHttpRequestHeaders(method, opBinding, partValues, headers, params);
    return method;
}