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:io.advantageous.conekt.http.impl.HttpClientRequestImpl.java

License:Open Source License

private HttpMethod toNettyHttpMethod(io.advantageous.conekt.http.HttpMethod method) {
    switch (method) {
    case CONNECT: {
        return HttpMethod.CONNECT;
    }/*  ww  w. j  ava  2 s.  c  o  m*/
    case GET: {
        return HttpMethod.GET;
    }
    case PUT: {
        return HttpMethod.PUT;
    }
    case POST: {
        return HttpMethod.POST;
    }
    case DELETE: {
        return HttpMethod.DELETE;
    }
    case HEAD: {
        return HttpMethod.HEAD;
    }
    case OPTIONS: {
        return HttpMethod.OPTIONS;
    }
    case TRACE: {
        return HttpMethod.TRACE;
    }
    case PATCH: {
        return HttpMethod.PATCH;
    }
    default:
        throw new IllegalArgumentException();
    }
}

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

License:Open Source License

@Override
public HttpServerRequest setExpectMultipart(boolean expect) {
    synchronized (conn) {
        checkEnded();//from ww  w.  ja  va  2  s  .  co m
        if (expect) {
            if (decoder == null) {
                String contentType = request.headers().get(HttpHeaders.Names.CONTENT_TYPE);
                if (contentType != null) {
                    HttpMethod method = request.getMethod();
                    String lowerCaseContentType = contentType.toLowerCase();
                    boolean isURLEncoded = lowerCaseContentType
                            .startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
                    if ((lowerCaseContentType.startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA)
                            || isURLEncoded)
                            && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)
                                    || method.equals(HttpMethod.PATCH) || method.equals(HttpMethod.DELETE))) {
                        decoder = new HttpPostRequestDecoder(new DataFactory(), request);
                    }
                }
            }
        } else {
            decoder = null;
        }
        return this;
    }
}

From source file:io.crate.protocols.http.HttpAuthUpstreamHandlerTest.java

@Test
public void testAuthorized() throws Exception {
    HttpAuthUpstreamHandler handler = new HttpAuthUpstreamHandler(Settings.EMPTY,
            new AlwaysOKNullAuthentication());
    EmbeddedChannel ch = new EmbeddedChannel(handler);

    DefaultHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/_sql");
    ch.writeInbound(request);/* w w w.  j  a v  a 2s. c o m*/

    assertThat(handler.authorized(), is(true));
}

From source file:io.crate.protocols.http.HttpAuthUpstreamHandlerTest.java

@Test
public void testNotNoHbaConfig() throws Exception {
    HttpAuthUpstreamHandler handler = new HttpAuthUpstreamHandler(Settings.EMPTY, authService);
    EmbeddedChannel ch = new EmbeddedChannel(handler);

    DefaultHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/_sql");
    request.headers().add(HttpHeaderNames.AUTHORIZATION.toString(), "Basic QWxhZGRpbjpPcGVuU2VzYW1l");
    request.headers().add("X-Real-Ip", "10.1.0.100");

    ch.writeInbound(request);//from w w w  .java2 s  . co  m
    assertFalse(handler.authorized());

    assertUnauthorized(ch.readOutbound(),
            "No valid auth.host_based.config entry found for host \"10.1.0.100\", user \"Aladdin\", protocol \"http\"\n");
}

From source file:io.crate.protocols.http.HttpAuthUpstreamHandlerTest.java

@Test
public void testUnauthorizedUser() throws Exception {
    HttpAuthUpstreamHandler handler = new HttpAuthUpstreamHandler(Settings.EMPTY, authService);
    EmbeddedChannel ch = new EmbeddedChannel(handler);

    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/_sql");

    ch.writeInbound(request);//from  w w  w  . j  a v  a2  s .  co  m

    assertFalse(handler.authorized());
    assertUnauthorized(ch.readOutbound(), "trust authentication failed for user \"crate\"\n");
}

From source file:io.crate.protocols.http.HttpAuthUpstreamHandlerTest.java

@Test
public void testClientCertUserHasPreferenceOverTrustAuthDefault() throws Exception {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SSLSession session = mock(SSLSession.class);
    when(session.getPeerCertificates()).thenReturn(new Certificate[] { ssc.cert() });

    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/_sql");
    String userName = HttpAuthUpstreamHandler.credentialsFromRequest(request, session, Settings.EMPTY).v1();

    assertThat(userName, is("example.com"));
}

From source file:io.crate.protocols.http.HttpAuthUpstreamHandlerTest.java

@Test
public void testUserAuthenticationWithDisabledHBA() throws Exception {
    User crateUser = User.of("crate", EnumSet.of(User.Role.SUPERUSER));
    Authentication authServiceNoHBA = new AlwaysOKAuthentication(userName -> crateUser);

    HttpAuthUpstreamHandler handler = new HttpAuthUpstreamHandler(Settings.EMPTY, authServiceNoHBA);
    EmbeddedChannel ch = new EmbeddedChannel(handler);

    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/_sql");
    request.headers().add(HttpHeaderNames.AUTHORIZATION.toString(), "Basic Y3JhdGU6");
    ch.writeInbound(request);/*from w w w  . j av a 2  s . c o m*/

    assertTrue(handler.authorized());
}

From source file:io.crate.protocols.http.HttpAuthUpstreamHandlerTest.java

@Test
public void testUnauthorizedUserWithDisabledHBA() throws Exception {
    Authentication authServiceNoHBA = new AlwaysOKAuthentication(userName -> null);
    HttpAuthUpstreamHandler handler = new HttpAuthUpstreamHandler(Settings.EMPTY, authServiceNoHBA);
    EmbeddedChannel ch = new EmbeddedChannel(handler);

    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/_sql");
    request.headers().add(HttpHeaderNames.AUTHORIZATION.toString(), "Basic QWxhZGRpbjpPcGVuU2VzYW1l");

    ch.writeInbound(request);/*from www. ja  v  a  2s  . c om*/

    assertFalse(handler.authorized());
    assertUnauthorized(ch.readOutbound(), "trust authentication failed for user \"Aladdin\"\n");
}

From source file:io.example.UploadClient.HttpUploadClient.java

License:Apache License

/**
 * Standard post without multipart but already support on Factory (memory management)
 *
 * @return the list of HttpData object (attribute and file) to be reused on next post
 *///  ww  w.  j a v a2  s  . c o  m
private static List<InterfaceHttpData> formpost(Bootstrap bootstrap, String host, int port, URI uriSimple,
        File file, HttpDataFactory factory, List<Entry<String, String>> headers) throws Exception {
    // XXX /formpost
    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    // Wait until the connection attempt succeeds or fails.
    Channel channel = future.sync().channel();

    // Prepare the HTTP request.
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            uriSimple.toASCIIString());

    // Use the PostBody encoder
    HttpPostRequestEncoder bodyRequestEncoder = new HttpPostRequestEncoder(factory, request, false); // false => not multipart

    // it is legal to add directly header or cookie into the request until finalize
    for (Entry<String, String> entry : headers) {
        request.headers().set(entry.getKey(), entry.getValue());
    }

    // add Form attribute
    bodyRequestEncoder.addBodyAttribute("getform", "POST");
    bodyRequestEncoder.addBodyAttribute("info", "first value");
    bodyRequestEncoder.addBodyAttribute("secondinfo", "secondvalue &");
    bodyRequestEncoder.addBodyAttribute("thirdinfo", textArea);
    bodyRequestEncoder.addBodyAttribute("fourthinfo", textAreaLong);
    bodyRequestEncoder.addBodyFileUpload("myfile", file, "application/x-zip-compressed", false);

    // finalize request
    request = bodyRequestEncoder.finalizeRequest();

    // Create the bodylist to be reused on the last version with Multipart support
    List<InterfaceHttpData> bodylist = bodyRequestEncoder.getBodyListAttributes();

    // send request
    channel.write(request);

    // test if request was chunked and if so, finish the write
    if (bodyRequestEncoder.isChunked()) { // could do either request.isChunked()
        // either do it through ChunkedWriteHandler
        channel.write(bodyRequestEncoder);
    }
    System.out.println("Sent formpost");
    channel.flush();

    // Do not clear here since we will reuse the InterfaceHttpData on the next request
    // for the example (limit action on client side). Take this as a broadcast of the same
    // request on both Post actions.
    //
    // On standard program, it is clearly recommended to clean all files after each request
    // bodyRequestEncoder.cleanFiles();

    // Wait for the server to close the connection.
    channel.closeFuture().sync();
    return bodylist;
}

From source file:io.example.UploadClient.HttpUploadClient.java

License:Apache License

/**
 * Multipart example// w ww .j  a  va2 s  .  co  m
 */
private static void formpostmultipart(Bootstrap bootstrap, String host, int port, URI uriFile,
        HttpDataFactory factory, List<Entry<String, String>> headers, List<InterfaceHttpData> bodylist)
        throws Exception {
    // XXX /formpostmultipart
    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    // Wait until the connection attempt succeeds or fails.
    Channel channel = future.sync().channel();

    // Prepare the HTTP request.
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            uriFile.toASCIIString());

    // Use the PostBody encoder
    HttpPostRequestEncoder bodyRequestEncoder = new HttpPostRequestEncoder(factory, request, true); // true => multipart

    // it is legal to add directly header or cookie into the request until finalize
    for (Entry<String, String> entry : headers) {
        request.headers().set(entry.getKey(), entry.getValue());
    }

    // add Form attribute from previous request in formpost()
    bodyRequestEncoder.setBodyHttpDatas(bodylist);

    // finalize request
    bodyRequestEncoder.finalizeRequest();

    // send request
    channel.write(request);

    // test if request was chunked and if so, finish the write
    if (bodyRequestEncoder.isChunked()) {
        channel.write(bodyRequestEncoder);
    }
    System.out.println("Sent formpostmultipart");
    channel.flush();

    // Now no more use of file representation (and list of HttpData)
    bodyRequestEncoder.cleanFiles();

    // Wait for the server to close the connection.
    channel.closeFuture().sync();
}