use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project traccar by tananaev.
the class OsmAndProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
FullHttpRequest request = (FullHttpRequest) msg;
QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
Map<String, List<String>> params = decoder.parameters();
if (params.isEmpty()) {
decoder = new QueryStringDecoder(request.content().toString(StandardCharsets.US_ASCII), false);
params = decoder.parameters();
}
Position position = new Position(getProtocolName());
position.setValid(true);
Network network = new Network();
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
for (String value : entry.getValue()) {
switch(entry.getKey()) {
case "id":
case "deviceid":
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
if (deviceSession == null) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
break;
case "valid":
position.setValid(Boolean.parseBoolean(value) || "1".equals(value));
break;
case "timestamp":
try {
long timestamp = Long.parseLong(value);
if (timestamp < Integer.MAX_VALUE) {
timestamp *= 1000;
}
position.setTime(new Date(timestamp));
} catch (NumberFormatException error) {
if (value.contains("T")) {
position.setTime(DateUtil.parseDate(value));
} else {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
position.setTime(dateFormat.parse(value));
}
}
break;
case "lat":
position.setLatitude(Double.parseDouble(value));
break;
case "lon":
position.setLongitude(Double.parseDouble(value));
break;
case "location":
String[] location = value.split(",");
position.setLatitude(Double.parseDouble(location[0]));
position.setLongitude(Double.parseDouble(location[1]));
break;
case "cell":
String[] cell = value.split(",");
if (cell.length > 4) {
network.addCellTower(CellTower.from(Integer.parseInt(cell[0]), Integer.parseInt(cell[1]), Integer.parseInt(cell[2]), Integer.parseInt(cell[3]), Integer.parseInt(cell[4])));
} else {
network.addCellTower(CellTower.from(Integer.parseInt(cell[0]), Integer.parseInt(cell[1]), Integer.parseInt(cell[2]), Integer.parseInt(cell[3])));
}
break;
case "wifi":
String[] wifi = value.split(",");
network.addWifiAccessPoint(WifiAccessPoint.from(wifi[0].replace('-', ':'), Integer.parseInt(wifi[1])));
break;
case "speed":
position.setSpeed(convertSpeed(Double.parseDouble(value), "kn"));
break;
case "bearing":
case "heading":
position.setCourse(Double.parseDouble(value));
break;
case "altitude":
position.setAltitude(Double.parseDouble(value));
break;
case "accuracy":
position.setAccuracy(Double.parseDouble(value));
break;
case "hdop":
position.set(Position.KEY_HDOP, Double.parseDouble(value));
break;
case "batt":
position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(value));
break;
case "driverUniqueId":
position.set(Position.KEY_DRIVER_UNIQUE_ID, value);
break;
default:
try {
position.set(entry.getKey(), Double.parseDouble(value));
} catch (NumberFormatException e) {
switch(value) {
case "true":
position.set(entry.getKey(), true);
break;
case "false":
position.set(entry.getKey(), false);
break;
default:
position.set(entry.getKey(), value);
break;
}
}
break;
}
}
}
if (position.getFixTime() == null) {
position.setTime(new Date());
}
if (network.getCellTowers() != null || network.getWifiAccessPoints() != null) {
position.setNetwork(network);
}
if (position.getLatitude() == 0 && position.getLongitude() == 0) {
getLastLocation(position, position.getDeviceTime());
}
if (position.getDeviceId() != 0) {
String response = null;
CommandsManager commandsManager = Context.getCommandsManager();
if (commandsManager != null) {
for (Command command : commandsManager.readQueuedCommands(position.getDeviceId(), 1)) {
response = command.getString(Command.KEY_DATA);
}
}
if (response != null) {
sendResponse(channel, HttpResponseStatus.OK, Unpooled.copiedBuffer(response, StandardCharsets.UTF_8));
} else {
sendResponse(channel, HttpResponseStatus.OK);
}
return position;
} else {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project traccar by tananaev.
the class PathAwayProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
FullHttpRequest request = (FullHttpRequest) msg;
QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, decoder.parameters().get("UserName").get(0));
if (deviceSession == null) {
return null;
}
Parser parser = new Parser(PATTERN, decoder.parameters().get("LOC").get(0));
if (!parser.matches()) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
position.setValid(true);
position.setLatitude(parser.nextDouble(0));
position.setLongitude(parser.nextDouble(0));
position.setAltitude(parser.nextDouble(0));
position.setSpeed(parser.nextDouble(0));
position.setCourse(parser.nextDouble(0));
if (channel != null) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
channel.writeAndFlush(new NetworkMessage(response, remoteAddress)).addListener(ChannelFutureListener.CLOSE);
}
return position;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project traccar by tananaev.
the class PiligrimProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
FullHttpRequest request = (FullHttpRequest) msg;
String uri = request.uri();
if (uri.startsWith("/config")) {
sendResponse(channel, "CONFIG: OK");
} else if (uri.startsWith("/addlog")) {
sendResponse(channel, "ADDLOG: OK");
} else if (uri.startsWith("/inform")) {
sendResponse(channel, "INFORM: OK");
} else if (uri.startsWith("/bingps")) {
sendResponse(channel, "BINGPS: OK");
QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, decoder.parameters().get("imei").get(0));
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
ByteBuf buf = request.content();
while (buf.readableBytes() > 2) {
// header
buf.readUnsignedByte();
int type = buf.readUnsignedByte();
// length
buf.readUnsignedByte();
if (type == MSG_GPS || type == MSG_GPS_SENSORS) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
DateBuilder dateBuilder = new DateBuilder().setDay(buf.readUnsignedByte()).setMonth(buf.getByte(buf.readerIndex()) & 0x0f).setYear(2010 + (buf.readUnsignedByte() >> 4)).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
position.setTime(dateBuilder.getDate());
double latitude = buf.readUnsignedByte();
latitude += buf.readUnsignedByte() / 60.0;
latitude += buf.readUnsignedByte() / 6000.0;
latitude += buf.readUnsignedByte() / 600000.0;
double longitude = buf.readUnsignedByte();
longitude += buf.readUnsignedByte() / 60.0;
longitude += buf.readUnsignedByte() / 6000.0;
longitude += buf.readUnsignedByte() / 600000.0;
int flags = buf.readUnsignedByte();
if (BitUtil.check(flags, 0)) {
latitude = -latitude;
}
if (BitUtil.check(flags, 1)) {
longitude = -longitude;
}
position.setLatitude(latitude);
position.setLongitude(longitude);
int satellites = buf.readUnsignedByte();
position.set(Position.KEY_SATELLITES, satellites);
position.setValid(satellites >= 3);
position.setSpeed(buf.readUnsignedByte());
double course = buf.readUnsignedByte() << 1;
course += (flags >> 2) & 1;
course += buf.readUnsignedByte() / 100.0;
position.setCourse(course);
if (type == MSG_GPS_SENSORS) {
double power = buf.readUnsignedByte();
power += buf.readUnsignedByte() << 8;
position.set(Position.KEY_POWER, power * 0.01);
double battery = buf.readUnsignedByte();
battery += buf.readUnsignedByte() << 8;
position.set(Position.KEY_BATTERY, battery * 0.01);
buf.skipBytes(6);
}
positions.add(position);
} else if (type == MSG_EVENTS) {
buf.skipBytes(13);
}
}
return positions;
}
return null;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project riposte by Nike-Inc.
the class RequestInfoImplTest method uber_constructor_works_for_valid_values.
@Test
public void uber_constructor_works_for_valid_values() {
// given
String uri = "/some/uri/path/%24foobar%26?notused=blah";
HttpMethod method = HttpMethod.PATCH;
Charset contentCharset = CharsetUtil.US_ASCII;
HttpHeaders headers = new DefaultHttpHeaders().add("header1", "val1").add(HttpHeaders.Names.CONTENT_TYPE, "text/text charset=" + contentCharset.displayName());
QueryStringDecoder queryParams = new QueryStringDecoder(uri + "?foo=bar&secondparam=secondvalue");
Set<Cookie> cookies = new HashSet<>(Arrays.asList(new DefaultCookie("cookie1", "val1"), new DefaultCookie("cookie2", "val2")));
Map<String, String> pathParams = Arrays.stream(new String[][] { { "pathParam1", "val1" }, { "pathParam2", "val2" } }).collect(Collectors.toMap(pair -> pair[0], pair -> pair[1]));
String content = UUID.randomUUID().toString();
byte[] contentBytes = content.getBytes();
LastHttpContent chunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(contentBytes));
chunk.trailingHeaders().add("trailingHeader1", "trailingVal1");
List<HttpContent> contentChunks = Collections.singletonList(chunk);
HttpVersion protocolVersion = HttpVersion.HTTP_1_1;
boolean keepAlive = true;
boolean fullRequest = true;
boolean isMultipart = false;
// when
RequestInfoImpl<?> requestInfo = new RequestInfoImpl<>(uri, method, headers, chunk.trailingHeaders(), queryParams, cookies, pathParams, contentChunks, protocolVersion, keepAlive, fullRequest, isMultipart);
// then
assertThat("getUri should return passed in value", requestInfo.getUri(), is(uri));
assertThat("getPath did not decode as expected", requestInfo.getPath(), is("/some/uri/path/$foobar&"));
assertThat(requestInfo.getMethod(), is(method));
assertThat(requestInfo.getHeaders(), is(headers));
assertThat(requestInfo.getTrailingHeaders(), is(chunk.trailingHeaders()));
assertThat(requestInfo.getQueryParams(), is(queryParams));
assertThat(requestInfo.getCookies(), is(cookies));
assertThat(requestInfo.pathTemplate, nullValue());
assertThat(requestInfo.pathParams, is(pathParams));
assertThat(requestInfo.getRawContentBytes(), is(contentBytes));
assertThat(requestInfo.getRawContent(), is(content));
assertThat(requestInfo.content, nullValue());
assertThat(requestInfo.getContentCharset(), is(contentCharset));
assertThat(requestInfo.getProtocolVersion(), is(protocolVersion));
assertThat(requestInfo.isKeepAliveRequested(), is(keepAlive));
assertThat(requestInfo.isCompleteRequestWithAllChunks, is(fullRequest));
assertThat(requestInfo.isMultipart, is(isMultipart));
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project riposte by Nike-Inc.
the class RequestInfoTest method getQueryParamSingle_returns_null_if_param_value_list_is_null.
@Test
public void getQueryParamSingle_returns_null_if_param_value_list_is_null() {
// given
QueryStringDecoder queryParamsMock = mock(QueryStringDecoder.class);
Map<String, List<String>> params = new HashMap<>();
doReturn(params).when(queryParamsMock).parameters();
RequestInfo<?> requestInfoSpy = getSpy();
doReturn(queryParamsMock).when(requestInfoSpy).getQueryParams();
// when
String value = requestInfoSpy.getQueryParamSingle("foo");
// then
assertThat(value, nullValue());
}
Aggregations