exceptionfactory commented on a change in pull request #5732:
URL: https://github.com/apache/nifi/pull/5732#discussion_r810528369



##########
File path: 
nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/document/ExtractDocumentText.java
##########
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.document;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.tika.Tika;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+@Tags({"extract, text, pdf, word, excel, powerpoint, office"})
+@CapabilityDescription("Run Apache Tika text extraction to extra the text from 
supported binary file formats such as PDF " +
+        "and Microsoft Office files.")
+public class ExtractDocumentText extends AbstractProcessor {
+    private static final String TEXT_PLAIN = "text/plain";
+
+    public static final Relationship REL_ORIGINAL = new 
Relationship.Builder().name("original")
+            .description("On successful extraction, the original flowfile goes 
here").build();
+
+    public static final Relationship REL_EXTRACTED = new 
Relationship.Builder().name("extracted")
+            .description("On successful extraction, the extracted content goes 
here").build();
+
+    public static final Relationship REL_FAILURE = new 
Relationship.Builder().name("failure")
+            .description("Content extraction failed").build();
+
+    private Set<Relationship> relationships = Collections.unmodifiableSet(new 
HashSet<>(Arrays.asList(REL_ORIGINAL, REL_EXTRACTED, REL_FAILURE)));
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return this.relationships;
+    }
+
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) throws ProcessException {
+        FlowFile flowFile = session.get();
+        if (flowFile == null) {
+            return;
+        }
+
+        FlowFile extracted = session.create(flowFile);
+        boolean error = false;
+        String mimeType = null;
+        try (InputStream is = session.read(flowFile);
+             Reader tikaReader = new Tika().parse(is);
+             OutputStream os = session.write(extracted);
+             OutputStreamWriter writer = new OutputStreamWriter(os)) {
+            IOUtils.copy(tikaReader, writer);
+            is.reset();
+            mimeType = new Tika().detect(is, 
flowFile.getAttribute(CoreAttributes.FILENAME.key()));
+        } catch (final Throwable t) {
+            getLogger().error("Extraction Failed {}", flowFile, t);
+            session.remove(extracted);
+            session.transfer(flowFile, REL_FAILURE);
+        } finally {
+            if (!error) {

Review comment:
       The `error` variable does not appear to be set in the previous try or 
catch blocks. Instead of setting and check a `boolean`, it looks like the 
successful operations could be moved inside the try block.

##########
File path: 
nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/document/ExtractDocumentText.java
##########
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.document;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.flowfile.attributes.CoreAttributes;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.tika.Tika;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+@Tags({"extract, text, pdf, word, excel, powerpoint, office"})
+@CapabilityDescription("Run Apache Tika text extraction to extra the text from 
supported binary file formats such as PDF " +
+        "and Microsoft Office files.")
+public class ExtractDocumentText extends AbstractProcessor {
+    private static final String TEXT_PLAIN = "text/plain";
+
+    public static final Relationship REL_ORIGINAL = new 
Relationship.Builder().name("original")
+            .description("On successful extraction, the original flowfile goes 
here").build();
+
+    public static final Relationship REL_EXTRACTED = new 
Relationship.Builder().name("extracted")
+            .description("On successful extraction, the extracted content goes 
here").build();
+
+    public static final Relationship REL_FAILURE = new 
Relationship.Builder().name("failure")
+            .description("Content extraction failed").build();
+
+    private Set<Relationship> relationships = Collections.unmodifiableSet(new 
HashSet<>(Arrays.asList(REL_ORIGINAL, REL_EXTRACTED, REL_FAILURE)));
+
+    @Override
+    public Set<Relationship> getRelationships() {
+        return this.relationships;
+    }
+
+    @Override
+    public void onTrigger(final ProcessContext context, final ProcessSession 
session) throws ProcessException {
+        FlowFile flowFile = session.get();
+        if (flowFile == null) {
+            return;
+        }
+
+        FlowFile extracted = session.create(flowFile);
+        boolean error = false;
+        String mimeType = null;
+        try (InputStream is = session.read(flowFile);
+             Reader tikaReader = new Tika().parse(is);
+             OutputStream os = session.write(extracted);
+             OutputStreamWriter writer = new OutputStreamWriter(os)) {
+            IOUtils.copy(tikaReader, writer);
+            is.reset();
+            mimeType = new Tika().detect(is, 
flowFile.getAttribute(CoreAttributes.FILENAME.key()));

Review comment:
       Is there a particular reason for reading the FlowFile content again in 
order to perform the content detection?  This seems duplicative of the 
`IdentityMimeType` processor, and thus does not seem essential to the purpose 
of text extraction. What do you think about moving the content type detection, 
which would avoid reading the contents multiple times and streamline this 
processor?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to