Repository: nifi Updated Branches: refs/heads/master 9583ca99c -> 8a5398eba
NIFI-3679: This closes #1655. Added Avro Content Viewer Signed-off-by: joewitt <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/8a5398eb Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/8a5398eb Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/8a5398eb Branch: refs/heads/master Commit: 8a5398eba771f0cb7733eb552c91e072c1ed40f8 Parents: 9583ca9 Author: Mark Payne <[email protected]> Authored: Thu Apr 6 17:09:08 2017 -0400 Committer: joewitt <[email protected]> Committed: Tue Apr 11 00:00:09 2017 -0400 ---------------------------------------------------------------------- .../nifi-standard-content-viewer/pom.xml | 37 ++++++++------ .../web/StandardContentViewerController.java | 51 +++++++++++++++++++- .../src/main/resources/META-INF/NOTICE | 18 +++++++ .../main/webapp/META-INF/nifi-content-viewer | 5 +- 4 files changed, 93 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/8a5398eb/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/pom.xml ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/pom.xml b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/pom.xml index 2afbf7b..200eed0 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/pom.xml +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/pom.xml @@ -1,18 +1,15 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <!-- - 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. - --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <!-- 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. --> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.apache.nifi</groupId> @@ -63,5 +60,15 @@ <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> + <dependency> + <groupId>org.apache.avro</groupId> + <artifactId>avro</artifactId> + </dependency> + <dependency> + <groupId>org.xerial.snappy</groupId> + <artifactId>snappy-java</artifactId> + <version>1.1.2</version> + </dependency> + </dependencies> </project> http://git-wip-us.apache.org/repos/asf/nifi/blob/8a5398eb/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/java/org/apache/nifi/web/StandardContentViewerController.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/java/org/apache/nifi/web/StandardContentViewerController.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/java/org/apache/nifi/web/StandardContentViewerController.java index 75aa62a..d8cb37d 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/java/org/apache/nifi/web/StandardContentViewerController.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/java/org/apache/nifi/web/StandardContentViewerController.java @@ -17,6 +17,11 @@ package org.apache.nifi.web; import com.fasterxml.jackson.databind.ObjectMapper; + +import org.apache.avro.file.DataFileStream; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericDatumReader; +import org.apache.avro.io.DatumReader; import org.apache.nifi.web.ViewableContent.DisplayMode; import javax.servlet.ServletException; @@ -33,9 +38,23 @@ import javax.xml.transform.stream.StreamSource; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; +import java.util.HashSet; +import java.util.Set; public class StandardContentViewerController extends HttpServlet { + private static final Set<String> supportedMimeTypes = new HashSet<>(); + + static { + supportedMimeTypes.add("application/json"); + supportedMimeTypes.add("application/xml"); + supportedMimeTypes.add("text/plain"); + supportedMimeTypes.add("text/csv"); + supportedMimeTypes.add("application/avro-binary"); + supportedMimeTypes.add("avro/binary"); + supportedMimeTypes.add("application/avro+binary"); + } + /** * * @param request servlet request @@ -48,8 +67,8 @@ public class StandardContentViewerController extends HttpServlet { final ViewableContent content = (ViewableContent) request.getAttribute(ViewableContent.CONTENT_REQUEST_ATTRIBUTE); // handle json/xml specifically, treat others as plain text - final String contentType = content.getContentType(); - if ("application/json".equals(contentType) || "application/xml".equals(contentType) || "text/plain".equals(contentType) || "text/csv".equals(contentType)) { + String contentType = content.getContentType(); + if (supportedMimeTypes.contains(contentType)) { final String formatted; // leave the content alone if specified @@ -81,6 +100,34 @@ public class StandardContentViewerController extends HttpServlet { // get the transformed xml formatted = writer.toString(); + } else if ("application/avro-binary".equals(contentType) || "avro/binary".equals(contentType) || "application/avro+binary".equals(contentType)) { + final StringBuilder sb = new StringBuilder(); + sb.append("["); + final DatumReader<GenericData.Record> datumReader = new GenericDatumReader<>(); + try (final DataFileStream<GenericData.Record> dataFileReader = new DataFileStream<>(content.getContentStream(), datumReader)) { + while (dataFileReader.hasNext()) { + final GenericData.Record record = dataFileReader.next(); + final String formattedRecord = record.toString(); + sb.append(formattedRecord); + sb.append(","); + // Do not format more than 10 MB of content. + if (sb.length() > 1024 * 1024 * 2) { + break; + } + } + } + + if (sb.length() > 1) { + sb.deleteCharAt(sb.length() - 1); + } + sb.append("]"); + final String json = sb.toString(); + + final ObjectMapper mapper = new ObjectMapper(); + final Object objectJson = mapper.readValue(json, Object.class); + formatted = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectJson); + + contentType = "application/json"; } else { // leave plain text alone when formatting formatted = content.getContent(); http://git-wip-us.apache.org/repos/asf/nifi/blob/8a5398eb/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/resources/META-INF/NOTICE ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/resources/META-INF/NOTICE b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/resources/META-INF/NOTICE index 44544b0..ed910bb 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/resources/META-INF/NOTICE +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/resources/META-INF/NOTICE @@ -17,3 +17,21 @@ The following binary components are provided under the Apache Software License v This product includes software from the Spring Framework, under the Apache License 2.0 (see: StringUtils.containsWhitespace()) + + (ASLv2) Apache Avro + The following NOTICE information applies: + Apache Avro + Copyright 2009-2013 The Apache Software Foundation + + (ASLv2) Snappy Java + The following NOTICE information applies: + This product includes software developed by Google + Snappy: http://code.google.com/p/snappy/ (New BSD License) + + This product includes software developed by Apache + PureJavaCrc32C from apache-hadoop-common http://hadoop.apache.org/ + (Apache 2.0 license) + + This library containd statically linked libstdc++. This inclusion is allowed by + "GCC RUntime Library Exception" + http://gcc.gnu.org/onlinedocs/libstdc++/manual/license.html \ No newline at end of file http://git-wip-us.apache.org/repos/asf/nifi/blob/8a5398eb/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/webapp/META-INF/nifi-content-viewer ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/webapp/META-INF/nifi-content-viewer b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/webapp/META-INF/nifi-content-viewer index 302c44a..e5e25f0 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/webapp/META-INF/nifi-content-viewer +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/webapp/META-INF/nifi-content-viewer @@ -15,4 +15,7 @@ application/xml application/json text/plain -text/csv \ No newline at end of file +text/csv +avro/binary +application/avro-binary +application/avro+binary \ No newline at end of file
