mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-22 21:55:39 +00:00
bug5440: removed gson dependency and replaced it with custom serializers and deserializers
This commit is contained in:
@@ -80,11 +80,6 @@
|
||||
<artifactId>elasticache-java-cluster-client</artifactId>
|
||||
<version>1.1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sap.sailing</groupId>
|
||||
<artifactId>org.json.simple</artifactId>
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.sap.sailing.ingestion;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import com.sap.sailing.ingestion.dto.AWSRequestWrapper;
|
||||
import com.sap.sailing.ingestion.dto.AWSResponseWrapper;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.Helpers;
|
||||
import com.sap.sse.shared.json.JsonDeserializationException;
|
||||
import com.sap.sse.shared.json.JsonDeserializer;
|
||||
import com.sap.sse.shared.json.JsonSerializer;
|
||||
|
||||
import software.amazon.awssdk.utils.IoUtils;
|
||||
|
||||
public class AWSInOutHandler {
|
||||
private final JsonDeserializer<AWSRequestWrapper> awsRequestDeserializer = new AWSRequestJsonDeserializer();
|
||||
private final JsonSerializer<AWSResponseWrapper<String>> awsResponseSerializer = new AWSResponseJsonSerializer<String>();
|
||||
|
||||
public JSONObject parseInputToJson(InputStream inputAsStream)
|
||||
throws IOException, JsonDeserializationException, ParseException {
|
||||
final byte[] streamAsBytes = IoUtils.toByteArray(inputAsStream);
|
||||
final Object awsRequestObject = JSONValue.parseWithException(new String(streamAsBytes));
|
||||
final JSONObject awsRequestJson = Helpers.toJSONObjectSafe(awsRequestObject);
|
||||
final AWSRequestWrapper requestWrapped = awsRequestDeserializer.deserialize(awsRequestJson);
|
||||
final Object requestBody = JSONValue.parseWithException(requestWrapped.getBody());
|
||||
final JSONObject requestObject = Helpers.toJSONObjectSafe(requestBody);
|
||||
return requestObject;
|
||||
}
|
||||
|
||||
public JSONObject createJsonResponse(String response) {
|
||||
final AWSResponseWrapper<String> awsResponseString = AWSResponseWrapper.successResponseAsJson(response);
|
||||
final JSONObject responseObject = awsResponseSerializer.serialize(awsResponseString);
|
||||
return responseObject;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.sap.sailing.ingestion;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.sap.sailing.ingestion.dto.AWSRequestWrapper;
|
||||
import com.sap.sse.shared.json.JsonDeserializationException;
|
||||
import com.sap.sse.shared.json.JsonDeserializer;
|
||||
|
||||
public class AWSRequestJsonDeserializer implements JsonDeserializer<AWSRequestWrapper> {
|
||||
public static final String HTTP_METHOD = "httpMethod";
|
||||
public static final String BODY = "body";
|
||||
public static final String IS_BASE64_ENCODED = "isBase64Encoded";
|
||||
|
||||
@Override
|
||||
public AWSRequestWrapper deserialize(JSONObject object) throws JsonDeserializationException {
|
||||
String httpMethod = String.valueOf(object.get(HTTP_METHOD));
|
||||
String body = String.valueOf(object.get(BODY));
|
||||
Boolean isBase64Encoded = Boolean.parseBoolean(String.valueOf(object.get(IS_BASE64_ENCODED)));
|
||||
AWSRequestWrapper request = new AWSRequestWrapper(httpMethod, body, isBase64Encoded);
|
||||
return request;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.sap.sailing.ingestion;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.sap.sailing.ingestion.dto.AWSResponseHttpHeader;
|
||||
import com.sap.sse.shared.json.JsonSerializer;
|
||||
|
||||
public class AWSResponseHttpHeaderJsonSerializer implements JsonSerializer<AWSResponseHttpHeader> {
|
||||
public static final String CONTENT_TYPE = "Content-Type";
|
||||
|
||||
@Override
|
||||
public JSONObject serialize(AWSResponseHttpHeader object) {
|
||||
JSONObject result = new JSONObject();
|
||||
result.put(CONTENT_TYPE, object.getContentType());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.sap.sailing.ingestion;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.sap.sailing.ingestion.dto.AWSResponseWrapper;
|
||||
import com.sap.sse.shared.json.JsonSerializer;
|
||||
|
||||
public class AWSResponseJsonSerializer<T> implements JsonSerializer<AWSResponseWrapper<T>> {
|
||||
public static final String STATUS_CODE = "statusCode";
|
||||
public static final String STATUS_DESCRIPTION = "statusDescription";
|
||||
public static final String HEADERS = "headers";
|
||||
public static final String BODY = "body";
|
||||
|
||||
@Override
|
||||
public JSONObject serialize(AWSResponseWrapper<T> object) {
|
||||
JSONObject result = new JSONObject();
|
||||
result.put(STATUS_CODE, object.getStatusCode());
|
||||
result.put(STATUS_DESCRIPTION, object.getStatusDescription());
|
||||
result.put(HEADERS, new AWSResponseHttpHeaderJsonSerializer().serialize(object.getHeaders()));
|
||||
result.put(BODY, object.getBody());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.sap.sailing.ingestion;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.sap.sailing.ingestion.dto.EndpointDTO;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.Helpers;
|
||||
import com.sap.sse.shared.json.JsonDeserializationException;
|
||||
import com.sap.sse.shared.json.JsonDeserializer;
|
||||
|
||||
public class EndpointJsonDeserializer implements JsonDeserializer<EndpointDTO> {
|
||||
public static final String ENDPOINT_UUID = "endpointUuid";
|
||||
public static final String ACTION = "action";
|
||||
public static final String ENDPOINT_CALLBACK_URL = "endpointCallbackUrl";
|
||||
public static final String DEVICES_UUID = "devicesUuid";
|
||||
|
||||
@Override
|
||||
public EndpointDTO deserialize(JSONObject object) throws JsonDeserializationException {
|
||||
final String endpointUuid = String.valueOf(object.get(ENDPOINT_UUID));
|
||||
final String action = String.valueOf(object.get(ACTION));
|
||||
final String endpointCallbackUrl = String.valueOf(object.get(ENDPOINT_CALLBACK_URL));
|
||||
final JSONArray jsonDevices = Helpers.toJSONArraySafe(object.get(DEVICES_UUID));
|
||||
final List<String> devicesUuid = new ArrayList<String>();
|
||||
for (int i = 0; i < jsonDevices.size(); i++) {
|
||||
String deviceUuid = String.valueOf(jsonDevices.get(i));
|
||||
devicesUuid.add(deviceUuid);
|
||||
}
|
||||
EndpointDTO endpoint = new EndpointDTO(endpointUuid, action, endpointCallbackUrl, devicesUuid);
|
||||
return endpoint;
|
||||
}
|
||||
}
|
||||
+30
-21
@@ -6,16 +6,15 @@ import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.redisson.api.RMap;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
|
||||
import com.google.gson.Gson;
|
||||
import com.sap.sailing.ingestion.dto.AWSRequestWrapper;
|
||||
import com.sap.sailing.ingestion.dto.AWSResponseWrapper;
|
||||
import com.sap.sailing.ingestion.dto.EndpointDTO;
|
||||
|
||||
import software.amazon.awssdk.utils.IoUtils;
|
||||
import com.sap.sse.shared.json.JsonDeserializationException;
|
||||
import com.sap.sse.shared.json.JsonDeserializer;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -48,44 +47,54 @@ import software.amazon.awssdk.utils.IoUtils;
|
||||
*
|
||||
*/
|
||||
public class EndpointRegistrationLambda implements RequestStreamHandler {
|
||||
private final AWSInOutHandler awsInOut = new AWSInOutHandler();
|
||||
private final JsonDeserializer<EndpointDTO> endpointDeserializer = new EndpointJsonDeserializer();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void handleRequest(InputStream inputAsStream, OutputStream outputAsStream, Context context) {
|
||||
public void handleRequest(final InputStream inputAsStream, final OutputStream outputAsStream, final Context context) {
|
||||
try {
|
||||
final byte[] streamAsBytes = IoUtils.toByteArray(inputAsStream);
|
||||
final AWSRequestWrapper requestWrapped = new Gson().fromJson(new String(streamAsBytes),
|
||||
AWSRequestWrapper.class);
|
||||
final EndpointDTO input = new Gson().fromJson(requestWrapped.getBody(), EndpointDTO.class);
|
||||
context.getLogger().log("Input: "+input);
|
||||
context.getLogger().log("Input body: "+requestWrapped.getBody());
|
||||
final JSONObject requestObject = awsInOut.parseInputToJson(inputAsStream);
|
||||
context.getLogger().log("Input body: " + requestObject.toJSONString());
|
||||
final EndpointDTO input = endpointDeserializer.deserialize(requestObject);
|
||||
if (input != null && input.getDevicesUuid() != null && input.getDevicesUuid().size() > 0) {
|
||||
final RMap<String, List<EndpointDTO>> cacheMap = RedisUtils.getCacheMap();
|
||||
for (final String deviceUuid : input.getDevicesUuid()) {
|
||||
final Object memObject = cacheMap.get(deviceUuid);
|
||||
final List<EndpointDTO> endpoints = memObject == null ? new ArrayList<>()
|
||||
: (List<EndpointDTO>) memObject;
|
||||
final List<EndpointDTO> endpoints = memObject == null ? new ArrayList<>() : (List<EndpointDTO>) memObject;
|
||||
if (input.isRegisterAction()) {
|
||||
if (!endpoints.contains(input)) {
|
||||
endpoints.add(input);
|
||||
context.getLogger().log("Added endpoint for device UUID " + deviceUuid + " with url "
|
||||
+ input.getEndpointCallbackUrl()+" and ID "+input.getEndpointUuid());
|
||||
+ input.getEndpointCallbackUrl() + " and ID " + input.getEndpointUuid());
|
||||
}
|
||||
} else if (input.isUnRegisterAction()) {
|
||||
endpoints.remove(input);
|
||||
context.getLogger().log("Removed endpoint for device UUID "+deviceUuid+" with url "+
|
||||
input.getEndpointCallbackUrl()+" and ID "+input.getEndpointUuid()+
|
||||
". Remaining subscriptions for device UUID: "+endpoints);
|
||||
context.getLogger().log("Removed endpoint for device UUID " + deviceUuid + " with url "
|
||||
+ input.getEndpointCallbackUrl() + " and ID " + input.getEndpointUuid()
|
||||
+ ". Remaining subscriptions for device UUID: " + endpoints);
|
||||
}
|
||||
cacheMap.put(deviceUuid, endpoints);
|
||||
}
|
||||
}
|
||||
String successResponse = new Gson()
|
||||
.toJson(AWSResponseWrapper.successResponseAsJson("\"" + input.getEndpointUuid() + "\""));
|
||||
final String successResponse = awsInOut.createJsonResponse("\"" + input.getEndpointUuid() + "\"").toJSONString();
|
||||
context.getLogger().log(successResponse);
|
||||
outputAsStream.write(successResponse.getBytes());
|
||||
outputAsStream.close();
|
||||
} catch (ParseException | JsonDeserializationException e) {
|
||||
context.getLogger().log("Exception trying to deserialize JSON input: " + e.getMessage());
|
||||
} catch (IOException ex) {
|
||||
context.getLogger().log(ex.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
inputAsStream.close();
|
||||
} catch (IOException e) {
|
||||
context.getLogger().log("Exception trying to close input: " + e.getMessage());
|
||||
}
|
||||
try {
|
||||
outputAsStream.close();
|
||||
} catch (IOException e) {
|
||||
context.getLogger().log("Exception trying to close output: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -64,7 +64,7 @@ public class FixCombinationLambda implements RequestStreamHandler {
|
||||
* Entry point for the λ on AWS. Defines the operation needed to combine single fixes to collections.
|
||||
*/
|
||||
@Override
|
||||
public void handleRequest(final InputStream input, final OutputStream output, final Context context) {
|
||||
public void handleRequest(final InputStream inputAsStream, final OutputStream outputAsStream, final Context context) {
|
||||
try {
|
||||
logger.info("FixCombination Lambda is starting");
|
||||
final Map<DeviceIdentifier, List<S3Object>> allSingleFixMetadataFromDevices = this.getMetadataOfNewFixes();
|
||||
@@ -76,12 +76,12 @@ public class FixCombinationLambda implements RequestStreamHandler {
|
||||
logger.log(Level.SEVERE, e.awsErrorDetails().errorMessage());
|
||||
} finally {
|
||||
try {
|
||||
input.close();
|
||||
inputAsStream.close();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Exception trying to close input: " + e.getMessage());
|
||||
}
|
||||
try {
|
||||
output.close();
|
||||
outputAsStream.close();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Exception trying to close output: " + e.getMessage());
|
||||
}
|
||||
|
||||
+32
-42
@@ -15,23 +15,16 @@ import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.redisson.api.RMap;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
|
||||
import com.google.gson.Gson;
|
||||
import com.sap.sailing.domain.common.DeviceIdentifier;
|
||||
import com.sap.sailing.domain.common.tracking.GPSFixMoving;
|
||||
import com.sap.sailing.domain.racelogtracking.impl.SmartphoneUUIDIdentifierImpl;
|
||||
import com.sap.sailing.ingestion.dto.AWSRequestWrapper;
|
||||
import com.sap.sailing.ingestion.dto.AWSResponseWrapper;
|
||||
import com.sap.sailing.ingestion.dto.EndpointDTO;
|
||||
import com.sap.sailing.ingestion.dto.FixHeaderDTO;
|
||||
import com.sap.sailing.ingestion.dto.GpsFixPayloadDTO;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.FlatSmartphoneUuidAndGPSFixMovingJsonDeserializer;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.Helpers;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.GPSFixMovingJsonSerializer;
|
||||
import com.sap.sse.common.Util.Pair;
|
||||
import com.sap.sse.shared.json.JsonDeserializationException;
|
||||
@@ -41,43 +34,38 @@ import com.sap.sse.shared.json.JsonSerializer;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||
import software.amazon.awssdk.utils.IoUtils;
|
||||
|
||||
/**
|
||||
* This λ accepts fixes of any kind that adhere to {@link FixHeaderDTO} structure wrapped inside an
|
||||
* {@link AWSRequestWrapper}. In most cases clients will want to submit GPS fixes thus adhering to the
|
||||
* {@link GpsFixPayloadDTO} structure. This structure will be recognized by most sailing servers.
|
||||
* This λ accepts fixes that adhere to the structure {@link Pair<UUID, List<GPSFixMoving>>} which are provided in the
|
||||
* body of an AWS request which is parsed with the {@link AWSInOutHandler}. In most cases clients will want to submit
|
||||
* GPS fixes thus adhering to the {@link GPSFixMoving} structure. This structure will be recognized by most sailing
|
||||
* servers.
|
||||
*/
|
||||
|
||||
//TODO remove Gson dependency
|
||||
public class FixIngestionLambda implements RequestStreamHandler {
|
||||
final S3Client s3Client = S3Client.builder().region(Configuration.S3_REGION).build();
|
||||
final RMap<String, List<EndpointDTO>> cacheMap = RedisUtils.getCacheMap();
|
||||
private final JsonDeserializer<Pair<UUID, List<GPSFixMoving>>> deserializer =
|
||||
new FlatSmartphoneUuidAndGPSFixMovingJsonDeserializer();
|
||||
private final JsonSerializer<GPSFixMoving> serializer = new GPSFixMovingJsonSerializer();
|
||||
private final AWSInOutHandler awsInOut = new AWSInOutHandler();
|
||||
private final JsonDeserializer<Pair<UUID, List<GPSFixMoving>>> gpsFixDeserializer = new FlatSmartphoneUuidAndGPSFixMovingJsonDeserializer();
|
||||
private final JsonSerializer<GPSFixMoving> gpsFixSerializer = new GPSFixMovingJsonSerializer();
|
||||
private final S3FixStorageStructure s3FixStorageStructure = new S3FixStorageStructure();
|
||||
private final S3Client s3Client = S3Client.builder().region(Configuration.S3_REGION).build();
|
||||
private final RMap<String, List<EndpointDTO>> cacheMap = RedisUtils.getCacheMap();
|
||||
private static final Logger logger = Logger.getLogger(FixIngestionLambda.class.getName());
|
||||
|
||||
|
||||
@Override
|
||||
public void handleRequest(final InputStream input, final OutputStream output, final Context context) {
|
||||
public void handleRequest(final InputStream inputAsStream, final OutputStream outputAsStream, final Context context) {
|
||||
try {
|
||||
logger.info("Starting Lambda");
|
||||
final byte[] streamAsBytes = IoUtils.toByteArray(input);
|
||||
logger.info("Input: " + new String(streamAsBytes));
|
||||
final AWSRequestWrapper dtoWrapped = new Gson().fromJson(new String(streamAsBytes), AWSRequestWrapper.class);
|
||||
Object requestBody = JSONValue.parseWithException(dtoWrapped.getBody());
|
||||
JSONObject requestObject = Helpers.toJSONObjectSafe(requestBody);
|
||||
Pair<UUID, List<GPSFixMoving>> data = deserializer.deserialize(requestObject);
|
||||
DeviceIdentifier deviceIdentifier = new SmartphoneUUIDIdentifierImpl(data.getA());
|
||||
List<GPSFixMoving> newFixes = data.getB();
|
||||
final byte[] bodyAsBytes = dtoWrapped.getBody().getBytes();
|
||||
final JSONObject requestObject = awsInOut.parseInputToJson(inputAsStream);
|
||||
logger.info("Input: " + requestObject.toJSONString());
|
||||
final Pair<UUID, List<GPSFixMoving>> data = gpsFixDeserializer.deserialize(requestObject);
|
||||
final DeviceIdentifier deviceIdentifier = new SmartphoneUUIDIdentifierImpl(data.getA());
|
||||
final List<GPSFixMoving> newFixes = data.getB();
|
||||
final byte[] bodyAsBytes = requestObject.toJSONString().getBytes();
|
||||
final ForkJoinPool dispatchToSubscribersTask = ForkJoinPool.commonPool();
|
||||
dispatchToSubscribersTask.submit(() -> {
|
||||
try {
|
||||
storeFixFileToS3(deviceIdentifier, newFixes);
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Exception trying to store fixes to S3: "+e.getMessage());
|
||||
logger.log(Level.SEVERE, "Exception trying to store fixes to S3: " + e.getMessage());
|
||||
}
|
||||
});
|
||||
final List<EndpointDTO> listOfEndpointsToTrigger = cacheMap.get(deviceIdentifier.getStringRepresentation());
|
||||
@@ -95,19 +83,21 @@ public class FixIngestionLambda implements RequestStreamHandler {
|
||||
} else {
|
||||
logger.info("No endpoint has been configured for Identifier " + deviceIdentifier.getStringRepresentation());
|
||||
}
|
||||
output.write(new Gson().toJson(AWSResponseWrapper.successResponseAsJson(deviceIdentifier.getStringRepresentation())).getBytes());
|
||||
String successResponse = awsInOut.createJsonResponse(deviceIdentifier.getStringRepresentation()).toJSONString();
|
||||
logger.info(successResponse);
|
||||
outputAsStream.write(successResponse.getBytes());
|
||||
} catch (ParseException | JsonDeserializationException e) {
|
||||
logger.log(Level.SEVERE, "Exception trying to deserialize JSON input: " + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
input.close();
|
||||
inputAsStream.close();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Exception trying to close input: " + e.getMessage());
|
||||
}
|
||||
try {
|
||||
output.close();
|
||||
outputAsStream.close();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Exception trying to close output: " + e.getMessage());
|
||||
}
|
||||
@@ -115,7 +105,7 @@ public class FixIngestionLambda implements RequestStreamHandler {
|
||||
}
|
||||
|
||||
private void dispatchToSubscribers(final EndpointDTO endpoint, final byte[] jsonAsBytes) {
|
||||
logger.info("Connecting to endpoint " + endpoint.getEndpointCallbackUrl()+" with ID "+endpoint.getEndpointUuid());
|
||||
logger.info("Connecting to endpoint " + endpoint.getEndpointCallbackUrl() + " with ID " + endpoint.getEndpointUuid());
|
||||
URL endpointUrl;
|
||||
try {
|
||||
endpointUrl = new URL(endpoint.getEndpointCallbackUrl());
|
||||
@@ -132,26 +122,26 @@ public class FixIngestionLambda implements RequestStreamHandler {
|
||||
os.write(jsonAsBytes);
|
||||
os.flush();
|
||||
final int responseCode = connectionToEndpoint.getResponseCode(); // reading is important to actually issue the request
|
||||
logger.info("Sent data "+new String(jsonAsBytes)+" to " + endpoint.getEndpointCallbackUrl()+" with response code "+responseCode);
|
||||
}
|
||||
logger.info("Sent data " + new String(jsonAsBytes) + " to " + endpoint.getEndpointCallbackUrl() + " with response code " + responseCode);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.SEVERE, "Exception trying to send data to "+endpointUrl+": "+ex.getMessage());
|
||||
logger.log(Level.SEVERE, "Exception trying to send data to " + endpointUrl + ": " + ex.getMessage());
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
logger.log(Level.SEVERE, "Malformed URL for end point "+endpoint.getEndpointCallbackUrl());
|
||||
logger.log(Level.SEVERE, "Malformed URL for end point " + endpoint.getEndpointCallbackUrl());
|
||||
}
|
||||
}
|
||||
|
||||
private void storeFixFileToS3(final DeviceIdentifier deviceIdentifier, final List<GPSFixMoving> newFixes)
|
||||
throws IOException {
|
||||
final String dataAsString = newFixes.toString();
|
||||
logger.info("Data to write: "+dataAsString);
|
||||
for (GPSFixMoving fix: newFixes) {
|
||||
logger.info("Data to write: " + dataAsString);
|
||||
for (GPSFixMoving fix : newFixes) {
|
||||
final String destinationKey = s3FixStorageStructure.generateKeyForSingleFix(deviceIdentifier, fix.getTimePoint());
|
||||
logger.info("Location: "+destinationKey);
|
||||
logger.info("Location: " + destinationKey);
|
||||
final PutObjectRequest putObjectRequest = PutObjectRequest.builder().bucket(Configuration.S3_BUCKET_NAME)
|
||||
.key(destinationKey).contentType("application/json").build();
|
||||
final JSONObject serializedFix = serializer.serialize(fix);
|
||||
final JSONObject serializedFix = gpsFixSerializer.serialize(fix);
|
||||
s3Client.putObject(putObjectRequest, RequestBody.fromString(serializedFix.toJSONString()));
|
||||
}
|
||||
logger.info("Finished putting object into S3");
|
||||
|
||||
+22
-9
@@ -1,16 +1,29 @@
|
||||
package com.sap.sailing.ingestion;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||
import com.google.gson.Gson;
|
||||
import com.sap.sailing.ingestion.dto.AWSRequestWrapper;
|
||||
import com.sap.sailing.ingestion.dto.AWSResponseWrapper;
|
||||
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
|
||||
import com.sap.sse.shared.json.JsonDeserializationException;
|
||||
|
||||
public class PingLambda implements RequestStreamHandler {
|
||||
private final AWSInOutHandler awsInOut = new AWSInOutHandler();
|
||||
|
||||
public class PingLambda implements RequestHandler<AWSRequestWrapper, String> {
|
||||
@Override
|
||||
public String handleRequest(AWSRequestWrapper input, Context context) {
|
||||
final String jsonAsString = new Gson().toJson(input);
|
||||
context.getLogger().log(jsonAsString);
|
||||
return new Gson().toJson(AWSResponseWrapper.successResponseAsJson(jsonAsString));
|
||||
public void handleRequest(final InputStream inputAsStream, final OutputStream outputAsStream, final Context context) {
|
||||
try {
|
||||
final String jsonAsString = awsInOut.parseInputToJson(inputAsStream).toJSONString();
|
||||
context.getLogger().log(jsonAsString);
|
||||
String successResponse = awsInOut.createJsonResponse(jsonAsString).toJSONString();
|
||||
outputAsStream.write(successResponse.getBytes());
|
||||
} catch (ParseException | JsonDeserializationException e) {
|
||||
context.getLogger().log("Exception trying to deserialize JSON input: " + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
context.getLogger().log(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-4
@@ -3,8 +3,6 @@ package com.sap.sailing.ingestion.dto;
|
||||
import java.io.Serializable;
|
||||
import java.util.Base64;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
/**
|
||||
* In most cases requests coming to a lambda will be serialized into JSON. That means that all fields a HTTP request
|
||||
* normally has can be found inside the JSON. This implementation just provides the body and the method.
|
||||
@@ -48,8 +46,11 @@ public class AWSRequestWrapper implements Serializable {
|
||||
private String body;
|
||||
private Boolean isBase64Encoded;
|
||||
|
||||
public <T> T getBodyAsType(Class<T> type) {
|
||||
return (T) new Gson().fromJson(getBody(), type);
|
||||
public AWSRequestWrapper(String httpMethod, String body, Boolean isBase64Encoded) {
|
||||
super();
|
||||
this.httpMethod = httpMethod;
|
||||
this.body = body;
|
||||
this.isBase64Encoded = isBase64Encoded;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
|
||||
+13
-5
@@ -5,7 +5,7 @@ import java.util.List;
|
||||
|
||||
public class EndpointDTO implements Serializable {
|
||||
private static final long serialVersionUID = 3115461658787136449L;
|
||||
|
||||
|
||||
public final static String REGISTER_ACTION = "register";
|
||||
public final static String UNREGISTER_ACTION = "unregister";
|
||||
|
||||
@@ -17,6 +17,14 @@ public class EndpointDTO implements Serializable {
|
||||
private String endpointCallbackUrl;
|
||||
private List<String> devicesUuid;
|
||||
|
||||
public EndpointDTO(String endpointUuid, String action, String endpointCallbackUrl, List<String> devicesUuid) {
|
||||
super();
|
||||
this.endpointUuid = endpointUuid;
|
||||
this.action = action;
|
||||
this.endpointCallbackUrl = endpointCallbackUrl;
|
||||
this.devicesUuid = devicesUuid;
|
||||
}
|
||||
|
||||
public String getEndpointUuid() {
|
||||
return endpointUuid;
|
||||
}
|
||||
@@ -48,7 +56,7 @@ public class EndpointDTO implements Serializable {
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
|
||||
public boolean isRegisterAction() {
|
||||
return getAction().equalsIgnoreCase(REGISTER_ACTION);
|
||||
}
|
||||
@@ -56,17 +64,17 @@ public class EndpointDTO implements Serializable {
|
||||
public boolean isUnRegisterAction() {
|
||||
return getAction().equalsIgnoreCase(UNREGISTER_ACTION);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this)
|
||||
return true;
|
||||
if (!(o instanceof EndpointDTO))
|
||||
return false;
|
||||
EndpointDTO other = (EndpointDTO)o;
|
||||
EndpointDTO other = (EndpointDTO) o;
|
||||
return getEndpointUuid().equals(other.getEndpointUuid());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getEndpointUuid().hashCode();
|
||||
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
package com.sap.sailing.ingestion.dto;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
public class FixHeaderDTO implements Serializable {
|
||||
private static final long serialVersionUID = -6871581519012495468L;
|
||||
|
||||
private String deviceUuid;
|
||||
private List<Object> fixes;
|
||||
|
||||
public String getDeviceUuid() {
|
||||
return deviceUuid;
|
||||
}
|
||||
|
||||
public void setDeviceUuid(String deviceUuid) {
|
||||
this.deviceUuid = deviceUuid;
|
||||
}
|
||||
|
||||
public List<Object> getFixes() {
|
||||
return fixes;
|
||||
}
|
||||
|
||||
public void setFixes(List<Object> fixes) {
|
||||
this.fixes = fixes;
|
||||
}
|
||||
}
|
||||
-58
@@ -1,58 +0,0 @@
|
||||
package com.sap.sailing.ingestion.dto;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class GpsFixPayloadDTO implements Serializable, Comparable<GpsFixPayloadDTO> {
|
||||
private static final long serialVersionUID = 6802355060150334552L;
|
||||
|
||||
private long timestamp;
|
||||
private double latitude;
|
||||
private double longitude;
|
||||
private double speed;
|
||||
private double course;
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(long timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public void setLatitude(double latitude) {
|
||||
this.latitude = latitude;
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public void setLongitude(double longitude) {
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
public double getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
public void setSpeed(double speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
public double getCourse() {
|
||||
return course;
|
||||
}
|
||||
|
||||
public void setCourse(double course) {
|
||||
this.course = course;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(GpsFixPayloadDTO o) {
|
||||
return (int) (this.getTimestamp() - o.getTimestamp());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user