knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lmc...@apache.org
Subject git commit: KNOX-353 adding support for hadoop java client through redirection
Date Sun, 27 Apr 2014 17:47:31 GMT
Repository: knox
Updated Branches:
  refs/heads/master c6762ce92 -> fc0041564


KNOX-353 adding support for hadoop java client through redirection

Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/fc004156
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/fc004156
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/fc004156

Branch: refs/heads/master
Commit: fc00415647d0ac1dcc4f0146425c192fd2caa7ab
Parents: c6762ce
Author: Larry McCay <lmccay@hortonworks.com>
Authored: Sun Apr 27 13:47:16 2014 -0400
Committer: Larry McCay <lmccay@hortonworks.com>
Committed: Sun Apr 27 13:47:16 2014 -0400

----------------------------------------------------------------------
 .../hadoop/gateway/GatewayRedirectServlet.java  | 86 ++++++++++++++++++++
 .../apache/hadoop/gateway/GatewayServer.java    |  7 +-
 .../gateway/config/impl/GatewayConfigImpl.java  | 25 +++++-
 .../gateway/deploy/DeploymentFactory.java       | 53 ++++++++++--
 .../hadoop/gateway/GatewayGlobalConfigTest.java | 11 +++
 .../gateway/GatewayRedirectServletTest.java     | 65 +++++++++++++++
 .../hadoop/gateway/config/GatewayConfig.java    |  3 +
 .../hadoop/gateway/GatewayTestConfig.java       | 17 ++++
 8 files changed, 256 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/fc004156/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
new file mode 100644
index 0000000..4c85c09
--- /dev/null
+++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayRedirectServlet.java
@@ -0,0 +1,86 @@
+/**
+ * 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.sendRedirect(location);
+  }
+} 
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/knox/blob/fc004156/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java
index 9ee0660..651cff6 100644
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java
+++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayServer.java
@@ -371,7 +371,12 @@ public class GatewayServer {
     errorHandler.setShowStacks(false);
     WebAppContext context = new WebAppContext();
     context.setDefaultsDescriptor( null );
-    context.setContextPath( "/" + config.getGatewayPath() + "/" + name );
+    if (!name.equals(config.getDefaultTopologyName())) {
+      context.setContextPath( "/" + config.getGatewayPath() + "/" + name );
+    }
+    else {
+      context.setContextPath( "/" );
+    }
     context.setWar( warPath );
     context.setErrorHandler(errorHandler);
     // internalUndeploy( topology ); KNOX-152

http://git-wip-us.apache.org/repos/asf/knox/blob/fc004156/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 fc27cfb..48ff286 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
@@ -64,6 +64,11 @@ 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 GATEWAY_DEFAULT_TOPOLOGY_NAME_PARAM = "default.app.topology.name";
+  private static final String GATEWAY_DEFAULT_TOPOLOGY_NAME = "_default";
+
   private static GatewayMessages log = MessagesFactory.get( GatewayMessages.class );
 
   private static final String GATEWAY_CONFIG_DIR_PREFIX = "conf";
@@ -101,7 +106,7 @@ public class GatewayConfigImpl extends Configuration implements GatewayConfig
{
   public static final String DEFAULT_DEPLOYMENT_DIR = "deployments";
   private static final String SSL_ENABLED = "ssl.enabled";
 //  public static final String DEFAULT_SHIRO_CONFIG_FILE = "shiro.ini";
-  
+
   public GatewayConfigImpl() {
     init();
   }
@@ -312,5 +317,23 @@ public class GatewayConfigImpl extends Configuration implements GatewayConfig
{
   public String getKerberosLoginConfig() {
     return get( KRB5_LOGIN_CONFIG );
   }
+
+  /* (non-Javadoc)
+   * @see org.apache.hadoop.gateway.config.GatewayConfig#getDefaultTopologyName()
+   */
+  @Override
+  public String getDefaultTopologyName() {
+    String name = get(GATEWAY_DEFAULT_TOPOLOGY_NAME_PARAM);
+    return name != null ? name : GATEWAY_DEFAULT_TOPOLOGY_NAME;
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.hadoop.gateway.config.GatewayConfig#getDefaultAppRedirectPath()
+   */
+  @Override
+  public String getDefaultAppRedirectPath() {
+    String path = get(DEFAULT_APP_REDIRECT_PATH_PARAM);
+    return path != null ? path : DEFAULT_APP_REDIRECT_PATH;
+  }
   
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/fc004156/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 10cfb00..1b639a0 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,6 +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.GatewayResources;
 import org.apache.hadoop.gateway.GatewayServlet;
 import org.apache.hadoop.gateway.config.GatewayConfig;
@@ -52,6 +53,7 @@ import java.util.Set;
 
 public abstract class DeploymentFactory {
 
+  private static final String DEFAULT_APP_REDIRECT_CONTEXT_PATH = "redirectTo";
   private static GatewayResources res = ResourcesFactory.get( GatewayResources.class );
   private static GatewayMessages log = MessagesFactory.get( GatewayMessages.class );
   private static GatewayServices gatewayServices = null;
@@ -73,15 +75,44 @@ public abstract class DeploymentFactory {
   }
 
   public static WebArchive createDeployment( GatewayConfig config, Topology topology ) {
-    Map<String,List<ProviderDeploymentContributor>> providers = selectContextProviders(
topology );
-    Map<String,List<ServiceDeploymentContributor>> services = selectContextServices(
topology );
-    DeploymentContext context = createDeploymentContext( config, topology, providers, services
);
-    initialize( context, providers, services );
-    contribute( context, providers, services );
-    finalize( context, providers, services );
+    DeploymentContext context = null;
+    if (!config.getDefaultTopologyName().equals(topology.getName())) {
+      Map<String,List<ProviderDeploymentContributor>> providers = selectContextProviders(
topology );
+      Map<String,List<ServiceDeploymentContributor>> services = selectContextServices(
topology );
+      context = createDeploymentContext( config, topology, providers, services );
+      initialize( context, providers, services );
+      contribute( context, providers, services );
+      finalize( context, providers, services );
+    }
+    else {
+      context = deployDefaultTopology(config, topology);
+    }
     return context.getWebArchive();
   }
 
+  private static DeploymentContext deployDefaultTopology(GatewayConfig config,
+      Topology topology) {
+    // this is the "default" topology which does some specialized
+    // redirects for compatibility with hadoop cli java client use
+    // we do not want the various listeners and providers added or
+    // the usual gateway.xml, etc.
+    DeploymentContext context;
+    Map<String,List<ProviderDeploymentContributor>> providers = new HashMap<String,List<ProviderDeploymentContributor>>();
+    Map<String,List<ServiceDeploymentContributor>> services = new HashMap<String,List<ServiceDeploymentContributor>>();
+    context = createDeploymentContext( config, topology, providers, services);
+    WebAppDescriptor wad = context.getWebAppDescriptor();
+    String servletName = context.getTopology().getName();
+    String servletClass = GatewayRedirectServlet.class.getName();
+    wad.createServlet().servletName( servletName ).servletClass( servletClass );
+    wad.createServletMapping().servletName( servletName ).urlPattern( "/*" );
+    ServletType<WebAppDescriptor> servlet = findServlet( context, context.getTopology().getName()
);
+    servlet.createInitParam()
+      .paramName( DEFAULT_APP_REDIRECT_CONTEXT_PATH )
+    .  paramValue( config.getDefaultAppRedirectPath() );
+    writeDeploymentDescriptor(context);
+    return context;
+  }
+
   private static DeploymentContext createDeploymentContext(
       GatewayConfig config, Topology topology,
       Map<String,List<ProviderDeploymentContributor>> providers,
@@ -343,15 +374,19 @@ public abstract class DeploymentFactory {
         }
       }
 
-      // Write the web.xml into the war.
-      Asset webXmlAsset = new StringAsset( context.getWebAppDescriptor().exportAsString()
);
-      context.getWebArchive().setWebXML( webXmlAsset );
+      writeDeploymentDescriptor(context);
 
     } catch ( IOException e ) {
       throw new RuntimeException( e );
     }
   }
 
+  private static void writeDeploymentDescriptor(DeploymentContext context) {
+    // Write the web.xml into the war.
+    Asset webXmlAsset = new StringAsset( context.getWebAppDescriptor().exportAsString() );
+    context.getWebArchive().setWebXML( webXmlAsset );
+  }
+
   public static ServletType<WebAppDescriptor> findServlet( DeploymentContext context,
String name ) {
     List<ServletType<WebAppDescriptor>> servlets = context.getWebAppDescriptor().getAllServlet();
     for( ServletType<WebAppDescriptor> servlet : servlets ) {

http://git-wip-us.apache.org/repos/asf/knox/blob/fc004156/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayGlobalConfigTest.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayGlobalConfigTest.java
b/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayGlobalConfigTest.java
index b6016ad..7abb043 100644
--- a/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayGlobalConfigTest.java
+++ b/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayGlobalConfigTest.java
@@ -61,4 +61,15 @@ public class GatewayGlobalConfigTest {
     //assertThat( config.getShiroConfigFile(), is( "shiro.ini") );
   }
 
+  @Test
+  public void testDefaultTopologyName() {
+    GatewayConfig config = new GatewayConfigImpl();
+    assertThat( config.getDefaultTopologyName(), is( "_default" ) );
+  }
+
+  @Test
+  public void testDefaultAppRedirectPath() {
+    GatewayConfig config = new GatewayConfigImpl();
+    assertThat( config.getDefaultAppRedirectPath(), is( "/gateway/sandbox" ) );
+  }
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/fc004156/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
new file mode 100644
index 0000000..5019e9a
--- /dev/null
+++ b/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayRedirectServletTest.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.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.sendRedirect("/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();
+    
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/fc004156/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/GatewayConfig.java
----------------------------------------------------------------------
diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/GatewayConfig.java
b/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/GatewayConfig.java
index f4f43e8..57b5acb 100644
--- a/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/GatewayConfig.java
+++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/config/GatewayConfig.java
@@ -76,5 +76,8 @@ public interface GatewayConfig {
   boolean isKerberosDebugEnabled();
   
   String getKerberosLoginConfig();
+  
+  String getDefaultTopologyName();
 
+  String getDefaultAppRedirectPath();
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/fc004156/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayTestConfig.java
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayTestConfig.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayTestConfig.java
index 93086ad..2ca2289 100644
--- a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayTestConfig.java
+++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayTestConfig.java
@@ -145,6 +145,23 @@ public class GatewayTestConfig implements GatewayConfig {
   public String getKerberosLoginConfig() {
     return kerberosLoginConfig;
   }
+
+  /* (non-Javadoc)
+   * @see org.apache.hadoop.gateway.config.GatewayConfig#getDefaultTopologyName()
+   */
+  @Override
+  public String getDefaultTopologyName() {
+    return "default";
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.hadoop.gateway.config.GatewayConfig#getDefaultAppRedirectPath()
+   */
+  @Override
+  public String getDefaultAppRedirectPath() {
+    // TODO Auto-generated method stub
+    return "/gateway/sandbox";
+  }
   
 //  public void setKerberosLoginConfig(String kerberosLoginConfig) {
 //   this.kerberosLoginConfig = kerberosLoginConfig;


Mime
View raw message