Search in sources :

Example 86 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.

the class SpotProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    HttpRequest request = (HttpRequest) msg;
    Document document = documentBuilder.parse(new ByteBufferBackedInputStream(request.getContent().toByteBuffer()));
    NodeList nodes = (NodeList) messageExpression.evaluate(document, XPathConstants.NODESET);
    List<Position> positions = new LinkedList<>();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, xPath.evaluate("esnName", node));
        if (deviceSession != null) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());
            position.setValid(true);
            position.setTime(DateUtil.parseDate(xPath.evaluate("timestamp", node)));
            position.setLatitude(Double.parseDouble(xPath.evaluate("latitude", node)));
            position.setLongitude(Double.parseDouble(xPath.evaluate("longitude", node)));
            position.set(Position.KEY_EVENT, xPath.evaluate("messageType", node));
            positions.add(position);
        }
    }
    sendResponse(channel, HttpResponseStatus.OK);
    return positions;
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) ByteBufferBackedInputStream(com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream)

Example 87 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project traccar by traccar.

the class FlespiProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    HttpRequest request = (HttpRequest) msg;
    JsonArray result = Json.createReader(new StringReader(request.getContent().toString(StandardCharsets.UTF_8))).readArray();
    List<Position> positions = new LinkedList<>();
    for (int i = 0; i < result.size(); i++) {
        JsonObject message = result.getJsonObject(i);
        JsonString ident = message.getJsonString("ident");
        if (ident == null) {
            continue;
        }
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident.getString());
        if (deviceSession == null) {
            continue;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        decodePosition(message, position);
        positions.add(position);
    }
    sendResponse(channel, HttpResponseStatus.OK);
    return positions;
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) JsonArray(javax.json.JsonArray) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) LinkedList(java.util.LinkedList)

Example 88 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project opentsdb by OpenTSDB.

the class TestRpcHandler method httpOptionsCORSPublic.

@Test
public void httpOptionsCORSPublic() {
    final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "/api/v1/version");
    req.headers().add(HttpHeaders.ORIGIN, "42.com");
    handleHttpRpc(req, new Answer<ChannelFuture>() {

        public ChannelFuture answer(final InvocationOnMock args) throws Throwable {
            DefaultHttpResponse response = (DefaultHttpResponse) args.getArguments()[0];
            assertEquals(HttpResponseStatus.OK, response.getStatus());
            assertEquals("42.com", response.headers().get(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
            return null;
        }
    });
    tsdb.getConfig().overrideConfig("tsd.http.request.cors_domains", "*");
    final RpcHandler rpc = new RpcHandler(tsdb, rpc_manager);
    rpc.messageReceived(ctx, message);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) SucceededChannelFuture(org.jboss.netty.channel.SucceededChannelFuture) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 89 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project opentsdb by OpenTSDB.

the class TestRpcHandler method httpCORSSpecificSimple.

@Test
public void httpCORSSpecificSimple() {
    final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/api/v1/version");
    req.headers().add(HttpHeaders.ORIGIN, "42.com");
    handleHttpRpc(req, new Answer<ChannelFuture>() {

        public ChannelFuture answer(final InvocationOnMock args) throws Throwable {
            DefaultHttpResponse response = (DefaultHttpResponse) args.getArguments()[0];
            assertEquals(HttpResponseStatus.OK, response.getStatus());
            assertEquals("42.com", response.headers().get(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
            return null;
        }
    });
    tsdb.getConfig().overrideConfig("tsd.http.request.cors_domains", "aurther.com,dent.net,42.com,beeblebrox.org");
    final RpcHandler rpc = new RpcHandler(tsdb, rpc_manager);
    rpc.messageReceived(ctx, message);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) SucceededChannelFuture(org.jboss.netty.channel.SucceededChannelFuture) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 90 with HttpRequest

use of org.jboss.netty.handler.codec.http.HttpRequest in project opentsdb by OpenTSDB.

the class TestRpcHandler method httpCORSPublicSimple.

@Test
public void httpCORSPublicSimple() {
    final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/api/v1/version");
    req.headers().add(HttpHeaders.ORIGIN, "42.com");
    handleHttpRpc(req, new Answer<ChannelFuture>() {

        public ChannelFuture answer(final InvocationOnMock args) throws Throwable {
            DefaultHttpResponse response = (DefaultHttpResponse) args.getArguments()[0];
            assertEquals(HttpResponseStatus.OK, response.getStatus());
            assertEquals("42.com", response.headers().get(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
            return null;
        }
    });
    tsdb.getConfig().overrideConfig("tsd.http.request.cors_domains", "*");
    final RpcHandler rpc = new RpcHandler(tsdb, rpc_manager);
    rpc.messageReceived(ctx, message);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) SucceededChannelFuture(org.jboss.netty.channel.SucceededChannelFuture) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)136 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)69 Channel (org.jboss.netty.channel.Channel)47 Test (org.junit.Test)47 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)46 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)43 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)31 ChannelFuture (org.jboss.netty.channel.ChannelFuture)26 List (java.util.List)19 ArrayList (java.util.ArrayList)17 Map (java.util.Map)17 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)17 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)17 InetSocketAddress (java.net.InetSocketAddress)14 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)13 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)13 SimpleHttpResponseHandler (com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler)12 SimpleTestHttpClient (com.linkedin.databus.core.test.netty.SimpleTestHttpClient)12 Configuration (org.apache.hadoop.conf.Configuration)12 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)12