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 <bbende@apache.org>
This commit is contained in:
Jeff Storck 2017-10-26 19:16:43 -04:00 committed by Bryan Bende
parent e3482cc772
commit d6744b9ee5
No known key found for this signature in database
GPG Key ID: A0DDA9ED50711C39
1 changed files with 40 additions and 4 deletions

View File

@ -56,6 +56,9 @@ public class ContentViewerController extends HttpServlet {
// 1.5kb - multiple of 12 (3 bytes = 4 base 64 encoded chars) // 1.5kb - multiple of 12 (3 bytes = 4 base 64 encoded chars)
private final static int BUFFER_LENGTH = 1536; 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. * Gets the content and defers to registered viewers to generate the markup.
* *
@ -301,11 +304,19 @@ public class ContentViewerController extends HttpServlet {
final String ref = request.getParameter("ref"); final String ref = request.getParameter("ref");
final String clientId = request.getParameter("clientId"); 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... // 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 // this is necessary for scenario's where the NiFi instance is behind a proxy running a different scheme
final URI refUri = UriBuilder.fromUri(ref) refUriBuilder.scheme(request.getScheme());
.scheme(request.getScheme())
.build(); // 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(); 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;
}
} }