Repository: nifi Updated Branches: refs/heads/master e3482cc77 -> d6744b9ee
NIFI-4528 The ref attribute in the request to view content sent by the client will now be inspected for the proxy context path. If found, it will be removed so the request can be successfully made within the cluster. This closes #2228. Signed-off-by: Bryan Bende <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/d6744b9e Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/d6744b9e Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/d6744b9e Branch: refs/heads/master Commit: d6744b9ee561b0b77b5c7fb3d0d066326ad7c375 Parents: e3482cc Author: Jeff Storck <[email protected]> Authored: Thu Oct 26 19:16:43 2017 -0400 Committer: Bryan Bende <[email protected]> Committed: Fri Oct 27 16:06:28 2017 -0400 ---------------------------------------------------------------------- .../nifi/web/ContentViewerController.java | 44 ++++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/d6744b9e/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-content-viewer/src/main/java/org/apache/nifi/web/ContentViewerController.java ---------------------------------------------------------------------- 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 ab8dcc1..96f46ae 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 @@ -56,7 +56,10 @@ public class ContentViewerController extends HttpServlet { // 1.5kb - multiple of 12 (3 bytes = 4 base 64 encoded chars) private final static int BUFFER_LENGTH = 1536; - /** + private static final String PROXY_CONTEXT_PATH_HTTP_HEADER = "X-ProxyContextPath"; + private static final String FORWARDED_CONTEXT_HTTP_HEADER = "X-Forwarded-Context"; + + /** * Gets the content and defers to registered viewers to generate the markup. * * @param request servlet request @@ -301,11 +304,19 @@ public class ContentViewerController extends HttpServlet { final String ref = request.getParameter("ref"); final String clientId = request.getParameter("clientId"); + final UriBuilder refUriBuilder = UriBuilder.fromUri(ref); + // base the data ref on the request parameter but ensure the scheme is based off the incoming request... // this is necessary for scenario's where the NiFi instance is behind a proxy running a different scheme - final URI refUri = UriBuilder.fromUri(ref) - .scheme(request.getScheme()) - .build(); + refUriBuilder.scheme(request.getScheme()); + + // If there is path context from a proxy, remove it since this request will be used inside the cluster + final String proxyContextPath = getFirstHeaderValue(request, PROXY_CONTEXT_PATH_HTTP_HEADER, FORWARDED_CONTEXT_HTTP_HEADER); + if (StringUtils.isNotBlank(proxyContextPath)) { + refUriBuilder.replacePath(StringUtils.substringAfter(UriBuilder.fromUri(ref).build().getPath(), proxyContextPath)); + } + + final URI refUri = refUriBuilder.build(); final String query = refUri.getQuery(); @@ -343,4 +354,29 @@ public class ContentViewerController extends HttpServlet { } }; } + + /** + * Returns the value for the first key discovered when inspecting the current request. Will + * return null if there are no keys specified or if none of the specified keys are found. + * + * @param keys http header keys + * @return the value for the first key found + */ + private String getFirstHeaderValue(HttpServletRequest httpServletRequest, final String... keys) { + if (keys == null) { + return null; + } + + for (final String key : keys) { + final String value = httpServletRequest.getHeader(key); + + // if we found an entry for this key, return the value + if (value != null) { + return value; + } + } + + // unable to find any matching keys + return null; + } }
