Skip to content

Commit

Permalink
Initial implementation of CoT -> Mcs in ReceiverPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
keith.williams committed Mar 18, 2021
1 parent 2e83e50 commit b665e81
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,16 @@ public void onMessage(Message message) {

if(CoTMcsConverter.messageFromSender(message)) {
if (VerboseLogging)
_logger.info("Message is from TAK Plugin");
_logger.info("Message originated from TAK Plugin");
//Bail - TODO it would be nice maybe to check flowtags or something else
return;
}

if(CoTMcsConverter.messageIsPing(message)){
if (VerboseLogging)
_logger.info("Ping message from TAK Client, skipping");
return;
}

_messageProducer.AddMessage(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.slf4j.LoggerFactory;

import tak.server.plugins.processing.*;
import tak.server.plugins.utilities.McsCoTConverter;
import tak.server.plugins.PluginConfiguration;
import tak.server.plugins.McsSenderPlugin;

Expand Down Expand Up @@ -62,6 +63,10 @@ public void SetupConsumption(MessageProducer producer, PluginConfiguration confi
String topic = delivery.getEnvelope().getRoutingKey();
if (McsSenderPlugin.VerboseLogging)
logger.info("Msg Received '" + topic + "':'" + message + "'");

if (McsCoTConverter.messageIsFromPlugin(message))
return;

_producer.AddMessage(topic, message);
};

Expand Down Expand Up @@ -93,6 +98,9 @@ public void publishEntityMessage(String message)
if (!_channel.isOpen()) return;

try {
if (McsSenderPlugin.VerboseLogging)
logger.info("Publishing to " + _entityRoutingKey + " message: " + message);

byte[] entityBytes = message.getBytes(StandardCharsets.UTF_8);
_channel.basicPublish(_exchangeName, _entityRoutingKey, null, entityBytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

public class CoTMcsConverter {
private static final Logger _logger = LoggerFactory.getLogger(CoTMcsConverter.class);
public static final String FROM_TAK = "fromTAK";

public static EntityDto convertToEntityDto(Message message) {
TakMessage takMessage = message.getPayload();
Expand Down Expand Up @@ -53,30 +54,21 @@ public static EntityDto convertToEntityDto(Message message) {
return EntityDto;
}

public static Boolean messageFromSender(Message message) {
Boolean fromSender = false;
try {
TakMessage takMessage = message.getPayload();
CotEvent cotEvent = takMessage.getCotEvent();
fromSender = cotEvent.getDetail().getXmlDetail().contains(McsCoTConverter.FROM_MCS);
} catch (Exception e) {
//Do Nothing
}
return fromSender;
}

public static String convertToJson(EntityDto EntityDto) {
Gson gson = new Gson();
JsonElement element = JsonParser.parseString(gson.toJson(EntityDto));

//Using org.json here for convenient xml-> serialization
JSONObject detailDataJsonObject = XML.toJSONObject(EntityDto.getDetail());
detailDataJsonObject.put(FROM_TAK, "true");
String detailJson = detailDataJsonObject.toString();

//Back to gson
JsonElement detailJobject = JsonParser.parseString(detailJson);
JsonObject jObject = element.getAsJsonObject();
jObject.add("detail", detailJobject);



return jObject.toString();
}
Expand All @@ -86,7 +78,7 @@ private static String convertTime(Long time) {
// 1970-01-01 00:00:00 UTC
String timeIso = "1970-01-01 00:00:00 UTC";
try {
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.from(ZoneOffset.UTC));
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
Instant instant = Instant.ofEpochMilli( time );
timeIso = formatter.format( instant );
} catch (Exception e) {
Expand All @@ -95,4 +87,28 @@ private static String convertTime(Long time) {

return timeIso;
}

public static Boolean messageFromSender(Message message) {
Boolean fromSender = false;
try {
TakMessage takMessage = message.getPayload();
CotEvent cotEvent = takMessage.getCotEvent();
fromSender = cotEvent.getDetail().getXmlDetail().contains(McsCoTConverter.FROM_MCS);
} catch (Exception e) {
//Do Nothing
}
return fromSender;
}

public static Boolean messageIsPing(Message message) {
Boolean isPing = false;
try {
TakMessage takMessage = message.getPayload();
CotEvent cotEvent = takMessage.getCotEvent();
isPing = cotEvent.getUid().contains("ping");
} catch (Exception e) {
//Do Nothing
}
return isPing;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ private static Long convertTime(String time) {

return timeMs;
}

public static Boolean messageIsFromPlugin(String message) {
return message.contains(CoTMcsConverter.FROM_TAK);
}
}

0 comments on commit b665e81

Please sign in to comment.