-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb254a1
commit fa4b8f6
Showing
15 changed files
with
793 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
.../aws-textract/src/main/java/io/camunda/connector/textract/caller/AsyncTextractCaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.camunda.connector.textract.caller; | ||
|
||
import com.amazonaws.services.textract.AmazonTextract; | ||
import com.amazonaws.services.textract.model.NotificationChannel; | ||
import com.amazonaws.services.textract.model.OutputConfig; | ||
import com.amazonaws.services.textract.model.StartDocumentAnalysisRequest; | ||
import com.amazonaws.services.textract.model.StartDocumentAnalysisResult; | ||
import io.camunda.connector.textract.model.TextractRequestData; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class AsyncTextractCaller implements TextractCaller<StartDocumentAnalysisResult> { | ||
|
||
@Override | ||
public StartDocumentAnalysisResult call(TextractRequestData requestData, AmazonTextract textractClient) { | ||
final StartDocumentAnalysisRequest startDocumentAnalysisRequest = new StartDocumentAnalysisRequest() | ||
.withFeatureTypes(this.prepareFeatureTypes(requestData)) | ||
.withDocumentLocation(this.prepareDocumentLocation(requestData)); | ||
|
||
startDocumentAnalysisRequest.withClientRequestToken(requestData.clientRequestToken()); | ||
|
||
startDocumentAnalysisRequest.withJobTag(requestData.jobTag()); | ||
|
||
startDocumentAnalysisRequest.withKMSKeyId(requestData.kmsKeyId()); | ||
|
||
this.prepareNotification(startDocumentAnalysisRequest, requestData); | ||
this.prepareOutput(startDocumentAnalysisRequest, requestData); | ||
|
||
return textractClient.startDocumentAnalysis(startDocumentAnalysisRequest); | ||
} | ||
|
||
private void prepareNotification(StartDocumentAnalysisRequest startDocumentAnalysisRequest, TextractRequestData requestData) { | ||
if (StringUtils.isEmpty(requestData.notificationChannelRoleArn()) && | ||
StringUtils.isEmpty(requestData.notificationChannelSnsTopicArn())) { | ||
return; | ||
} | ||
|
||
final NotificationChannel notificationChannel = new NotificationChannel(); | ||
if (!StringUtils.isEmpty(requestData.notificationChannelRoleArn())) { | ||
notificationChannel.withRoleArn(requestData.notificationChannelRoleArn()); | ||
} | ||
|
||
if (!StringUtils.isEmpty(requestData.notificationChannelSnsTopicArn())) { | ||
notificationChannel.withSNSTopicArn(requestData.notificationChannelSnsTopicArn()); | ||
} | ||
|
||
startDocumentAnalysisRequest.withNotificationChannel(notificationChannel); | ||
} | ||
|
||
private void prepareOutput(StartDocumentAnalysisRequest startDocumentAnalysisRequest, TextractRequestData requestData) { | ||
if (StringUtils.isEmpty(requestData.outputConfigS3Bucket()) && | ||
StringUtils.isEmpty(requestData.outputConfigS3Prefix())) { | ||
return; | ||
} | ||
|
||
final OutputConfig outputConfig = new OutputConfig(); | ||
if (!StringUtils.isEmpty(requestData.outputConfigS3Bucket())) { | ||
outputConfig.withS3Bucket(requestData.outputConfigS3Bucket()); | ||
} | ||
|
||
if (!StringUtils.isEmpty(requestData.outputConfigS3Prefix())) { | ||
outputConfig.withS3Prefix(requestData.outputConfigS3Prefix()); | ||
} | ||
|
||
startDocumentAnalysisRequest.withOutputConfig(outputConfig); | ||
} | ||
|
||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...s-textract/src/main/java/io/camunda/connector/textract/caller/PollingTextractCalller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.camunda.connector.textract.caller; | ||
|
||
import com.amazonaws.services.textract.AmazonTextract; | ||
import com.amazonaws.services.textract.AmazonTextractAsync; | ||
import com.amazonaws.services.textract.model.GetDocumentAnalysisRequest; | ||
import com.amazonaws.services.textract.model.GetDocumentAnalysisResult; | ||
import com.amazonaws.services.textract.model.StartDocumentAnalysisRequest; | ||
import com.amazonaws.services.textract.model.StartDocumentAnalysisResult; | ||
import io.camunda.connector.textract.model.TextractRequestData; | ||
import io.camunda.connector.textract.model.TextractTask; | ||
|
||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
||
public class PollingTextractCalller implements TextractCaller<GetDocumentAnalysisResult> { | ||
public final short DELAY_BETWEEN_POLLING = 5; | ||
|
||
@Override | ||
public GetDocumentAnalysisResult call(TextractRequestData requestData, AmazonTextract textractClient) throws Exception { | ||
final StartDocumentAnalysisRequest startDocReq = new StartDocumentAnalysisRequest() | ||
.withFeatureTypes(this.prepareFeatureTypes(requestData)) | ||
.withDocumentLocation(this.prepareDocumentLocation(requestData)); | ||
|
||
final StartDocumentAnalysisResult result = textractClient.startDocumentAnalysis(startDocReq); | ||
final var documentAnalysisReq = new GetDocumentAnalysisRequest().withJobId(result.getJobId()); | ||
final var textractTask = new TextractTask(documentAnalysisReq, (AmazonTextractAsync) textractClient); | ||
|
||
ScheduledFuture<GetDocumentAnalysisResult> future; | ||
try (ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor()) { | ||
future = executorService.schedule(textractTask, 0, SECONDS); | ||
|
||
while (this.continuePolling(future.get().getJobStatus())) { | ||
future = executorService.schedule(textractTask, DELAY_BETWEEN_POLLING, SECONDS); | ||
} | ||
} | ||
|
||
return future.get(); | ||
} | ||
|
||
private boolean continuePolling(final String status) { | ||
if (status.equals("SUCCEEDED")) { | ||
return false; | ||
} | ||
return !status.equals("FAILED"); | ||
|
||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...s/aws-textract/src/main/java/io/camunda/connector/textract/caller/SyncTextractCaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.camunda.connector.textract.caller; | ||
|
||
import com.amazonaws.services.textract.AmazonTextract; | ||
import com.amazonaws.services.textract.model.AnalyzeDocumentRequest; | ||
import com.amazonaws.services.textract.model.AnalyzeDocumentResult; | ||
import com.amazonaws.services.textract.model.Document; | ||
import com.amazonaws.services.textract.model.S3Object; | ||
import io.camunda.connector.textract.model.TextractRequestData; | ||
|
||
public class SyncTextractCaller implements TextractCaller<AnalyzeDocumentResult>{ | ||
|
||
@Override | ||
public AnalyzeDocumentResult call(TextractRequestData requestData, AmazonTextract textractClient){ | ||
final S3Object s3Obj = this.prepareS3Obj(requestData); | ||
final Document document = new Document().withS3Object(s3Obj); | ||
|
||
final AnalyzeDocumentRequest analyzeDocumentRequest = new AnalyzeDocumentRequest() | ||
.withFeatureTypes(this.prepareFeatureTypes(requestData)) | ||
.withDocument(document); | ||
|
||
return textractClient.analyzeDocument(analyzeDocumentRequest); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...s/aws/aws-textract/src/main/java/io/camunda/connector/textract/caller/TextractCaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.camunda.connector.textract.caller; | ||
|
||
import com.amazonaws.AmazonWebServiceResult; | ||
import com.amazonaws.ResponseMetadata; | ||
import com.amazonaws.services.textract.AmazonTextract; | ||
import com.amazonaws.services.textract.AmazonTextractAsync; | ||
import com.amazonaws.services.textract.model.DocumentLocation; | ||
import com.amazonaws.services.textract.model.FeatureType; | ||
import com.amazonaws.services.textract.model.S3Object; | ||
import io.camunda.connector.textract.model.TextractRequestData; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public interface TextractCaller<T extends AmazonWebServiceResult<ResponseMetadata>> { | ||
|
||
T call(final TextractRequestData request, final AmazonTextract textractClient) throws Exception; | ||
|
||
default S3Object prepareS3Obj(final TextractRequestData requestData) { | ||
return new S3Object() | ||
.withBucket(requestData.documentS3Bucket()) | ||
.withName(requestData.documentName()) | ||
.withVersion(requestData.documentVersion()); | ||
} | ||
|
||
default Set<String> prepareFeatureTypes(final TextractRequestData request) { | ||
final Set<String> types = new HashSet<>(); | ||
if (request.analyzeForms()) types.add(FeatureType.FORMS.name()); | ||
if (request.analyzeLayout()) types.add(FeatureType.LAYOUT.name()); | ||
if (request.analyzeSignatures()) types.add(FeatureType.SIGNATURES.name()); | ||
if (request.analyzeTables()) types.add(FeatureType.TABLES.name()); | ||
return types; | ||
} | ||
|
||
default DocumentLocation prepareDocumentLocation(final TextractRequestData request) { | ||
final S3Object s3Obj = prepareS3Obj(request); | ||
return new DocumentLocation() | ||
.withS3Object(s3Obj); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...tors/aws/aws-textract/src/main/java/io/camunda/connector/textract/model/TextractTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.camunda.connector.textract.model; | ||
|
||
import com.amazonaws.services.textract.AmazonTextractAsync; | ||
import com.amazonaws.services.textract.model.GetDocumentAnalysisRequest; | ||
import com.amazonaws.services.textract.model.GetDocumentAnalysisResult; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
public class TextractTask implements Callable<GetDocumentAnalysisResult> { | ||
|
||
private final GetDocumentAnalysisRequest docAnalysisReq; | ||
|
||
private final AmazonTextractAsync amazonTextract; | ||
|
||
public TextractTask(GetDocumentAnalysisRequest documentAnalysisRequest, AmazonTextractAsync amazonTextract) { | ||
this.docAnalysisReq = documentAnalysisRequest; | ||
this.amazonTextract = amazonTextract; | ||
} | ||
|
||
@Override | ||
public GetDocumentAnalysisResult call() { | ||
return this.amazonTextract.getDocumentAnalysis(docAnalysisReq); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../src/main/java/io/camunda/connector/textract/suppliers/util/AmazonTextractClientUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.camunda.connector.textract.suppliers.util; | ||
|
||
import com.amazonaws.services.textract.AmazonTextract; | ||
import com.amazonaws.services.textract.AmazonTextractAsync; | ||
import com.amazonaws.services.textract.AmazonTextractAsyncClientBuilder; | ||
import com.amazonaws.services.textract.AmazonTextractClientBuilder; | ||
import io.camunda.connector.aws.CredentialsProviderSupport; | ||
import io.camunda.connector.textract.model.TextractRequest; | ||
|
||
public class AmazonTextractClientUtil { | ||
|
||
private AmazonTextractClientUtil() { | ||
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); | ||
} | ||
|
||
public static AmazonTextract getSyncTextractClient(final TextractRequest request) { | ||
return AmazonTextractClientBuilder.standard() | ||
.withCredentials(CredentialsProviderSupport.credentialsProvider(request)) | ||
.withRegion(request.getConfiguration().region()) | ||
.build(); | ||
} | ||
|
||
public static AmazonTextractAsync getAsyncTextractClient(final TextractRequest request) { | ||
return AmazonTextractAsyncClientBuilder.standard() | ||
.withCredentials(CredentialsProviderSupport.credentialsProvider(request)) | ||
.withRegion(request.getConfiguration().region()) | ||
.build(); | ||
} | ||
|
||
} |
Oops, something went wrong.