瀏覽代碼

AMBARI-11516 - Views : Add URLConnectionProvider interface (tbeerbower)

tbeerbower 10 年之前
父節點
當前提交
6c0f9acef0

+ 7 - 0
ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java

@@ -38,6 +38,7 @@ import org.apache.ambari.view.Masker;
 import org.apache.ambari.view.ResourceProvider;
 import org.apache.ambari.view.SecurityException;
 import org.apache.ambari.view.SystemException;
+import org.apache.ambari.view.URLConnectionProvider;
 import org.apache.ambari.view.ViewContext;
 import org.apache.ambari.view.ViewController;
 import org.apache.ambari.view.ViewDefinition;
@@ -262,6 +263,12 @@ public class ViewContextImpl implements ViewContext, ViewController {
     return streamProvider;
   }
 
+  @Override
+  public URLConnectionProvider getURLConnectionProvider() {
+    ensureURLStreamProvider();
+    return streamProvider;
+  }
+
   @Override
   public synchronized AmbariStreamProvider getAmbariStreamProvider() {
     if (ambariStreamProvider == null) {

+ 65 - 10
ambari-server/src/main/java/org/apache/ambari/server/view/ViewURLStreamProvider.java

@@ -20,6 +20,7 @@ package org.apache.ambari.server.view;
 
 import org.apache.ambari.server.controller.internal.URLStreamProvider;
 import org.apache.ambari.server.proxy.ProxyService;
+import org.apache.ambari.view.URLConnectionProvider;
 import org.apache.ambari.view.ViewContext;
 import org.apache.commons.io.IOUtils;
 
@@ -36,7 +37,7 @@ import java.util.Map;
 /**
  * Wrapper around an internal URL stream provider.
  */
-public class ViewURLStreamProvider implements org.apache.ambari.view.URLStreamProvider {
+public class ViewURLStreamProvider implements org.apache.ambari.view.URLStreamProvider, URLConnectionProvider {
 
   /**
    * The key for the "doAs" header.
@@ -87,7 +88,6 @@ public class ViewURLStreamProvider implements org.apache.ambari.view.URLStreamPr
   public InputStream readAs(String spec, String requestMethod, String body, Map<String, String> headers,
                             String userName)
       throws IOException {
-
     return readFrom(addDoAs(spec, userName), requestMethod, body, headers);
   }
 
@@ -112,6 +112,55 @@ public class ViewURLStreamProvider implements org.apache.ambari.view.URLStreamPr
     return readAs(spec, requestMethod, body, headers, viewContext.getUsername());
   }
 
+  @Override
+  public HttpURLConnection getConnection(String spec,
+                                         String requestMethod,
+                                         String body,
+                                         Map<String, String> headers) throws IOException {
+    return getHttpURLConnection(spec, requestMethod, headers, body == null ? null : body.getBytes());
+  }
+
+  @Override
+  public HttpURLConnection getConnection(String spec,
+                                         String requestMethod,
+                                         InputStream body,
+                                         Map<String, String> headers) throws IOException {
+    return getHttpURLConnection(spec, requestMethod, headers, body == null ? null : IOUtils.toByteArray(body));
+  }
+
+  @Override
+  public HttpURLConnection getConnectionAs(String spec,
+                                           String requestMethod,
+                                           String body,
+                                           Map<String, String> headers,
+                                           String userName) throws IOException {
+    return getConnection(addDoAs(spec, userName), requestMethod, body, headers);
+  }
+
+  @Override
+  public HttpURLConnection getConnectionAs(String spec,
+                                           String requestMethod,
+                                           InputStream body,
+                                           Map<String, String> headers,
+                                           String userName) throws IOException {
+    return getConnection(addDoAs(spec, userName), requestMethod, body, headers);
+  }
+
+  @Override
+  public HttpURLConnection getConnectionAsCurrent(String spec,
+                                                  String requestMethod,
+                                                  String body,
+                                                  Map<String, String> headers) throws IOException {
+    return getConnectionAs(spec, requestMethod, body, headers, viewContext.getUsername());
+  }
+
+  @Override
+  public HttpURLConnection getConnectionAsCurrent(String spec,
+                                                  String requestMethod,
+                                                  InputStream body,
+                                                  Map<String, String> headers) throws IOException {
+    return getConnectionAs(spec, requestMethod, body, headers, viewContext.getUsername());
+  }
 
   // ----- helper methods ----------------------------------------------------
 
@@ -133,18 +182,24 @@ public class ViewURLStreamProvider implements org.apache.ambari.view.URLStreamPr
   // get the input stream response from the underlying stream provider
   private InputStream getInputStream(String spec, String requestMethod, Map<String, String> headers, byte[] info)
       throws IOException {
-    // adapt the headers to the 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()));
-    }
-
-    HttpURLConnection connection = streamProvider.processURL(spec, requestMethod, info, headerMap);
+    HttpURLConnection connection = getHttpURLConnection(spec, requestMethod, headers, info);
 
     int responseCode = connection.getResponseCode();
 
     return responseCode >= ProxyService.HTTP_ERROR_RANGE_START ?
         connection.getErrorStream() : connection.getInputStream();
   }
-}
 
+  // get the input stream response from the underlying stream provider
+  private HttpURLConnection getHttpURLConnection(String spec, String requestMethod,
+                                                 Map<String, String> headers, byte[] info)
+      throws IOException {
+    // adapt the headers to the 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, info, headerMap);
+  }
+
+}

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

@@ -27,6 +27,7 @@ import org.apache.ambari.server.view.configuration.InstanceConfig;
 import org.apache.ambari.server.view.configuration.InstanceConfigTest;
 import org.apache.ambari.server.view.configuration.ViewConfigTest;
 import org.apache.ambari.view.ResourceProvider;
+import org.apache.ambari.view.ViewContext;
 import org.apache.ambari.view.cluster.Cluster;
 import org.junit.Assert;
 import org.junit.Test;
@@ -197,6 +198,30 @@ public class ViewContextImplTest {
     verify(viewRegistry);
   }
 
+  @Test
+  public void testGetURLConnectionProvider() throws Exception {
+    InstanceConfig instanceConfig = InstanceConfigTest.getInstanceConfigs().get(0);
+    ViewEntity viewDefinition = ViewEntityTest.getViewEntity();
+    ViewInstanceEntity viewInstanceDefinition = new ViewInstanceEntity(viewDefinition, instanceConfig);
+    ViewRegistry viewRegistry = createNiceMock(ViewRegistry.class);
+    ViewURLStreamProvider urlStreamProvider = createNiceMock(ViewURLStreamProvider.class);
+
+    ResourceProvider provider = createNiceMock(ResourceProvider.class);
+    Resource.Type type = new Resource.Type("MY_VIEW/myType");
+
+    viewInstanceDefinition.addResourceProvider(type, provider);
+
+    ViewContext viewContext = new ViewContextImpl(viewInstanceDefinition, viewRegistry);
+
+    expect(viewRegistry.createURLStreamProvider(viewContext)).andReturn(urlStreamProvider);
+
+    replay(viewRegistry);
+
+    Assert.assertEquals(urlStreamProvider, viewContext.getURLConnectionProvider());
+
+    verify(viewRegistry);
+  }
+
   @Test
   public void testGetAmbariStreamProvider() throws Exception {
     InstanceConfig instanceConfig = InstanceConfigTest.getInstanceConfigs().get(0);

+ 75 - 6
ambari-server/src/test/java/org/apache/ambari/server/view/ViewURLStreamProviderTest.java

@@ -74,8 +74,6 @@ public class ViewURLStreamProviderTest {
     InputStream inputStream = createNiceMock(InputStream.class);
     ViewContext viewContext = createNiceMock(ViewContext.class);
 
-    String body = null;
-
     Map<String, String> headers = new HashMap<String, String>();
     headers.put("header", "headerValue");
 
@@ -89,7 +87,7 @@ public class ViewURLStreamProviderTest {
 
     ViewURLStreamProvider viewURLStreamProvider = new ViewURLStreamProvider(viewContext, streamProvider);
 
-    Assert.assertEquals(inputStream, viewURLStreamProvider.readFrom("spec", "requestMethod", body, headers));
+    Assert.assertEquals(inputStream, viewURLStreamProvider.readFrom("spec", "requestMethod", (String) null, headers));
 
     verify(streamProvider, urlConnection, inputStream);
   }
@@ -183,8 +181,6 @@ public class ViewURLStreamProviderTest {
     InputStream inputStream = createNiceMock(InputStream.class);
     ViewContext viewContext = createNiceMock(ViewContext.class);
 
-    InputStream body = null;
-
     Map<String, String> headers = new HashMap<String, String>();
     headers.put("header", "headerValue");
 
@@ -198,7 +194,7 @@ public class ViewURLStreamProviderTest {
 
     ViewURLStreamProvider viewURLStreamProvider = new ViewURLStreamProvider(viewContext, streamProvider);
 
-    Assert.assertEquals(inputStream, viewURLStreamProvider.readFrom("spec", "requestMethod", body, headers));
+    Assert.assertEquals(inputStream, viewURLStreamProvider.readFrom("spec", "requestMethod", (InputStream) null, headers));
 
     verify(streamProvider, urlConnection, inputStream);
   }
@@ -259,4 +255,77 @@ public class ViewURLStreamProviderTest {
 
     verify(streamProvider, urlConnection, inputStream, viewContext);
   }
+
+  @Test
+  public void testGetConnection() throws Exception {
+
+    URLStreamProvider streamProvider = createNiceMock(URLStreamProvider.class);
+    HttpURLConnection urlConnection = createNiceMock(HttpURLConnection.class);
+    ViewContext viewContext = createNiceMock(ViewContext.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(eq("spec"), eq("requestMethod"), aryEq("params".getBytes()), eq(headerMap))).andReturn(urlConnection);
+
+    replay(streamProvider, urlConnection);
+
+    ViewURLStreamProvider viewURLStreamProvider = new ViewURLStreamProvider(viewContext, streamProvider);
+
+    Assert.assertEquals(urlConnection, viewURLStreamProvider.getConnection("spec", "requestMethod", "params", headers));
+
+    verify(streamProvider, urlConnection);
+  }
+
+  @Test
+  public void testGetConnectionAs() throws Exception {
+
+    URLStreamProvider streamProvider = createNiceMock(URLStreamProvider.class);
+    HttpURLConnection urlConnection = createNiceMock(HttpURLConnection.class);
+    ViewContext viewContext = createNiceMock(ViewContext.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(eq("spec?doAs=joe"), eq("requestMethod"), aryEq("params".getBytes()), eq(headerMap))).andReturn(urlConnection);
+
+    replay(streamProvider, urlConnection);
+
+    ViewURLStreamProvider viewURLStreamProvider = new ViewURLStreamProvider(viewContext, streamProvider);
+
+    Assert.assertEquals(urlConnection, viewURLStreamProvider.getConnectionAs("spec", "requestMethod", "params", headers, "joe"));
+
+    verify(streamProvider, urlConnection);
+  }
+
+  @Test
+  public void testGetConnectionCurrent() throws Exception {
+
+    URLStreamProvider streamProvider = createNiceMock(URLStreamProvider.class);
+    HttpURLConnection urlConnection = createNiceMock(HttpURLConnection.class);
+    ViewContext viewContext = createNiceMock(ViewContext.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(eq("spec?doAs=joe"), eq("requestMethod"), aryEq("params".getBytes()), eq(headerMap))).andReturn(urlConnection);
+    expect(viewContext.getUsername()).andReturn("joe").anyTimes();
+
+    replay(streamProvider, urlConnection, viewContext);
+
+    ViewURLStreamProvider viewURLStreamProvider = new ViewURLStreamProvider(viewContext, streamProvider);
+
+    Assert.assertEquals(urlConnection, viewURLStreamProvider.getConnectionAsCurrent("spec", "requestMethod", "params", headers));
+
+    verify(streamProvider, urlConnection, viewContext);
+  }
 }

+ 2 - 2
ambari-views/pom.xml

@@ -127,9 +127,9 @@
                 <configuration>
                     <excludes>
                         <exclude>**/*.json</exclude>
-                    </excludes>
-                    <excludes>
                         <exclude>**/*.iml</exclude>
+                        <exclude>**/*.lst</exclude>
+                        <exclude>**/rat.txt</exclude>
                     </excludes>
                 </configuration>
                 <executions>

+ 130 - 0
ambari-views/src/main/java/org/apache/ambari/view/URLConnectionProvider.java

@@ -0,0 +1,130 @@
+/**
+ * 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.ambari.view;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.util.Map;
+
+/**
+ * Provider of a URL connection.
+ */
+public interface URLConnectionProvider {
+  /**
+   * Get the HttpURLConnection specified by the given URL spec.
+   *
+   * @param spec           the String to parse as a URL
+   * @param requestMethod  the HTTP method (GET,POST,PUT,etc.).
+   * @param body           the body of the request; may be null
+   * @param headers        the headers of the request; may be null
+   *
+   * @return the HttpURLConnection to read the input stream from
+   *
+   * @throws java.io.IOException if an error occurred connecting to the server
+   */
+  public HttpURLConnection getConnection(String spec, String requestMethod, String body, Map<String, String> headers)
+      throws IOException;
+
+  /**
+   * Get the HttpURLConnection specified by the given URL spec.
+   *
+   * @param spec           the String to parse as a URL
+   * @param requestMethod  the HTTP method (GET,POST,PUT,etc.).
+   * @param body           the body of the request; may be null
+   * @param headers        the headers of the request; may be null
+   *
+   * @return the HttpURLConnection to read the input stream from
+   *
+   * @throws IOException if an error occurred connecting to the server
+   */
+  public HttpURLConnection getConnection(String spec, String requestMethod, InputStream body, Map<String, String> headers)
+      throws IOException;
+
+  /**
+   * Get the HttpURLConnection specified by the
+   * given URL spec as the given user.  This method sets the
+   * "doAs" user header to impersonate the user over the request.
+   *
+   * @param spec           the String to parse as a URL
+   * @param requestMethod  the HTTP method (GET,POST,PUT,etc.).
+   * @param body           the body of the request; may be null
+   * @param headers        the headers of the request; may be null
+   * @param userName       the "doAs" user name
+   *
+   * @return the HttpURLConnection to read the input stream from
+   *
+   * @throws IOException if an error occurred connecting to the server
+   */
+  public HttpURLConnection getConnectionAs(String spec, String requestMethod, String body, Map<String, String> headers,
+                                           String userName)
+      throws IOException;
+
+  /**
+   * Get the HttpURLConnection specified by the given URL
+   * spec as the given user.  This method sets the
+   * "doAs" user header to impersonate the user over the request.
+   *
+   * @param spec           the String to parse as a URL
+   * @param requestMethod  the HTTP method (GET,POST,PUT,etc.).
+   * @param body           the body of the request; may be null
+   * @param headers        the headers of the request; may be null
+   * @param userName       the "doAs" user name
+   *
+   * @return the HttpURLConnection to read the input stream from
+   *
+   * @throws IOException if an error occurred connecting to the server
+   */
+  public HttpURLConnection getConnectionAs(String spec, String requestMethod, InputStream body, Map<String, String> headers,
+                                           String userName) throws IOException;
+
+  /**
+   * Get the HttpURLConnection specified by the given URL
+   * spec as the current user.  This method sets the "doAs" user header to impersonate
+   * the user over the request.
+   *
+   * @param spec           the String to parse as a URL
+   * @param requestMethod  the HTTP method (GET,POST,PUT,etc.).
+   * @param body           the body of the request; may be null
+   * @param headers        the headers of the request; may be null
+   *
+   * @return the HttpURLConnection to read the input stream from
+   *
+   * @throws IOException if an error occurred connecting to the server
+   */
+  public HttpURLConnection getConnectionAsCurrent(String spec, String requestMethod, String body, Map<String, String> headers)
+      throws IOException;
+
+  /**
+   * Get the HttpURLConnection specified by the given URL spec
+   * as the current user.  This method sets the "doAs" user header to impersonate
+   * the user over the request.
+   *
+   * @param spec           the String to parse as a URL
+   * @param requestMethod  the HTTP method (GET,POST,PUT,etc.).
+   * @param body           the body of the request; may be null
+   * @param headers        the headers of the request; may be null
+   *
+   * @return the HttpURLConnection to read the input stream from
+   *
+   * @throws IOException if an error occurred connecting to the server
+   */
+  public HttpURLConnection getConnectionAsCurrent(String spec, String requestMethod, InputStream body, Map<String, String> headers)
+      throws IOException;
+}

+ 7 - 0
ambari-views/src/main/java/org/apache/ambari/view/ViewContext.java

@@ -150,6 +150,13 @@ public interface ViewContext {
    */
   public URLStreamProvider getURLStreamProvider();
 
+  /**
+   * Get a URL connection provider.
+   *
+   * @return a connection provider
+   */
+  public URLConnectionProvider getURLConnectionProvider();
+
   /**
    * Get an Ambari stream provider.
    *