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.ballerinalang.test.agent.server.WebServer.java

License:Open Source License

/**
 * Adds a POST route./*w  ww  . ja v a2  s .  com*/
 *
 * @param path The URL path.
 * @param handler The request handler.
 * @return This WebServer.
 */
public WebServer post(final String path, final BHandler handler) {
    this.routeTable.addRoute(new Route(HttpMethod.POST, path, handler));
    return this;
}

From source file:org.ballerinalang.test.service.http.sample.ExpectContinueTestCase.java

License:Open Source License

@Test(description = "Test 100 continue response and for request with expect:100-continue header")
public void test100Continue() {
    HttpClient httpClient = new HttpClient("localhost", servicePort);

    DefaultHttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/continue");
    DefaultLastHttpContent reqPayload = new DefaultLastHttpContent(
            Unpooled.wrappedBuffer(TestUtils.LARGE_ENTITY.getBytes()));

    httpRequest.headers().set(HttpHeaderNames.CONTENT_LENGTH, TestUtils.LARGE_ENTITY.getBytes().length);
    httpRequest.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN);
    httpRequest.headers().set("X-Status", "Positive");

    List<FullHttpResponse> responses = httpClient.sendExpectContinueRequest(httpRequest, reqPayload);

    Assert.assertFalse(httpClient.waitForChannelClose());

    // 100-continue response
    Assert.assertEquals(responses.get(0).status(), HttpResponseStatus.CONTINUE);
    Assert.assertEquals(Integer.parseInt(responses.get(0).headers().get(HttpHeaderNames.CONTENT_LENGTH)), 0);

    // Actual response
    String responsePayload = TestUtils.getEntityBodyFrom(responses.get(1));
    Assert.assertEquals(responses.get(1).status(), HttpResponseStatus.OK);
    Assert.assertEquals(responsePayload, TestUtils.LARGE_ENTITY);
    Assert.assertEquals(responsePayload.getBytes().length, TestUtils.LARGE_ENTITY.getBytes().length);
    Assert.assertEquals(Integer.parseInt(responses.get(1).headers().get(HttpHeaderNames.CONTENT_LENGTH)),
            TestUtils.LARGE_ENTITY.getBytes().length);
}

From source file:org.ballerinalang.test.service.http.sample.ExpectContinueTestCase.java

License:Open Source License

@Test(description = "Test ignoring inbound payload with a 417 response for request with expect:100-continue header")
public void test100ContinueNegative() {
    HttpClient httpClient = new HttpClient("localhost", servicePort);

    DefaultHttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/continue");
    DefaultLastHttpContent reqPayload = new DefaultLastHttpContent(
            Unpooled.wrappedBuffer(TestUtils.LARGE_ENTITY.getBytes()));

    httpRequest.headers().set(HttpHeaderNames.CONTENT_LENGTH, TestUtils.LARGE_ENTITY.getBytes().length);
    httpRequest.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN);

    List<FullHttpResponse> responses = httpClient.sendExpectContinueRequest(httpRequest, reqPayload);

    Assert.assertFalse(httpClient.waitForChannelClose());

    // 417 Expectation Failed response
    Assert.assertEquals(responses.get(0).status(), HttpResponseStatus.EXPECTATION_FAILED,
            "Response code mismatch");
    int length = Integer.parseInt(responses.get(0).headers().get(HttpHeaderNames.CONTENT_LENGTH));
    Assert.assertEquals(length, 26, "Content length mismatched");
    String payload = responses.get(0).content().readCharSequence(length, Charset.defaultCharset()).toString();
    Assert.assertEquals(payload, "Do not send me any payload", "Entity body mismatched");
    // Actual response
    Assert.assertEquals(responses.size(), 1,
            "Multiple responses received when only a 417 response was expected");
}

From source file:org.ballerinalang.test.service.http.sample.ExpectContinueTestCase.java

License:Open Source License

@Test
public void test100ContinuePassthrough() {
    HttpClient httpClient = new HttpClient("localhost", servicePort);

    DefaultHttpRequest reqHeaders = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            "/continue/testPassthrough");
    DefaultLastHttpContent reqPayload = new DefaultLastHttpContent(
            Unpooled.wrappedBuffer(TestUtils.LARGE_ENTITY.getBytes()));

    reqHeaders.headers().set(HttpHeaderNames.CONTENT_LENGTH, TestUtils.LARGE_ENTITY.getBytes().length);
    reqHeaders.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN);

    List<FullHttpResponse> responses = httpClient.sendExpectContinueRequest(reqHeaders, reqPayload);

    Assert.assertFalse(httpClient.waitForChannelClose());

    // 100-continue response
    Assert.assertEquals(responses.get(0).status(), HttpResponseStatus.CONTINUE);
    Assert.assertEquals(Integer.parseInt(responses.get(0).headers().get(HttpHeaderNames.CONTENT_LENGTH)), 0);

    // Actual response
    String responsePayload = TestUtils.getEntityBodyFrom(responses.get(1));
    Assert.assertEquals(responses.get(1).status(), HttpResponseStatus.OK);
    Assert.assertEquals(responsePayload, TestUtils.LARGE_ENTITY);
    Assert.assertEquals(responsePayload.getBytes().length, TestUtils.LARGE_ENTITY.getBytes().length);
    Assert.assertEquals(Integer.parseInt(responses.get(1).headers().get(HttpHeaderNames.CONTENT_LENGTH)),
            TestUtils.LARGE_ENTITY.getBytes().length);
}

From source file:org.bridje.http.impl.HttpBridletRequestImpl.java

License:Apache License

@Override
public boolean isPost() {
    return getMethod().equals(HttpMethod.POST.name());
}

From source file:org.caratarse.auth.services.Routes.java

License:Apache License

public static void define(Configuration config, RestExpress server) {
    //TODO: Your routes here...

    server.uri("/populates.{format}", config.getPopulateController()).action("readAll", HttpMethod.GET)
            .action("deleteAll", HttpMethod.DELETE).name(Constants.Routes.POPULATE_COLLECTION);

    server.uri("/users/{userUuid}.{format}", config.getUserController())
            .method(HttpMethod.GET, HttpMethod.PUT, HttpMethod.DELETE).name(Constants.Routes.USER_READ_ROUTE);
    server.uri("/users.{format}", config.getUserController()).action("readAll", HttpMethod.GET)
            .method(HttpMethod.POST).name(Constants.Routes.USER_COLLECTION_READ_ROUTE);
    server.uri("/users/{userUuid}/authorizations.{format}", config.getUserAuthorizationController())
            .action("readAll", HttpMethod.GET).name(Constants.Routes.USER_AUTHORIZATIONS_ROUTE);
    server.uri("/users/{userUuid}/authorizations/{authorizationName}.{format}",
            config.getUserAuthorizationController()).method(HttpMethod.GET, HttpMethod.PUT, HttpMethod.DELETE)
            .action("addAuthorizationToUser", HttpMethod.POST).name(Constants.Routes.USER_AUTHORIZATION_ROUTE);
    //// or...//w  w w  .  j a  v  a 2s  .  com
    //      server.regex("/some.regex", config.getRouteController());
}

From source file:org.ebayopensource.scc.cache.RequestKeyGeneratorTest.java

License:Apache License

@Test
public void test() {
    RequestKeyGenerator keyGen = new RequestKeyGenerator(s_appConfig);
    DefaultFullHttpRequest req1 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET,
            "http://s.ebay.com/v1/s1");
    DefaultFullHttpRequest req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET,
            "http://s.ebay.com/v1/s1");
    assertEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.POST, "http://s.ebay.com/v1/s1");
    assertNotEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://s.ebay.com/v1/s1");
    assertEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "http://s.ebay.com/v1/s2");
    assertNotEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "http://s.ebay.com/v1/s1");
    req1.headers().add("header1", "value1");
    req2.headers().add("header1", "value1");
    assertEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "http://s.ebay.com/v1/s1");
    req2.headers().add("header1", "value2");
    assertNotEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2 = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "http://s.ebay.com/v1/s1");
    req2.headers().add("header1", "value1");
    req2.trailingHeaders().add("header1", "value1");
    assertEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));

    req2.headers().add("Date", "idvalue");
    assertEquals(keyGen.generateKey(req1), keyGen.generateKey(req2));
}

From source file:org.ebayopensource.scc.filter.NettyRequestProxyFilterTest.java

License:Apache License

@Test
public void testHandleNonProxyRequest() throws IOException {
    AppConfiguration appConfig = new AppConfiguration(new ConfigLoader(), null);
    appConfig.init();//from   w  w  w . j ava  2  s . co  m
    PolicyManager policyManager = mock(PolicyManager.class);
    NettyRequestProxyFilter filter = new NettyRequestProxyFilter(policyManager, appConfig);

    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.attr(any(AttributeKey.class))).thenReturn(mock(Attribute.class));
    assertNull(filter.filterRequest(mock(HttpRequest.class), ctx));

    DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/version");
    HttpHeaders.setHost(req, "localhost:32876");

    HttpResponse resp = filter.filterRequest(req, ctx);
    assertTrue(resp instanceof FullHttpResponse);
    FullHttpResponse response = (FullHttpResponse) resp;
    assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
    assertTrue(HttpHeaders.getContentLength(response) > 0);
    assertEquals(HttpResponseStatus.OK, response.getStatus());

    req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/version");
    HttpHeaders.setHost(req, "127.0.0.1:32876");

    resp = filter.filterRequest(req, ctx);
    assertTrue(resp instanceof FullHttpResponse);
    response = (FullHttpResponse) resp;
    assertEquals(HttpResponseStatus.NOT_FOUND, response.getStatus());
    assertEquals(0, HttpHeaders.getContentLength(response));

    req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/NOTFOUND");
    HttpHeaders.setHost(req, "127.0.0.1:32876");

    resp = filter.filterRequest(req, ctx);
    assertTrue(resp instanceof FullHttpResponse);
    response = (FullHttpResponse) resp;
    assertEquals(HttpResponseStatus.NOT_FOUND, response.getStatus());
    assertEquals(0, HttpHeaders.getContentLength(response));
}

From source file:org.eclipse.milo.opcua.stack.client.transport.http.OpcClientHttpCodec.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, UaTransportRequest transportRequest, List<Object> out)
        throws Exception {

    logger.debug("encoding: " + transportRequest.getRequest());

    ctx.channel().attr(KEY_PENDING_REQUEST).set(transportRequest);

    ByteBuf content = Unpooled.buffer();

    switch (transportProfile) {
    case HTTPS_UABINARY: {
        OpcUaBinaryStreamEncoder encoder = new OpcUaBinaryStreamEncoder(content);
        encoder.writeMessage(null, transportRequest.getRequest());
        break;/*from   w  w w.j av a 2  s .com*/
    }

    case HTTPS_UAXML: {
        // TODO put document into a SOAP message.
        throw new UaException(StatusCodes.Bad_InternalError, "no encoder for transport: " + transportProfile);
    }

    default:
        throw new UaException(StatusCodes.Bad_InternalError, "no encoder for transport: " + transportProfile);
    }

    String endpointUrl = endpoint.getEndpointUrl();

    FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            EndpointUtil.getPath(endpointUrl), content);

    httpRequest.headers().set(HttpHeaderNames.HOST, EndpointUtil.getHost(endpointUrl));
    httpRequest.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    httpRequest.headers().set(HttpHeaderNames.CONTENT_TYPE, UABINARY_CONTENT_TYPE);
    httpRequest.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
    httpRequest.headers().set("OPCUA-SecurityPolicy", config.getEndpoint().getSecurityPolicyUri());

    out.add(httpRequest);
}

From source file:org.elasticsearch.hadoop.http.netty4.Netty4HttpRequest.java

License:Apache License

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

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

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

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

    if (httpMethod == HttpMethod.HEAD) {
        return Method.HEAD;
    }//from   ww  w.j a  v  a 2  s  . c o m

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

    return Method.GET;
}