Skip to content

Commit

Permalink
add java files
Browse files Browse the repository at this point in the history
  • Loading branch information
scmacdon committed Aug 31, 2023
1 parent 2b16073 commit 11f67f0
Show file tree
Hide file tree
Showing 13 changed files with 783 additions and 0 deletions.
218 changes: 218 additions & 0 deletions javav2/usecases/creating_fsa_app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>creating_fsa_app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.20.45</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.19.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.5</version> <!-- Use the latest version -->
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb-enhanced</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>apache-client</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ses</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>rekognition</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>textract</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3control</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>translate</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>comprehend</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>polly</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb-enhanced</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sns</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-booter</artifactId>
<version>3.0.0-M3</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>lambda</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package com.example.fsa;

public class FSAApplicationResources {

public static final String STORAGE_BUCKET = "fsa-scmacdon-javascript-mediabucket8240391c-1wba4q2k3xco9";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package com.example.fsa;

import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.lambda.LambdaClient;
import software.amazon.awssdk.services.lambda.model.GetFunctionConfigurationRequest;
import software.amazon.awssdk.services.lambda.model.GetFunctionConfigurationResponse;
import software.amazon.awssdk.services.lambda.model.LambdaException;
import software.amazon.awssdk.services.lambda.model.UpdateFunctionCodeRequest;
import software.amazon.awssdk.services.lambda.model.UpdateFunctionCodeResponse;
import software.amazon.awssdk.services.lambda.waiters.LambdaWaiter;

public class ModifyLambda {

public static void main(String[]args) {
String analyzeSentimentLambda = "fsa-scmacdon-javascript-fnAnalyzeSentimentCEDFFDBD-wd0RnOdSxMsv" ;
String synthesizeAudioLambda = "fsa-scmacdon-javascript-fnSynthesizeAudio134971D4-x8Q5178Y4ZBH" ;
String extractTextLambda = "fsa-scmacdon-javascript-fnExtractText98FC28B4-NRiFEZmyPPlo" ;
String translateTextLambda = "fsa-scmacdon-javascript-fnAnalyzeSentimentCEDFFDBD-wd0RnOdSxMsv" ;
String bucketName = FSAApplicationResources.STORAGE_BUCKET;
String key = "creating_fsa_app-1.0-SNAPSHOT.jar";

Region region = Region.US_EAST_1;
LambdaClient awsLambda = LambdaClient.builder()
.region(region)
.build();

// Update all four Lambda functions.
updateFunctionCode(awsLambda, analyzeSentimentLambda, bucketName, key);
updateFunctionCode(awsLambda, synthesizeAudioLambda, bucketName, key);
updateFunctionCode(awsLambda, extractTextLambda, bucketName, key);
updateFunctionCode(awsLambda, translateTextLambda, bucketName, key);
System.out.println("You have successfully updated the AWS Lambda functions");
}

public static void updateFunctionCode(LambdaClient awsLambda, String functionName, String bucketName, String key) {
try {
LambdaWaiter waiter = awsLambda.waiter();
UpdateFunctionCodeRequest functionCodeRequest = UpdateFunctionCodeRequest.builder()
.functionName(functionName)
.publish(true)
.s3Bucket(bucketName)
.s3Key(key)
.build();

UpdateFunctionCodeResponse response = awsLambda.updateFunctionCode(functionCodeRequest) ;
GetFunctionConfigurationRequest getFunctionConfigRequest = GetFunctionConfigurationRequest.builder()
.functionName(functionName)
.build();

WaiterResponse<GetFunctionConfigurationResponse> waiterResponse = waiter.waitUntilFunctionUpdated(getFunctionConfigRequest);
waiterResponse.matched().response().ifPresent(System.out::println);
System.out.println("The last modified value is " +response.lastModified());

} catch(LambdaException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package com.example.fsa.handlers;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.example.fsa.services.PollyService;
import com.example.fsa.services.S3Service;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Map;

public class PollyHanlder implements RequestHandler<Map<String, Object>, String> {
@Override
public String handleRequest(Map<String, Object> requestObject, Context context) {
S3Service s3Service = new S3Service();
PollyService pollyService = new PollyService();
String myValues = requestObject.toString();
context.getLogger().log("*** ALL values: " +myValues);
String translatedText = getTranslatedText(myValues);
String key = getKeyName(myValues);
String newFileName = convertFileEx(key);
context.getLogger().log("*** Translated Text: " +translatedText +" and new key is "+newFileName);
try {
InputStream is = pollyService.synthesize(translatedText);
String audioFile = s3Service.putAudio(is, newFileName);
context.getLogger().log("You have successfully added the " +audioFile +" in the S3 bucket");
return audioFile ;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

// This method extracts the value following translated_text using Reg Exps.
private String getTranslatedText(String myString) {
// Define the regular expression pattern to match key-value pair.
Pattern pattern = Pattern.compile("translated_text\\s*=\\s*([^,}]*)");
Matcher matcher = pattern.matcher(myString);

String extractedValue = null;
if (matcher.find()) {
// Find the second occurrence.
if (matcher.find()) {
extractedValue = matcher.group(1);
}
}

if (extractedValue != null) {
return extractedValue;
}
return "";
}

// This method extracts the key using Reg Exps.
private static String getKeyName(String input) {
String pattern = "object=([^,}]+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);

if (m.find()) {
System.out.println("Found value: " + m.group(1));
return m.group(1);
}
return "";
}

// Replaces the file extension to mp3.
public static String convertFileEx(String originalFileName) {
String newExtension = "mp3";

// Get the index of the last dot (.) in the filename.
int lastIndex = originalFileName.lastIndexOf(".");
if (lastIndex > 0) {
// Extract the file name without extension.
String fileNameWithoutExtension = originalFileName.substring(0, lastIndex);
return fileNameWithoutExtension + "." + newExtension;
}
return "";
}
}
Loading

0 comments on commit 11f67f0

Please sign in to comment.