From b30a172e2156b5ada2a91071420c005962ec6324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Liebau?= Date: Mon, 22 Feb 2016 08:25:42 +0100 Subject: [PATCH] NIFI-1539 - Add normalization of content type for content viewing Add code to ContentViewerController to strip content type of any trailing parameters and lowercase the type and subtype. Added function to ViewableContent to enable retrieving the original value of the content type if needed. --- .../java/org/apache/nifi/web/ViewableContent.java | 8 +++++++- .../org/apache/nifi/web/ContentViewerController.java | 12 +++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/nifi-api/src/main/java/org/apache/nifi/web/ViewableContent.java b/nifi-api/src/main/java/org/apache/nifi/web/ViewableContent.java index 180385e2f6ba..d506b5dbf189 100644 --- a/nifi-api/src/main/java/org/apache/nifi/web/ViewableContent.java +++ b/nifi-api/src/main/java/org/apache/nifi/web/ViewableContent.java @@ -59,7 +59,13 @@ public enum DisplayMode { String getFileName(); /** - * @return mime type of the content + * @return mime type of the content, value is lowercase and stripped of all parameters if there were any */ String getContentType(); + + /** + * @return unchanged mime type of the content + */ + String getRawContentType(); + } diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-content-viewer/src/main/java/org/apache/nifi/web/ContentViewerController.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-content-viewer/src/main/java/org/apache/nifi/web/ContentViewerController.java index efc5db88e4ab..65f9353863d0 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-content-viewer/src/main/java/org/apache/nifi/web/ContentViewerController.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-content-viewer/src/main/java/org/apache/nifi/web/ContentViewerController.java @@ -150,6 +150,7 @@ protected void doGet(final HttpServletRequest request, final HttpServletResponse // buffer the content to support reseting in case we need to detect the content type or char encoding try (final BufferedInputStream bis = new BufferedInputStream(downloadableContent.getContent());) { final String mimeType; + final String normalizedMimeType; // when standalone and we don't know the type is null as we were able to directly access the content bypassing the rest endpoint, // when clustered and we don't know the type set to octet stream since the content was retrieved from the node's rest endpoint @@ -171,6 +172,10 @@ protected void doGet(final HttpServletRequest request, final HttpServletResponse mimeType = downloadableContent.getType(); } + // Extract only mime type and subtype from content type (anything after the first ; are parameters) + // Lowercase so subsequent code does not need to implement case insensitivity + normalizedMimeType = mimeType.split(";",2)[0].toLowerCase(); + // add attributes needed for the header request.setAttribute("filename", downloadableContent.getFilename()); request.setAttribute("contentType", mimeType); @@ -202,7 +207,7 @@ protected void doGet(final HttpServletRequest request, final HttpServletResponse request.getRequestDispatcher("/WEB-INF/jsp/hexview.jsp").include(request, response); } else { // lookup a viewer for the content - final String contentViewerUri = servletContext.getInitParameter(mimeType); + final String contentViewerUri = servletContext.getInitParameter(normalizedMimeType); // handle no viewer for content type if (contentViewerUri == null) { @@ -244,6 +249,11 @@ public String getFileName() { @Override public String getContentType() { + return normalizedMimeType; + } + + @Override + public String getRawContentType() { return mimeType; } });