Java Code Examples for io.netty.handler.codec.http.HttpMethod#DELETE

The following examples show how to use io.netty.handler.codec.http.HttpMethod#DELETE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestUtils.java    From serve with Apache License 2.0 6 votes vote down vote up
public static void unregisterModel(
        Channel channel, String modelName, String version, boolean syncChannel)
        throws InterruptedException {
    String requestURL = "/models/" + modelName;
    if (version != null) {
        requestURL += "/" + version;
    }

    HttpRequest req =
            new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, requestURL);
    if (syncChannel) {
        channel.writeAndFlush(req).sync();
        channel.closeFuture().sync();
    } else {
        channel.writeAndFlush(req);
    }
}
 
Example 2
Source File: NettyResponseChannelTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Tests keep-alive for different HTTP methods and error statuses.
 */
@Test
public void keepAliveTest() {
  HttpMethod[] HTTP_METHODS = {HttpMethod.POST, HttpMethod.PUT, HttpMethod.GET, HttpMethod.HEAD, HttpMethod.DELETE};
  EmbeddedChannel channel = createEmbeddedChannel();
  for (HttpMethod httpMethod : HTTP_METHODS) {
    for (RestServiceErrorCode errorCode : RestServiceErrorCode.values()) {
      channel = doKeepAliveTest(channel, httpMethod, errorCode, getExpectedHttpResponseStatus(errorCode), 0, null);
    }
    channel = doKeepAliveTest(channel, httpMethod, null, HttpResponseStatus.INTERNAL_SERVER_ERROR, 0, null);
    channel = doKeepAliveTest(channel, httpMethod, null, HttpResponseStatus.INTERNAL_SERVER_ERROR, 0, true);
    channel = doKeepAliveTest(channel, httpMethod, null, HttpResponseStatus.INTERNAL_SERVER_ERROR, 0, false);
  }
  // special test for put because the keep alive depends on content size (0 already tested above)
  channel = doKeepAliveTest(channel, HttpMethod.PUT, null, HttpResponseStatus.INTERNAL_SERVER_ERROR, 1, null);
  channel = doKeepAliveTest(channel, HttpMethod.PUT, null, HttpResponseStatus.INTERNAL_SERVER_ERROR, 100, null);
  channel.close();
}
 
Example 3
Source File: ReactorNettySender.java    From micrometer with Apache License 2.0 6 votes vote down vote up
private HttpMethod toNettyHttpMethod(Method method) {
    switch (method) {
        case PUT:
            return HttpMethod.PUT;
        case POST:
            return HttpMethod.POST;
        case HEAD:
            return HttpMethod.HEAD;
        case GET:
            return HttpMethod.GET;
        case DELETE:
            return HttpMethod.DELETE;
        case OPTIONS:
            return HttpMethod.OPTIONS;
        default:
            throw new UnsupportedOperationException("http method " + method.toString() + " is not supported by the reactor netty client");
    }
}
 
Example 4
Source File: HttpClient.java    From multi-model-server with Apache License 2.0 6 votes vote down vote up
public boolean unregisterModel(String modelName) throws InterruptedException, IOException {
    Channel channel = connect(bootstrap, managementPort);

    DefaultFullHttpRequest req =
            new DefaultFullHttpRequest(
                    HttpVersion.HTTP_1_1,
                    HttpMethod.DELETE,
                    "/models/" + URLEncoder.encode(modelName, StandardCharsets.UTF_8.name()));
    channel.writeAndFlush(req).sync();

    channel.closeFuture().sync();

    int statusCode = handler.getStatusCode();
    String ret = handler.getContent();
    if (statusCode == 200) {
        logger.info("unregisterModel: {} success.", modelName);
        logger.trace(ret);
        return true;
    }
    logger.warn("unregisterModel: {} failed: {}", modelName, ret);
    return false;
}
 
Example 5
Source File: ThriftUnmarshaller.java    From xio with Apache License 2.0 6 votes vote down vote up
private static HttpMethod build(Http1Method method) {
  if (method != null) {
    switch (method) {
      case CONNECT:
        return HttpMethod.CONNECT;
      case DELETE:
        return HttpMethod.DELETE;
      case GET:
        return HttpMethod.GET;
      case HEAD:
        return HttpMethod.HEAD;
      case OPTIONS:
        return HttpMethod.OPTIONS;
      case PATCH:
        return HttpMethod.PATCH;
      case POST:
        return HttpMethod.POST;
      case PUT:
        return HttpMethod.PUT;
      case TRACE:
        return HttpMethod.TRACE;
    }
  }
  return null;
}
 
Example 6
Source File: HandlerWrapper.java    From blynk-server with GNU General Public License v3.0 6 votes vote down vote up
public HandlerWrapper(UriTemplate uriTemplate, Method method, Object handler, GlobalStats globalStats) {
    this.uriTemplate = uriTemplate;
    this.classMethod = method;
    this.handler = handler;

    if (method.isAnnotationPresent(POST.class)) {
        this.httpMethod = HttpMethod.POST;
    } else if (method.isAnnotationPresent(PUT.class)) {
        this.httpMethod = HttpMethod.PUT;
    } else if (method.isAnnotationPresent(DELETE.class)) {
        this.httpMethod = HttpMethod.DELETE;
    } else {
        this.httpMethod = HttpMethod.GET;
    }

    Metric metricAnnotation = method.getAnnotation(Metric.class);
    if (metricAnnotation != null) {
        metricIndex = metricAnnotation.value();
    } else {
        metricIndex = -1;
    }

    this.params = new Param[method.getParameterCount()];
    this.globalStats = globalStats;
}
 
Example 7
Source File: HttpServerHandlerTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertChannelReadUnsupportedMethod() {
    fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, "/scaling/job/stop");
    httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest);
    ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
    verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
    FullHttpResponse fullHttpResponse = (FullHttpResponse) argumentCaptor.getValue();
    assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("Not support request!"));
}
 
Example 8
Source File: ConfigHandler.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Override
protected HttpRequest encodeRequest(final ChannelHandlerContext ctx, final ConfigRequest msg) throws Exception {
    if (msg instanceof RestApiRequest) {
        return encodeRestApiRequest(ctx, (RestApiRequest) msg);
    }
    HttpMethod httpMethod = HttpMethod.GET;
    if (msg instanceof FlushRequest || msg instanceof InsertBucketRequest
            || msg instanceof UpdateBucketRequest) {
        httpMethod = HttpMethod.POST;
    } else if (msg instanceof UpsertUserRequest) {
      httpMethod = HttpMethod.PUT;
    } else if (msg instanceof RemoveBucketRequest || msg instanceof RemoveUserRequest) {
        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 if (msg instanceof UpsertUserRequest) {
        content = Unpooled.copiedBuffer(((UpsertUserRequest) 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 || msg instanceof UpsertUserRequest) {
        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.username(), msg.password());
    return request;
}
 
Example 9
Source File: HttpServerHandlerTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Test
public void assertChannelReadUnsupportedUrl() {
    fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, "/scaling/1");
    httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest);
    ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
    verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
    FullHttpResponse fullHttpResponse = (FullHttpResponse) argumentCaptor.getValue();
    assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("Not support request!"));
}
 
Example 10
Source File: ModelServerTest.java    From multi-model-server with Apache License 2.0 5 votes vote down vote up
private void testUnregisterModelNotFound() throws InterruptedException {
    Channel channel = connect(true);
    Assert.assertNotNull(channel);

    HttpRequest req =
            new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, "/models/fake");
    channel.writeAndFlush(req).sync();
    channel.closeFuture().sync();

    ErrorResponse resp = JsonUtils.GSON.fromJson(result, ErrorResponse.class);

    Assert.assertEquals(resp.getCode(), HttpResponseStatus.NOT_FOUND.code());
    Assert.assertEquals(resp.getMessage(), "Model not found: fake");
}
 
Example 11
Source File: ModelServerTest.java    From multi-model-server with Apache License 2.0 5 votes vote down vote up
private void unloadTests(Channel channel, String modelName) throws InterruptedException {
    result = null;
    latch = new CountDownLatch(1);
    String expected = "Model \"" + modelName + "\" unregistered";
    String url = "/models/" + modelName;
    HttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, url);
    channel.writeAndFlush(req);
    latch.await();
    StatusResponse resp = JsonUtils.GSON.fromJson(result, StatusResponse.class);
    Assert.assertEquals(resp.getStatus(), expected);
}
 
Example 12
Source File: ModelServerTest.java    From multi-model-server with Apache License 2.0 5 votes vote down vote up
private void testUnregisterModel(Channel channel) throws InterruptedException {
    result = null;
    latch = new CountDownLatch(1);
    HttpRequest req =
            new DefaultFullHttpRequest(
                    HttpVersion.HTTP_1_1, HttpMethod.DELETE, "/models/noop_v0.1");
    channel.writeAndFlush(req);
    latch.await();

    StatusResponse resp = JsonUtils.GSON.fromJson(result, StatusResponse.class);
    Assert.assertEquals(resp.getStatus(), "Model \"noop_v0.1\" unregistered");
}
 
Example 13
Source File: HandlePublisher.java    From reactor-guice with Apache License 2.0 4 votes vote down vote up
private Mono<?> invokeMethod(HttpServerRequest request, HttpServerResponse response, Method method, Object handleObject, com.doopp.reactor.guice.RequestAttribute requestAttribute, ModelMap modelMap) {

        // value of url quest
        Map<String, List<String>> questParams = new HashMap<>();
        // values of form post
        Map<String, List<String>> formParams = new HashMap<>();
        // values of file upload
        Map<String, List<FileUpload>> fileParams = new HashMap<>();
        // get results
        this.queryParams(request, questParams);

        Mono<Object[]> objectMono;

        if (request.method() == HttpMethod.POST || request.method() == HttpMethod.PUT || request.method() == HttpMethod.DELETE) {
            objectMono = request.receive()
                .aggregate()
                .flatMap(byteBuf -> {
                    this.formParams(request, byteBuf, formParams, fileParams);
                    return methodParams(
                            method,
                            request,
                            response,
                            requestAttribute,
                            modelMap,
                            byteBuf,
                            questParams,
                            formParams,
                            fileParams
                    );
                });
        } else {
                // this.formParams(request, null, formParams, fileParams);
                objectMono = methodParams(
                        method,
                        request,
                        response,
                        requestAttribute,
                        modelMap,
                        null,
                        questParams,
                        formParams,
                        fileParams
                );
        }
        return objectMono.flatMap(oo -> {
            fileParams
                    .forEach((name, fileUploads)->fileUploads
                            .forEach(ReferenceCounted::release));
            try {
                Object result = method.invoke(handleObject, oo);
                return (result instanceof Mono<?>) ? (Mono<?>) result : Mono.just(result);
            } catch (Exception e) {
                return Mono.error(e);
            }
        });
    }
 
Example 14
Source File: Route.java    From Discord4J with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static Route delete(String uri) {
    return new Route(HttpMethod.DELETE, uri);
}
 
Example 15
Source File: Recipes.java    From xio with Apache License 2.0 4 votes vote down vote up
public static HttpRequest newRequestDelete(String urlPath) {
  return new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, urlPath);
}
 
Example 16
Source File: Recipes.java    From xrpc with Apache License 2.0 4 votes vote down vote up
public static HttpRequest newRequestDelete(String urlPath) {
  return new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, urlPath);
}
 
Example 17
Source File: HttpRequestUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean isWriteFromBrowserWithoutOrigin(HttpRequest request) {
  HttpMethod method = request.method();

  return StringUtil.isEmpty(getOrigin(request)) && isRegularBrowser(request) && (method == HttpMethod.POST || method == HttpMethod.PATCH || method == HttpMethod.PUT || method == HttpMethod.DELETE);
}
 
Example 18
Source File: NettyInvocationBuilder.java    From docker-java with Apache License 2.0 3 votes vote down vote up
private HttpRequest prepareDeleteRequest(String uri) {

        FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, uri);

        setDefaultHeaders(request);

        return request;
    }
 
Example 19
Source File: EtcdKeyDeleteRequest.java    From etcd4j with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs an EtcdKeysRequest
 *
 * @param clientImpl   the client to handle this request
 * @param key          key to change
 * @param retryHandler Handles retries on fails
 */
public EtcdKeyDeleteRequest(EtcdClientImpl clientImpl, String key, RetryPolicy retryHandler) {
  super(clientImpl, HttpMethod.DELETE, retryHandler, key);
}