Search in sources :

Example 36 with DefaultLastHttpContent

use of io.netty.handler.codec.http.DefaultLastHttpContent in project proxyee-down by monkeyWie.

the class ResponseTextIntercept method afterResponse.

@Override
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpContent httpContent, HttpProxyInterceptPipeline pipeline) throws Exception {
    if (isMatch) {
        try {
            contentBuf.writeBytes(httpContent.content());
            if (httpContent instanceof LastHttpContent) {
                ByteUtil.insertText(contentBuf, ByteUtil.findText(contentBuf, "<head>"), hookResponse(), Charset.forName("UTF-8"));
                HttpContent hookHttpContent = new DefaultLastHttpContent();
                if (isGzip) {
                    // 转化成gzip编码
                    byte[] temp = new byte[contentBuf.readableBytes()];
                    contentBuf.readBytes(temp);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    GZIPOutputStream outputStream = new GZIPOutputStream(baos);
                    outputStream.write(temp);
                    outputStream.finish();
                    hookHttpContent.content().writeBytes(baos.toByteArray());
                } else {
                    hookHttpContent.content().writeBytes(contentBuf);
                }
                ReferenceCountUtil.release(contentBuf);
                pipeline.getDefault().afterResponse(clientChannel, proxyChannel, hookHttpContent, pipeline);
            }
        } finally {
            ReferenceCountUtil.release(httpContent);
        }
    } else {
        pipeline.afterResponse(clientChannel, proxyChannel, httpContent);
    }
}
Also used : DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 37 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 38 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 39 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 40 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)

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