in gateway-server/src/main/java/org/apache/knox/gateway/GatewayFilter.java [115:218]
public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse ) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)servletRequest;
HttpServletResponse httpResponse = (HttpServletResponse)servletResponse;
//TODO: The resulting pathInfo + query needs to be added to the servlet context somehow so that filters don't need to rebuild it. This is done in HttpClientDispatch right now for example.
String path = httpRequest.getPathInfo();
String requestPath = ServletRequestUtils.getRequestPath(httpRequest);
String requestPathWithQuery = ServletRequestUtils.getRequestPathWithQuery(httpRequest);
Template pathWithQueryTemplate;
try {
pathWithQueryTemplate = Parser.parseLiteral( requestPathWithQuery );
} catch( URISyntaxException e ) {
throw new ServletException( e );
}
String contextWithPathAndQuery = ServletRequestUtils.getContextPathWithQuery(httpRequest);
LOG.receivedRequest( httpRequest.getMethod(), requestPath );
servletRequest.setAttribute(
AbstractGatewayFilter.SOURCE_REQUEST_URL_ATTRIBUTE_NAME, pathWithQueryTemplate );
servletRequest.setAttribute(
AbstractGatewayFilter.SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME, contextWithPathAndQuery );
Matcher<Chain>.Match match = chains.match( pathWithQueryTemplate );
// if there was no match then look for a default service for the topology
if (match == null) {
Topology topology = (Topology) servletRequest.getServletContext().getAttribute("org.apache.knox.gateway.topology");
if (topology != null) {
String defaultServicePath = topology.getDefaultServicePath();
if (defaultServicePath != null) {
try {
String newPathWithQuery = defaultServicePath + "/" + pathWithQueryTemplate;
match = chains.match(Parser.parseLiteral(newPathWithQuery));
String origUrl = ((HttpServletRequest) servletRequest).getRequestURL().toString();
String url = origUrl;
if (path == null || "/".equals(path)) {
url += defaultServicePath;
} else {
int index = origUrl.indexOf(path);
url = origUrl.substring(0, index) + "/" + defaultServicePath + path;
}
servletRequest = new ForwardedRequest((HttpServletRequest) servletRequest,
defaultServicePath,
url);
} catch (URISyntaxException e) {
throw new ServletException( e );
}
}
}
}
/* If request contains X-Request-Id header use it else use random uuid as correlation id */
final String reqID =
StringUtils.isBlank(((HttpServletRequest) servletRequest).getHeader(REQUEST_ID_HEADER_NAME)) ?
UUID.randomUUID().toString() :
((HttpServletRequest) servletRequest).getHeader(REQUEST_ID_HEADER_NAME);
assignCorrelationRequestId(reqID);
// Populate Audit/correlation parameters
AuditContext auditContext = auditService.getContext();
if(auditContext == null) {
auditContext = auditService.createContext();
}
auditContext.setTargetServiceName( match == null ? null : match.getValue().getResourceRole() );
auditContext.setRemoteIp( getRemoteAddress(servletRequest) );
auditContext.setRemoteHostname( servletRequest.getRemoteHost() );
auditService.attachContext(auditContext);
auditor.audit(
Action.ACCESS, contextWithPathAndQuery, ResourceType.URI,
ActionOutcome.UNAVAILABLE, RES.requestMethod(((HttpServletRequest)servletRequest).getMethod()));
if( match != null ) {
Chain chain = match.getValue();
servletRequest.setAttribute( AbstractGatewayFilter.TARGET_SERVICE_ROLE, chain.getResourceRole() );
try {
chain.doFilter(
UrlEncodedFormRequest.isUrlEncodedForm(servletRequest)
? new UrlEncodedFormRequest((HttpServletRequest) servletRequest)
: servletRequest,
servletResponse);
} catch( IOException | RuntimeException | ThreadDeath | ServletException e ) {
LOG.failedToExecuteFilter( e );
auditor.audit( Action.ACCESS, contextWithPathAndQuery, ResourceType.URI, ActionOutcome.FAILURE );
throw e;
} catch( Throwable e ) {
LOG.failedToExecuteFilter( e );
auditor.audit( Action.ACCESS, contextWithPathAndQuery, ResourceType.URI, ActionOutcome.FAILURE );
throw new ServletException( e );
}
} else {
LOG.failedToMatchPath( requestPath );
httpResponse.setStatus( HttpServletResponse.SC_NOT_FOUND );
}
if("/".equals(requestPath)) {
this.addCacheHeaders(httpResponse);
}
//KAM[ Don't do this or the Jetty default servlet will overwrite any response setup by the filter.
// filterChain.doFilter( servletRequest, servletResponse );
//]
}