diff options
Diffstat (limited to 'src/main/java/org/traccar/protocol/GlobalstarProtocolDecoder.java')
-rw-r--r-- | src/main/java/org/traccar/protocol/GlobalstarProtocolDecoder.java | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/src/main/java/org/traccar/protocol/GlobalstarProtocolDecoder.java b/src/main/java/org/traccar/protocol/GlobalstarProtocolDecoder.java index 5739a1224..382509793 100644 --- a/src/main/java/org/traccar/protocol/GlobalstarProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/GlobalstarProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2019 - 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. @@ -17,28 +17,41 @@ package org.traccar.protocol; import com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufOutputStream; 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.HttpHeaderNames; import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpVersion; import org.traccar.BaseHttpProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; import org.traccar.Protocol; import org.traccar.helper.DataConverter; import org.traccar.model.Position; import org.w3c.dom.Document; +import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import java.net.SocketAddress; +import java.text.SimpleDateFormat; import java.util.Date; import java.util.LinkedList; import java.util.List; @@ -67,6 +80,40 @@ public class GlobalstarProtocolDecoder extends BaseHttpProtocolDecoder { } } + private void sendResponse(Channel channel, String messageId) throws TransformerException { + + Document document = documentBuilder.newDocument(); + Element rootElement = document.createElement("stuResponseMsg"); + rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); + rootElement.setAttribute( + "xsi:noNamespaceSchemaLocation", "http://cody.glpconnect.com/XSD/StuResponse_Rev1_0.xsd"); + rootElement.setAttribute("deliveryTimeStamp", new SimpleDateFormat("dd/MM/yyyy hh:mm:ss z").format(new Date())); + rootElement.setAttribute("messageID", "00000000000000000000000000000000"); + rootElement.setAttribute("correlationID", messageId); + document.appendChild(rootElement); + + Element state = document.createElement("state"); + state.appendChild(document.createTextNode("pass")); + rootElement.appendChild(state); + + Element stateMessage = document.createElement("stateMessage"); + stateMessage.appendChild(document.createTextNode("Messages received and stored successfully")); + rootElement.appendChild(stateMessage); + + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + ByteBuf content = Unpooled.buffer(); + transformer.transform(new DOMSource(document), new StreamResult(new ByteBufOutputStream(content))); + + if (channel != null) { + FullHttpResponse response = new DefaultFullHttpResponse( + HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content); + response.headers() + .add(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes()) + .add(HttpHeaderNames.CONTENT_TYPE, "text/xml"); + channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); + } + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -112,7 +159,7 @@ public class GlobalstarProtocolDecoder extends BaseHttpProtocolDecoder { } } - sendResponse(channel, HttpResponseStatus.OK); + sendResponse(channel, document.getFirstChild().getAttributes().getNamedItem("messageID").getNodeValue()); return positions; } |