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

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

Introduction

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

Prototype

HttpMethod OPTIONS

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

Click Source Link

Document

The OPTIONS method represents a request for information about the communication options available on the request/response chain identified by the Request-URI.

Usage

From source file:org.elasticsearch.http.nio.NioHttpRequest.java

License:Apache License

@Override
public RestRequest.Method method() {
    HttpMethod httpMethod = request.method();
    if (httpMethod == HttpMethod.GET)
        return RestRequest.Method.GET;

    if (httpMethod == HttpMethod.POST)
        return RestRequest.Method.POST;

    if (httpMethod == HttpMethod.PUT)
        return RestRequest.Method.PUT;

    if (httpMethod == HttpMethod.DELETE)
        return RestRequest.Method.DELETE;

    if (httpMethod == HttpMethod.HEAD) {
        return RestRequest.Method.HEAD;
    }// w  ww  . java2  s. c  o  m

    if (httpMethod == HttpMethod.OPTIONS) {
        return RestRequest.Method.OPTIONS;
    }

    if (httpMethod == HttpMethod.PATCH) {
        return RestRequest.Method.PATCH;
    }

    if (httpMethod == HttpMethod.TRACE) {
        return RestRequest.Method.TRACE;
    }

    if (httpMethod == HttpMethod.CONNECT) {
        return RestRequest.Method.CONNECT;
    }

    throw new IllegalArgumentException("Unexpected http method: " + httpMethod);
}

From source file:org.graylog2.inputs.transports.netty.HttpHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest request) throws Exception {
    final Channel channel = ctx.channel();
    final boolean keepAlive = HttpUtil.isKeepAlive(request);
    final HttpVersion httpRequestVersion = request.protocolVersion();
    final String origin = request.headers().get(HttpHeaderNames.ORIGIN);

    // to allow for future changes, let's be at least a little strict in what we accept here.
    if (HttpMethod.OPTIONS.equals(request.method())) {
        writeResponse(channel, keepAlive, httpRequestVersion, HttpResponseStatus.OK, origin);
        return;/*from  www.  j  a v a2s .c  o  m*/
    } else if (!HttpMethod.POST.equals(request.method())) {
        writeResponse(channel, keepAlive, httpRequestVersion, HttpResponseStatus.METHOD_NOT_ALLOWED, origin);
        return;
    }

    final boolean correctPath = "/gelf".equals(request.uri());
    if (correctPath && request instanceof FullHttpRequest) {
        final FullHttpRequest fullHttpRequest = (FullHttpRequest) request;
        final ByteBuf buffer = fullHttpRequest.content();

        // send on to raw message handler
        writeResponse(channel, keepAlive, httpRequestVersion, HttpResponseStatus.ACCEPTED, origin);
        ctx.fireChannelRead(buffer);
    } else {
        writeResponse(channel, keepAlive, httpRequestVersion, HttpResponseStatus.NOT_FOUND, origin);
    }
}

From source file:org.graylog2.inputs.transports.netty.HttpHandlerTest.java

License:Open Source License

@Test
public void successfullyProcessOPTIONSRequest() {
    final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "/gelf");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.ORIGIN, "http://example.com");
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);

    channel.writeInbound(httpRequest);/*from   ww  w.  j a v a 2  s  .  c o  m*/
    channel.finish();

    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.OK);

    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
    assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
    assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS))
            .isEqualTo("Authorization, Content-Type");
}

From source file:org.graylog2.inputs.transports.netty.HttpHandlerTest.java

License:Open Source License

@Test
public void successfullyProcessOPTIONSRequestWithoutOrigin() {
    final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "/gelf");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);

    channel.writeInbound(httpRequest);/*w  ww.  j a  va2s  . c  o  m*/
    channel.finish();

    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.OK);

    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.contains(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN)).isFalse();
    assertThat(headers.contains(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isFalse();
    assertThat(headers.contains(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS)).isFalse();
}

From source file:org.restexpress.RestExpressTest.java

License:Apache License

@Test
public void shouldCallAltMethods() throws ClientProtocolException, IOException {
    int port = nextPort();
    String testUrl = createUrl(TEST_URL_PATTERN, port);
    RestExpress re = new RestExpress();
    NoopController controller = new NoopController();
    re.uri(TEST_PATH, controller).method(HttpMethod.HEAD, HttpMethod.OPTIONS, HttpMethod.PATCH);
    re.bind(port);/* ww  w .  j  ava2 s. c  om*/

    waitForStartup();

    HttpGet get = new HttpGet(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(get);
        assertEquals(405, response.getStatusLine().getStatusCode());
    } finally {
        get.releaseConnection();
    }

    HttpOptions options = new HttpOptions(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(options);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(0, controller.delete);
        assertEquals(0, controller.read);
        assertEquals(0, controller.create);
        assertEquals(0, controller.update);
        assertEquals(1, controller.options);
        assertEquals(0, controller.head);
        assertEquals(0, controller.patch);
    } finally {
        options.releaseConnection();
    }

    HttpHead head = new HttpHead(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(head);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(0, controller.delete);
        assertEquals(0, controller.read);
        assertEquals(0, controller.create);
        assertEquals(0, controller.update);
        assertEquals(1, controller.options);
        assertEquals(1, controller.head);
        assertEquals(0, controller.patch);
    } finally {
        head.releaseConnection();
    }

    HttpPatch patch = new HttpPatch(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(patch);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(0, controller.delete);
        assertEquals(0, controller.read);
        assertEquals(0, controller.create);
        assertEquals(0, controller.update);
        assertEquals(1, controller.options);
        assertEquals(1, controller.head);
        assertEquals(1, controller.patch);
    } finally {
        patch.releaseConnection();
    }

    re.shutdown(true);
}

From source file:org.restexpress.RestExpressTest.java

License:Apache License

@Test
public void shouldCallAltNamedMethods() throws ClientProtocolException, IOException {
    int port = nextPort();
    String testUrl = createUrl(TEST_URL_PATTERN, port);
    RestExpress re = new RestExpress();
    AltController controller = new AltController();
    re.uri(TEST_PATH, controller).action("altHead", HttpMethod.HEAD).action("altOptions", HttpMethod.OPTIONS)
            .action("altPatch", HttpMethod.PATCH);
    re.bind(port);//from w  w w. j a v a2s . co m

    waitForStartup();

    HttpGet get = new HttpGet(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(get);
        assertEquals(405, response.getStatusLine().getStatusCode());
    } finally {
        get.releaseConnection();
    }

    HttpOptions options = new HttpOptions(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(options);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(1, controller.options);
        assertEquals(0, controller.head);
        assertEquals(0, controller.patch);
    } finally {
        options.releaseConnection();
    }

    HttpHead head = new HttpHead(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(head);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(1, controller.options);
        assertEquals(1, controller.head);
        assertEquals(0, controller.patch);
    } finally {
        head.releaseConnection();
    }

    HttpPatch patch = new HttpPatch(testUrl);
    try {
        HttpResponse response = (HttpResponse) CLIENT.execute(patch);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(1, controller.options);
        assertEquals(1, controller.head);
        assertEquals(1, controller.patch);
    } finally {
        patch.releaseConnection();
    }

    re.shutdown(true);
}

From source file:org.restexpress.route.RouteMapping.java

License:Apache License

public RouteMapping() {
    super();//from   w  w  w .  java 2  s.  c  o  m
    routes = new HashMap<HttpMethod, List<Route>>();
    routes.put(HttpMethod.DELETE, deleteRoutes);
    routes.put(HttpMethod.GET, getRoutes);
    routes.put(HttpMethod.POST, postRoutes);
    routes.put(HttpMethod.PUT, putRoutes);
    routes.put(HttpMethod.HEAD, headRoutes);
    routes.put(HttpMethod.OPTIONS, optionRoutes);
}

From source file:org.restexpress.route.RouteResolverTest.java

License:Apache License

@Test(expected = MethodNotAllowedException.class)
public void shouldThrowMethodNotAllowed() {
    FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS,
            "/foo/foo23.json?value=ignored");
    httpRequest.headers().add("Host", "testing-host");
    Request request = new Request(httpRequest, null);
    resolver.resolve(request);/* w  w  w . j a  va2 s.co m*/
}

From source file:org.restexpress.route.RouteResolverTest.java

License:Apache License

@Test
public void shouldSendAllowedMethodsForCrudRoute() {
    FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS,
            "/foo/foo23.json?value=ignored");
    httpRequest.headers().add("Host", "testing-host");
    Request request = new Request(httpRequest, null);
    try {/* ww  w  .  j  a v  a2s  .c o  m*/
        resolver.resolve(request);
    } catch (MethodNotAllowedException e) {
        List<HttpMethod> allowed = e.getAllowedMethods();
        assertEquals(4, allowed.size());
        assertTrue(allowed.contains(HttpMethod.GET));
        assertTrue(allowed.contains(HttpMethod.PUT));
        assertTrue(allowed.contains(HttpMethod.POST));
        assertTrue(allowed.contains(HttpMethod.DELETE));
    }
}

From source file:org.restexpress.route.RouteResolverTest.java

License:Apache License

@Test
public void shouldSendAllowedMethodsForPostRoute() {
    FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS,
            "/foo.json?value=ignored");
    httpRequest.headers().add("Host", "testing-host");
    Request request = new Request(httpRequest, null);
    try {//from   w ww.  ja v a2 s . co m
        resolver.resolve(request);
    } catch (MethodNotAllowedException e) {
        List<HttpMethod> allowed = e.getAllowedMethods();
        assertEquals(1, allowed.size());
        assertTrue(allowed.contains(HttpMethod.POST));
    }
}