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

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

Introduction

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

Prototype

HttpMethod POST

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

Click Source Link

Document

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.

Usage

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

License:Open Source License

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

    httpRequest.content().writeBytes(GELF_MESSAGE);

    channel.writeInbound(httpRequest);/*from   w ww  .j  av a 2 s. co m*/
    channel.finish();

    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
    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");
    assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.CLOSE.toString());
}

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

License:Open Source License

@Test
public void withKeepalive() {
    final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            "/gelf");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

    httpRequest.content().writeBytes(GELF_MESSAGE);

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

    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.KEEP_ALIVE.toString());
}

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

License:Open Source License

@Test
public void withJSONContentType() {
    final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            "/gelf");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);

    httpRequest.content().writeBytes(GELF_MESSAGE);

    channel.writeInbound(httpRequest);//from w  w  w  .  ja  v a2  s.c  om
    channel.finish();

    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.CLOSE.toString());
}

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

License:Open Source License

@Test
public void withCustomContentType() {
    final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            "/gelf");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, "foo/bar");
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);

    httpRequest.content().writeBytes(GELF_MESSAGE);

    channel.writeInbound(httpRequest);//from ww w  . j a  va 2 s  .  co  m
    channel.finish();

    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.CLOSE.toString());
}

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

License:Open Source License

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

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

    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.NOT_FOUND);
    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.hawkular.apm.tests.client.http.NettyHttpITest.java

License:Apache License

@Override
public void init() {
    server = HttpServer.newServer().enableWireLogging(LogLevel.DEBUG).start((req, resp) -> {
        if (req.getHeader(Constants.HAWKULAR_APM_TRACEID) == null) {
            return resp.setStatus(HttpResponseStatus.BAD_REQUEST);
        }//from w  ww  .  ja v  a  2  s.  com
        if (req.getHttpMethod() == HttpMethod.POST || req.getHttpMethod() == HttpMethod.PUT) {
            req.getContent().subscribe(bb -> System.out.println("DATA = " + bb.toString()));
        }
        return resp.writeString(Observable.just(HELLO_WORLD));
    });
    super.init();
}

From source file:org.hawkular.apm.tests.client.http.NettyNoResponseHttpITest.java

License:Apache License

@Override
public void init() {
    server = HttpServer.newServer().enableWireLogging(LogLevel.DEBUG).start((req, resp) -> {
        if (req.getHeader(Constants.HAWKULAR_APM_TRACEID) == null) {
            return resp.setStatus(HttpResponseStatus.BAD_REQUEST);
        }/*from   ww  w .jav a  2 s . c om*/
        if (req.getHttpMethod() == HttpMethod.POST || req.getHttpMethod() == HttpMethod.PUT) {
            req.getContent().subscribe(bb -> System.out.println("DATA = " + bb.toString()));
        }
        resp.setStatus(HttpResponseStatus.OK);
        return resp;
    });

    super.init();
}

From source file:org.iotivity.cloud.base.protocols.http.HCProxyProcessor.java

License:Open Source License

/**
 * This function returns a message created by the request,
 * which implements IRequest(used in the cloud)
 * translated from the HTTP request./*from www.ja v  a  2s.co  m*/
 * 
 * @return requestMessage
 */
public Message getRequestMessage() {

    Message requestMessage = null;

    String coapUriPath = mTargetCoapPath;
    String coapUriQuery = mTargetCoapQuery;
    ContentFormat coapContentFormat = null;
    if (mContentFormat != null) {
        if (mContentFormat.equalsIgnoreCase(APPLICATION_CBOR)) {
            coapContentFormat = ContentFormat.APPLICATION_CBOR;
        }
    }
    byte[] coapPayload = mCborContent;

    if (mHttpMethod == HttpMethod.POST) {

        CoapRequest coapRequest = new CoapRequest(RequestMethod.POST);
        coapRequest.setToken(createToken());
        coapRequest.setUriPath(coapUriPath);
        if (coapUriQuery != null) {
            coapRequest.setUriQuery(coapUriQuery);
        }
        if (coapPayload != null) {
            coapRequest.setContentFormat(coapContentFormat);
            coapRequest.setPayload(coapPayload);
        }
        requestMessage = coapRequest;

    } else if (mHttpMethod == HttpMethod.PUT) {

        CoapRequest coapRequest = new CoapRequest(RequestMethod.PUT);
        coapRequest.setToken(createToken());
        coapRequest.setUriPath(coapUriPath);
        if (coapUriQuery != null) {
            coapRequest.setUriQuery(coapUriQuery);
        }
        if (coapPayload != null) {
            coapRequest.setContentFormat(coapContentFormat);
            coapRequest.setPayload(coapPayload);
        }
        requestMessage = coapRequest;

    } else if (mHttpMethod == HttpMethod.GET) {

        CoapRequest coapRequest = new CoapRequest(RequestMethod.GET);
        coapRequest.setToken(createToken());
        coapRequest.setUriPath(coapUriPath);
        if (coapUriQuery != null) {
            coapRequest.setUriQuery(coapUriQuery);
        }
        requestMessage = coapRequest;

    } else if (mHttpMethod == HttpMethod.DELETE) {

        CoapRequest coapRequest = new CoapRequest(RequestMethod.DELETE);
        coapRequest.setToken(createToken());
        coapRequest.setUriPath(coapUriPath);
        if (coapUriQuery != null) {
            coapRequest.setUriQuery(coapUriQuery);
        }
        requestMessage = coapRequest;
    }

    return requestMessage;
}

From source file:org.iotivity.cloud.base.protocols.http.HCProxyProcessor.java

License:Open Source License

/**
 * This function returns an error information
 * (HTTP status code and reason phrase) if there is an error.
 * //  ww w. j  a  v a 2s  .  co m
 * @return errorStatusCode
 */
public String checkHttpRequest() {

    String errorStatusCode = null;

    if (mHttpMethod == HttpMethod.POST) {
        if (mContentFormat == null) {
            errorStatusCode = "500 Internal Server Error: " + "Content-Type does not recognized.";
        } else if (mCborContent == null) {
            errorStatusCode = "400 Bad Request: Payload is empty.";
        }
    }

    if (mHostingHttpUri == null || mTargetCoapPath == null) {
        errorStatusCode = "400 Bad Request: " + "Please use proper URI Path " + HC_PROXY_PATH
                + "<Host, e.g. ip:port>/<Path>.";
    }

    return errorStatusCode;
}

From source file:org.iotivity.cloud.base.protocols.proxy.CoapHttpProxyHandler.java

License:Open Source License

CoapRequest httpRequestToCoAPRequest(String uri, HttpRequest httpRequest) {
    CoapRequest coapRequest;//ww  w  .  j a  v a2s  . com

    // TODO: coapRequest converter required
    // coapRequest.getOptions().setUriQuery();
    if (httpRequest.getMethod() == HttpMethod.GET) {
        coapRequest = new CoapRequest(CoapMethod.GET);
    } else if (httpRequest.getMethod() == HttpMethod.PUT) {
        coapRequest = new CoapRequest(CoapMethod.PUT);
    } else if (httpRequest.getMethod() == HttpMethod.POST) {
        coapRequest = new CoapRequest(CoapMethod.POST);
    } else if (httpRequest.getMethod() == HttpMethod.DELETE) {
        coapRequest = new CoapRequest(CoapMethod.DELETE);
    } else {
        throw new IllegalArgumentException();
    }

    coapRequest.setUriPath(uri);

    return coapRequest;
}