@Test public void testDecodeDelete() throws Exception { DefaultResourceRequest decoded = decode(HttpMethod.DELETE, "/memory/people/bob"); assertThat(decoded.requestType()).isEqualTo(RequestType.DELETE); assertThat(decoded.resourcePath().segments()).hasSize(3); assertThat(decoded.resourcePath().segments().get(0).name()).isEqualTo("memory"); assertThat(decoded.resourcePath().segments().get(1).name()).isEqualTo("people"); assertThat(decoded.resourcePath().segments().get(2).name()).isEqualTo("bob"); assertThat(decoded.requestContext().pagination()).isNotNull(); assertThat(decoded.requestContext().pagination()).isEqualTo(Pagination.NONE); assertThat(decoded.state()).isNull(); }
@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 { 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()); 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()); } }
HttpResourceRequestDecoder extends MessageToMessageDecoder<DefaultHttpRequest> { @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 { 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()); 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()); } } }
HttpResourceRequestDecoder extends MessageToMessageDecoder<DefaultHttpRequest> { @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 { 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()); 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()); } } HttpResourceRequestDecoder(ResourceCodecManager codecManager); }
HttpResourceRequestDecoder extends MessageToMessageDecoder<DefaultHttpRequest> { @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 { 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()); 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()); } } HttpResourceRequestDecoder(ResourceCodecManager codecManager); @Override void exceptionCaught(ChannelHandlerContext ctx, Throwable cause); }
HttpResourceRequestDecoder extends MessageToMessageDecoder<DefaultHttpRequest> { @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 { 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()); 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()); } } HttpResourceRequestDecoder(ResourceCodecManager codecManager); @Override void exceptionCaught(ChannelHandlerContext ctx, Throwable cause); static final String HTTP_REQUEST; }