public void handleRequest()

in src/main/java/com/amazonaws/services/neptune/export/NeptuneExportLambda.java [57:190]


    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {

        Logger logger = s -> context.getLogger().log(s);

        ObjectMapper objectMapper = new ObjectMapper();

        JsonNode json = objectMapper.readTree(IOUtils.toString(inputStream, UTF_8.name()));

        String cmd = json.has("command") ?
                json.path("command").textValue() :
                EnvironmentVariableUtils.getOptionalEnv("COMMAND", "export-pg");

        ObjectNode params = json.has("params") ?
                (ObjectNode) json.get("params") :
                objectMapper.readTree("{}").deepCopy();

        String outputS3Path = json.has("outputS3Path") ?
                json.path("outputS3Path").textValue() :
                EnvironmentVariableUtils.getOptionalEnv("OUTPUT_S3_PATH", "");

        String sseKmsKeyId = json.has("sseKmsKeyId") ?
                json.path("sseKmsKeyId").textValue() :
                EnvironmentVariableUtils.getOptionalEnv("SSE_KMS_KEY_ID", "");

        boolean createExportSubdirectory = Boolean.parseBoolean(
                json.has("createExportSubdirectory") ?
                        json.path("createExportSubdirectory").toString() :
                        EnvironmentVariableUtils.getOptionalEnv("CREATE_EXPORT_SUBDIRECTORY", "true"));

        boolean overwriteExisting = Boolean.parseBoolean(
                json.has("overwriteExisting") ?
                        json.path("overwriteExisting").toString() :
                        EnvironmentVariableUtils.getOptionalEnv("OVERWRITE_EXISTING", "false"));

        boolean uploadToS3OnError = Boolean.parseBoolean(
                json.has("uploadToS3OnError") ?
                        json.path("uploadToS3OnError").toString() :
                        EnvironmentVariableUtils.getOptionalEnv("UPLOAD_TO_S3_ON_ERROR", "true"));

        String configFileS3Path = json.has("configFileS3Path") ?
                json.path("configFileS3Path").textValue() :
                EnvironmentVariableUtils.getOptionalEnv("CONFIG_FILE_S3_PATH", "");

        String queriesFileS3Path = json.has("queriesFileS3Path") ?
                json.path("queriesFileS3Path").textValue() :
                EnvironmentVariableUtils.getOptionalEnv("QUERIES_FILE_S3_PATH", "");

        String completionFileS3Path = json.has("completionFileS3Path") ?
                json.path("completionFileS3Path").textValue() :
                EnvironmentVariableUtils.getOptionalEnv("COMPLETION_FILE_S3_PATH", "");

        String s3Region = json.has("s3Region") ?
                json.path("s3Region").textValue() :
                EnvironmentVariableUtils.getOptionalEnv("S3_REGION",
                        EnvironmentVariableUtils.getOptionalEnv("AWS_REGION", ""));

        ObjectNode completionFilePayload = json.has("completionFilePayload") ?
                json.path("completionFilePayload").deepCopy() :
                objectMapper.readTree(
                        EnvironmentVariableUtils.getOptionalEnv(
                                "COMPLETION_FILE_PAYLOAD",
                                "{}")).
                        deepCopy();

        ObjectNode additionalParams = json.has("additionalParams") ?
                json.path("additionalParams").deepCopy() :
                objectMapper.readTree("{}").deepCopy();

        int maxConcurrency = json.has("jobSize") ?
                JobSize.parse(json.path("jobSize").textValue()).maxConcurrency() :
                -1;

        // We are masking 3/4 of the KMS Key ID as it is potentially sensitive information.
        String maskedKeyId = StringUtils.isBlank(sseKmsKeyId) ?
                sseKmsKeyId :
                sseKmsKeyId.substring(0, sseKmsKeyId.length()/4) +
                        sseKmsKeyId.substring(sseKmsKeyId.length()/4).replaceAll("\\w","*");

        AwsCredentialsProvider s3CredentialsProvider = getS3CredentialsProvider(json, params, s3Region);

        logger.log("cmd                       : " + cmd);
        logger.log("params                    : " + params.toPrettyString());
        logger.log("outputS3Path              : " + outputS3Path);
        logger.log("createExportSubdirectory  : " + createExportSubdirectory);
        logger.log("overwriteExisting         : " + overwriteExisting);
        logger.log("uploadToS3OnError         : " + uploadToS3OnError);
        logger.log("configFileS3Path          : " + configFileS3Path);
        logger.log("queriesFileS3Path         : " + queriesFileS3Path);
        logger.log("completionFileS3Path      : " + completionFileS3Path);
        logger.log("s3Region                  : " + s3Region);
        logger.log("sseKmsKeyId               : " + maskedKeyId);
        logger.log("completionFilePayload     : " + completionFilePayload.toPrettyString());
        logger.log("additionalParams          : " + additionalParams.toPrettyString());
        logger.log("maxFileDescriptorCount    : " + maxFileDescriptorCount);

        if (!cmd.contains(" ") && !params.isEmpty()) {
            cmd = ParamConverter.fromJson(cmd, params).toString();
        }

        logger.log("revised cmd           : " + cmd);

        NeptuneExportService neptuneExportService = new NeptuneExportService(
                cmd,
                localOutputPath,
                cleanOutputPath,
                outputS3Path,
                createExportSubdirectory,
                overwriteExisting,
                uploadToS3OnError,
                configFileS3Path,
                queriesFileS3Path,
                completionFileS3Path,
                completionFilePayload,
                additionalParams,
                maxConcurrency,
                s3Region,
                maxFileDescriptorCount,
                sseKmsKeyId,
                s3CredentialsProvider);

        S3ObjectInfo outputS3ObjectInfo = neptuneExportService.execute();

        if (StringUtils.isEmpty(outputS3Path)) {
            return;
        }

        if (outputS3ObjectInfo != null) {
            try (Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, UTF_8))) {
                writer.write(outputS3ObjectInfo.toString());
            }
        } else {
            System.exit(-1);
        }
    }