4545
4646public class ParquetContentViewerController extends HttpServlet {
4747 private static final Logger logger = LoggerFactory .getLogger (ParquetContentViewerController .class );
48- private static final long maxBytes = 1024 * 1024 * 2 ; //10MB
49- private static final int bufferSize = 8 * 1024 ; // 8KB
48+ private static final long MAX_CONTENT_SIZE = 1024 * 1024 * 2 ; // 10MB
49+ private static final int BUFFER_SIZE = 8 * 1024 ; // 8KB
5050 private static final ObjectMapper objectMapper = new ObjectMapper ().enable (SerializationFeature .INDENT_OUTPUT );
5151
52+ static {
53+ objectMapper .getFactory ().configure (JsonGenerator .Feature .AUTO_CLOSE_TARGET , false );
54+ }
55+
5256 @ Override
5357 public void doGet (final HttpServletRequest request , final HttpServletResponse response ) throws IOException {
5458 final ContentRequestContext requestContext = new HttpServletContentRequestContext (request );
@@ -75,30 +79,12 @@ public void doGet(final HttpServletRequest request, final HttpServletResponse re
7579
7680 response .setStatus (HttpServletResponse .SC_OK );
7781
78- final boolean formatted = Boolean .parseBoolean (request .getParameter ("formatted" ));
79- if (!formatted ) {
80- final InputStream contentStream = downloadableContent .getContent ();
81- contentStream .transferTo (response .getOutputStream ());
82- return ;
83- }
84-
85- // allow the user to drive the data type but fall back to the content type if necessary
86- String displayName = request .getParameter ("mimeTypeDisplayName" );
87- if (displayName == null ) {
88- displayName = downloadableContent .getType ();
89- }
90-
91- if (displayName == null || !(displayName .equals ("parquet" ) || displayName .equals ("application/vnd.apache.parquet" ))) {
92- response .sendError (HttpURLConnection .HTTP_BAD_REQUEST , "Unknown content type" );
93- return ;
94- }
95-
9682 try {
9783 //Convert InputStream to a seekable InputStream
9884 byte [] data = getInputStreamBytes (downloadableContent .getContent ());
9985
10086 if (data .length == 0 ) {
101- response .getOutputStream (). write ( "Content size is too large to display." . getBytes () );
87+ response .sendError ( HttpURLConnection . HTTP_INTERNAL_ERROR , "Content size is too large to display." );
10288 return ;
10389 }
10490
@@ -114,8 +100,8 @@ public void doGet(final HttpServletRequest request, final HttpServletResponse re
114100
115101 //Format and write out each record
116102 GenericRecord record ;
117- objectMapper .getFactory ().configure (JsonGenerator .Feature .AUTO_CLOSE_TARGET , false );
118103 boolean firstRecord = true ;
104+ response .getOutputStream ().write ("[\n " .getBytes ());
119105 while ((record = reader .read ()) != null ) {
120106 if (firstRecord ) {
121107 firstRecord = false ;
@@ -125,6 +111,7 @@ public void doGet(final HttpServletRequest request, final HttpServletResponse re
125111
126112 objectMapper .writerWithDefaultPrettyPrinter ().writeValue (response .getOutputStream (), objectMapper .readTree (record .toString ()));
127113 }
114+ response .getOutputStream ().write ("\n ]" .getBytes ());
128115 } catch (final Throwable t ) {
129116 logger .warn ("Unable to format FlowFile content" , t );
130117 response .sendError (HttpURLConnection .HTTP_INTERNAL_ERROR , "Unable to format FlowFile content" );
@@ -133,11 +120,11 @@ public void doGet(final HttpServletRequest request, final HttpServletResponse re
133120
134121 private byte [] getInputStreamBytes (final InputStream inputStream ) throws IOException {
135122 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
136- byte [] buffer = new byte [bufferSize ];
123+ byte [] buffer = new byte [BUFFER_SIZE ];
137124 long totalRead = 0 ;
138125
139- while (totalRead < maxBytes ) {
140- int bytesRead = inputStream .read (buffer , 0 , bufferSize );
126+ while (totalRead < MAX_CONTENT_SIZE ) {
127+ int bytesRead = inputStream .read (buffer , 0 , BUFFER_SIZE );
141128 if (bytesRead == -1 ) {
142129 break ;
143130 }
@@ -146,7 +133,7 @@ private byte[] getInputStreamBytes(final InputStream inputStream) throws IOExcep
146133 }
147134
148135 // Return empty array if inputStream has more data beyond maxBytes
149- if (totalRead >= maxBytes && inputStream .read () != -1 ) {
136+ if (totalRead >= MAX_CONTENT_SIZE && inputStream .read () != -1 ) {
150137 return new byte [0 ];
151138 }
152139
0 commit comments