KNOX-2123 - Setting requestURI using the given servletRequest in case the service is unavailable and logging it with the appropriate action outcome (#219)
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayFilter.java b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayFilter.java
index afd2ba7..64a0f75 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayFilter.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayFilter.java
@@ -32,6 +32,7 @@
import org.apache.knox.gateway.i18n.messages.MessagesFactory;
import org.apache.knox.gateway.i18n.resources.ResourcesFactory;
import org.apache.knox.gateway.topology.Topology;
+import org.apache.knox.gateway.util.ServletRequestUtils;
import org.apache.knox.gateway.util.urltemplate.Matcher;
import org.apache.knox.gateway.util.urltemplate.Parser;
import org.apache.knox.gateway.util.urltemplate.Template;
@@ -101,11 +102,9 @@
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 servlet = httpRequest.getServletPath();
String path = httpRequest.getPathInfo();
- String query = httpRequest.getQueryString();
- String requestPath = ( servlet == null ? "" : servlet ) + ( path == null ? "" : path );
- String requestPathWithQuery = requestPath + ( query == null ? "" : "?" + query );
+ String requestPath = ServletRequestUtils.getRequestPath(httpRequest);
+ String requestPathWithQuery = ServletRequestUtils.getRequestPathWithQuery(httpRequest);
Template pathWithQueryTemplate;
try {
@@ -113,7 +112,7 @@
} catch( URISyntaxException e ) {
throw new ServletException( e );
}
- String contextWithPathAndQuery = httpRequest.getContextPath() + requestPathWithQuery;
+ String contextWithPathAndQuery = ServletRequestUtils.getContextPathWithQuery(httpRequest);
LOG.receivedRequest( httpRequest.getMethod(), requestPath );
servletRequest.setAttribute(
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServlet.java b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServlet.java
index 2a5f757..785bc39 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServlet.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServlet.java
@@ -33,6 +33,7 @@
import org.apache.knox.gateway.services.ServiceType;
import org.apache.knox.gateway.services.GatewayServices;
import org.apache.knox.gateway.services.metrics.MetricsService;
+import org.apache.knox.gateway.util.ServletRequestUtils;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@@ -58,10 +59,9 @@
private static final GatewayResources res = ResourcesFactory.get( GatewayResources.class );
private static final GatewayMessages LOG = MessagesFactory.get( GatewayMessages.class );
- private static AuditService auditService = AuditServiceFactory.getAuditService();
- private static Auditor auditor = AuditServiceFactory.getAuditService()
- .getAuditor( AuditConstants.DEFAULT_AUDITOR_NAME,
- AuditConstants.KNOX_SERVICE_NAME, AuditConstants.KNOX_COMPONENT_NAME );
+ private static final AuditService auditService = AuditServiceFactory.getAuditService();
+ private static final Auditor auditor = AuditServiceFactory.getAuditService().getAuditor(AuditConstants.DEFAULT_AUDITOR_NAME, AuditConstants.KNOX_SERVICE_NAME,
+ AuditConstants.KNOX_COMPONENT_NAME);
private FilterConfigAdapter filterConfig;
private GatewayFilter filter;
@@ -140,9 +140,7 @@
} else {
((HttpServletResponse)servletResponse).setStatus( HttpServletResponse.SC_SERVICE_UNAVAILABLE );
}
- String requestUri = (String)servletRequest.getAttribute( AbstractGatewayFilter.SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME );
- int status = ((HttpServletResponse)servletResponse).getStatus();
- auditor.audit( Action.ACCESS, requestUri, ResourceType.URI, ActionOutcome.SUCCESS, res.responseStatus( status ) );
+ auditLog(servletRequest, servletResponse);
} finally {
auditService.detachContext();
}
@@ -170,14 +168,25 @@
} else {
((HttpServletResponse)servletResponse).setStatus( HttpServletResponse.SC_SERVICE_UNAVAILABLE );
}
- String requestUri = (String)servletRequest.getAttribute( AbstractGatewayFilter.SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME );
- int status = ((HttpServletResponse)servletResponse).getStatus();
- auditor.audit( Action.ACCESS, requestUri, ResourceType.URI, ActionOutcome.SUCCESS, res.responseStatus( status ) );
+ auditLog(servletRequest, servletResponse);
} finally {
auditService.detachContext();
}
}
+ private void auditLog(ServletRequest servletRequest, ServletResponse servletResponse) {
+ final int status = ((HttpServletResponse) servletResponse).getStatus();
+ final String requestUri, actionOutcome;
+ if (HttpServletResponse.SC_SERVICE_UNAVAILABLE == status) {
+ requestUri = ServletRequestUtils.getContextPathWithQuery(servletRequest);
+ actionOutcome = ActionOutcome.UNAVAILABLE;
+ } else {
+ requestUri = (String) servletRequest.getAttribute(AbstractGatewayFilter.SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME);
+ actionOutcome = ActionOutcome.SUCCESS;
+ }
+ auditor.audit(Action.ACCESS, requestUri, ResourceType.URI, actionOutcome, res.responseStatus(status));
+ }
+
@Override
public String getServletInfo() {
return res.gatewayServletInfo();
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/util/ServletRequestUtils.java b/gateway-server/src/main/java/org/apache/knox/gateway/util/ServletRequestUtils.java
new file mode 100644
index 0000000..7d37521
--- /dev/null
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/util/ServletRequestUtils.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.util;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * Provides useful methods to fetch different parts from {@link ServletRequest} and {@link HttpServletRequest} interfaces.
+ */
+public class ServletRequestUtils {
+
+ public static String getRequestPath(ServletRequest servletRequest) {
+ return getRequestPath((HttpServletRequest) servletRequest);
+ }
+
+ public static String getRequestPath(HttpServletRequest httpServletRequest) {
+ return emptyOrValue(httpServletRequest.getServletPath()) + emptyOrValue(httpServletRequest.getPathInfo());
+ }
+
+ public static String getRequestPathWithQuery(ServletRequest servletRequest) {
+ return getRequestPathWithQuery((HttpServletRequest) servletRequest);
+ }
+
+ public static String getRequestPathWithQuery(HttpServletRequest httpServletRequest) {
+ return getRequestPath(httpServletRequest) + emptyOrValue(httpServletRequest.getQueryString(), "?");
+ }
+
+ public static String getContextPathWithQuery(ServletRequest servletRequest) {
+ return getContextPathWithQuery((HttpServletRequest) servletRequest);
+ }
+
+ public static String getContextPathWithQuery(HttpServletRequest httpServletRequest) {
+ return httpServletRequest.getContextPath() + getRequestPathWithQuery(httpServletRequest);
+ }
+
+ private static String emptyOrValue(String toTest) {
+ return emptyOrValue(toTest, null);
+ }
+
+ private static String emptyOrValue(String toTest, String prefix) {
+ if (toTest == null) {
+ return "";
+ } else {
+ return prefix == null ? toTest : prefix + toTest;
+ }
+ }
+
+}