Example usage for org.apache.http.nio NHttpMessageParser parse

List of usage examples for org.apache.http.nio NHttpMessageParser parse

Introduction

In this page you can find the example usage for org.apache.http.nio NHttpMessageParser parse.

Prototype

T parse() throws IOException, HttpException;

Source Link

Document

Attempts to parse a complete message head from the content of the internal buffer.

Usage

From source file:com.jeny.atmosphere.integration.websocket.RawWebSocketProtocol.java

/**
 * Since protocol is simple http tunneling over web socket using RAW approach it parses web socket body as string which represents http request by HTTP standard.
 * It takes all parameters from passed http request and takes some parameters (like Cookies, Content Type and etc) from initial http request if
 * they are missing in the passed http request. On the client side is supposed that it will take passed parameters and missing parameters from
 * browser context during form http request (like Accept-Charset, Accept-Encoding, User-Agent and etc).
 * {@inheritDoc}/*w w w .ja  va2  s  . co m*/
 */
@Override
public List<AtmosphereRequest> onMessage(WebSocket webSocket, String d) {

    HttpRequest request = null;
    try {
        HttpParams params = new BasicHttpParams();
        SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 128, params);
        HttpRequestFactory requestFactory = new DefaultHttpRequestFactory();
        NHttpMessageParser<HttpRequest> requestParser = new DefaultHttpRequestParser(inbuf, null,
                requestFactory, params);
        requestParser.fillBuffer(newChannel(d, "UTF-8"));
        request = requestParser.parse();

    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (HttpException e) {
        throw new RuntimeException(e);
    }

    AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource();
    if (resource == null) {
        logger.error("Invalid state. No AtmosphereResource has been suspended");
        return null;
    }
    AtmosphereRequest initialRequest = resource.getRequest();

    Map<String, Object> attributesMap = new HashMap<String, Object>();
    attributesMap.put(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.SIMPLE_HTTP_OVER_WEBSOCKET);

    // Propagate the original attribute to WebSocket message.
    attributesMap.putAll(initialRequest.attributes());

    // Determine value of path info, request URI
    String pathInfo = request.getRequestLine().getUri();
    UriBuilder pathInfoUriBuilder = UriBuilder.fromUri(pathInfo);
    URI pathInfoUri = pathInfoUriBuilder.build();
    String requestURI = pathInfoUri.getPath();

    // take the Method Type of passed http request
    methodType = request.getRequestLine().getMethod();

    // take the Content Type of passed http request
    contentType = request.getFirstHeader(HttpHeaders.CONTENT_TYPE) != null
            ? request.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue()
            : initialRequest.getContentType();

    // take the body of passed http request
    String body = null; // TODO how we can take it?

    // We need to create a new AtmosphereRequest as WebSocket message may arrive concurrently on the same connection.
    AtmosphereRequest atmosphereRequest = new AtmosphereRequest.Builder()
            // use HttpServletRequestWrapper to propagate passed http request parameters, headers, cookies and etc.
            // if some parameters (headers, cookies and etc) takes from initial request if they are missing.
            .request(new HttpServletRequestWrapper(initialRequest, request))

            .method(methodType).contentType(contentType).requestURI(requestURI).pathInfo(pathInfo)

            .attributes(attributesMap)

            .body(d) // TODO Unfortunately org.apache.http doesn't allow to take a body need to find workaround
            .destroyable(destroyable).build();

    List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>();
    list.add(atmosphereRequest);

    return list;
}