瀏覽代碼

AMBARI-4863 - Ambari Views : Allow headers to be passed to View URLStreamProvider

tbeerbower 11 年之前
父節點
當前提交
b6b9415ae3

+ 34 - 12
ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java

@@ -29,6 +29,9 @@ import org.springframework.security.core.userdetails.UserDetails;
 
 
 import java.io.IOException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Map;
 
 
 /**
 /**
@@ -56,7 +59,7 @@ public class ViewContextImpl implements ViewContext {
    */
    */
   public ViewContextImpl(ViewInstanceDefinition viewInstanceDefinition) {
   public ViewContextImpl(ViewInstanceDefinition viewInstanceDefinition) {
     this.viewInstanceDefinition = viewInstanceDefinition;
     this.viewInstanceDefinition = viewInstanceDefinition;
-    this.streamProvider         = new ViewURLStreamProvider();
+    this.streamProvider         = ViewURLStreamProvider.getProvider();
   }
   }
 
 
 
 
@@ -113,7 +116,7 @@ public class ViewContextImpl implements ViewContext {
   /**
   /**
    * Wrapper around internal URL stream provider.
    * Wrapper around internal URL stream provider.
    */
    */
-  private static class ViewURLStreamProvider implements URLStreamProvider {
+  protected static class ViewURLStreamProvider implements URLStreamProvider {
     private static final int DEFAULT_REQUEST_CONNECT_TIMEOUT = 5000;
     private static final int DEFAULT_REQUEST_CONNECT_TIMEOUT = 5000;
     private static final int DEFAULT_REQUEST_READ_TIMEOUT    = 10000;
     private static final int DEFAULT_REQUEST_READ_TIMEOUT    = 10000;
 
 
@@ -125,22 +128,41 @@ public class ViewContextImpl implements ViewContext {
 
 
     // ----- Constructor -----------------------------------------------------
     // ----- Constructor -----------------------------------------------------
 
 
-    private ViewURLStreamProvider() {
-      ComponentSSLConfiguration configuration = ComponentSSLConfiguration.instance();
-      streamProvider =
-          new org.apache.ambari.server.controller.internal.URLStreamProvider(
-              DEFAULT_REQUEST_CONNECT_TIMEOUT, DEFAULT_REQUEST_READ_TIMEOUT,
-              configuration.getTruststorePath(),
-              configuration.getTruststorePassword(),
-              configuration.getTruststoreType());
+    protected ViewURLStreamProvider(org.apache.ambari.server.controller.internal.URLStreamProvider streamProvider) {
+      this.streamProvider = streamProvider;
     }
     }
 
 
 
 
     // ----- URLStreamProvider -----------------------------------------------
     // ----- URLStreamProvider -----------------------------------------------
 
 
     @Override
     @Override
-    public InputStream readFrom(String spec, String requestMethod, String params) throws IOException {
-      return streamProvider.readFrom(spec, requestMethod, params);
+    public InputStream readFrom(String spec, String requestMethod, String params, Map<String, String> headers)
+        throws IOException {
+      // adapt to org.apache.ambari.server.controller.internal.URLStreamProvider processURL signature
+      Map<String, List<String>> headerMap = new HashMap<String, List<String>>();
+      for (Map.Entry<String, String> entry : headers.entrySet()) {
+        headerMap.put(entry.getKey(), Collections.singletonList(entry.getValue()));
+      }
+      return streamProvider.processURL(spec, requestMethod, params, headerMap).getInputStream();
+    }
+
+
+    // ----- helper methods --------------------------------------------------
+
+    /**
+     * Factory method.
+     *
+     * @return a new URL stream provider.
+     */
+    protected static ViewURLStreamProvider getProvider() {
+      ComponentSSLConfiguration configuration = ComponentSSLConfiguration.instance();
+      org.apache.ambari.server.controller.internal.URLStreamProvider streamProvider =
+          new org.apache.ambari.server.controller.internal.URLStreamProvider(
+              DEFAULT_REQUEST_CONNECT_TIMEOUT, DEFAULT_REQUEST_READ_TIMEOUT,
+              configuration.getTruststorePath(),
+              configuration.getTruststorePassword(),
+              configuration.getTruststoreType());
+      return new ViewURLStreamProvider(streamProvider);
     }
     }
   }
   }
 }
 }

+ 51 - 0
ambari-server/src/test/java/org/apache/ambari/server/view/ViewContextImplTest.java

@@ -18,6 +18,7 @@
 
 
 package org.apache.ambari.server.view;
 package org.apache.ambari.server.view;
 
 
+import org.apache.ambari.server.controller.internal.URLStreamProvider;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.view.configuration.InstanceConfig;
 import org.apache.ambari.server.view.configuration.InstanceConfig;
 import org.apache.ambari.server.view.configuration.InstanceConfigTest;
 import org.apache.ambari.server.view.configuration.InstanceConfigTest;
@@ -25,9 +26,17 @@ import org.apache.ambari.view.ResourceProvider;
 import org.junit.Assert;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.Test;
 
 
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Map;
 
 
 import static org.easymock.EasyMock.createNiceMock;
 import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
 
 
 /**
 /**
  * ViewContextImpl tests.
  * ViewContextImpl tests.
@@ -89,4 +98,46 @@ public class ViewContextImplTest {
 
 
     Assert.assertEquals(provider, viewContext.getResourceProvider("myType"));
     Assert.assertEquals(provider, viewContext.getResourceProvider("myType"));
   }
   }
+
+  @Test
+  public void testGetURLStreamProvider() throws Exception {
+    InstanceConfig instanceConfig = InstanceConfigTest.getInstanceConfigs().get(0);
+    ViewDefinition viewDefinition = ViewDefinitionTest.getViewDefinition();
+    ViewInstanceDefinition viewInstanceDefinition = new ViewInstanceDefinition(viewDefinition, instanceConfig);
+
+    ResourceProvider provider = createNiceMock(ResourceProvider.class);
+    Resource.Type type = new Resource.Type("MY_VIEW/myType");
+
+    viewInstanceDefinition.addResourceProvider(type, provider);
+
+    ViewContextImpl viewContext = new ViewContextImpl(viewInstanceDefinition);
+
+    Assert.assertNotNull(viewContext.getURLStreamProvider());
+  }
+
+  @Test
+  public void testViewURLStreamProvider() throws Exception {
+
+    URLStreamProvider streamProvider = createNiceMock(URLStreamProvider.class);
+    HttpURLConnection urlConnection = createNiceMock(HttpURLConnection.class);
+    InputStream inputStream = createNiceMock(InputStream.class);
+
+    Map<String, String> headers = new HashMap<String, String>();
+    headers.put("header", "headerValue");
+
+    Map<String, List<String>> headerMap = new HashMap<String, List<String>>();
+    headerMap.put("header", Collections.singletonList("headerValue"));
+
+    expect(streamProvider.processURL("spec", "requestMethod", "params", headerMap)).andReturn(urlConnection);
+    expect(urlConnection.getInputStream()).andReturn(inputStream);
+
+    replay(streamProvider, urlConnection, inputStream);
+
+    ViewContextImpl.ViewURLStreamProvider viewURLStreamProvider =
+        new ViewContextImpl.ViewURLStreamProvider(streamProvider);
+
+    Assert.assertEquals(inputStream, viewURLStreamProvider.readFrom("spec", "requestMethod", "params", headers));
+
+    verify(streamProvider, urlConnection, inputStream);
+  }
 }
 }

+ 4 - 1
ambari-views/src/main/java/org/apache/ambari/view/URLStreamProvider.java

@@ -20,6 +20,7 @@ package org.apache.ambari.view;
 
 
 import java.io.IOException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
+import java.util.Map;
 
 
 /**
 /**
  * Provider of a URL stream.
  * Provider of a URL stream.
@@ -31,10 +32,12 @@ public interface URLStreamProvider {
    * @param spec           the String to parse as a URL
    * @param spec           the String to parse as a URL
    * @param requestMethod  the HTTP method (GET,POST,PUT,etc.).
    * @param requestMethod  the HTTP method (GET,POST,PUT,etc.).
    * @param params         the body of the request; may be null
    * @param params         the body of the request; may be null
+   * @param headers        the headers of the request; may be null
    *
    *
    * @return the input stream
    * @return the input stream
    *
    *
    * @throws IOException if an error occurred connecting to the server
    * @throws IOException if an error occurred connecting to the server
    */
    */
-  public InputStream readFrom(String spec, String requestMethod, String params) throws IOException;
+  public InputStream readFrom(String spec, String requestMethod, String params, Map<String, String> headers)
+      throws IOException;
 }
 }