NIFI-3679: This closes #1655. Added Avro Content Viewer

Signed-off-by: joewitt <joewitt@apache.org>
This commit is contained in:
Mark Payne 2017-04-06 17:09:08 -04:00 committed by joewitt
parent 9583ca99c1
commit 8a5398eba7
4 changed files with 93 additions and 18 deletions

View File

@ -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"> <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 <!-- Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with this
this work for additional information regarding copyright ownership. work for additional information regarding copyright ownership. The ASF licenses
The ASF licenses this file to You under the Apache License, Version 2.0 this file to You under the Apache License, Version 2.0 (the "License"); you
(the "License"); you may not use this file except in compliance with may not use this file except in compliance with the License. You may obtain
the License. You may obtain a copy of the License at a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless
http://www.apache.org/licenses/LICENSE-2.0 required by applicable law or agreed to in writing, software distributed
Unless required by applicable law or agreed to in writing, software under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
distributed under the License is distributed on an "AS IS" BASIS, OR CONDITIONS OF ANY KIND, either express or implied. See the License for
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. the specific language governing permissions and limitations under the License. -->
See the License for the specific language governing permissions and
limitations under the License.
-->
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.apache.nifi</groupId> <groupId>org.apache.nifi</groupId>
@ -63,5 +60,15 @@
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
</dependency> </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> </dependencies>
</project> </project>

View File

@ -17,6 +17,11 @@
package org.apache.nifi.web; package org.apache.nifi.web;
import com.fasterxml.jackson.databind.ObjectMapper; 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 org.apache.nifi.web.ViewableContent.DisplayMode;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -33,9 +38,23 @@ import javax.xml.transform.stream.StreamSource;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.HashSet;
import java.util.Set;
public class StandardContentViewerController extends HttpServlet { 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 * @param request servlet request
@ -48,8 +67,8 @@ public class StandardContentViewerController extends HttpServlet {
final ViewableContent content = (ViewableContent) request.getAttribute(ViewableContent.CONTENT_REQUEST_ATTRIBUTE); final ViewableContent content = (ViewableContent) request.getAttribute(ViewableContent.CONTENT_REQUEST_ATTRIBUTE);
// handle json/xml specifically, treat others as plain text // handle json/xml specifically, treat others as plain text
final String contentType = content.getContentType(); String contentType = content.getContentType();
if ("application/json".equals(contentType) || "application/xml".equals(contentType) || "text/plain".equals(contentType) || "text/csv".equals(contentType)) { if (supportedMimeTypes.contains(contentType)) {
final String formatted; final String formatted;
// leave the content alone if specified // leave the content alone if specified
@ -81,6 +100,34 @@ public class StandardContentViewerController extends HttpServlet {
// get the transformed xml // get the transformed xml
formatted = writer.toString(); 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 { } else {
// leave plain text alone when formatting // leave plain text alone when formatting
formatted = content.getContent(); formatted = content.getContent();

View File

@ -17,3 +17,21 @@ The following binary components are provided under the Apache Software License v
This product includes software from the Spring Framework, This product includes software from the Spring Framework,
under the Apache License 2.0 (see: StringUtils.containsWhitespace()) 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

View File

@ -15,4 +15,7 @@
application/xml application/xml
application/json application/json
text/plain text/plain
text/csv text/csv
avro/binary
application/avro-binary
application/avro+binary