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:de.albahrani.pi4j.rest.sysinfo.RaspberrySystemInfoController.java

License:Apache License

private boolean handleCORS(Request request, Response response) {
    boolean continueProcessing = true;

    response.addHeader("Access-Control-Allow-Origin", this.allowedCorsOrigin);

    if (HttpMethod.OPTIONS.equals(request.getHttpMethod())) {
        response.addHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
        response.addHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
        response.setResponseStatus(HttpResponseStatus.OK);
        continueProcessing = false;// w  w  w  . j a  va  2  s .co m
    }

    return continueProcessing;
}

From source file:de.albahrani.pi4j.rest.sysinfo.RaspberrySystemInfoControllerTest.java

License:Apache License

@Parameters
public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] { { HttpMethod.GET, true }, { HttpMethod.OPTIONS, true },
            { HttpMethod.GET, false }, { HttpMethod.OPTIONS, false } });
}

From source file:de.albahrani.pi4j.rest.sysinfo.RaspberrySystemInfoControllerTestRest.java

License:Apache License

@Test
public void testSystemClockInfo() {
    RestExpress server = mock(RestExpress.class);
    RaspberrySystemInfoController controller = new RaspberrySystemInfoController();

    Map<String, ParameterizedRouteBuilder> routeBuilders = new HashMap<>();

    String[] expectedRouteNames = new String[] { "test/clock", "test/java", "test/memory", "test/cpu",
            "test/os", "test/features", "test" };
    for (int i = 0; i < expectedRouteNames.length; i++) {
        String expectedRouteName = expectedRouteNames[i];
        ParameterizedRouteBuilder routeBuilder = mock(ParameterizedRouteBuilder.class);
        when(server.uri(eq(expectedRouteName), eq(controller))).thenReturn(routeBuilder);
        when(routeBuilder.action(anyString(), any())).thenReturn(routeBuilder);
        routeBuilders.put(expectedRouteName, routeBuilder);
    }/*from  w w  w.  j a  v  a  2 s  .  co  m*/
    controller.attach(server, "test");

    Set<Entry<String, ParameterizedRouteBuilder>> entrySet = routeBuilders.entrySet();
    for (Entry<String, ParameterizedRouteBuilder> entry : entrySet) {
        verify(server).uri(entry.getKey(), controller);
        ParameterizedRouteBuilder routeBuilder = entry.getValue();
        verify(routeBuilder).action(anyString(), eq(HttpMethod.GET));
        verify(routeBuilder).action(anyString(), eq(HttpMethod.OPTIONS));
        verifyNoMoreInteractions(routeBuilder);
    }
    verifyNoMoreInteractions(server);
}

From source file:de.albahrani.pi4j.rest.sysinfo.RaspberrySystemInfoControllerTestRest.java

License:Apache License

@Test
public void testCorsSetting() {
    RaspberrySystemInfoController controller = new RaspberrySystemInfoController();
    Pi4jSystemInfoFacade systemInfoProvider = createSystemInfoProviderMock();
    controller.setSystemInfo(systemInfoProvider);

    Request request = mock(Request.class);
    when(request.getHttpMethod()).thenReturn(HttpMethod.GET, HttpMethod.OPTIONS);
    Response response = new Response();

    controller.setAllowedCorsOrigin("http://abc.def.com");
    controller.getSystemClockInfo(request, response);

    String origin = response.getHeader("Access-Control-Allow-Origin");
    assertEquals("http://abc.def.com", origin);

    Response responseOptions = new Response();
    controller.getSystemClockInfo(request, responseOptions);
    String originOptions = responseOptions.getHeader("Access-Control-Allow-Origin");
    assertEquals("http://abc.def.com", originOptions);

}

From source file:divconq.http.multipart.HttpPostRequestEncoder.java

License:Apache License

/**
 *
 * @param factory//from   ww  w  .j a va 2s  . c  om
 *            the factory used to create InterfaceHttpData
 * @param request
 *            the request to encode
 * @param multipart
 *            True if the FORM is a ENCTYPE="multipart/form-data"
 * @param charset
 *            the charset to use as default
 * @param encoderMode
 *            the mode for the encoder to use. See {@link EncoderMode} for the details.
 * @throws NullPointerException
 *             for request or charset or factory
 * @throws ErrorDataEncoderException
 *             if the request is not a POST
 */
public HttpPostRequestEncoder(HttpDataFactory factory, HttpRequest request, boolean multipart, Charset charset,
        EncoderMode encoderMode) throws ErrorDataEncoderException {
    if (factory == null) {
        throw new NullPointerException("factory");
    }
    if (request == null) {
        throw new NullPointerException("request");
    }
    if (charset == null) {
        throw new NullPointerException("charset");
    }
    HttpMethod method = request.getMethod();
    if (!(method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT) || method.equals(HttpMethod.PATCH)
            || method.equals(HttpMethod.OPTIONS))) {
        throw new ErrorDataEncoderException("Cannot create a Encoder if not a POST");
    }
    this.request = request;
    this.charset = charset;
    this.factory = factory;
    // Fill default values
    bodyListDatas = new ArrayList<InterfaceHttpData>();
    // default mode
    isLastChunk = false;
    isLastChunkSent = false;
    isMultipart = multipart;
    multipartHttpDatas = new ArrayList<InterfaceHttpData>();
    this.encoderMode = encoderMode;
    if (isMultipart) {
        initDataMultipart();
    }
}

From source file:dpfmanager.shell.modules.server.post.HttpPostHandler.java

License:Open Source License

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof HttpRequest) {
        HttpRequest request = this.request = (HttpRequest) msg;
        if (request.method().equals(HttpMethod.POST) || request.method().equals(HttpMethod.OPTIONS)) {
            // Start new POST request
            init();//from  www . j  a v a2 s . c o m
            decoder = new HttpPostRequestDecoder(factory, request);
        } else {
            sendError(ctx, HttpResponseStatus.METHOD_NOT_ALLOWED);
            return;
        }
    }

    // POST request
    if (decoder != null) {
        if (msg instanceof HttpContent) {
            // New chunk is received
            try {
                HttpContent chunk = (HttpContent) msg;
                decoder.offer(chunk);
                readHttpDataChunkByChunk();
                if (chunk instanceof LastHttpContent) {
                    tractReadPost(ctx);
                    reset();
                    return;
                }
            } catch (ErrorDataDecoderException e1) {
                e1.printStackTrace();
                ctx.channel().close();
                return;
            }
        }
    }
}

From source file:edumsg.netty.EduMsgNettyServerInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel arg0) {
    CorsConfig corsConfig = CorsConfig.withAnyOrigin()
            .allowedRequestHeaders("X-Requested-With", "Content-Type", "Content-Length")
            .allowedRequestMethods(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE,
                    HttpMethod.OPTIONS)
            .build();/*from  ww  w  . j  a v  a  2 s . c o  m*/
    ChannelPipeline p = arg0.pipeline();
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(arg0.alloc()));
    }
    p.addLast("decoder", new HttpRequestDecoder());
    p.addLast("encoder", new HttpResponseEncoder());
    p.addLast(new CorsHandler(corsConfig));
    p.addLast(new EduMsgNettyServerHandler());
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest options(int port, String host, String requestURI) {
    return request(io.advantageous.conekt.http.HttpMethod.OPTIONS, port, host, requestURI);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest options(int port, String host, String requestURI,
        Handler<HttpClientResponse> responseHandler) {
    return request(io.advantageous.conekt.http.HttpMethod.OPTIONS, port, host, requestURI, responseHandler);
}

From source file:io.advantageous.conekt.http.impl.HttpClientImpl.java

License:Open Source License

@Override
public HttpClientRequest options(String requestURI) {
    return request(io.advantageous.conekt.http.HttpMethod.OPTIONS, requestURI);
}