You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2019/09/12 08:50:35 UTC

[camel] 01/02: CAMEL-13973 - Camel-AWS Translate: Detect the source language automatically

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 0578a9932fd02c9787869f616baddea5e9bf2cc6
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu Sep 12 10:45:20 2019 +0200

    CAMEL-13973 - Camel-AWS Translate: Detect the source language automatically
---
 .../src/main/docs/aws-translate-component.adoc     |  3 +-
 .../aws/translate/TranslateConfiguration.java      | 13 +++++++++
 .../component/aws/translate/TranslateProducer.java | 32 ++++++++++++++--------
 .../TranslateProducerIntegrationTest.java          | 20 ++++++++++++++
 .../TranslateComponentConfiguration.java           | 12 ++++++++
 5 files changed, 68 insertions(+), 12 deletions(-)

diff --git a/components/camel-aws-translate/src/main/docs/aws-translate-component.adoc b/components/camel-aws-translate/src/main/docs/aws-translate-component.adoc
index 4aff367..0416d09 100644
--- a/components/camel-aws-translate/src/main/docs/aws-translate-component.adoc
+++ b/components/camel-aws-translate/src/main/docs/aws-translate-component.adoc
@@ -63,13 +63,14 @@ with the following path and query parameters:
 |===
 
 
-=== Query Parameters (10 parameters):
+=== Query Parameters (11 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
 | *accessKey* (producer) | Amazon AWS Access Key |  | String
+| *autodetectSourceLanguage* (producer) | Being able to autodetect the source language | false | boolean
 | *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
 | *operation* (producer) | *Required* The operation to perform |  | TranslateOperations
 | *proxyHost* (producer) | To define a proxy host when instantiating the Translate client |  | String
diff --git a/components/camel-aws-translate/src/main/java/org/apache/camel/component/aws/translate/TranslateConfiguration.java b/components/camel-aws-translate/src/main/java/org/apache/camel/component/aws/translate/TranslateConfiguration.java
index 2c2833f..5fe21de 100644
--- a/components/camel-aws-translate/src/main/java/org/apache/camel/component/aws/translate/TranslateConfiguration.java
+++ b/components/camel-aws-translate/src/main/java/org/apache/camel/component/aws/translate/TranslateConfiguration.java
@@ -45,6 +45,8 @@ public class TranslateConfiguration implements Cloneable {
     private Integer proxyPort;
     @UriParam
     private String region;
+    @UriParam(label = "producer", defaultValue = "false")
+    private boolean autodetectSourceLanguage = false;
 
     public AmazonTranslate getTranslateClient() {
         return translateClient;
@@ -125,6 +127,17 @@ public class TranslateConfiguration implements Cloneable {
     public void setRegion(String region) {
         this.region = region;
     }
+    
+    public boolean isAutodetectSourceLanguage() {
+        return autodetectSourceLanguage;
+    }
+
+    /**
+     * Being able to autodetect the source language
+     */
+    public void setAutodetectSourceLanguage(boolean autodetectSourceLanguage) {
+        this.autodetectSourceLanguage = autodetectSourceLanguage;
+    }
 
     // *************************************************
     //
diff --git a/components/camel-aws-translate/src/main/java/org/apache/camel/component/aws/translate/TranslateProducer.java b/components/camel-aws-translate/src/main/java/org/apache/camel/component/aws/translate/TranslateProducer.java
index 76f16e1..1634c9f 100644
--- a/components/camel-aws-translate/src/main/java/org/apache/camel/component/aws/translate/TranslateProducer.java
+++ b/components/camel-aws-translate/src/main/java/org/apache/camel/component/aws/translate/TranslateProducer.java
@@ -16,11 +16,6 @@
  */
 package org.apache.camel.component.aws.translate;
 
-import com.amazonaws.AmazonServiceException;
-import com.amazonaws.services.translate.AmazonTranslate;
-import com.amazonaws.services.translate.model.TranslateTextRequest;
-import com.amazonaws.services.translate.model.TranslateTextResult;
-
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
@@ -28,6 +23,11 @@ import org.apache.camel.support.DefaultProducer;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.URISupport;
 
+import com.amazonaws.AmazonServiceException;
+import com.amazonaws.services.translate.AmazonTranslate;
+import com.amazonaws.services.translate.model.TranslateTextRequest;
+import com.amazonaws.services.translate.model.TranslateTextResult;
+
 /**
  * A Producer which sends messages to the Amazon Translate Service
  * <a href="http://aws.amazon.com/translate/">AWS Translate</a>
@@ -78,13 +78,23 @@ public class TranslateProducer extends DefaultProducer {
 
     private void translateText(AmazonTranslate translateClient, Exchange exchange) {
         TranslateTextRequest request = new TranslateTextRequest();
-        String source = exchange.getIn().getHeader(TranslateConstants.SOURCE_LANGUAGE, String.class);
-        String target = exchange.getIn().getHeader(TranslateConstants.TARGET_LANGUAGE, String.class);
-        if (ObjectHelper.isEmpty(source) || ObjectHelper.isEmpty(target)) {
-            throw new IllegalArgumentException("Source and target language must be specified");
+        if (!getConfiguration().isAutodetectSourceLanguage()) {
+            String source = exchange.getIn().getHeader(TranslateConstants.SOURCE_LANGUAGE, String.class);
+            String target = exchange.getIn().getHeader(TranslateConstants.TARGET_LANGUAGE, String.class);
+            if (ObjectHelper.isEmpty(source) || ObjectHelper.isEmpty(target)) {
+                throw new IllegalArgumentException("Source and target language must be specified");
+            }
+            request.setSourceLanguageCode(source);
+            request.setTargetLanguageCode(target);
+        } else {
+            String source = "auto";
+            String target = exchange.getIn().getHeader(TranslateConstants.TARGET_LANGUAGE, String.class);
+            if (ObjectHelper.isEmpty(source) || ObjectHelper.isEmpty(target)) {
+                throw new IllegalArgumentException("Target language must be specified when autodetection of source language is enabled");
+            }
+            request.setSourceLanguageCode(source);
+            request.setTargetLanguageCode(target);
         }
-        request.setSourceLanguageCode(source);
-        request.setTargetLanguageCode(target);
         request.setText(exchange.getMessage().getBody(String.class));
         TranslateTextResult result;
         try {
diff --git a/components/camel-aws-translate/src/test/java/org/apache/camel/component/aws/translate/integration/TranslateProducerIntegrationTest.java b/components/camel-aws-translate/src/test/java/org/apache/camel/component/aws/translate/integration/TranslateProducerIntegrationTest.java
index 9118b7d..1a937d0 100644
--- a/components/camel-aws-translate/src/test/java/org/apache/camel/component/aws/translate/integration/TranslateProducerIntegrationTest.java
+++ b/components/camel-aws-translate/src/test/java/org/apache/camel/component/aws/translate/integration/TranslateProducerIntegrationTest.java
@@ -53,6 +53,25 @@ public class TranslateProducerIntegrationTest extends CamelTestSupport {
         String resultGet = (String)exchange.getIn().getBody();
         assertEquals("Hallo, Miss.", resultGet);
     }
+    
+    @Test
+    public void translateTextAutodetectSourceTest() throws Exception {
+
+        mock.expectedMessageCount(1);
+        Exchange exchange = template.request("direct:translateTextAuto", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(TranslateConstants.OPERATION, TranslateOperations.translateText);
+                exchange.getIn().setHeader(TranslateConstants.TARGET_LANGUAGE, TranslateLanguageEnum.GERMAN);
+                exchange.getIn().setBody("Ciao Signorina");
+            }
+        });
+
+        assertMockEndpointsSatisfied();
+
+        String resultGet = (String)exchange.getIn().getBody();
+        assertEquals("Hallo, Miss.", resultGet);
+    }
 
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
@@ -60,6 +79,7 @@ public class TranslateProducerIntegrationTest extends CamelTestSupport {
             @Override
             public void configure() throws Exception {
                 from("direct:translateText").to("aws-translate://test?accessKey=RAW(xxxx)&secretKey=RAW(xxxx)&region=EU_WEST_1&operation=translateText").to("mock:result");
+                from("direct:translateTextAuto").to("aws-translate://test?accessKey=RAW(xxxx)&secretKey=RAW(xxxx)&region=EU_WEST_1&operation=translateText&autodetectSourceLanguage=true").to("mock:result");
             }
         };
     }
diff --git a/platforms/spring-boot/components-starter/camel-aws-translate-starter/src/main/java/org/apache/camel/component/aws/translate/springboot/TranslateComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-aws-translate-starter/src/main/java/org/apache/camel/component/aws/translate/springboot/TranslateComponentConfiguration.java
index 0f3a6bc..9de7b56 100644
--- a/platforms/spring-boot/components-starter/camel-aws-translate-starter/src/main/java/org/apache/camel/component/aws/translate/springboot/TranslateComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-aws-translate-starter/src/main/java/org/apache/camel/component/aws/translate/springboot/TranslateComponentConfiguration.java
@@ -134,6 +134,10 @@ public class TranslateComponentConfiguration
          * Regions.EU_WEST_1.name()
          */
         private String region;
+        /**
+         * Being able to autodetect the source language
+         */
+        private Boolean autodetectSourceLanguage = false;
 
         public AmazonTranslate getTranslateClient() {
             return translateClient;
@@ -190,5 +194,13 @@ public class TranslateComponentConfiguration
         public void setRegion(String region) {
             this.region = region;
         }
+
+        public Boolean getAutodetectSourceLanguage() {
+            return autodetectSourceLanguage;
+        }
+
+        public void setAutodetectSourceLanguage(Boolean autodetectSourceLanguage) {
+            this.autodetectSourceLanguage = autodetectSourceLanguage;
+        }
     }
 }
\ No newline at end of file