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);
}
}
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);
}
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;
}
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);
}
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);
}
Aggregations