Search in sources :

Example 6 with ChannelBufferOutputStream

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);
    }
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) HttpException(com.nabalive.framework.web.exception.HttpException) HttpException(com.nabalive.framework.web.exception.HttpException) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) PrintWriter(java.io.PrintWriter)

Example 7 with ChannelBufferOutputStream

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();
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) PrintWriter(java.io.PrintWriter)

Example 8 with ChannelBufferOutputStream

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;
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) RpcResponse(org.msec.rpc.RpcResponse) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 9 with ChannelBufferOutputStream

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;
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) IOException(java.io.IOException)

Example 10 with ChannelBufferOutputStream

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;
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) RpcRequest(org.msec.rpc.RpcRequest) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

ChannelBufferOutputStream (org.jboss.netty.buffer.ChannelBufferOutputStream)19 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)12 IOException (java.io.IOException)5 OutputStream (java.io.OutputStream)3 TaskMessage (backtype.storm.messaging.TaskMessage)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 Callback (com.stumbleupon.async.Callback)2 Deferred (com.stumbleupon.async.Deferred)2 PrintWriter (java.io.PrintWriter)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 ArrayList (java.util.ArrayList)2 TSSubQuery (net.opentsdb.core.TSSubQuery)2 MessageEvent (org.jboss.netty.channel.MessageEvent)2 DbusEventV1Factory (com.linkedin.databus.core.DbusEventV1Factory)1 StreamEventsArgs (com.linkedin.databus.core.StreamEventsArgs)1 HttpException (com.nabalive.framework.web.exception.HttpException)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1