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.wso2.carbon.mss.internal.router.HttpServerTest.java

License:Open Source License

@Test
public void testUploadReject() throws Exception {
    HttpURLConnection urlConn = request("/test/v1/uploadReject", HttpMethod.POST, true);
    try {/*from www . j a v  a  2 s. com*/
        urlConn.setChunkedStreamingMode(1024);
        urlConn.getOutputStream().write("Rejected Content".getBytes(Charsets.UTF_8));
        try {
            urlConn.getInputStream();
            Assert.fail();
        } catch (IOException e) {
            // Expect to get exception since server response with 400. Just drain the error stream.
            ByteStreams.toByteArray(urlConn.getErrorStream());
            Assert.assertEquals(HttpResponseStatus.BAD_REQUEST.code(), urlConn.getResponseCode());
        }
    } finally {
        urlConn.disconnect();
    }
}

From source file:org.wso2.carbon.mss.internal.router.HttpServerTest.java

License:Open Source License

@Test
public void testNoPathPostMethod() throws Exception {
    HttpURLConnection urlConn = request("/test/v1", HttpMethod.POST);
    Assert.assertEquals("no-@Path-POST", getContent(urlConn));
    urlConn.disconnect();/*  ww w  . ja v  a2  s.c  o  m*/
}

From source file:org.wso2.carbon.mss.internal.router.HttpServerTest.java

License:Open Source License

@Test
public void testExceptionHandler() throws IOException {
    HttpURLConnection urlConn = request("/test/v1/customException", HttpMethod.POST);
    Assert.assertEquals(TestHandler.CustomException.HTTP_RESPONSE_STATUS.code(), urlConn.getResponseCode());
    urlConn.disconnect();//from w  w w  .  j  a  va2  s .  c  o  m
}

From source file:org.wso2.carbon.mss.internal.router.HttpServerTest.java

License:Open Source License

@Test
public void testConsumeJsonProduceString() throws IOException {
    HttpURLConnection urlConn = request("/test/v1/jsonConsumeStringProduce", HttpMethod.POST);
    urlConn.setRequestProperty(HttpHeaders.Names.CONTENT_TYPE, "text/json");
    Gson gson = new Gson();
    Pet pet = petInstance();/*from  w ww .j  av  a2  s .  c om*/
    writeContent(urlConn, gson.toJson(pet));
    Assert.assertEquals(HttpResponseStatus.OK.code(), urlConn.getResponseCode());
    Assert.assertEquals(pet.getDetails(), getContent(urlConn));
    urlConn.disconnect();
}

From source file:org.wso2.carbon.mss.internal.router.HttpServerTest.java

License:Open Source License

@Test
public void testConsumeStringProduceJson() throws IOException {
    HttpURLConnection urlConn = request("/test/v1/textConsumeJsonProduce", HttpMethod.POST);
    urlConn.setRequestProperty(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
    String str = "send-something";
    writeContent(urlConn, str);//from  w  w  w  .ja  v  a2s.c o m
    Assert.assertEquals(HttpResponseStatus.OK.code(), urlConn.getResponseCode());
    Gson gson = new Gson();
    String content = getContent(urlConn);
    TextBean textBean = gson.fromJson(content, TextBean.class);
    Assert.assertEquals(str, textBean.getText());
    urlConn.disconnect();
}

From source file:org.wso2.carbon.mss.internal.router.HttpServerTest.java

License:Open Source License

@Test
public void testConsumeStringProduceString() throws IOException {
    HttpURLConnection urlConn = request("/test/v1/textConsumeTextProduce", HttpMethod.POST);
    urlConn.setRequestProperty(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
    String str = "send-something";
    writeContent(urlConn, str);//from  w  w w. j  a v a2s.  c om
    Assert.assertEquals(HttpResponseStatus.OK.code(), urlConn.getResponseCode());
    Assert.assertEquals(str + "-processed", getContent(urlConn));
    urlConn.disconnect();
}

From source file:org.wso2.carbon.mss.internal.router.HttpServerTest.java

License:Open Source License

protected HttpURLConnection request(String path, HttpMethod method, boolean keepAlive) throws IOException {
    URL url = baseURI.resolve(path).toURL();
    HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    if (method == HttpMethod.POST || method == HttpMethod.PUT) {
        urlConn.setDoOutput(true);//  w  w w .j  a  va  2s  .c  o  m
    }
    urlConn.setRequestMethod(method.name());
    if (!keepAlive) {
        urlConn.setRequestProperty(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    }

    return urlConn;
}

From source file:org.wso2.carbon.mss.internal.router.HttpsServerTest.java

License:Open Source License

@Override
protected HttpURLConnection request(String path, HttpMethod method, boolean keepAlive) throws IOException {
    URL url = baseURI.resolve(path).toURL();
    HttpsURLConnection.setDefaultSSLSocketFactory(sslClientContext.getClientContext().getSocketFactory());
    HostnameVerifier allHostsValid = (hostname1, session) -> true;

    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    HttpURLConnection urlConn = (HttpsURLConnection) url.openConnection();
    if (method == HttpMethod.POST || method == HttpMethod.PUT) {
        urlConn.setDoOutput(true);//from   ww  w  .  j  a  v a  2  s  .  c o m
    }
    urlConn.setRequestMethod(method.name());
    if (!keepAlive) {
        urlConn.setRequestProperty(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    }
    return urlConn;
}

From source file:org.wso2.carbon.mss.internal.router.URLRewriterTest.java

License:Open Source License

private HttpURLConnection request(String path, HttpMethod method) throws IOException {
    URL url = baseURI.resolve(path).toURL();
    HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    if (method == HttpMethod.POST || method == HttpMethod.PUT) {
        urlConn.setDoOutput(true);// w  ww  .java  2  s  . c  om
    }
    urlConn.setRequestMethod(method.name());

    return urlConn;
}

From source file:org.wso2.carbon.mss.security.oauth2.OAuth2SecurityInterceptor.java

License:Open Source License

/**
 * Validated the given accessToken with an external key server.
 *
 * @param accessToken AccessToken to be validated.
 * @return the response from the key manager server.
 *//*from   w w  w .j a  v a 2 s  .  co  m*/
private String getValidatedTokenResponse(String accessToken) throws MSSSecurityException {
    URL url;
    try {
        url = new URL(AUTH_SERVER_URL);
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        urlConn.setDoOutput(true);
        urlConn.setRequestMethod(HttpMethod.POST.name());
        urlConn.getOutputStream().write(("token=" + accessToken).getBytes(Charsets.UTF_8));
        return new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8);
    } catch (java.io.IOException e) {
        log.error("Error invoking Authorization Server", e);
        throw new MSSSecurityException(SecurityErrorCode.GENERIC_ERROR, "Error invoking Authorization Server",
                e);
    }
}