diff --git a/nifi-assembly/LICENSE b/nifi-assembly/LICENSE index 5e6b490f41..5abc79a51c 100644 --- a/nifi-assembly/LICENSE +++ b/nifi-assembly/LICENSE @@ -913,3 +913,33 @@ The binary distribution of this product bundles 'leveldbjni-all-1.8.jar' which i The binary distribution of this product bundles 'Woodstox StAX 2 API' which is "licensed under standard BSD license" + +This product bundles 'Adobe XMPCore' which is available under "The BSD license". More +information can be found here: http://www.adobe.com/devnet/xmp/library/eula-xmp-library-java.html + + Copyright (c) 2009, Adobe Systems Incorporated All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of Adobe Systems Incorporated, nor the names of its + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANT ABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/nifi-assembly/NOTICE b/nifi-assembly/NOTICE index 4efa96f2bb..3362740c1a 100644 --- a/nifi-assembly/NOTICE +++ b/nifi-assembly/NOTICE @@ -704,6 +704,11 @@ The following binary components are provided under the Apache Software License v Google Guice - Extensions - Servlet Copyright 2006-2011 Google, Inc. + (ASLv2) Metadata-Extractor + The following NOTICE information applies: + Metadata-Extractor + Copyright 2002-2015 Drew Noakes + ************************ Common Development and Distribution License 1.1 ************************ diff --git a/nifi-assembly/pom.xml b/nifi-assembly/pom.xml index a712b86638..bd4a5aac6c 100644 --- a/nifi-assembly/pom.xml +++ b/nifi-assembly/pom.xml @@ -217,6 +217,12 @@ language governing permissions and limitations under the License. --> nifi-ambari-nar nar + + org.apache.nifi + nifi-image-nar + 0.3.0-SNAPSHOT + nar + diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-nar/pom.xml b/nifi-nar-bundles/nifi-image-bundle/nifi-image-nar/pom.xml new file mode 100644 index 0000000000..760e67f1e2 --- /dev/null +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-nar/pom.xml @@ -0,0 +1,37 @@ + + + + 4.0.0 + + + org.apache.nifi + nifi-image-bundle + 0.3.0-SNAPSHOT + + + nifi-image-nar + 0.3.0-SNAPSHOT + nar + + + + org.apache.nifi + nifi-image-processors + 0.3.0-SNAPSHOT + + + + diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-nar/src/main/resources/META-INF/LICENSE b/nifi-nar-bundles/nifi-image-bundle/nifi-image-nar/src/main/resources/META-INF/LICENSE new file mode 100644 index 0000000000..2c3e42f5e1 --- /dev/null +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-nar/src/main/resources/META-INF/LICENSE @@ -0,0 +1,239 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed 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. + +APACHE NIFI SUBCOMPONENTS: + +The Apache NiFi project contains subcomponents with separate copyright +notices and license terms. Your use of the source code for the these +subcomponents is subject to the terms and conditions of the following +licenses. + +This product bundles 'Adobe XMPCore' which is available under "The BSD license". More +information can be found here: http://www.adobe.com/devnet/xmp/library/eula-xmp-library-java.html + + Copyright (c) 2009, Adobe Systems Incorporated All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of Adobe Systems Incorporated, nor the names of its + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANT ABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-nar/src/main/resources/META-INF/NOTICE b/nifi-nar-bundles/nifi-image-bundle/nifi-image-nar/src/main/resources/META-INF/NOTICE new file mode 100644 index 0000000000..44658fbcf9 --- /dev/null +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-nar/src/main/resources/META-INF/NOTICE @@ -0,0 +1,16 @@ +nifi-image-nar +Copyright 2015 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + +****************** +Apache Software License v2 +****************** + +The following binary components are provided under the Apache Software License v2 + + (ASLv2) Metadata-Extractor + The following NOTICE information applies: + Metadata-Extractor + Copyright 2002-2015 Drew Noakes diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/pom.xml b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/pom.xml new file mode 100644 index 0000000000..4b9bc37bbb --- /dev/null +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/pom.xml @@ -0,0 +1,62 @@ + + + + 4.0.0 + + + org.apache.nifi + nifi-image-bundle + 0.3.0-SNAPSHOT + + + nifi-image-processors + jar + + + + org.apache.nifi + nifi-api + + + org.apache.nifi + nifi-processor-utils + + + org.apache.nifi + nifi-mock + test + + + com.drewnoakes + metadata-extractor + 2.7.2 + + + + + + + org.apache.rat + apache-rat-plugin + + + src/test/resources/notImage.txt + + + + + + diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java new file mode 100644 index 0000000000..7fe6195e4d --- /dev/null +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java @@ -0,0 +1,165 @@ +/* + * 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.image; + +import com.drew.imaging.ImageMetadataReader; +import com.drew.imaging.ImageProcessingException; +import com.drew.metadata.Directory; +import com.drew.metadata.Metadata; +import com.drew.metadata.Tag; +import org.apache.nifi.annotation.behavior.SupportsBatching; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.logging.ProcessorLog; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.ProcessorInitializationContext; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.io.InputStreamCallback; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.util.ObjectHolder; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.HashSet; +import java.util.Map; +import java.util.HashMap; + +@Tags({"Exif", "Exchangeable", "image", "file", "format", "JPG", "GIF", "PNG", "BMP", "metadata","IPTC", "XMP"}) +@CapabilityDescription("Extract the image metadata from flowfiles containing images. This processor relies on this " + + "metadata extractor library https://github.com/drewnoakes/metadata-extractor. It extracts a long list of " + + "metadata types including but not limited to EXIF, IPTC, XMP and Photoshop fields. For the full list visit " + + "the library's website." + + "NOTE: The library being used loads the images into memory so extremely large images may cause problems.") +@WritesAttributes({@WritesAttribute(attribute = ".", description = "The extracted image metadata " + + "will be inserted with the attribute name \".\". ")}) +@SupportsBatching +public class ExtractImageMetadata extends AbstractProcessor { + + public static final PropertyDescriptor MAX_NUMBER_OF_ATTRIBUTES = new PropertyDescriptor.Builder() + .name("Max Number of Attributes") + .description("Specify the max number of attributes to add to the flowfile. There is no guarantee in what order" + + " the tags will be processed. By default it will process all of them.") + .required(false) + .addValidator(StandardValidators.NON_NEGATIVE_INTEGER_VALIDATOR) + .build(); + + public static final Relationship SUCCESS = new Relationship.Builder() + .name("success") + .description("Any FlowFile that successfully has image metadata extracted will be routed to success") + .build(); + + public static final Relationship FAILURE = new Relationship.Builder() + .name("failure") + .description("Any FlowFile that fails to have image metadata extracted will be routed to failure") + .build(); + + private Set relationships; + private List properties; + + @Override + protected void init(final ProcessorInitializationContext context) { + + final List properties = new ArrayList<>(); + properties.add(MAX_NUMBER_OF_ATTRIBUTES); + this.properties = Collections.unmodifiableList(properties); + + final Set relationships = new HashSet<>(); + relationships.add(SUCCESS); + relationships.add(FAILURE); + this.relationships = Collections.unmodifiableSet(relationships); + } + + @Override + public Set getRelationships() { + return this.relationships; + } + + @Override + protected List getSupportedPropertyDescriptors() { + return this.properties; + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + FlowFile flowfile = session.get(); + if (flowfile == null) { + return; + } + + final ProcessorLog logger = this.getLogger(); + final ObjectHolder value = new ObjectHolder<>(null); + final Integer max = context.getProperty(MAX_NUMBER_OF_ATTRIBUTES).asInteger(); + + try { + session.read(flowfile, new InputStreamCallback() { + @Override + public void process(InputStream in) throws IOException { + try { + Metadata imageMetadata = ImageMetadataReader.readMetadata(in); + value.set(imageMetadata); + } catch (ImageProcessingException ex) { + throw new ProcessException(ex); + } + } + }); + + Metadata metadata = value.get(); + Map results = getTags(max, metadata); + + // Write the results to an attribute + if (!results.isEmpty()) { + flowfile = session.putAllAttributes(flowfile, results); + } + + session.transfer(flowfile, SUCCESS); + } catch (ProcessException e) { + logger.error("Failed to extract image metadata from {} due to {}", new Object[]{flowfile, e}); + session.transfer(flowfile, FAILURE); + } + } + + private Map getTags(Integer max, Metadata metadata) { + Map results = new HashMap<>(); + int i =0; + + for (Directory directory : metadata.getDirectories()) { + for (Tag tag : directory.getTags()) { + results.put(directory.getName() + "." + tag.getTagName(), tag.getDescription()); + + if(max!=null) { + i++; + if (i >= max) { + return results; + } + } + } + } + + return results; + } +} diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/java/org/apache/nifi/processors/image/ResizeImage.java b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/java/org/apache/nifi/processors/image/ResizeImage.java new file mode 100644 index 0000000000..c085b5fd5c --- /dev/null +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/java/org/apache/nifi/processors/image/ResizeImage.java @@ -0,0 +1,194 @@ +/* + * 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.image; + +import org.apache.nifi.annotation.behavior.EventDriven; +import org.apache.nifi.annotation.behavior.SupportsBatching; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +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.nifi.processor.io.StreamCallback; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.util.StopWatch; + +import javax.imageio.ImageIO; +import javax.imageio.ImageReader; +import javax.imageio.stream.ImageInputStream; +import java.awt.Image; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Set; +import java.util.HashSet; +import java.util.List; +import java.util.Iterator; +import java.util.concurrent.TimeUnit; + +@EventDriven +@SupportsBatching +@Tags({ "resize", "image", "jpg", "jpeg", "png", "bmp", "wbmp", "gif" }) +@CapabilityDescription("Resizes an image to user-specified dimensions. This Processor uses the image codecs registered with the " + + "environment that NiFi is running in. By default, this includes JPEG, PNG, BMP, WBMP, and GIF images.") +public class ResizeImage extends AbstractProcessor { + static final AllowableValue RESIZE_DEFAULT = new AllowableValue("Default", "Default", "Use the default algorithm"); + static final AllowableValue RESIZE_FAST = new AllowableValue("Scale Fast", "Scale Fast", "Emphasize speed of the scaling over smoothness"); + static final AllowableValue RESIZE_SMOOTH = new AllowableValue("Scale Smooth", "Scale Smooth", "Emphasize smoothness of the scaling over speed"); + static final AllowableValue RESIZE_REPLICATE = new AllowableValue("Replicate Scale Filter", "Replicate Scale Filter", "Use the Replicate Scale Filter algorithm"); + static final AllowableValue RESIZE_AREA_AVERAGING = new AllowableValue("Area Averaging", "Area Averaging", "Use the Area Averaging scaling algorithm"); + + static final PropertyDescriptor IMAGE_WIDTH = new PropertyDescriptor.Builder() + .name("Image Width (in pixels)") + .description("The desired number of pixels for the image's width") + .required(true) + .expressionLanguageSupported(true) + .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR) + .build(); + static final PropertyDescriptor IMAGE_HEIGHT = new PropertyDescriptor.Builder() + .name("Image Height (in pixels)") + .description("The desired number of pixels for the image's height") + .required(true) + .expressionLanguageSupported(true) + .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR) + .build(); + static final PropertyDescriptor SCALING_ALGORITHM = new PropertyDescriptor.Builder() + .name("Scaling Algorithm") + .description("Specifies which algorithm should be used to resize the image") + .required(true) + .allowableValues(RESIZE_DEFAULT, RESIZE_FAST, RESIZE_SMOOTH, RESIZE_REPLICATE, RESIZE_AREA_AVERAGING) + .defaultValue(RESIZE_DEFAULT.getValue()) + .build(); + + static final Relationship REL_SUCCESS = new Relationship.Builder() + .name("success") + .description("A FlowFile is routed to this relationship if it is successfully resized") + .build(); + static final Relationship REL_FAILURE = new Relationship.Builder() + .name("failure") + .description("A FlowFile is routed to this relationship if it is not in the specified format") + .build(); + + @Override + protected List getSupportedPropertyDescriptors() { + final List properties = new ArrayList<>(); + properties.add(IMAGE_WIDTH); + properties.add(IMAGE_HEIGHT); + properties.add(SCALING_ALGORITHM); + return properties; + } + + @Override + public Set getRelationships() { + final Set relationships = new HashSet<>(); + relationships.add(REL_SUCCESS); + relationships.add(REL_FAILURE); + return relationships; + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + FlowFile flowFile = session.get(); + if (flowFile == null) { + return; + } + + final int width, height; + try { + width = context.getProperty(IMAGE_WIDTH).evaluateAttributeExpressions(flowFile).asInteger(); + height = context.getProperty(IMAGE_HEIGHT).evaluateAttributeExpressions(flowFile).asInteger(); + } catch (final NumberFormatException nfe) { + getLogger().error("Failed to resize {} due to {}", new Object[] { flowFile, nfe }); + session.transfer(flowFile, REL_FAILURE); + return; + } + + final String algorithm = context.getProperty(SCALING_ALGORITHM).getValue(); + final int hints; + if (algorithm.equalsIgnoreCase(RESIZE_DEFAULT.getValue())) { + hints = Image.SCALE_DEFAULT; + } else if (algorithm.equalsIgnoreCase(RESIZE_FAST.getValue())) { + hints = Image.SCALE_FAST; + } else if (algorithm.equalsIgnoreCase(RESIZE_SMOOTH.getValue())) { + hints = Image.SCALE_SMOOTH; + } else if (algorithm.equalsIgnoreCase(RESIZE_REPLICATE.getValue())) { + hints = Image.SCALE_REPLICATE; + } else if (algorithm.equalsIgnoreCase(RESIZE_AREA_AVERAGING.getValue())) { + hints = Image.SCALE_AREA_AVERAGING; + } else { + throw new AssertionError("Invalid Scaling Algorithm: " + algorithm); + } + + final StopWatch stopWatch = new StopWatch(true); + try { + flowFile = session.write(flowFile, new StreamCallback() { + @Override + public void process(final InputStream rawIn, final OutputStream out) throws IOException { + try (final BufferedInputStream in = new BufferedInputStream(rawIn)) { + final ImageInputStream iis = ImageIO.createImageInputStream(in); + if (iis == null) { + throw new ProcessException("FlowFile is not in a valid format"); + } + + final Iterator readers = ImageIO.getImageReaders(iis); + if (!readers.hasNext()) { + throw new ProcessException("FlowFile is not in a valid format"); + } + + final ImageReader reader = readers.next(); + final String formatName = reader.getFormatName(); + reader.setInput(iis, true); + final BufferedImage image = reader.read(0); + + final Image scaledImage = image.getScaledInstance(width, height, hints); + final BufferedImage scaledBufferedImg; + if (scaledImage instanceof BufferedImage) { + scaledBufferedImg = (BufferedImage) scaledImage; + } else { + scaledBufferedImg = new BufferedImage(scaledImage.getWidth(null), scaledImage.getHeight(null), image.getType()); + final Graphics2D graphics = scaledBufferedImg.createGraphics(); + try { + graphics.drawImage(scaledImage, 0, 0, null); + } finally { + graphics.dispose(); + } + } + + ImageIO.write(scaledBufferedImg, formatName, out); + } + } + }); + + session.getProvenanceReporter().modifyContent(flowFile, stopWatch.getElapsed(TimeUnit.MILLISECONDS)); + session.transfer(flowFile, REL_SUCCESS); + } catch (final ProcessException pe) { + getLogger().error("Failed to resize {} due to {}", new Object[] { flowFile, pe }); + session.transfer(flowFile, REL_FAILURE); + } + } + +} diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor new file mode 100644 index 0000000000..224cb54172 --- /dev/null +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor @@ -0,0 +1,16 @@ +# 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. +org.apache.nifi.processors.image.ExtractImageMetadata +org.apache.nifi.processors.image.ResizeImage \ No newline at end of file diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java new file mode 100644 index 0000000000..cf31e15e54 --- /dev/null +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java @@ -0,0 +1,161 @@ +/* + * 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.image; + +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + + +public class ExtractImageMetadataTest { + private static String BMP_HEADER = "BMP Header."; + private static String JPEG_HEADER = "JPEG."; + private static String GIF_HEADER = "GIF Header."; + private static String PNG_HEADER = "PNG."; + + private TestRunner testRunner; + + @Before + public void init() { + testRunner = TestRunners.newTestRunner(ExtractImageMetadata.class); + } + + @Test + public void testFailedExtraction() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/notImage.txt", ExtractImageMetadata.FAILURE,null); + } + + @Test + public void testExtractJPG() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/simple.jpg", ExtractImageMetadata.SUCCESS,null); + Map attributes = flowFile.getAttributes(); + + assertEquals("800 pixels", attributes.get(JPEG_HEADER + "Image Width")); + assertEquals("600 pixels", attributes.get(JPEG_HEADER + "Image Height")); + assertEquals("8 bits", attributes.get(JPEG_HEADER + "Data Precision")); + assertEquals("Baseline", attributes.get(JPEG_HEADER + "Compression Type")); + assertEquals("3", attributes.get(JPEG_HEADER + "Number of Components")); + assertEquals("Y component: Quantization table 0, Sampling factors 2 horiz/2 vert", + attributes.get(JPEG_HEADER + "Component 1")); + assertEquals("Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert", + attributes.get(JPEG_HEADER + "Component 2")); + assertEquals("Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert", + attributes.get(JPEG_HEADER + "Component 3")); + } + + @Test + public void testExtractGIF() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow( + "src/test/resources/photoshop-8x12-32colors-alpha.gif", ExtractImageMetadata.SUCCESS,null); + Map attributes = flowFile.getAttributes(); + + assertEquals("8", attributes.get(GIF_HEADER + "Image Width")); + assertEquals("12", attributes.get(GIF_HEADER + "Image Height")); + assertEquals("true", attributes.get(GIF_HEADER + "Has Global Color Table")); + assertEquals("32", attributes.get(GIF_HEADER + "Color Table Size")); + assertEquals("8", attributes.get(GIF_HEADER + "Transparent Color Index")); + assertEquals("89a", attributes.get(GIF_HEADER + "GIF Format Version")); + assertEquals("5", attributes.get(GIF_HEADER + "Bits per Pixel")); + assertEquals("false", attributes.get(GIF_HEADER + "Is Color Table Sorted")); + } + + @Test + public void testExtractPNG() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/mspaint-8x10.png", ExtractImageMetadata.SUCCESS, null); + Map attributes = flowFile.getAttributes(); + + assertEquals("8", attributes.get(PNG_HEADER + "Image Width")); + assertEquals("12", attributes.get(PNG_HEADER + "Image Height")); + assertEquals("0.45455", attributes.get(PNG_HEADER + "Image Gamma")); + assertEquals("Deflate", attributes.get(PNG_HEADER + "Compression Type")); + assertEquals("No Interlace", attributes.get(PNG_HEADER + "Interlace Method")); + assertEquals("Perceptual", attributes.get(PNG_HEADER + "sRGB Rendering Intent")); + assertEquals("Adaptive", attributes.get(PNG_HEADER + "Filter Method")); + assertEquals("8", attributes.get(PNG_HEADER + "Bits Per Sample")); + assertEquals("True Color", attributes.get(PNG_HEADER + "Color Type")); + } + @Test + public void testExtractBMP() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/16color-10x10.bmp", ExtractImageMetadata.SUCCESS, null); + Map attributes = flowFile.getAttributes(); + + assertEquals("10", attributes.get(BMP_HEADER+"Image Width")); + assertEquals("10", attributes.get(BMP_HEADER+"Image Height")); + assertEquals("4", attributes.get(BMP_HEADER+"Bits Per Pixel")); + assertEquals("None", attributes.get(BMP_HEADER+"Compression")); + assertEquals("0", attributes.get(BMP_HEADER+"X Pixels per Meter")); + assertEquals("0", attributes.get(BMP_HEADER+"Y Pixels per Meter")); + assertEquals("0", attributes.get(BMP_HEADER+"Palette Colour Count")); + assertEquals("0", attributes.get(BMP_HEADER+"Important Colour Count")); + assertEquals("1", attributes.get(BMP_HEADER+"Planes")); + assertEquals("40", attributes.get(BMP_HEADER+"Header Size")); + } + @Test + public void testExtractLimitedAttributesBMP() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/16color-10x10.bmp", ExtractImageMetadata.SUCCESS, "5"); + Map attributes = flowFile.getAttributes(); + + assertEquals("10", attributes.get(BMP_HEADER+"Image Width")); + assertEquals("10", attributes.get(BMP_HEADER+"Image Height")); + assertEquals("4", attributes.get(BMP_HEADER+"Bits Per Pixel")); + assertEquals("1", attributes.get(BMP_HEADER+"Planes")); + assertEquals("40", attributes.get(BMP_HEADER+"Header Size")); + + + assertNull(attributes.get(BMP_HEADER + "Compression")); + assertNull(attributes.get(BMP_HEADER + "X Pixels per Meter")); + assertNull(attributes.get(BMP_HEADER + "Y Pixels per Meter")); + assertNull(attributes.get(BMP_HEADER + "Palette Colour Count")); + assertNull(attributes.get(BMP_HEADER + "Important Colour Count")); + } + + public MockFlowFile verifyTestRunnerFlow(String pathStr,Relationship rel, String max) throws IOException { + Path path = Paths.get(pathStr); + testRunner.enqueue(path); + if(max != null) { + testRunner.setProperty(ExtractImageMetadata.MAX_NUMBER_OF_ATTRIBUTES, max); + } + + testRunner.run(); + testRunner.assertAllFlowFilesTransferred(rel, 1); + + + MockFlowFile flowFile = testRunner.getFlowFilesForRelationship(rel).get(0); + testRunner.assertQueueEmpty(); + + testRunner.enqueue(flowFile); + testRunner.clearTransferState(); + testRunner.run(); + testRunner.assertAllFlowFilesTransferred(rel, 1); + + flowFile = testRunner.getFlowFilesForRelationship(rel).get(0); + flowFile.assertContentEquals(new File(pathStr)); + return flowFile; + } +} diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/java/org/apache/nifi/processors/image/TestResizeImage.java b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/java/org/apache/nifi/processors/image/TestResizeImage.java new file mode 100644 index 0000000000..53bf17ea08 --- /dev/null +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/java/org/apache/nifi/processors/image/TestResizeImage.java @@ -0,0 +1,57 @@ +/* + * 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.image; + +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Test; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; + +import static org.junit.Assert.assertEquals; + +public class TestResizeImage { + + @Test + public void testResize() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ResizeImage()); + runner.setProperty(ResizeImage.IMAGE_HEIGHT, "64"); + runner.setProperty(ResizeImage.IMAGE_WIDTH, "64"); + runner.setProperty(ResizeImage.SCALING_ALGORITHM, ResizeImage.RESIZE_SMOOTH); + + runner.enqueue(Paths.get("src/test/resources/simple.jpg")); + runner.run(); + + runner.assertAllFlowFilesTransferred(ResizeImage.REL_SUCCESS, 1); + final MockFlowFile mff = runner.getFlowFilesForRelationship(ResizeImage.REL_SUCCESS).get(0); + final byte[] data = mff.toByteArray(); + + final BufferedImage img = ImageIO.read(new ByteArrayInputStream(data)); + assertEquals(64, img.getWidth()); + assertEquals(64, img.getHeight()); + final File out = new File("target/simple.jpg"); + ImageIO.write(img, "JPG", out); + } + +} diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/16color-10x10.bmp b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/16color-10x10.bmp new file mode 100644 index 0000000000..26206f458f Binary files /dev/null and b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/16color-10x10.bmp differ diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/mspaint-8x10.png b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/mspaint-8x10.png new file mode 100644 index 0000000000..9e74690a6e Binary files /dev/null and b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/mspaint-8x10.png differ diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/notImage.txt b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/notImage.txt new file mode 100644 index 0000000000..6738fd11ee --- /dev/null +++ b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/notImage.txt @@ -0,0 +1 @@ +This file is not an image and is used for testing the image metadata extractor. \ No newline at end of file diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/photoshop-8x12-32colors-alpha.gif b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/photoshop-8x12-32colors-alpha.gif new file mode 100644 index 0000000000..0f6183d7b7 Binary files /dev/null and b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/photoshop-8x12-32colors-alpha.gif differ diff --git a/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/simple.jpg b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/simple.jpg new file mode 100644 index 0000000000..18da83786f Binary files /dev/null and b/nifi-nar-bundles/nifi-image-bundle/nifi-image-processors/src/test/resources/simple.jpg differ diff --git a/nifi-nar-bundles/nifi-image-bundle/pom.xml b/nifi-nar-bundles/nifi-image-bundle/pom.xml new file mode 100644 index 0000000000..5f3599c4b5 --- /dev/null +++ b/nifi-nar-bundles/nifi-image-bundle/pom.xml @@ -0,0 +1,34 @@ + + + + 4.0.0 + + + org.apache.nifi + nifi-nar-bundles + 0.3.0-SNAPSHOT + + + nifi-image-bundle + 0.3.0-SNAPSHOT + pom + + + nifi-image-processors + nifi-image-nar + + + diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/MonitorActivity.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/MonitorActivity.java index 99a29e5bb6..29006239da 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/MonitorActivity.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/MonitorActivity.java @@ -16,6 +16,26 @@ */ package org.apache.nifi.processors.standard; +import org.apache.nifi.annotation.behavior.SideEffectFree; +import org.apache.nifi.annotation.behavior.TriggerSerially; +import org.apache.nifi.annotation.behavior.TriggerWhenEmpty; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.flowfile.attributes.CoreAttributes; +import org.apache.nifi.logging.ProcessorLog; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.ProcessorInitializationContext; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.io.OutputStreamCallback; +import org.apache.nifi.processor.util.StandardValidators; + import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; @@ -30,25 +50,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; -import org.apache.nifi.annotation.behavior.SideEffectFree; -import org.apache.nifi.annotation.behavior.TriggerSerially; -import org.apache.nifi.annotation.behavior.TriggerWhenEmpty; -import org.apache.nifi.annotation.documentation.CapabilityDescription; -import org.apache.nifi.annotation.documentation.Tags; -import org.apache.nifi.annotation.behavior.WritesAttribute; -import org.apache.nifi.annotation.behavior.WritesAttributes; -import org.apache.nifi.components.PropertyDescriptor; -import org.apache.nifi.flowfile.FlowFile; -import org.apache.nifi.flowfile.attributes.CoreAttributes; -import org.apache.nifi.logging.ProcessorLog; -import org.apache.nifi.processor.AbstractProcessor; -import org.apache.nifi.processor.ProcessContext; -import org.apache.nifi.processor.ProcessSession; -import org.apache.nifi.processor.ProcessorInitializationContext; -import org.apache.nifi.processor.Relationship; -import org.apache.nifi.processor.io.OutputStreamCallback; -import org.apache.nifi.processor.util.StandardValidators; - @SideEffectFree @TriggerSerially @TriggerWhenEmpty @@ -149,6 +150,15 @@ public class MonitorActivity extends AbstractProcessor { return properties; } + @OnScheduled + public void resetLastSuccessfulTransfer() { + setLastSuccessfulTransfer(System.currentTimeMillis()); + } + + protected final void setLastSuccessfulTransfer(final long timestamp) { + latestSuccessTransfer.set(timestamp); + } + @Override public void onTrigger(final ProcessContext context, final ProcessSession session) { final long thresholdMillis = context.getProperty(THRESHOLD).asTimePeriod(TimeUnit.MILLISECONDS); diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestMonitorActivity.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestMonitorActivity.java index 2e8744154e..f02e6da3fa 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestMonitorActivity.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestMonitorActivity.java @@ -31,7 +31,7 @@ public class TestMonitorActivity { @Test public void testFirstMessage() throws InterruptedException, IOException { - final TestRunner runner = TestRunners.newTestRunner(new MonitorActivity()); + final TestRunner runner = TestRunners.newTestRunner(new TestableProcessor(1000L)); runner.setProperty(MonitorActivity.CONTINUALLY_SEND_MESSAGES, "false"); runner.setProperty(MonitorActivity.THRESHOLD, "100 millis"); @@ -101,7 +101,7 @@ public class TestMonitorActivity { @Test public void testFirstMessageWithInherit() throws InterruptedException, IOException { - final TestRunner runner = TestRunners.newTestRunner(new MonitorActivity()); + final TestRunner runner = TestRunners.newTestRunner(new TestableProcessor(1000L)); runner.setProperty(MonitorActivity.CONTINUALLY_SEND_MESSAGES, "false"); runner.setProperty(MonitorActivity.THRESHOLD, "100 millis"); runner.setProperty(MonitorActivity.COPY_ATTRIBUTES, "true"); @@ -188,4 +188,39 @@ public class TestMonitorActivity { String.format("lineage start dates match when they shouldn't original=%1$s restored=%2$s", originalFlowFile.getLineageStartDate(), restoredFlowFile.getLineageStartDate()), restoredFlowFile.getLineageStartDate() != originalFlowFile.getLineageStartDate()); } + + @Test + public void testFirstRunNoMessages() throws InterruptedException, IOException { + // don't use the TestableProcessor, we want the real timestamp from @OnScheduled + final TestRunner runner = TestRunners.newTestRunner(new MonitorActivity()); + runner.setProperty(MonitorActivity.CONTINUALLY_SEND_MESSAGES, "false"); + runner.setProperty(MonitorActivity.THRESHOLD, "100 millis"); + + Thread.sleep(1000L); + + // shouldn't generate inactivity b/c run() will reset the lastSuccessfulTransfer + runner.run(); + runner.assertTransferCount(MonitorActivity.REL_SUCCESS, 0); + runner.assertTransferCount(MonitorActivity.REL_INACTIVE, 0); + runner.assertTransferCount(MonitorActivity.REL_ACTIVITY_RESTORED, 0); + runner.clearTransferState(); + } + + /** + * Since each call to run() will call @OnScheduled methods which will set the lastSuccessfulTransfer to the + * current time, we need a way to create an artificial time difference between calls to run. + */ + private class TestableProcessor extends MonitorActivity { + + private final long timestampDifference; + + public TestableProcessor(final long timestampDifference) { + this.timestampDifference = timestampDifference; + } + + @Override + public void resetLastSuccessfulTransfer() { + setLastSuccessfulTransfer(System.currentTimeMillis() - timestampDifference); + } + } } diff --git a/nifi-nar-bundles/pom.xml b/nifi-nar-bundles/pom.xml index fdcececcaa..1be2cfab2b 100644 --- a/nifi-nar-bundles/pom.xml +++ b/nifi-nar-bundles/pom.xml @@ -43,6 +43,7 @@ nifi-mongodb-bundle nifi-flume-bundle nifi-ambari-bundle + nifi-image-bundle