Example usage for io.netty.handler.codec.http DefaultHttpRequest getMethod

List of usage examples for io.netty.handler.codec.http DefaultHttpRequest getMethod

Introduction

In this page you can find the example usage for io.netty.handler.codec.http DefaultHttpRequest getMethod.

Prototype

@Override
    @Deprecated
    public HttpMethod getMethod() 

Source Link

Usage

From source file:io.liveoak.container.analytics.AnalyticsBandwidthHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof ByteBuf) {
        bytesRead += ((ByteBuf) msg).readableBytes();
        startTime = startTime != 0 ? startTime : System.currentTimeMillis();

    } else if (msg instanceof DefaultHttpRequest) {
        DefaultHttpRequest req = (DefaultHttpRequest) msg;
        event = new AnalyticsEvent();

        startTime = startTime != 0 ? startTime : System.currentTimeMillis();
        event.setTimestamp(startTime);/*  w  w  w.  j  av a2s.com*/
        event.setMethod(req.getMethod().name());
        event.setUri(req.getUri());
        event.clientAddress(ctx.channel().remoteAddress());
        //event.uri(req.resourcePath().toString());
    } else if (msg instanceof ResourceRequest) {
        ResourceRequest req = (ResourceRequest) msg;
        event.setApplication(req.resourcePath().head().toString());
        event.setUserId(req.requestContext().securityContext().getSubject());
        event.setApiRequest(true);
    }
    super.channelRead(ctx, msg);
}

From source file:io.liveoak.container.protocols.http.HttpResourceRequestDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, DefaultHttpRequest msg, List<Object> out) throws Exception {

    URI uri = new URI(msg.getUri());
    String query = uri.getRawQuery();
    if (query == null) {
        query = "?";
    } else {/*  ww  w. ja v  a  2 s. c o  m*/
        query = "?" + query;
    }

    QueryStringDecoder decoder = new QueryStringDecoder(query);

    String path = uri.getPath();

    int lastDotLoc = path.lastIndexOf('.');

    String extension = null;

    if (lastDotLoc > 0) {
        extension = path.substring(lastDotLoc + 1);
    }

    String acceptHeader = msg.headers().get(HttpHeaders.Names.ACCEPT);
    if (acceptHeader == null) {
        acceptHeader = "application/json";
    }
    MediaTypeMatcher mediaTypeMatcher = new DefaultMediaTypeMatcher(acceptHeader, extension);

    ResourceParams params = DefaultResourceParams.instance(decoder.parameters());

    // for cases when content is preset, and bypasses HttpRequestBodyHandler
    ByteBuf content = null;
    if (msg instanceof DefaultFullHttpRequest) {
        content = ((DefaultFullHttpRequest) msg).content().retain();
    }

    if (msg.getMethod().equals(HttpMethod.POST)) {
        String contentTypeHeader = msg.headers().get(HttpHeaders.Names.CONTENT_TYPE);
        MediaType contentType = new MediaType(contentTypeHeader);
        out.add(new DefaultResourceRequest.Builder(RequestType.CREATE, new ResourcePath(path))
                .resourceParams(params).mediaTypeMatcher(mediaTypeMatcher)
                .requestAttribute(HttpHeaders.Names.AUTHORIZATION,
                        msg.headers().get(HttpHeaders.Names.AUTHORIZATION))
                .requestAttribute(HttpHeaders.Names.CONTENT_TYPE, contentType)
                .requestAttribute(HTTP_REQUEST, msg)
                .resourceState(new DefaultLazyResourceState(codecManager, contentType, content)).build());
    } else if (msg.getMethod().equals(HttpMethod.GET)) {
        out.add(new DefaultResourceRequest.Builder(RequestType.READ, new ResourcePath(path))
                .resourceParams(params).mediaTypeMatcher(mediaTypeMatcher)
                .requestAttribute(HttpHeaders.Names.AUTHORIZATION,
                        msg.headers().get(HttpHeaders.Names.AUTHORIZATION))
                .requestAttribute(HttpHeaders.Names.ACCEPT, new MediaType(acceptHeader))
                .requestAttribute(HTTP_REQUEST, msg).pagination(decodePagination(params))
                .returnFields(decodeReturnFields(params)).sorting(decodeSorting(params)).build());
    } else if (msg.getMethod().equals(HttpMethod.PUT)) {
        String contentTypeHeader = msg.headers().get(HttpHeaders.Names.CONTENT_TYPE);
        MediaType contentType = new MediaType(contentTypeHeader);
        out.add(new DefaultResourceRequest.Builder(RequestType.UPDATE, new ResourcePath(path))
                .resourceParams(params).mediaTypeMatcher(mediaTypeMatcher)
                .requestAttribute(HttpHeaders.Names.AUTHORIZATION,
                        msg.headers().get(HttpHeaders.Names.AUTHORIZATION))
                .requestAttribute(HttpHeaders.Names.CONTENT_TYPE, contentType)
                .requestAttribute(HTTP_REQUEST, msg)
                .resourceState(new DefaultLazyResourceState(codecManager, contentType, content)).build());
    } else if (msg.getMethod().equals(HttpMethod.DELETE)) {
        out.add(new DefaultResourceRequest.Builder(RequestType.DELETE, new ResourcePath(path))
                .resourceParams(params).mediaTypeMatcher(mediaTypeMatcher)
                .requestAttribute(HttpHeaders.Names.AUTHORIZATION,
                        msg.headers().get(HttpHeaders.Names.AUTHORIZATION))
                .requestAttribute(HttpHeaders.Names.ACCEPT, new MediaType(acceptHeader))
                .requestAttribute(HTTP_REQUEST, msg).build());
    }
}