Search in sources :

Example 1 with Conversions

use of org.apache.avro.Conversions in project nifi by apache.

the class StandardContentViewerController method doGet.

/**
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final ViewableContent content = (ViewableContent) request.getAttribute(ViewableContent.CONTENT_REQUEST_ATTRIBUTE);
    // handle json/xml specifically, treat others as plain text
    String contentType = content.getContentType();
    if (supportedMimeTypes.contains(contentType)) {
        final String formatted;
        // leave the content alone if specified
        if (DisplayMode.Original.equals(content.getDisplayMode())) {
            formatted = content.getContent();
        } else {
            if ("application/json".equals(contentType)) {
                // format json
                final ObjectMapper mapper = new ObjectMapper();
                final Object objectJson = mapper.readValue(content.getContentStream(), Object.class);
                formatted = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectJson);
            } else if ("application/xml".equals(contentType) || "text/xml".equals(contentType)) {
                // format xml
                final StringWriter writer = new StringWriter();
                try {
                    final StreamSource source = new StreamSource(content.getContentStream());
                    final StreamResult result = new StreamResult(writer);
                    final TransformerFactory transformFactory = TransformerFactory.newInstance();
                    final Transformer transformer = transformFactory.newTransformer();
                    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                    transformer.transform(source, result);
                } catch (final TransformerFactoryConfigurationError | TransformerException te) {
                    throw new IOException("Unable to transform content as XML: " + te, te);
                }
                // 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("[");
                // Use Avro conversions to display logical type values in human readable way.
                final GenericData genericData = new GenericData() {

                    @Override
                    protected void toString(Object datum, StringBuilder buffer) {
                        // Since these types are not quoted and produce a malformed JSON string, quote it here.
                        if (datum instanceof LocalDate || datum instanceof LocalTime || datum instanceof DateTime) {
                            buffer.append("\"").append(datum).append("\"");
                            return;
                        }
                        super.toString(datum, buffer);
                    }
                };
                genericData.addLogicalTypeConversion(new Conversions.DecimalConversion());
                genericData.addLogicalTypeConversion(new TimeConversions.DateConversion());
                genericData.addLogicalTypeConversion(new TimeConversions.TimeConversion());
                genericData.addLogicalTypeConversion(new TimeConversions.TimestampConversion());
                final DatumReader<GenericData.Record> datumReader = new GenericDatumReader<>(null, null, genericData);
                try (final DataFileStream<GenericData.Record> dataFileReader = new DataFileStream<>(content.getContentStream(), datumReader)) {
                    while (dataFileReader.hasNext()) {
                        final GenericData.Record record = dataFileReader.next();
                        final String formattedRecord = genericData.toString(record);
                        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();
            }
        }
        // defer to the jsp
        request.setAttribute("mode", contentType);
        request.setAttribute("content", formatted);
        request.getRequestDispatcher("/WEB-INF/jsp/codemirror.jsp").include(request, response);
    } else {
        final PrintWriter out = response.getWriter();
        out.println("Unexpected content type: " + contentType);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) StringWriter(java.io.StringWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PrintWriter(java.io.PrintWriter) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) LocalTime(org.joda.time.LocalTime) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) DataFileStream(org.apache.avro.file.DataFileStream) GenericData(org.apache.avro.generic.GenericData) TimeConversions(org.apache.avro.data.TimeConversions) TimeConversions(org.apache.avro.data.TimeConversions) Conversions(org.apache.avro.Conversions)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Conversions (org.apache.avro.Conversions)1 TimeConversions (org.apache.avro.data.TimeConversions)1 DataFileStream (org.apache.avro.file.DataFileStream)1 GenericData (org.apache.avro.generic.GenericData)1 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)1 DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1 LocalTime (org.joda.time.LocalTime)1