use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.
the class NettyHttpRequestHandler method buildHttpResponse.
@SuppressWarnings({ "rawtypes", "unchecked" })
protected FullHttpResponse buildHttpResponse(Response response, boolean keepAlive) throws Exception {
Object value = response.getValue();
byte[] responseBytes = null;
if (value instanceof Message) {
Marshaller marshaller = ProtoUtils.jsonMarshaller((Message) value.getClass().getMethod("getDefaultInstance", null).invoke(null, null));
InputStream is = marshaller.stream(value);
responseBytes = new byte[is.available()];
is.read(responseBytes);
} else {
// TODO not pb
}
FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(responseBytes));
httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/x-www-form-urlencoded");
httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, httpResponse.content().readableBytes());
if (keepAlive) {
httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
} else {
httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
}
return httpResponse;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.
the class NettyHttpRequestHandler method processHttpRequest.
protected void processHttpRequest(ChannelHandlerContext ctx, FullHttpRequest httpRequest) {
FullHttpResponse httpResponse = null;
try {
httpResponse = (FullHttpResponse) messageHandler.handle(serverChannel, httpRequest);
} catch (Exception e) {
LoggerUtil.error("NettyHttpHandler process http request fail.", e);
httpResponse = buildErrorResponse(e.getMessage());
} finally {
httpRequest.content().release();
}
sendResponse(ctx, httpResponse);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.
the class YarMessageHandlerWarpperTest method testHandle.
@Test
public void testHandle() throws Exception {
YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
final YarResponse yarResponse = YarProtocolUtil.buildDefaultErrorResponse("test err", "JSON");
YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {
@Override
public Object handle(Channel channel, Object message) {
AttachmentRequest request = (AttachmentRequest) message;
verifyAttachments(request.getAttachments());
return yarResponse;
}
});
FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
assertNotNull(httpResponse);
assertNotNull(httpResponse.content());
YarResponse retYarResponse = getYarResponse(httpResponse);
assertNotNull(retYarResponse);
assertEquals(yarResponse, retYarResponse);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.
the class YarMessageHandlerWarpperTest method testAbnormal.
@Test
public void testAbnormal() throws Exception {
final String errmsg = "rpc process error";
YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {
@Override
public Object handle(Channel channel, Object message) {
throw new RuntimeException(errmsg);
}
});
// yar协议无法解析
FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(null, uri));
assertNotNull(httpResponse);
assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
YarResponse retYarResponse = getYarResponse(httpResponse);
assertNotNull(retYarResponse);
assertNotNull(retYarResponse.getError());
// yar协议可以正常解析,但后续处理异常
YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
assertNotNull(httpResponse);
assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
retYarResponse = getYarResponse(httpResponse);
assertNotNull(retYarResponse);
assertEquals(errmsg, retYarResponse.getError());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project netty by netty.
the class WebSocketClientHandshaker method processHandshake.
/**
* Process the opening handshake initiated by {@link #handshake}}.
*
* @param channel
* Channel
* @param response
* HTTP response containing the closing handshake details
* @param promise
* the {@link ChannelPromise} to notify once the handshake completes.
* @return future
* the {@link ChannelFuture} which is notified once the handshake completes.
*/
public final ChannelFuture processHandshake(final Channel channel, HttpResponse response, final ChannelPromise promise) {
if (response instanceof FullHttpResponse) {
try {
finishHandshake(channel, (FullHttpResponse) response);
promise.setSuccess();
} catch (Throwable cause) {
promise.setFailure(cause);
}
} else {
ChannelPipeline p = channel.pipeline();
ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
if (ctx == null) {
ctx = p.context(HttpClientCodec.class);
if (ctx == null) {
return promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "an HttpResponseDecoder or HttpClientCodec"));
}
}
// Add aggregator and ensure we feed the HttpResponse so it is aggregated. A limit of 8192 should be more
// then enough for the websockets handshake payload.
//
// TODO: Make handshake work without HttpObjectAggregator at all.
String aggregatorName = "httpAggregator";
p.addAfter(ctx.name(), aggregatorName, new HttpObjectAggregator(8192));
p.addAfter(aggregatorName, "handshaker", new SimpleChannelInboundHandler<FullHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
// Remove ourself and do the actual handshake
ctx.pipeline().remove(this);
try {
finishHandshake(channel, msg);
promise.setSuccess();
} catch (Throwable cause) {
promise.setFailure(cause);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// Remove ourself and fail the handshake promise.
ctx.pipeline().remove(this);
promise.setFailure(cause);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// Fail promise if Channel was closed
if (!promise.isDone()) {
promise.tryFailure(new ClosedChannelException());
}
ctx.fireChannelInactive();
}
});
try {
ctx.fireChannelRead(ReferenceCountUtil.retain(response));
} catch (Throwable cause) {
promise.setFailure(cause);
}
}
return promise;
}
Aggregations