Repository: knox Updated Branches: refs/heads/master d90efa63b -> 712c80ef8
KNOX-358 - refactor redirecting servlet into a forwarding servlet Project: http://git-wip-us.apache.org/repos/asf/knox/repo Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/712c80ef Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/712c80ef Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/712c80ef Branch: refs/heads/master Commit: 712c80ef857adc242c6c99a969a6a01f3ed9ff35 Parents: d90efa6 Author: Larry McCay <lmc...@hortonworks.com> Authored: Thu May 1 14:44:31 2014 -0400 Committer: Larry McCay <lmc...@hortonworks.com> Committed: Thu May 1 14:44:31 2014 -0400 ---------------------------------------------------------------------- .../gateway/GatewayForwardingServlet.java | 101 +++++++++++++++++++ .../hadoop/gateway/GatewayRedirectServlet.java | 87 ---------------- .../gateway/config/impl/GatewayConfigImpl.java | 4 +- .../gateway/deploy/DeploymentFactory.java | 4 +- .../gateway/GatewayForwardingServletTest.java | 65 ++++++++++++ .../gateway/GatewayRedirectServletTest.java | 67 ------------ 6 files changed, 170 insertions(+), 158 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/knox/blob/712c80ef/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayForwardingServlet.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayForwardingServlet.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayForwardingServlet.java new file mode 100644 index 0000000..e31a31c --- /dev/null +++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayForwardingServlet.java @@ -0,0 +1,101 @@ +/** + * 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.hadoop.gateway; + +import java.io.*; + +import javax.servlet.*; +import javax.servlet.http.*; + +public class GatewayForwardingServlet extends HttpServlet{ + + private static final long serialVersionUID = 1L; + private String redirectToContext = null; + + @Override + protected void doHead(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + doGet(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + doGet(req, resp); + } + + @Override + protected void doPut(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + doGet(req, resp); + } + + @Override + protected void doDelete(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + doGet(req, resp); + } + + @Override + protected void doOptions(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + doGet(req, resp); + } + + @Override + public void init(ServletConfig config) throws ServletException { + super.init(config); + + redirectToContext = config.getInitParameter("redirectTo"); + } + + public void doGet(HttpServletRequest request, + HttpServletResponse response) + throws ServletException, IOException + { + String path = ""; + String pathInfo = request.getPathInfo(); + if (pathInfo != null && pathInfo.length() > 0) { + path = path + pathInfo; + } + String qstr = request.getQueryString(); + if (qstr != null && qstr.length() > 0) { + path = path + "?" + qstr; + } + + // Perform cross context dispatch to the configured topology context + ServletContext ctx = getServletContext().getContext(redirectToContext); + RequestDispatcher dispatcher = ctx.getRequestDispatcher(path); + dispatcher.forward(request, response); + } + + public static class MyRequest extends HttpServletRequestWrapper { + private String redirectTo = null; + + public MyRequest(HttpServletRequest request, String redirectTo) { + super(request); + } + + @Override + public String getContextPath() { + return redirectTo; + } + + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/712c80ef/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayRedirectServlet.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayRedirectServlet.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayRedirectServlet.java deleted file mode 100644 index 95b90ef..0000000 --- a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayRedirectServlet.java +++ /dev/null @@ -1,87 +0,0 @@ -/** - * 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.hadoop.gateway; - -import java.io.*; - -import javax.servlet.*; -import javax.servlet.http.*; - -public class GatewayRedirectServlet extends HttpServlet{ - - private static final long serialVersionUID = 1L; - private String redirectToContext = null; - - @Override - protected void doHead(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - doGet(req, resp); - } - - @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - doGet(req, resp); - } - - @Override - protected void doPut(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - doGet(req, resp); - } - - @Override - protected void doDelete(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - doGet(req, resp); - } - - @Override - protected void doOptions(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - doGet(req, resp); - } - - @Override - public void init(ServletConfig config) throws ServletException { - super.init(config); - - redirectToContext = config.getInitParameter("redirectTo"); - } - - public void doGet(HttpServletRequest request, - HttpServletResponse response) - throws ServletException, IOException - { - String location = redirectToContext; - String ctxPath = request.getServletContext().getContextPath(); - if (ctxPath != null && ctxPath.length() > 0) { - location = location + ctxPath; - } - String pathInfo = request.getPathInfo(); - if (pathInfo != null && pathInfo.length() > 0) { - location = location + pathInfo; - } - String qstr = request.getQueryString(); - if (qstr != null && qstr.length() > 0) { - location = location + "?" + qstr; - } - response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT); - response.setHeader("Location", location); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/712c80ef/gateway-server/src/main/java/org/apache/hadoop/gateway/config/impl/GatewayConfigImpl.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/config/impl/GatewayConfigImpl.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/config/impl/GatewayConfigImpl.java index 48ff286..d7aab45 100644 --- a/gateway-server/src/main/java/org/apache/hadoop/gateway/config/impl/GatewayConfigImpl.java +++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/config/impl/GatewayConfigImpl.java @@ -65,7 +65,7 @@ import java.util.Map; public class GatewayConfigImpl extends Configuration implements GatewayConfig { private static final String DEFAULT_APP_REDIRECT_PATH_PARAM = "default.app.redirect.path"; - private static final String DEFAULT_APP_REDIRECT_PATH = "/gateway/sandbox"; + private static final String DEFAULT_APP_REDIRECT_PATH = "/sandbox"; private static final String GATEWAY_DEFAULT_TOPOLOGY_NAME_PARAM = "default.app.topology.name"; private static final String GATEWAY_DEFAULT_TOPOLOGY_NAME = "_default"; @@ -333,7 +333,7 @@ public class GatewayConfigImpl extends Configuration implements GatewayConfig { @Override public String getDefaultAppRedirectPath() { String path = get(DEFAULT_APP_REDIRECT_PATH_PARAM); - return path != null ? path : DEFAULT_APP_REDIRECT_PATH; + return path != null ? path : "/" + getGatewayPath() + DEFAULT_APP_REDIRECT_PATH; } } http://git-wip-us.apache.org/repos/asf/knox/blob/712c80ef/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java index 1b639a0..82d7689 100644 --- a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java +++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java @@ -18,7 +18,7 @@ package org.apache.hadoop.gateway.deploy; import org.apache.hadoop.gateway.GatewayMessages; -import org.apache.hadoop.gateway.GatewayRedirectServlet; +import org.apache.hadoop.gateway.GatewayForwardingServlet; import org.apache.hadoop.gateway.GatewayResources; import org.apache.hadoop.gateway.GatewayServlet; import org.apache.hadoop.gateway.config.GatewayConfig; @@ -102,7 +102,7 @@ public abstract class DeploymentFactory { context = createDeploymentContext( config, topology, providers, services); WebAppDescriptor wad = context.getWebAppDescriptor(); String servletName = context.getTopology().getName(); - String servletClass = GatewayRedirectServlet.class.getName(); + String servletClass = GatewayForwardingServlet.class.getName(); wad.createServlet().servletName( servletName ).servletClass( servletClass ); wad.createServletMapping().servletName( servletName ).urlPattern( "/*" ); ServletType<WebAppDescriptor> servlet = findServlet( context, context.getTopology().getName() ); http://git-wip-us.apache.org/repos/asf/knox/blob/712c80ef/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayForwardingServletTest.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayForwardingServletTest.java b/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayForwardingServletTest.java new file mode 100644 index 0000000..6d32188 --- /dev/null +++ b/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayForwardingServletTest.java @@ -0,0 +1,65 @@ +/** + * 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.hadoop.gateway; + +import java.io.IOException; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.easymock.EasyMock; +import org.easymock.IMocksControl; +import org.junit.Test; + +public class GatewayForwardingServletTest { + + @Test + public void testRedirectDefaults() throws ServletException, IOException { + IMocksControl mockControl = EasyMock.createControl(); + ServletConfig config = (ServletConfig)mockControl.createMock(ServletConfig.class); + ServletContext context = (ServletContext)mockControl.createMock(ServletContext.class); + HttpServletRequest request = (HttpServletRequest)mockControl.createMock(HttpServletRequest.class); + HttpServletResponse response = (HttpServletResponse)mockControl.createMock(HttpServletResponse.class); + RequestDispatcher dispatcher = (RequestDispatcher)mockControl.createMock(RequestDispatcher.class); + // setup expectations + EasyMock.expect(config.getServletName()).andStubReturn("default"); + EasyMock.expect(config.getServletContext()).andStubReturn(context); + EasyMock.expect(config.getInitParameter("redirectTo")).andReturn("/gateway/sandbox"); + EasyMock.expect(request.getMethod()).andReturn("GET"); + EasyMock.expect(request.getPathInfo()).andReturn("/webhdfs/v1/tmp"); + EasyMock.expect(request.getQueryString()).andReturn("op=LISTSTATUS"); + EasyMock.expect(context.getContext("/gateway/sandbox")).andReturn(context); + EasyMock.expect(context.getRequestDispatcher("/webhdfs/v1/tmp?op=LISTSTATUS")).andReturn(dispatcher); + dispatcher.forward(request, response); + EasyMock.expectLastCall().once(); + // logging + context.log((String)EasyMock.anyObject()); + EasyMock.expectLastCall().anyTimes(); + // run the test + mockControl.replay(); + GatewayForwardingServlet servlet = new GatewayForwardingServlet(); + servlet.init(config); + servlet.service(request, response); + mockControl.verify(); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/712c80ef/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayRedirectServletTest.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayRedirectServletTest.java b/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayRedirectServletTest.java deleted file mode 100644 index b015012..0000000 --- a/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayRedirectServletTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * 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.hadoop.gateway; - -import java.io.IOException; - -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.easymock.EasyMock; -import org.easymock.IMocksControl; - -import junit.framework.TestCase; - -public class GatewayRedirectServletTest extends TestCase { - - public void testRedirectDefaults() throws ServletException, IOException { - IMocksControl mockControl = EasyMock.createControl(); - ServletConfig config = (ServletConfig)mockControl.createMock(ServletConfig.class); - ServletContext context = (ServletContext)mockControl.createMock(ServletContext.class); - HttpServletRequest request = (HttpServletRequest)mockControl.createMock(HttpServletRequest.class); - HttpServletResponse response = (HttpServletResponse)mockControl.createMock(HttpServletResponse.class); - // setup expectations - EasyMock.expect(config.getServletName()).andStubReturn("default"); - EasyMock.expect(config.getServletContext()).andStubReturn(context); - EasyMock.expect(config.getInitParameter("redirectTo")).andReturn("/gateway/sandbox"); - EasyMock.expect(request.getServletContext()).andReturn(context); - EasyMock.expect(request.getMethod()).andReturn("GET"); - EasyMock.expect(request.getPathInfo()).andReturn("/webhdfs/v1/tmp"); - EasyMock.expect(request.getQueryString()).andReturn("op=LISTSTATUS"); - EasyMock.expect(context.getContextPath()).andReturn(""); - response.setStatus(307); - EasyMock.expectLastCall().once(); - response.setHeader("Location", "/gateway/sandbox/webhdfs/v1/tmp?op=LISTSTATUS"); - EasyMock.expectLastCall().once(); - // logging - context.log((String)EasyMock.anyObject()); - EasyMock.expectLastCall().anyTimes(); - // run the test - mockControl.replay(); - GatewayRedirectServlet servlet = new GatewayRedirectServlet(); - servlet.init(config); - servlet.service(request, response); -// assertTrue(response.getStatus() == 302); - mockControl.verify(); - - } - -}