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:cf.service.NettyBrokerServer.java

License:Open Source License

public NettyBrokerServer(SimpleHttpServer server, Provisioner provisioner, String authToken) {
    super(provisioner, authToken);
    server.addHandler(Pattern.compile("/+gateway/v1/configurations/(.*?)/handles(/(.*))?"),
            new RequestHandler() {
                @Override//  w w w. ja  v a  2  s .  c o  m
                public HttpResponse handleRequest(HttpRequest request, Matcher uriMatcher, ByteBuf body)
                        throws RequestException {
                    validateAuthToken(request);
                    // Bind service
                    if (request.getMethod() == HttpMethod.POST) {
                        final BindRequest bindRequest = decode(BindRequest.class, body);
                        final BindResponse bindResponse = bindService(bindRequest);
                        return encodeResponse(bindResponse);
                    }
                    // Unbind service
                    if (request.getMethod() == HttpMethod.DELETE) {
                        if (uriMatcher.groupCount() != 3) {
                            throw new RequestException(HttpResponseStatus.NOT_FOUND);
                        }
                        final String serviceInstanceId = uriMatcher.group(1);
                        final String handleId = uriMatcher.group(3);
                        unbindService(serviceInstanceId, handleId);
                        return encodeResponse(EMPTY_JSON_OBJECT);
                    }
                    throw new RequestException(HttpResponseStatus.METHOD_NOT_ALLOWED);
                }
            });
    server.addHandler(Pattern.compile("/+gateway/v1/configurations(/(.*))?"), new RequestHandler() {
        @Override
        public HttpResponse handleRequest(HttpRequest request, Matcher uriMatcher, ByteBuf body)
                throws RequestException {
            validateAuthToken(request);
            // Create service
            if (request.getMethod() == HttpMethod.POST) {
                final CreateRequest createRequest = decode(CreateRequest.class, body);
                final CreateResponse createResponse = createService(createRequest);
                return encodeResponse(createResponse);
            }
            // Delete service
            if (request.getMethod() == HttpMethod.DELETE) {
                if (uriMatcher.groupCount() != 2) {
                    throw new RequestException(HttpResponseStatus.NOT_FOUND);
                }
                final String serviceInstanceId = uriMatcher.group(2);
                deleteService(serviceInstanceId);
                return encodeResponse(EMPTY_JSON_OBJECT);
            }
            throw new RequestException(HttpResponseStatus.METHOD_NOT_ALLOWED);
        }
    });
}

From source file:com.ahanda.techops.noty.clientTest.ClientHandler.java

License:Apache License

public void logout(Channel ch) {
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, "/logout",
            Unpooled.buffer(0));/*  ww  w.  j av a 2 s  .  c  o  m*/

    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");

    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);

    l.info("readable bytes {}", request.content().readableBytes());

    // Send the HTTP request.
    ch.writeAndFlush(request);
}

From source file:com.barchart.netty.rest.server.RestHandlerBase.java

License:BSD License

@Override
public void handle(final HttpServerRequest request) throws IOException {

    final HttpMethod method = request.getMethod();

    try {/*from   w ww  .  ja  va2  s .co m*/
        if (method == HttpMethod.GET) {
            get(request);
        } else if (method == HttpMethod.POST) {
            post(request);
        } else if (method == HttpMethod.PUT) {
            put(request);
        } else if (method == HttpMethod.DELETE) {
            delete(request);
        } else {
            complete(request.response(), HttpResponseStatus.METHOD_NOT_ALLOWED,
                    method.name() + " not implemented");
        }
    } catch (final Throwable t) {
        complete(request.response(), HttpResponseStatus.INTERNAL_SERVER_ERROR, t.getMessage());
    }

}

From source file:com.barchart.netty.rest.server.TestRestHandler.java

License:BSD License

@Test
public void testDelete() throws Exception {

    request.method = HttpMethod.DELETE;
    service.handle(request);//from   w  ww.  j  av  a2  s. co m

    assertEquals(1, handler.requests);
    assertEquals(0, handler.get);
    assertEquals(0, handler.put);
    assertEquals(0, handler.post);
    assertEquals(1, handler.delete);
    assertEquals(0, handler.exceptions);

}

From source file:com.couchbase.client.core.endpoint.config.ConfigHandler.java

License:Apache License

@Override
protected HttpRequest encodeRequest(final ChannelHandlerContext ctx, final ConfigRequest msg) throws Exception {
    HttpMethod httpMethod = HttpMethod.GET;
    if (msg instanceof FlushRequest || msg instanceof InsertBucketRequest
            || msg instanceof UpdateBucketRequest) {
        httpMethod = HttpMethod.POST;// w w  w .jav  a  2  s  . c o  m
    } else if (msg instanceof RemoveBucketRequest) {
        httpMethod = HttpMethod.DELETE;
    }

    ByteBuf content;
    if (msg instanceof InsertBucketRequest) {
        content = Unpooled.copiedBuffer(((InsertBucketRequest) msg).payload(), CharsetUtil.UTF_8);
    } else if (msg instanceof UpdateBucketRequest) {
        content = Unpooled.copiedBuffer(((UpdateBucketRequest) msg).payload(), CharsetUtil.UTF_8);
    } else {
        content = Unpooled.EMPTY_BUFFER;
    }

    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, msg.path(), content);
    request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent());
    if (msg instanceof InsertBucketRequest || msg instanceof UpdateBucketRequest) {
        request.headers().set(HttpHeaders.Names.ACCEPT, "*/*");
        request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded");
    }
    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
    request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx));

    addHttpBasicAuth(ctx, request, msg.bucket(), msg.password());
    return request;
}

From source file:com.couchbase.client.core.endpoint.search.SearchHandler.java

License:Apache License

@Override
protected HttpRequest encodeRequest(ChannelHandlerContext ctx, SearchRequest msg) throws Exception {
    HttpMethod httpMethod = HttpMethod.GET;
    if (msg instanceof UpsertSearchIndexRequest) {
        httpMethod = HttpMethod.PUT;//from   w  ww  . j a  v a 2  s  .c om
    } else if (msg instanceof RemoveSearchIndexRequest) {
        httpMethod = HttpMethod.DELETE;
    } else if (msg instanceof SearchQueryRequest) {
        httpMethod = HttpMethod.POST;
    }

    ByteBuf content;
    if (msg instanceof UpsertSearchIndexRequest) {
        content = Unpooled.copiedBuffer(((UpsertSearchIndexRequest) msg).payload(), CharsetUtil.UTF_8);
    } else if (msg instanceof SearchQueryRequest) {
        content = Unpooled.copiedBuffer(((SearchQueryRequest) msg).payload(), CharsetUtil.UTF_8);
    } else {
        content = Unpooled.EMPTY_BUFFER;
    }

    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, msg.path(), content);
    request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent());
    if (msg instanceof UpsertSearchIndexRequest || msg instanceof SearchQueryRequest) {
        request.headers().set(HttpHeaders.Names.ACCEPT, "*/*");
        request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
    }
    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
    request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx));

    addHttpBasicAuth(ctx, request, msg.bucket(), msg.password());
    return request;
}

From source file:com.couchbase.client.core.endpoint.view.ViewHandler.java

License:Apache License

@Override
protected HttpRequest encodeRequest(final ChannelHandlerContext ctx, final ViewRequest msg) throws Exception {
    if (msg instanceof KeepAliveRequest) {
        FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.HEAD, "/",
                Unpooled.EMPTY_BUFFER);/*  www  .java  2s . com*/
        request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent());
        request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);
        return request;
    }

    StringBuilder path = new StringBuilder();

    HttpMethod method = HttpMethod.GET;
    ByteBuf content = null;
    if (msg instanceof ViewQueryRequest) {
        ViewQueryRequest queryMsg = (ViewQueryRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.design() : queryMsg.design());
        if (queryMsg.spatial()) {
            path.append("/_spatial/");
        } else {
            path.append("/_view/");
        }
        path.append(queryMsg.view());

        int queryLength = queryMsg.query() == null ? 0 : queryMsg.query().length();
        int keysLength = queryMsg.keys() == null ? 0 : queryMsg.keys().length();
        boolean hasQuery = queryLength > 0;
        boolean hasKeys = keysLength > 0;

        if (hasQuery || hasKeys) {
            if (queryLength + keysLength < MAX_GET_LENGTH) {
                //the query is short enough for GET
                //it has query, query+keys or keys only
                if (hasQuery) {
                    path.append("?").append(queryMsg.query());
                    if (hasKeys) {
                        path.append("&keys=").append(encodeKeysGet(queryMsg.keys()));
                    }
                } else {
                    //it surely has keys if not query
                    path.append("?keys=").append(encodeKeysGet(queryMsg.keys()));
                }
            } else {
                //the query is too long for GET, use the keys as JSON body
                if (hasQuery) {
                    path.append("?").append(queryMsg.query());
                }
                String keysContent = encodeKeysPost(queryMsg.keys());

                //switch to POST
                method = HttpMethod.POST;
                //body is "keys" but in JSON
                content = ctx.alloc().buffer(keysContent.length());
                content.writeBytes(keysContent.getBytes(CHARSET));
            }
        }
    } else if (msg instanceof GetDesignDocumentRequest) {
        GetDesignDocumentRequest queryMsg = (GetDesignDocumentRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name());
    } else if (msg instanceof UpsertDesignDocumentRequest) {
        method = HttpMethod.PUT;
        UpsertDesignDocumentRequest queryMsg = (UpsertDesignDocumentRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name());
        content = Unpooled.copiedBuffer(queryMsg.body(), CHARSET);
    } else if (msg instanceof RemoveDesignDocumentRequest) {
        method = HttpMethod.DELETE;
        RemoveDesignDocumentRequest queryMsg = (RemoveDesignDocumentRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name());
    } else {
        throw new IllegalArgumentException("Unknown incoming ViewRequest type " + msg.getClass());
    }

    if (content == null) {
        content = Unpooled.buffer(0);
    }
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, path.toString(),
            content);
    request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent());
    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
    request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx));
    addHttpBasicAuth(ctx, request, msg.bucket(), msg.password());

    return request;
}

From source file:com.github.ambry.admin.AdminIntegrationTest.java

License:Open Source License

/**
 * Deletes the blob with blob ID {@code blobId} and verifies the response returned.
 * @param blobId the blob ID of the blob to DELETE.
 * @throws ExecutionException//w w  w  .j  ava  2 s.c o m
 * @throws InterruptedException
 */
private void deleteBlobAndVerify(String blobId) throws ExecutionException, InterruptedException {
    FullHttpRequest httpRequest = buildRequest(HttpMethod.DELETE, blobId, null, null);
    verifyDeleted(httpRequest, HttpResponseStatus.ACCEPTED);
}

From source file:com.github.ambry.admin.AdminIntegrationTest.java

License:Open Source License

/**
 * Verifies that the right response code is returned for GET, HEAD and DELETE once a blob is deleted.
 * @param blobId the ID of the blob that was deleted.
 * @throws ExecutionException/*from  w w w  .ja va  2  s . co  m*/
 * @throws InterruptedException
 */
private void verifyOperationsAfterDelete(String blobId) throws ExecutionException, InterruptedException {
    FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, blobId, null, null);
    verifyDeleted(httpRequest, HttpResponseStatus.GONE);

    httpRequest = buildRequest(HttpMethod.HEAD, blobId, null, null);
    verifyDeleted(httpRequest, HttpResponseStatus.GONE);

    httpRequest = buildRequest(HttpMethod.DELETE, blobId, null, null);
    verifyDeleted(httpRequest, HttpResponseStatus.ACCEPTED);
}

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

License:Open Source License

/**
 * Tests non health check requests handling
 * @throws java.io.IOException/*from   w  w w .j  a v a2  s  . c  o  m*/
 */
@Test
public void requestHandleWithNonHealthCheckRequestTest() throws IOException {
    testNonHealthCheckRequest(HttpMethod.POST, "POST");
    testNonHealthCheckRequest(HttpMethod.GET, "GET");
    testNonHealthCheckRequest(HttpMethod.DELETE, "DELETE");
}