Example usage for org.apache.http.message BasicHttpEntityEnclosingRequest BasicHttpEntityEnclosingRequest

List of usage examples for org.apache.http.message BasicHttpEntityEnclosingRequest BasicHttpEntityEnclosingRequest

Introduction

In this page you can find the example usage for org.apache.http.message BasicHttpEntityEnclosingRequest BasicHttpEntityEnclosingRequest.

Prototype

public BasicHttpEntityEnclosingRequest(RequestLine requestLine) 

Source Link

Usage

From source file:org.eclipse.californium.proxy.HttpTranslator.java

/**
 * Gets the http request starting from a CoAP request. The method creates
 * the HTTP request through its request line. The request line is built with
 * the uri coming from the string representing the CoAP method and the uri
 * obtained from the proxy-uri option. If a payload is provided, the HTTP
 * request encloses an HTTP entity and consequently the content-type is set.
 * Finally, the CoAP options are mapped to the HTTP headers.
 * /*from   w w w  .  j a va2  s  .c o  m*/
 * @param coapRequest
 *            the coap request
 * 
 * 
 * 
 * @return the http request * @throws TranslationException the translation
 *         exception * @throws URISyntaxException the uRI syntax exception
 */
public static HttpRequest getHttpRequest(Request coapRequest) throws TranslationException {
    if (coapRequest == null) {
        throw new IllegalArgumentException("coapRequest == null");
    }

    HttpRequest httpRequest = null;

    String coapMethod = null;
    switch (coapRequest.getCode()) {
    case GET:
        coapMethod = "GET";
        break;
    case POST:
        coapMethod = "POST";
        break;
    case PUT:
        coapMethod = "PUT";
        break;
    case DELETE:
        coapMethod = "DELETE";
        break;
    }

    // get the proxy-uri
    URI proxyUri;
    try {
        /*
         * The new draft (14) only allows one proxy-uri option. Thus, this
         * code segment has changed.
         */
        String proxyUriString = URLDecoder.decode(coapRequest.getOptions().getProxyUri(), "UTF-8");
        proxyUri = new URI(proxyUriString);
    } catch (UnsupportedEncodingException e) {
        LOGGER.warning("UTF-8 do not support this encoding: " + e);
        throw new TranslationException("UTF-8 do not support this encoding", e);
    } catch (URISyntaxException e) {
        LOGGER.warning("Cannot translate the server uri" + e);
        throw new InvalidFieldException("Cannot get the proxy-uri from the coap message", e);
    }

    // create the requestLine
    RequestLine requestLine = new BasicRequestLine(coapMethod, proxyUri.toString(), HttpVersion.HTTP_1_1);

    // get the http entity
    HttpEntity httpEntity = getHttpEntity(coapRequest);

    // create the http request
    if (httpEntity == null) {
        httpRequest = new BasicHttpRequest(requestLine);
    } else {
        httpRequest = new BasicHttpEntityEnclosingRequest(requestLine);
        ((HttpEntityEnclosingRequest) httpRequest).setEntity(httpEntity);

        // get the content-type from the entity and set the header
        ContentType contentType = ContentType.get(httpEntity);
        httpRequest.setHeader("content-type", contentType.toString());
    }

    // set the headers
    Header[] headers = getHttpHeaders(coapRequest.getOptions().asSortedList(), "");
    for (Header header : headers) {
        httpRequest.addHeader(header);
    }

    return httpRequest;
}