Search in sources :

Example 1 with KinesisAnalyticsInputPreprocessingResponse

use of com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse in project aws-ingesting-click-logs-using-terraform by aws-samples.

the class ClickLoggerStreamHandler method handleRequest.

@Override
public KinesisAnalyticsInputPreprocessingResponse handleRequest(final KinesisFirehoseEvent event, final Context context) {
    final LambdaLogger logger = context.getLogger();
    final String success_response = new String("200 OK");
    logger.log("EVENT: " + gson.toJson(event));
    String env_table = System.getenv("DB_TABLE");
    if (!StringUtils.isBlank(env_table)) {
        dynamo_table_name = env_table;
    }
    // System.getenv("REGION");
    String env_region = System.getenv("AWS_REGION");
    logger.log("Environment region name - " + env_region);
    if (!StringUtils.isBlank(env_region)) {
        region = env_region;
    }
    List<Record> records = new ArrayList<>();
    event.getRecords().forEach(kinesisRecord -> {
        String clickJson = new String(kinesisRecord.getData().array());
        logger.log("Individual record: " + kinesisRecord.getData());
        Gson gson = new Gson();
        try {
            ClickLogRequest clickLogRequest = gson.fromJson(clickJson, ClickLogRequest.class);
            String req = clickLogRequest.getRequestid() + " - " + clickLogRequest.getCallerid() + "  - " + clickLogRequest.getComponent() + " - " + clickLogRequest.getType() + " - " + clickLogRequest.getAction() + " - " + clickLogRequest.getUser() + " - " + clickLogRequest.getClientip() + " - " + clickLogRequest.getCreatedtime();
            Boolean valid_input = true;
            logger.log("Incoming request variables - " + req);
            if (clickLogRequest != null) {
                logger.log("Validating inputs");
                if (StringUtils.isBlank(clickLogRequest.getRequestid())) {
                    logger.log("error occurred - requestid missing");
                    valid_input = false;
                }
                if (StringUtils.isBlank(clickLogRequest.getContextid())) {
                    logger.log("error occurred - contextid missing");
                    valid_input = false;
                }
                if (StringUtils.isBlank(clickLogRequest.getCallerid())) {
                    logger.log("error occurred - caller missing");
                    valid_input = false;
                }
                if (StringUtils.isBlank(clickLogRequest.getType())) {
                    logger.log("error occurred - type missing");
                    valid_input = false;
                }
                if (StringUtils.isBlank(clickLogRequest.getAction())) {
                    logger.log("error occurred - action missing");
                    valid_input = false;
                }
                if (StringUtils.isBlank(clickLogRequest.getComponent())) {
                    logger.log("error occurred - component missing");
                    valid_input = false;
                }
                String user = "GUEST";
                if (StringUtils.isBlank(clickLogRequest.getUser())) {
                    logger.log("setting default user");
                    clickLogRequest.setUser(user);
                }
                String clientip = "APIGWY";
                if (StringUtils.isBlank(clickLogRequest.getClientip())) {
                    logger.log("setting default clientip");
                    clickLogRequest.setClientip(clientip);
                }
                String datetime = "";
                if (StringUtils.isBlank(clickLogRequest.getCreatedtime())) {
                    logger.log("setting default createdtime");
                    Format f = new SimpleDateFormat("mm-dd-yyyy hh:mm:ss");
                    datetime = f.format(new Date());
                    clickLogRequest.setCreatedtime(datetime);
                }
                logger.log("Validated inputs");
            }
            req = clickLogRequest.getRequestid() + " - " + clickLogRequest.getCallerid() + "  - " + clickLogRequest.getComponent() + " - " + clickLogRequest.getType() + " - " + clickLogRequest.getAction() + " - " + clickLogRequest.getUser() + " - " + clickLogRequest.getClientip() + " - " + clickLogRequest.getCreatedtime();
            logger.log("Modified request variables - " + req);
            logger.log("Valid Input - " + String.valueOf(valid_input));
            System.out.println("Calling updateclicklogs method for the received clicklogrequest");
            if (valid_input) {
                updateClickLogs(clickLogRequest);
            }
            Record record = new Record();
            record.setRecordId(kinesisRecord.getRecordId());
            record.setData(kinesisRecord.getData());
            Result result = KinesisAnalyticsInputPreprocessingResponse.Result.Ok;
            record.setResult(result);
            records.add(record);
        } catch (JsonSyntaxException jsEx) {
            System.out.println(jsEx);
        }
    });
    KinesisAnalyticsInputPreprocessingResponse response = new KinesisAnalyticsInputPreprocessingResponse(records);
    logger.log(success_response);
    return response;
}
Also used : ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) ClickLogRequest(com.clicklogs.model.ClickLogRequest) Date(java.util.Date) Result(com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse.Result) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) JsonSyntaxException(com.google.gson.JsonSyntaxException) Record(com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse.Record) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) SimpleDateFormat(java.text.SimpleDateFormat) KinesisAnalyticsInputPreprocessingResponse(com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse)

Example 2 with KinesisAnalyticsInputPreprocessingResponse

use of com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse 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)

Example 3 with KinesisAnalyticsInputPreprocessingResponse

use of com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse in project aws-ingesting-click-logs-using-terraform by aws-samples.

the class ClickLoggerStreamHanderTest method invokeTest.

@Test
void invokeTest() {
    KinesisAnalyticsInputPreprocessingResponse response = new KinesisAnalyticsInputPreprocessingResponse();
    response = clickLoggerSreamHandler.handleRequest(event, context);
    Assert.assertNotNull(response);
// Assert.assertEquals(response.getRecords()., 1);
}
Also used : KinesisAnalyticsInputPreprocessingResponse(com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse) Test(org.junit.Test)

Aggregations

KinesisAnalyticsInputPreprocessingResponse (com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse)3 Record (com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse.Record)2 ArrayList (java.util.ArrayList)2 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)1 Result (com.amazonaws.services.lambda.runtime.events.KinesisAnalyticsInputPreprocessingResponse.Result)1 KinesisFirehoseEvent (com.amazonaws.services.lambda.runtime.events.KinesisFirehoseEvent)1 ClickLogRequest (com.clicklogs.model.ClickLogRequest)1 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)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 Format (java.text.Format)1