Search in sources :

Example 1 with KinesisFirehoseEvent

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;
}
Also used : KinesisFirehoseEvent(com.amazonaws.services.lambda.runtime.events.KinesisFirehoseEvent) MalformedURLException(java.net.MalformedURLException) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ByteBuffer(java.nio.ByteBuffer) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonElement(com.google.gson.JsonElement) Record(com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse.Record) KinesisAnalyticsInputPreprocessingResponse(com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse)

Aggregations

KinesisAnalyticsInputPreprocessingResponse (com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse)1 Record (com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse.Record)1 KinesisFirehoseEvent (com.amazonaws.services.lambda.runtime.events.KinesisFirehoseEvent)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1