use of org.jboss.netty.buffer.ChannelBufferOutputStream in project NabAlive by jcheype.
the class HttpApiServerHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) {
HttpRequest httpRequest = (HttpRequest) event.getMessage();
logger.debug("Request: {}", httpRequest);
QueryStringDecoder qs = new QueryStringDecoder(httpRequest.getUri());
final Request request = new Request(ctx, httpRequest, qs);
final Response response = new Response(ctx, httpRequest);
try {
execBeforeFilters(request, response);
execHttpMethod(request, response);
execAfterFilters(request, response);
} catch (HttpException e) {
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, e.getStatus());
ctx.getChannel().write(httpResponse).addListener(ChannelFutureListener.CLOSE);
} catch (Exception e) {
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
if (logger.isDebugEnabled()) {
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
PrintWriter printWriter = new PrintWriter(new ChannelBufferOutputStream(channelBuffer));
e.printStackTrace(printWriter);
printWriter.close();
httpResponse.setContent(channelBuffer);
}
logger.error("web server error: ", e);
ctx.getChannel().write(httpResponse).addListener(ChannelFutureListener.CLOSE);
}
}
use of org.jboss.netty.buffer.ChannelBufferOutputStream in project NabAlive by jcheype.
the class HttpApiServerHandler method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
if (e.getCause() instanceof ClosedChannelException) {
return;
}
String errorid = UUID.randomUUID().toString();
logger.error("ERROR HTTP: {}\n", errorid, e.getCause());
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
httpResponse.setHeader("id", errorid);
if (logger.isDebugEnabled()) {
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
PrintWriter printWriter = new PrintWriter(new ChannelBufferOutputStream(channelBuffer));
e.getCause().printStackTrace(printWriter);
printWriter.close();
httpResponse.setContent(channelBuffer);
}
ctx.getChannel().write(httpResponse).addListener(ChannelFutureListener.CLOSE);
Channel ch = e.getChannel();
ch.close();
}
use of org.jboss.netty.buffer.ChannelBufferOutputStream in project MSEC by Tencent.
the class ResponseEncoder method encode.
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
if (msg instanceof RpcResponse) {
RpcResponse message = (RpcResponse) msg;
try {
ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.dynamicBuffer(estimatedLength, ctx.getChannel().getConfig().getBufferFactory()));
ChannelBuffer buffer;
if (message.getSerializeMode() == RpcRequest.SerializeMode.SERIALIZE_MODE_PROTOBUF) {
buffer = serializeProtobufPakcage(ctx, message, bout);
} else {
buffer = serializeHTTPPakcage(ctx, message, bout);
}
return buffer;
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
throw ex;
}
}
return null;
}
use of org.jboss.netty.buffer.ChannelBufferOutputStream in project MSEC by Tencent.
the class NettyClient method sendRequestAndWaitResponse.
// ��RPC��
public RpcResponse sendRequestAndWaitResponse(CustomPackageLengthChecker lengthChecker, byte[] requestData, RpcRequest request, int timeoutMillis) {
RpcResponse response = null;
ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.dynamicBuffer(1024));
Object[] objects = new Object[3];
objects[0] = requestData;
objects[1] = lengthChecker;
objects[2] = new Long(request.getSeq());
if (sendRequest(objects) == null)
return null;
CallbackFuture callback = new CallbackFuture(request, this);
sessions.put(request.getSeq(), callback);
try {
response = callback.getResponse(timeoutMillis);
} catch (Exception ex) {
// TODO: set exception
System.out.println("Exception occurs: " + ex);
response = new RpcResponse();
response.setErrno(-1);
response.setError(ex);
} finally {
sessions.remove(request.getSeq());
}
return response;
}
use of org.jboss.netty.buffer.ChannelBufferOutputStream in project MSEC by Tencent.
the class RequestEncoder method encode.
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
if (msg instanceof RpcRequest) {
RpcRequest message = (RpcRequest) msg;
try {
ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.dynamicBuffer(estimatedLength, ctx.getChannel().getConfig().getBufferFactory()));
// Set proto flag = 0
NettyCodecUtils.setAttachment(ctx, Constants.ATTACHMENT_CUSTOM_PROTO_FLAG, new Integer(0));
ChannelBuffer ret = encode(ctx, message, bout);
return ret;
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
throw ex;
}
}
if (msg instanceof Object[]) {
Object[] objects = (Object[]) msg;
// Set proto flag=1
NettyCodecUtils.setAttachment(ctx, Constants.ATTACHMENT_CUSTOM_PROTO_FLAG, new Integer(1));
NettyCodecUtils.setAttachment(ctx, Constants.ATTACHMENT_CUSTOM_PROTO_CODEC, objects[1]);
NettyCodecUtils.setAttachment(ctx, Constants.ATTACHMENT_CUSTOM_PROTO_SEQUENCE, objects[2]);
if (objects[0] instanceof byte[]) {
byte[] msgData = (byte[]) objects[0];
int tmpLength = estimatedLength;
if (msgData.length > tmpLength) {
tmpLength = msgData.length;
}
ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.dynamicBuffer(tmpLength, ctx.getChannel().getConfig().getBufferFactory()));
bout.write(msgData);
return bout.buffer();
} else {
return objects[0];
}
}
return null;
}
Aggregations