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; }
@Test public void testDecodeGet() throws Exception { DefaultResourceRequest decoded = decode(HttpMethod.GET, "/memory/people/bob"); assertThat(decoded.requestType()).isEqualTo(RequestType.READ); 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.state()).isNull(); }
@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(); }
@Test public void testDecodePost() throws Exception { DefaultResourceRequest decoded = decode(HttpMethod.POST, "/memory/people/bob", "{ name: 'bob', int: 1024, maxInt: 2147483647, minInt: -2147483648, longNeg: -2147483659, longPos: 2147483648 }"); assertThat(decoded.requestType()).isEqualTo(RequestType.CREATE); 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()).isNotNull(); assertThat(decoded.state()).isInstanceOf(ResourceState.class); ResourceState state = decoded.state(); assertThat(state.getProperty("name")).isEqualTo("bob"); assertThat(state.getProperty("int")).isEqualTo(1024); assertThat(state.getProperty("maxInt")).isEqualTo(2147483647); assertThat(state.getProperty("minInt")).isEqualTo(-2147483648); assertThat(state.getProperty("longNeg")).isEqualTo(-2147483659L); assertThat(state.getProperty("longPos")).isEqualTo(2147483648L); }
@Test public void testDecodePut() throws Exception { DefaultResourceRequest decoded = decode(HttpMethod.PUT, "/memory/people/bob", "{ name: 'bob', int: 1024, maxInt: 2147483647, minInt: -2147483648, longNeg: -2147483659, longPos: 2147483648 }"); assertThat(decoded.requestType()).isEqualTo(RequestType.UPDATE); 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()).isNotNull(); assertThat(decoded.state()).isInstanceOf(ResourceState.class); ResourceState state = decoded.state(); assertThat(state.getProperty("name")).isEqualTo("bob"); assertThat(state.getProperty("int")).isEqualTo(1024); assertThat(state.getProperty("maxInt")).isEqualTo(2147483647); assertThat(state.getProperty("minInt")).isEqualTo(-2147483648); assertThat(state.getProperty("longNeg")).isEqualTo(-2147483659L); assertThat(state.getProperty("longPos")).isEqualTo(2147483648L); }