From 3b3d6d4eb2c9de250f22598f16b05811cf3ff17f Mon Sep 17 00:00:00 2001 From: veteranbv Date: Thu, 17 May 2018 12:47:19 -0400 Subject: [PATCH] NIFI-5171: fixed Yandex Jersey issues by adding dependency to POM and modified API call to now detect for languages Added license to YandexTranslate NOTICE file Updated to use StringUtils.isBlank for detecting sourceLanguage field being blank and languages to file to align with new logic Signed-off-by: Mark Payne --- .../src/main/resources/META-INF/NOTICE | 1 + .../nifi-yandex-processors/pom.xml | 7 ++++- .../processors/yandex/YandexTranslate.java | 26 ++++++++++++------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/nifi-nar-bundles/nifi-language-translation-bundle/nifi-language-translation-nar/src/main/resources/META-INF/NOTICE b/nifi-nar-bundles/nifi-language-translation-bundle/nifi-language-translation-nar/src/main/resources/META-INF/NOTICE index ff3e1806ee..93c10db53b 100644 --- a/nifi-nar-bundles/nifi-language-translation-bundle/nifi-language-translation-nar/src/main/resources/META-INF/NOTICE +++ b/nifi-nar-bundles/nifi-language-translation-bundle/nifi-language-translation-nar/src/main/resources/META-INF/NOTICE @@ -51,6 +51,7 @@ The following binary components are provided under the Common Development and Di (CDDL 1.1) (GPL2 w/ CPE) jersey-common (org.glassfish.jersey.core:jersey-common:jar:2.26 - https://jersey.github.io/) (CDDL 1.1) (GPL2 w/ CPE) jersey-entity-filtering (org.glassfish.jersey.ext:jersey-entity-filtering:jar:2.26 - https://jersey.github.io/) (CDDL 1.1) (GPL2 w/ CPE) jersey-media-json-jackson (org.glassfish.jersey.media:jersey-media-json-jackson:jar:2.26 - https://jersey.github.io/) + (CDDL 1.1) (GPL2 w/ CPE) jersey-hk2 (org.glassfish.jersey.inject:jersey-hk2:jar:2.26 - https://jersey.github.io/) (CDDL 1.1) (GPL2 w/ CPE) Old JAXB Runtime (com.sun.xml.bind:jaxb-impl:jar:2.2.3-1 - http://jaxb.java.net/) (CDDL 1.1) (GPL2 w/ CPE) Java Architecture For XML Binding (javax.xml.bind:jaxb-api:jar:2.2.2 - https://jaxb.dev.java.net/) (CDDL 1.1) (GPL2 w/ CPE) javax.ws.rs-api (javax.ws.rs:javax.ws.rs-api:jar:2.1 - http://jax-rs-spec.java.net) diff --git a/nifi-nar-bundles/nifi-language-translation-bundle/nifi-yandex-processors/pom.xml b/nifi-nar-bundles/nifi-language-translation-bundle/nifi-yandex-processors/pom.xml index d9a22d91ab..473a929cff 100644 --- a/nifi-nar-bundles/nifi-language-translation-bundle/nifi-yandex-processors/pom.xml +++ b/nifi-nar-bundles/nifi-language-translation-bundle/nifi-yandex-processors/pom.xml @@ -53,7 +53,12 @@ jersey-media-json-jackson ${jersey.version} - + + org.glassfish.jersey.inject + jersey-hk2 + ${jersey.version} + + org.apache.nifi nifi-mock diff --git a/nifi-nar-bundles/nifi-language-translation-bundle/nifi-yandex-processors/src/main/java/org/apache/nifi/processors/yandex/YandexTranslate.java b/nifi-nar-bundles/nifi-language-translation-bundle/nifi-yandex-processors/src/main/java/org/apache/nifi/processors/yandex/YandexTranslate.java index 5f1ae88758..f222cd96f7 100644 --- a/nifi-nar-bundles/nifi-language-translation-bundle/nifi-yandex-processors/src/main/java/org/apache/nifi/processors/yandex/YandexTranslate.java +++ b/nifi-nar-bundles/nifi-language-translation-bundle/nifi-yandex-processors/src/main/java/org/apache/nifi/processors/yandex/YandexTranslate.java @@ -16,6 +16,7 @@ */ package org.apache.nifi.processors.yandex; +import org.apache.commons.lang3.StringUtils; import org.apache.nifi.annotation.behavior.DynamicProperty; import org.apache.nifi.annotation.behavior.InputRequirement; import org.apache.nifi.annotation.behavior.InputRequirement.Requirement; @@ -71,8 +72,8 @@ import java.util.Set; @Tags({"yandex", "translate", "translation", "language"}) @CapabilityDescription("Translates content and attributes from one language to another") @WritesAttributes({ - @WritesAttribute(attribute = "yandex.translate.failure.reason", description = "If the text cannot be translated, this attribute will be set indicating the reason for the failure"), - @WritesAttribute(attribute = "language", description = "When the translation succeeds, if the content was translated, this attribute will be set indicating the new language of the content") + @WritesAttribute(attribute = "yandex.translate.failure.reason", description = "If the text cannot be translated, this attribute will be set indicating the reason for the failure"), + @WritesAttribute(attribute = "language", description = "When the translation succeeds, if the content was translated, this attribute will be set indicating the new language of the content") }) @DynamicProperty(name = "The name of an attribute to set that will contain the translated text of the value", value = "The value to translate", @@ -88,9 +89,8 @@ public class YandexTranslate extends AbstractProcessor { .build(); public static final PropertyDescriptor SOURCE_LANGUAGE = new PropertyDescriptor.Builder() .name("Input Language") - .description("The language of incoming data") - .required(true) - .defaultValue("es") + .description("The language of incoming data. If no language is set, Yandex will attempt to detect the incoming language automatically.") + .required(false) .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) .addValidator(new LanguageNameValidator()) .build(); @@ -211,10 +211,14 @@ public class YandexTranslate extends AbstractProcessor { protected Invocation prepareResource(final String key, final List text, final String sourceLanguage, final String destLanguage) { Invocation.Builder builder = client.target(URL).request(MediaType.APPLICATION_JSON); - final MultivaluedHashMap entity = new MultivaluedHashMap();; + final MultivaluedHashMap entity = new MultivaluedHashMap(); entity.put("text", text); entity.add("key", key); - entity.add("lang", sourceLanguage + "-" + destLanguage); + if ((StringUtils.isBlank(sourceLanguage))) { + entity.add("lang", destLanguage); + } else { + entity.add("lang", sourceLanguage + "-" + destLanguage); + } return builder.buildPost(Entity.form(entity)); } @@ -266,7 +270,7 @@ public class YandexTranslate extends AbstractProcessor { if (response.getStatus() != Response.Status.OK.getStatusCode()) { getLogger().error("Failed to translate text using Yandex for {}; response was {}: {}; routing to {}", new Object[]{ - flowFile, response.getStatus(), response.getStatusInfo().getReasonPhrase(), REL_TRANSLATION_FAILED.getName()}); + flowFile, response.getStatus(), response.getStatusInfo().getReasonPhrase(), REL_TRANSLATION_FAILED.getName()}); flowFile = session.putAttribute(flowFile, "yandex.translate.failure.reason", response.getStatusInfo().getReasonPhrase()); session.transfer(flowFile, REL_TRANSLATION_FAILED); return; @@ -306,6 +310,10 @@ public class YandexTranslate extends AbstractProcessor { @Override public ValidationResult validate(final String subject, final String input, final ValidationContext context) { + if ((StringUtils.isBlank(input))) { + return new ValidationResult.Builder().subject(subject).input(input).valid(true).explanation("No Language Input Present").build(); + } + if (context.isExpressionLanguagePresent(input)) { return new ValidationResult.Builder().subject(subject).input(input).valid(true).explanation("Expression Language Present").build(); } @@ -318,4 +326,4 @@ public class YandexTranslate extends AbstractProcessor { } } -} +} \ No newline at end of file