aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-03-15 16:13:52 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2020-03-15 16:13:52 -0700
commit83494fe445a9c7cb4f6b7de1e97e0d257f8bc7c2 (patch)
tree54dfa2aba776ae6d6a2a19b4882f3ad6bd6729c7 /src/main/java
parent606afeb37451f921fe3aecffab439cc2bcc7a175 (diff)
downloadtrackermap-server-83494fe445a9c7cb4f6b7de1e97e0d257f8bc7c2.tar.gz
trackermap-server-83494fe445a9c7cb4f6b7de1e97e0d257f8bc7c2.tar.bz2
trackermap-server-83494fe445a9c7cb4f6b7de1e97e0d257f8bc7c2.zip
Support HTTP response content
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/traccar/BaseHttpProtocolDecoder.java15
-rw-r--r--src/main/java/org/traccar/protocol/LeafSpyProtocolDecoder.java7
-rw-r--r--src/main/java/org/traccar/protocol/PiligrimProtocolDecoder.java17
3 files changed, 17 insertions, 22 deletions
diff --git a/src/main/java/org/traccar/BaseHttpProtocolDecoder.java b/src/main/java/org/traccar/BaseHttpProtocolDecoder.java
index 57a68acac..b762be12c 100644
--- a/src/main/java/org/traccar/BaseHttpProtocolDecoder.java
+++ b/src/main/java/org/traccar/BaseHttpProtocolDecoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
*/
package org.traccar;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
@@ -29,9 +31,16 @@ public abstract class BaseHttpProtocolDecoder extends BaseProtocolDecoder {
}
public void sendResponse(Channel channel, HttpResponseStatus status) {
+ sendResponse(channel, status, null);
+ }
+
+ public void sendResponse(Channel channel, HttpResponseStatus status, ByteBuf buf) {
if (channel != null) {
- HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
- response.headers().add(HttpHeaderNames.CONTENT_LENGTH, 0);
+ if (buf == null) {
+ buf = Unpooled.buffer(0);
+ }
+ HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, buf);
+ response.headers().add(HttpHeaderNames.CONTENT_LENGTH, buf.readableBytes());
channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
}
}
diff --git a/src/main/java/org/traccar/protocol/LeafSpyProtocolDecoder.java b/src/main/java/org/traccar/protocol/LeafSpyProtocolDecoder.java
index 5b352a961..8b47701fd 100644
--- a/src/main/java/org/traccar/protocol/LeafSpyProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/LeafSpyProtocolDecoder.java
@@ -122,13 +122,8 @@ public class LeafSpyProtocolDecoder extends BaseHttpProtocolDecoder {
}
if (position.getDeviceId() != 0) {
- if (channel != null) {
- HttpResponse response = new DefaultFullHttpResponse(
- HttpVersion.HTTP_1_1,
- HttpResponseStatus.OK,
+ sendResponse(channel, HttpResponseStatus.OK,
Unpooled.copiedBuffer("\"status\":\"0\"", StandardCharsets.US_ASCII));
- channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
- }
return position;
} else {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
diff --git a/src/main/java/org/traccar/protocol/PiligrimProtocolDecoder.java b/src/main/java/org/traccar/protocol/PiligrimProtocolDecoder.java
index 47aa86da7..26ce2fe53 100644
--- a/src/main/java/org/traccar/protocol/PiligrimProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/PiligrimProtocolDecoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2014 - 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2014 - 2020 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,15 +18,11 @@ package org.traccar.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
-import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
-import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
-import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.QueryStringDecoder;
-import org.traccar.BaseProtocolDecoder;
+import org.traccar.BaseHttpProtocolDecoder;
import org.traccar.DeviceSession;
-import org.traccar.NetworkMessage;
import org.traccar.Protocol;
import org.traccar.helper.BitUtil;
import org.traccar.helper.DateBuilder;
@@ -37,19 +33,14 @@ import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
-public class PiligrimProtocolDecoder extends BaseProtocolDecoder {
+public class PiligrimProtocolDecoder extends BaseHttpProtocolDecoder {
public PiligrimProtocolDecoder(Protocol protocol) {
super(protocol);
}
private void sendResponse(Channel channel, String message) {
- if (channel != null) {
- FullHttpResponse response = new DefaultFullHttpResponse(
- HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
- Unpooled.copiedBuffer(message, StandardCharsets.US_ASCII));
- channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
- }
+ sendResponse(channel, HttpResponseStatus.OK, Unpooled.copiedBuffer(message, StandardCharsets.US_ASCII));
}
public static final int MSG_GPS = 0xF1;