Search in sources :

Example 51 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project NabAlive by jcheype.

the class RecordController method init.

@PostConstruct
void init() {
    restHandler.post(new Route("/vl/record.jsp") {

        @Override
        public void handle(Request request, Response response, Map<String, String> map) throws Exception {
            String mac = checkNotNull(request.getParam("sn")).toLowerCase();
            if (!connectionManager.containsKey(mac))
                throw new HttpException(HttpResponseStatus.NOT_FOUND, "sn is not connected");
            Nabaztag nabaztag = checkNotNull(nabaztagDAO.findOne("macAddress", mac));
            ChannelBuffer content = request.request.getContent();
            logger.debug("record orig size: {}", content.readableBytes());
            ChannelBufferInputStream inputStream = new ChannelBufferInputStream(content);
            TmpData sound = new TmpData();
            sound.setData(ByteStreams.toByteArray(inputStream));
            tmpDataDAO.save(sound, WriteConcern.SAFE);
            String host = request.request.getHeader("Host");
            String url = "http://" + host + "/record/" + sound.getId().toString();
            logger.debug("sound url: {}", url);
            final String command = "ST " + url + "\nMW\n";
            Query<Nabaztag> query = nabaztagDAO.createQuery();
            query.filter("subscribe.objectId", nabaztag.getId().toString());
            List<Nabaztag> nabaztags = nabaztagDAO.find(query).asList();
            logger.debug("sending to {} subscribers", nabaztags.size());
            for (Nabaztag nab : nabaztags) {
                if (connectionManager.containsKey(nab.getMacAddress())) {
                    final Nabaztag nabTmp = nab;
                    Runnable runnable = new Runnable() {

                        @Override
                        public void run() {
                            logger.debug("sending to {}", nabTmp.getMacAddress());
                            logger.debug("command {}", command);
                            messageService.sendMessage(nabTmp.getMacAddress(), command);
                        }
                    };
                    ses.schedule(runnable, 500, TimeUnit.MILLISECONDS);
                }
            }
            response.write("ok");
        }
    }).get(new Route("/record/:recordId") {

        @Override
        public void handle(Request request, Response response, Map<String, String> map) throws Exception {
            ObjectId recordId = new ObjectId(checkNotNull(map.get("recordId")));
            TmpData sound = checkNotNull(tmpDataDAO.get(recordId));
            response.write(sound.getData());
        }
    });
}
Also used : ObjectId(org.bson.types.ObjectId) Request(com.nabalive.framework.web.Request) HttpException(com.nabalive.framework.web.exception.HttpException) ExecutionException(java.util.concurrent.ExecutionException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Response(com.nabalive.framework.web.Response) Nabaztag(com.nabalive.data.core.model.Nabaztag) TmpData(com.nabalive.data.core.model.TmpData) HttpException(com.nabalive.framework.web.exception.HttpException) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) Map(java.util.Map) Route(com.nabalive.framework.web.Route) PostConstruct(javax.annotation.PostConstruct)

Example 52 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project NabAlive by jcheype.

the class ChorBuilder method build.

public ChannelBuffer build() {
    ChannelBuffer res = ChannelBuffers.dynamicBuffer();
    res.writeInt(channelBuffer.readableBytes());
    res.writeBytes(channelBuffer);
    res.writeZero(4);
    return res;
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 53 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project NabAlive by jcheype.

the class NabaliveServerHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    final ChannelBuffer messageCB = (ChannelBuffer) e.getMessage();
    String message = messageCB.toString(CharsetUtil.UTF_8);
    final Status status = getStatus(ctx);
    if (status.getJid() != null)
        logger.debug(status.getJid().getUser() + " <<<<<<<<<<< {}", message);
    else
        logger.debug("<<<<<<<<<<< {}", message);
    if (message.startsWith("<?xml ")) {
        onStreamOpen(ctx, e, message);
    } else if (message.startsWith("<auth ")) {
        onAuth(ctx, e, message);
    } else if (message.startsWith("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'")) {
        onResponse(ctx, e, message);
    } else if (message.startsWith("<iq ")) {
        iqHandler.onMessage(ctx, e, status, message);
    } else if (message.startsWith("<presence ")) {
        presenceHandler.onMessage(ctx, e, status, message);
    } else if (message.startsWith("<message ")) {
        messageHandler.onMessage(ctx, e, status, message);
    }
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 54 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project NabAlive by jcheype.

the class BasePacket method getFullData.

public ChannelBuffer getFullData() {
    final ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
    channelBuffer.writeByte(0x7f);
    channelBuffer.writeByte(getType());
    final ChannelBuffer encryptedData = getData();
    int len = encryptedData.readableBytes();
    byte[] lenBytes = new byte[] { (byte) (len >>> 16), (byte) (len >>> 8), (byte) len };
    channelBuffer.writeBytes(lenBytes);
    channelBuffer.writeBytes(encryptedData);
    channelBuffer.writeByte(0xFF);
    return channelBuffer;
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 55 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer 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)

Aggregations

ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)494 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)70 Test (org.junit.Test)67 DeviceSession (org.traccar.DeviceSession)64 Position (org.traccar.model.Position)62 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)61 Test (org.testng.annotations.Test)49 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)48 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)44 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)37 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)34 ChannelFuture (org.jboss.netty.channel.ChannelFuture)28 Checkpoint (com.linkedin.databus.core.Checkpoint)27 ByteBuffer (java.nio.ByteBuffer)27 RecoverablePduException (com.cloudhopper.smpp.type.RecoverablePduException)26 UnrecoverablePduException (com.cloudhopper.smpp.type.UnrecoverablePduException)26 DateBuilder (org.traccar.helper.DateBuilder)26 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 IOException (java.io.IOException)23 ArrayList (java.util.ArrayList)23