Search in sources :

Example 51 with DefaultLastHttpContent

use of io.netty.handler.codec.http.DefaultLastHttpContent in project ballerina by ballerina-lang.

the class HttpResourceDispatcher method handleOptionsRequest.

private static void handleOptionsRequest(HTTPCarbonMessage cMsg, HttpService service) throws URITemplateException {
    HTTPCarbonMessage response = HttpUtil.createHttpCarbonMessage(false);
    if (cMsg.getHeader(HttpHeaderNames.ALLOW.toString()) != null) {
        response.setHeader(HttpHeaderNames.ALLOW.toString(), cMsg.getHeader(HttpHeaderNames.ALLOW.toString()));
    } else if (service.getBasePath().equals(cMsg.getProperty(HttpConstants.TO)) && !service.getAllAllowedMethods().isEmpty()) {
        response.setHeader(HttpHeaderNames.ALLOW.toString(), DispatcherUtil.concatValues(service.getAllAllowedMethods(), false));
    } else {
        cMsg.setProperty(HttpConstants.HTTP_STATUS_CODE, 404);
        throw new BallerinaConnectorException("no matching resource found for path : " + cMsg.getProperty(HttpConstants.TO) + " , method : " + "OPTIONS");
    }
    CorsHeaderGenerator.process(cMsg, response, false);
    response.setProperty(HttpConstants.HTTP_STATUS_CODE, 200);
    response.addHttpContent(new DefaultLastHttpContent());
    HttpUtil.sendOutboundResponse(cMsg, response);
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 52 with DefaultLastHttpContent

use of io.netty.handler.codec.http.DefaultLastHttpContent in project ballerina by ballerina-lang.

the class HttpUtil method createErrorMessage.

public static HTTPCarbonMessage createErrorMessage(String payload, int statusCode) {
    HTTPCarbonMessage response = HttpUtil.createHttpCarbonMessage(false);
    response.waitAndReleaseAllEntities();
    if (payload != null) {
        payload = lowerCaseTheFirstLetter(payload);
        response.addHttpContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer(payload.getBytes())));
    } else {
        response.addHttpContent(new DefaultLastHttpContent());
    }
    setHttpStatusCodes(statusCode, response);
    return response;
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 53 with DefaultLastHttpContent

use of io.netty.handler.codec.http.DefaultLastHttpContent in project ballerina by ballerina-lang.

the class ServiceTest method testGetStringAfterSetString.

@Test(description = "Test getString after setting string")
public void testGetStringAfterSetString() {
    List<Header> headers = new ArrayList<Header>();
    headers.add(new Header("Content-Type", TEXT_PLAIN));
    HTTPTestRequest setStringrequestMsg = MessageUtils.generateHTTPMessage("/echo/setString", "POST", headers, null);
    String stringresponseMsgPayload = "hello";
    setStringrequestMsg.waitAndReleaseAllEntities();
    setStringrequestMsg.addHttpContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer(stringresponseMsgPayload.getBytes())));
    Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, setStringrequestMsg);
    HTTPTestRequest requestMsg = MessageUtils.generateHTTPMessage("/echo/getString", "GET");
    HTTPCarbonMessage responseMsg = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, requestMsg);
    Assert.assertNotNull(responseMsg);
    String responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(responseMsg).getInputStream());
    Assert.assertNotNull(responseMsgPayload);
    Assert.assertEquals(responseMsgPayload, stringresponseMsgPayload);
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Header(org.wso2.carbon.messaging.Header) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) ArrayList(java.util.ArrayList) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 54 with DefaultLastHttpContent

use of io.netty.handler.codec.http.DefaultLastHttpContent in project ballerina by ballerina-lang.

the class ServiceTest method testSetString.

@Test
public void testSetString() {
    List<Header> headers = new ArrayList<Header>();
    headers.add(new Header("Content-Type", TEXT_PLAIN));
    HTTPTestRequest requestMsg = MessageUtils.generateHTTPMessage("/echo/setString", "POST", headers, null);
    requestMsg.waitAndReleaseAllEntities();
    requestMsg.addHttpContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer("hello".getBytes())));
    HTTPCarbonMessage responseMsg = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, requestMsg);
    Assert.assertNotNull(responseMsg);
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Header(org.wso2.carbon.messaging.Header) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) ArrayList(java.util.ArrayList) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 55 with DefaultLastHttpContent

use of io.netty.handler.codec.http.DefaultLastHttpContent in project netty by netty.

the class HttpPostMultipartRequestDecoderBenchmark method testHighNumberChunks.

public double testHighNumberChunks(boolean big, boolean noDisk) {
    String BOUNDARY = "01f136d9282f";
    int size = 8 * 1024;
    int chunkNumber = 64;
    StringBuilder stringBuilder = new StringBuilder(size);
    stringBuilder.setLength(size);
    String data = stringBuilder.toString();
    byte[] bodyStartBytes = ("--" + BOUNDARY + "\n" + "Content-Disposition: form-data; name=\"msg_id\"\n\n15200\n--" + BOUNDARY + "\nContent-Disposition: form-data; name=\"msg1\"; filename=\"file1.txt\"\n\n" + data).getBytes(CharsetUtil.UTF_8);
    byte[] bodyPartBigBytes = data.getBytes(CharsetUtil.UTF_8);
    byte[] intermediaryBytes = ("\n--" + BOUNDARY + "\nContent-Disposition: form-data; name=\"msg2\"; filename=\"file2.txt\"\n\n" + data).getBytes(CharsetUtil.UTF_8);
    byte[] finalBigBytes = ("\n" + "--" + BOUNDARY + "--\n").getBytes(CharsetUtil.UTF_8);
    ByteBuf firstBuf = Unpooled.wrappedBuffer(bodyStartBytes);
    ByteBuf finalBuf = Unpooled.wrappedBuffer(finalBigBytes);
    ByteBuf nextBuf;
    if (big) {
        nextBuf = Unpooled.wrappedBuffer(bodyPartBigBytes);
    } else {
        nextBuf = Unpooled.wrappedBuffer(intermediaryBytes);
    }
    DefaultHttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.POST, "/up");
    req.headers().add(HttpHeaderNames.CONTENT_TYPE, "multipart/form-data; boundary=" + BOUNDARY);
    long start = System.nanoTime();
    DefaultHttpDataFactory defaultHttpDataFactory = new DefaultHttpDataFactory(noDisk ? 1024 * 1024 : 16 * 1024);
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(defaultHttpDataFactory, req);
    firstBuf.retain();
    decoder.offer(new DefaultHttpContent(firstBuf));
    firstBuf.release();
    for (int i = 1; i < chunkNumber; i++) {
        nextBuf.retain();
        decoder.offer(new DefaultHttpContent(nextBuf));
        nextBuf.release();
        nextBuf.readerIndex(0);
    }
    finalBuf.retain();
    decoder.offer(new DefaultLastHttpContent(finalBuf));
    finalBuf.release();
    while (decoder.hasNext()) {
        InterfaceHttpData httpData = decoder.next();
    }
    while (finalBuf.refCnt() > 0) {
        finalBuf.release();
    }
    while (nextBuf.refCnt() > 0) {
        nextBuf.release();
    }
    while (finalBuf.refCnt() > 0) {
        finalBuf.release();
    }
    long stop = System.nanoTime();
    double time = (stop - start) / 1000000.0;
    defaultHttpDataFactory.cleanAllHttpData();
    defaultHttpDataFactory.cleanRequestHttpData(req);
    decoder.destroy();
    return time;
}
Also used : DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(io.netty.buffer.ByteBuf) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest)

Aggregations

DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)73 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)32 Test (org.junit.Test)27 ByteBuf (io.netty.buffer.ByteBuf)26 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)26 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)24 HttpContent (io.netty.handler.codec.http.HttpContent)17 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)12 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)11 HttpResponse (io.netty.handler.codec.http.HttpResponse)11 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)10 Test (org.junit.jupiter.api.Test)10 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)7 Channel (io.netty.channel.Channel)6 Test (org.testng.annotations.Test)6 SessionContext (com.netflix.zuul.context.SessionContext)5 JsonObjectDecoder (io.netty.handler.codec.json.JsonObjectDecoder)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5