Example usage for io.netty.handler.codec.http HttpMethod DELETE

List of usage examples for io.netty.handler.codec.http HttpMethod DELETE

Introduction

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

Prototype

HttpMethod DELETE

To view the source code for io.netty.handler.codec.http HttpMethod DELETE.

Click Source Link

Document

The DELETE method requests that the origin server delete the resource identified by the Request-URI.

Usage

From source file:com.github.ambry.rest.NettyMessageProcessorTest.java

License:Open Source License

/**
 * Tests for the common case request handling flow.
 * @throws IOException/* w  w  w.jav  a  2  s. co m*/
 */
@Test
public void requestHandleWithGoodInputTest() throws IOException {
    doRequestHandleWithoutKeepAlive(HttpMethod.GET, RestMethod.GET);
    doRequestHandleWithoutKeepAlive(HttpMethod.DELETE, RestMethod.DELETE);
    doRequestHandleWithoutKeepAlive(HttpMethod.HEAD, RestMethod.HEAD);

    EmbeddedChannel channel = createChannel();
    doRequestHandleWithKeepAlive(channel, HttpMethod.GET, RestMethod.GET);
    doRequestHandleWithKeepAlive(channel, HttpMethod.DELETE, RestMethod.DELETE);
    doRequestHandleWithKeepAlive(channel, HttpMethod.HEAD, RestMethod.HEAD);
}

From source file:com.github.ambry.rest.NettyMultipartRequestTest.java

License:Open Source License

/**
 * Tests instantiation of {@link NettyMultipartRequest} with different {@link HttpMethod} types.
 * </p>//from w  ww. j  a  v a2 s. com
 * Only {@link HttpMethod#POST} should succeed.
 * @throws RestServiceException
 */
@Test
public void instantiationTest() throws RestServiceException {
    // POST will succeed.
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    closeRequestAndValidate(new NettyMultipartRequest(httpRequest, nettyMetrics));

    // Methods that will fail. Can include other methods, but these should be enough.
    HttpMethod[] methods = { HttpMethod.GET, HttpMethod.DELETE, HttpMethod.HEAD };
    for (HttpMethod method : methods) {
        httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, "/");
        try {
            new NettyMultipartRequest(httpRequest, nettyMetrics);
            fail("Creation of NettyMultipartRequest should have failed for " + method);
        } catch (IllegalArgumentException e) {
            // expected. Nothing to do.
        }
    }
}

From source file:com.github.ambry.rest.NettyRequest.java

License:Open Source License

/**
 * Wraps the {@code request} in an implementation of {@link RestRequest} so that other layers can understand the
 * request./*from  ww w.j av a2s  .  c  om*/
 * <p/>
 * Note on content size: The content size is deduced in the following order:-
 * 1. From the {@link RestUtils.Headers#BLOB_SIZE} header.
 * 2. If 1 fails, from the {@link HttpHeaders.Names#CONTENT_LENGTH} header.
 * 3. If 2 fails, it is set to -1 which means that the content size is unknown.
 * If content size is set in the header (i.e. not -1), the actual content size should match that value. Otherwise, an
 * exception will be thrown.
 * @param request the {@link HttpRequest} that needs to be wrapped.
 * @param nettyMetrics the {@link NettyMetrics} instance to use.
 * @throws IllegalArgumentException if {@code request} is null.
 * @throws RestServiceException if the {@link HttpMethod} defined in {@code request} is not recognized as a
 *                                {@link RestMethod}.
 */
public NettyRequest(HttpRequest request, NettyMetrics nettyMetrics) throws RestServiceException {
    if (request == null) {
        throw new IllegalArgumentException("Received null HttpRequest");
    }
    restRequestMetricsTracker.nioMetricsTracker.markRequestReceived();
    HttpMethod httpMethod = request.getMethod();
    if (httpMethod == HttpMethod.GET) {
        restMethod = RestMethod.GET;
    } else if (httpMethod == HttpMethod.POST) {
        restMethod = RestMethod.POST;
    } else if (httpMethod == HttpMethod.DELETE) {
        restMethod = RestMethod.DELETE;
    } else if (httpMethod == HttpMethod.HEAD) {
        restMethod = RestMethod.HEAD;
    } else {
        nettyMetrics.unsupportedHttpMethodError.inc();
        throw new RestServiceException("http method not supported: " + httpMethod,
                RestServiceErrorCode.UnsupportedHttpMethod);
    }
    this.request = request;
    this.query = new QueryStringDecoder(request.getUri());
    this.nettyMetrics = nettyMetrics;

    if (HttpHeaders.getHeader(request, RestUtils.Headers.BLOB_SIZE, null) != null) {
        size = Long.parseLong(HttpHeaders.getHeader(request, RestUtils.Headers.BLOB_SIZE));
    } else {
        size = HttpHeaders.getContentLength(request, -1);
    }

    // query params.
    for (Map.Entry<String, List<String>> e : query.parameters().entrySet()) {
        StringBuilder value = null;
        if (e.getValue() != null) {
            StringBuilder combinedValues = combineVals(new StringBuilder(), e.getValue());
            if (combinedValues.length() > 0) {
                value = combinedValues;
            }
        }
        allArgs.put(e.getKey(), value);
    }

    Set<io.netty.handler.codec.http.Cookie> nettyCookies = null;
    // headers.
    for (Map.Entry<String, String> e : request.headers()) {
        StringBuilder sb;
        if (e.getKey().equals(HttpHeaders.Names.COOKIE)) {
            String value = e.getValue();
            if (value != null) {
                nettyCookies = CookieDecoder.decode(value);
            }
        } else {
            boolean valueNull = request.headers().get(e.getKey()) == null;
            if (!valueNull && allArgs.get(e.getKey()) == null) {
                sb = new StringBuilder(e.getValue());
                allArgs.put(e.getKey(), sb);
            } else if (!valueNull) {
                sb = (StringBuilder) allArgs.get(e.getKey());
                sb.append(MULTIPLE_HEADER_VALUE_DELIMITER).append(e.getValue());
            } else if (!allArgs.containsKey(e.getKey())) {
                allArgs.put(e.getKey(), null);
            }
        }
    }

    // turn all StringBuilders into String
    for (Map.Entry<String, Object> e : allArgs.entrySet()) {
        if (allArgs.get(e.getKey()) != null) {
            allArgs.put(e.getKey(), (e.getValue()).toString());
        }
    }
    // add cookies to the args as java cookies
    if (nettyCookies != null) {
        Set<javax.servlet.http.Cookie> cookies = convertHttpToJavaCookies(nettyCookies);
        allArgs.put(RestUtils.Headers.COOKIE, cookies);
    }
    allArgsReadOnly = Collections.unmodifiableMap(allArgs);
}

From source file:com.github.ambry.rest.NettyRequestTest.java

License:Open Source License

/**
 * Tests conversion of {@link HttpRequest} to {@link NettyRequest} given good input.
 * @throws RestServiceException/*from w w w .  ja va 2s  . co m*/
 */
@Test
public void conversionWithGoodInputTest() throws RestServiceException {
    // headers
    HttpHeaders headers = new DefaultHttpHeaders(false);
    headers.add(HttpHeaders.Names.CONTENT_LENGTH, new Random().nextInt(Integer.MAX_VALUE));
    headers.add("headerKey", "headerValue1");
    headers.add("headerKey", "headerValue2");
    headers.add("overLoadedKey", "headerOverloadedValue");
    headers.add("paramNoValueInUriButValueInHeader", "paramValueInHeader");
    headers.add("headerNoValue", (Object) null);
    headers.add("headerNoValueButValueInUri", (Object) null);

    // params
    Map<String, List<String>> params = new HashMap<String, List<String>>();
    List<String> values = new ArrayList<String>(2);
    values.add("paramValue1");
    values.add("paramValue2");
    params.put("paramKey", values);
    values = new ArrayList<String>(1);
    values.add("paramOverloadedValue");
    params.put("overLoadedKey", values);
    values = new ArrayList<String>(1);
    values.add("headerValueInUri");
    params.put("headerNoValueButValueInUri", values);
    params.put("paramNoValue", null);
    params.put("paramNoValueInUriButValueInHeader", null);

    StringBuilder uriAttachmentBuilder = new StringBuilder("?");
    for (Map.Entry<String, List<String>> param : params.entrySet()) {
        if (param.getValue() != null) {
            for (String value : param.getValue()) {
                uriAttachmentBuilder.append(param.getKey()).append("=").append(value).append("&");
            }
        } else {
            uriAttachmentBuilder.append(param.getKey()).append("&");
        }
    }
    uriAttachmentBuilder.deleteCharAt(uriAttachmentBuilder.length() - 1);
    String uriAttachment = uriAttachmentBuilder.toString();

    NettyRequest nettyRequest;
    String uri;
    Set<Cookie> cookies = new HashSet<Cookie>();
    Cookie httpCookie = new DefaultCookie("CookieKey1", "CookieValue1");
    cookies.add(httpCookie);
    httpCookie = new DefaultCookie("CookieKey2", "CookieValue2");
    cookies.add(httpCookie);
    headers.add(RestUtils.Headers.COOKIE, getCookiesHeaderValue(cookies));

    uri = "/GET" + uriAttachment;
    nettyRequest = createNettyRequest(HttpMethod.GET, uri, headers);
    validateRequest(nettyRequest, RestMethod.GET, uri, headers, params, cookies);
    closeRequestAndValidate(nettyRequest);

    uri = "/POST" + uriAttachment;
    nettyRequest = createNettyRequest(HttpMethod.POST, uri, headers);
    validateRequest(nettyRequest, RestMethod.POST, uri, headers, params, cookies);
    closeRequestAndValidate(nettyRequest);

    uri = "/DELETE" + uriAttachment;
    nettyRequest = createNettyRequest(HttpMethod.DELETE, uri, headers);
    validateRequest(nettyRequest, RestMethod.DELETE, uri, headers, params, cookies);
    closeRequestAndValidate(nettyRequest);

    uri = "/HEAD" + uriAttachment;
    nettyRequest = createNettyRequest(HttpMethod.HEAD, uri, headers);
    validateRequest(nettyRequest, RestMethod.HEAD, uri, headers, params, cookies);
    closeRequestAndValidate(nettyRequest);
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Tests keep-alive for different HTTP methods and error statuses.
 *//*www  .  j  a v  a2  s . c om*/
@Test
public void keepAliveTest() {
    HttpMethod[] HTTP_METHODS = { HttpMethod.POST, HttpMethod.GET, HttpMethod.HEAD, HttpMethod.DELETE };
    EmbeddedChannel channel = createEmbeddedChannel();
    for (HttpMethod httpMethod : HTTP_METHODS) {
        for (Map.Entry<RestServiceErrorCode, HttpResponseStatus> entry : REST_ERROR_CODE_TO_HTTP_STATUS
                .entrySet()) {
            HttpHeaders httpHeaders = new DefaultHttpHeaders();
            httpHeaders.set(MockNettyMessageProcessor.REST_SERVICE_ERROR_CODE_HEADER_NAME, entry.getKey());
            channel.writeInbound(RestTestUtils.createRequest(httpMethod,
                    TestingUri.OnResponseCompleteWithRestException.toString(), httpHeaders));
            HttpResponse response = (HttpResponse) channel.readOutbound();
            assertEquals("Unexpected response status", entry.getValue(), response.getStatus());
            if (!(response instanceof FullHttpResponse)) {
                // empty the channel
                while (channel.readOutbound() != null) {
                }
            }
            boolean shouldBeAlive = !httpMethod.equals(HttpMethod.POST)
                    && !NettyResponseChannel.CLOSE_CONNECTION_ERROR_STATUSES.contains(entry.getValue());
            assertEquals("Channel state (open/close) not as expected", shouldBeAlive, channel.isActive());
            assertEquals("Connection header should be consistent with channel state", shouldBeAlive,
                    HttpHeaders.isKeepAlive(response));
            if (!shouldBeAlive) {
                channel = createEmbeddedChannel();
            }
        }
    }
    channel.close();
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Tests for the common case request handling flow.
 * @throws IOException//from ww w.  j  a v a  2  s .  c  o m
 */
@Test
public void requestHandleWithGoodInputTest() throws IOException {
    doRequestHandleTest(HttpMethod.POST, "POST", false);
    doRequestHandleTest(HttpMethod.GET, "GET", false);
    doRequestHandleTest(HttpMethod.DELETE, "DELETE", false);
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Tests for multiple requests with keep alive.
 * @throws IOException//from   www.ja v a  2  s.com
 */
@Test
public void requestHandleWithGoodInputTestWithKeepAlive() throws IOException {
    doRequestHandleWithKeepAliveTest(HttpMethod.POST, "POST");
    doRequestHandleWithKeepAliveTest(HttpMethod.GET, "GET");
    doRequestHandleWithKeepAliveTest(HttpMethod.DELETE, "DELETE");
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Tests two successive request without completing first request
 * @throws IOException/*from  w  w  w .  j  ava  2  s . com*/
 */
@Test
public void requestHandleWithTwoSuccessiveRequest() throws IOException {
    doRequestHandleWithMultipleRequest(HttpMethod.POST, "POST");
    doRequestHandleWithMultipleRequest(HttpMethod.GET, "GET");
    doRequestHandleWithMultipleRequest(HttpMethod.DELETE, "DELETE");
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Tests for the request handling flow for close
 * @throws IOException//www  .j  ava 2  s. c om
 */
@Test
public void requestHandleOnCloseTest() throws IOException {
    doRequestHandleTest(HttpMethod.POST, EchoMethodHandler.CLOSE_URI, true);
    doRequestHandleTest(HttpMethod.GET, EchoMethodHandler.CLOSE_URI, true);
    doRequestHandleTest(HttpMethod.DELETE, EchoMethodHandler.CLOSE_URI, true);
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Tests for the request handling flow on disconnect
 * @throws IOException/*w  ww  . j a  v a2s. c o m*/
 */
@Test
public void requestHandleOnDisconnectTest() throws IOException {
    // disonnecting the embedded channel, calls close of PubliAccessLogRequestHandler
    doRequestHandleTest(HttpMethod.POST, EchoMethodHandler.DISCONNECT_URI, true);
    doRequestHandleTest(HttpMethod.GET, EchoMethodHandler.DISCONNECT_URI, true);
    doRequestHandleTest(HttpMethod.DELETE, EchoMethodHandler.DISCONNECT_URI, true);
}