use of com.amazonaws.services.lambda.runtime.events.KinesisFirehoseEvent in project Insights by CognizantOneDevOps.
the class LambdaFunctionHandler method handleRequest.
/**
* Get the Kinesis event, De-serialize the event and get event Body and URL
* @param KinesisFirehoseEvent
* @return transformedEvent
*/
@Override
public KinesisAnalyticsInputPreprocessingResponse handleRequest(KinesisFirehoseEvent event, Context context) {
KinesisAnalyticsInputPreprocessingResponse transformedEvent = new KinesisAnalyticsInputPreprocessingResponse();
List<Record> transRecords = new ArrayList<Record>();
int responseCode = 200;
for (com.amazonaws.services.lambda.runtime.events.KinesisFirehoseEvent.Record record : event.getRecords()) {
ByteBuffer bufferData = record.getData();
InputStream data = new ByteArrayInputStream(bufferData.array());
JsonElement element = parseReader(new InputStreamReader(data));
JsonObject json = element.getAsJsonObject();
String body = json.get("body").toString();
String tool = json.get("tool").getAsString();
String url = System.getenv(tool);
try {
if (responseCode == 200 && url != null && !MAINTENANCE_MODE) {
responseCode = 0;
URL insightsUrl = new URL(url);
HttpURLConnection con = (HttpURLConnection) insightsUrl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
con.setConnectTimeout(CONNECTION_TIMEOUT);
OutputStream os = con.getOutputStream();
os.write(body.getBytes());
os.flush();
os.close();
responseCode = con.getResponseCode();
if (responseCode == 200) {
Record transRecord = new Record(record.getRecordId(), Result.Dropped, bufferData);
transRecords.add(transRecord);
} else {
log.error("Error while conecting URL " + url + " return response code " + responseCode);
}
}
} catch (MalformedURLException e) {
log.error("Invalid URL fot the tool " + tool + " " + e.toString());
} catch (Exception e) {
log.error("Error connecting to URL " + url + " " + e.toString());
} finally {
if (responseCode != 200 || MAINTENANCE_MODE || url == null) {
if (url == null) {
log.error("Url Entry is null for the tool " + tool);
}
Record transRecord = new Record(record.getRecordId(), Result.Ok, bufferData);
transRecords.add(transRecord);
}
}
}
transformedEvent.setRecords(transRecords);
return transformedEvent;
}
Aggregations