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



##########
File path: 
nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/document/ExtractDocumentText.java
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.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;
+        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);
+        } catch (final Throwable t) {
+            error = true;
+            getLogger().error("Extraction Failed {}", flowFile, t);
+            session.remove(extracted);
+            session.transfer(flowFile, REL_FAILURE);
+        } finally {
+            if (!error) {
+                Map<String, String> mimeAttrs = new HashMap<>();
+                mimeAttrs.put("mime.type", TEXT_PLAIN);
+                extracted = session.putAllAttributes(extracted, mimeAttrs);
+                session.transfer(extracted, REL_EXTRACTED);
+                session.transfer(flowFile, REL_ORIGINAL);
+            }

Review comment:
       > Is there a reason for setting and checking the error flag versus just 
calling the successful transfer in the try block?
   
   I don't know if it was fixed recently, but traditionally, `transfer` 
wouldn't work if the input/output streams are still open. Didn't want to defeat 
the purpose of try-with block.




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