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 <markap14@hotmail.com>
This commit is contained in:
veteranbv 2018-05-17 12:47:19 -04:00 committed by Mark Payne
parent 1597492fed
commit 3b3d6d4eb2
3 changed files with 24 additions and 10 deletions

View File

@ -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)

View File

@ -53,7 +53,12 @@
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-mock</artifactId>

View File

@ -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<String> 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 {
}
}
}
}