You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by tb...@apache.org on 2015/05/29 03:46:57 UTC
ambari git commit: AMBARI-11516 - Views : Add URLConnectionProvider
interface (tbeerbower)
Repository: ambari
Updated Branches:
refs/heads/trunk c74ebac52 -> 6c0f9acef
AMBARI-11516 - Views : Add URLConnectionProvider interface (tbeerbower)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/6c0f9ace
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/6c0f9ace
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/6c0f9ace
Branch: refs/heads/trunk
Commit: 6c0f9acef0f7ada38ebbd59a4fc2a70652050f4c
Parents: c74ebac
Author: tbeerbower <tb...@hortonworks.com>
Authored: Thu May 28 21:46:27 2015 -0400
Committer: tbeerbower <tb...@hortonworks.com>
Committed: Thu May 28 21:46:37 2015 -0400
----------------------------------------------------------------------
.../ambari/server/view/ViewContextImpl.java | 7 +
.../server/view/ViewURLStreamProvider.java | 75 +++++++++--
.../ambari/server/view/ViewContextImplTest.java | 25 ++++
.../server/view/ViewURLStreamProviderTest.java | 81 +++++++++++-
ambari-views/pom.xml | 4 +-
.../ambari/view/URLConnectionProvider.java | 130 +++++++++++++++++++
.../org/apache/ambari/view/ViewContext.java | 7 +
7 files changed, 311 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/6c0f9ace/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
index 9a4aae2..8788346 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
+++ b/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;
@@ -263,6 +264,12 @@ public class ViewContextImpl implements ViewContext, ViewController {
}
@Override
+ public URLConnectionProvider getURLConnectionProvider() {
+ ensureURLStreamProvider();
+ return streamProvider;
+ }
+
+ @Override
public synchronized AmbariStreamProvider getAmbariStreamProvider() {
if (ambariStreamProvider == null) {
ambariStreamProvider = viewRegistry.createAmbariStreamProvider();
http://git-wip-us.apache.org/repos/asf/ambari/blob/6c0f9ace/ambari-server/src/main/java/org/apache/ambari/server/view/ViewURLStreamProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewURLStreamProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewURLStreamProvider.java
index bb3b8aa..c8ee284 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewURLStreamProvider.java
+++ b/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);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/6c0f9ace/ambari-server/src/test/java/org/apache/ambari/server/view/ViewContextImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewContextImplTest.java b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewContextImplTest.java
index b470da2..e111811 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewContextImplTest.java
+++ b/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;
@@ -198,6 +199,30 @@ public class ViewContextImplTest {
}
@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);
ViewEntity viewDefinition = ViewEntityTest.getViewEntity();
http://git-wip-us.apache.org/repos/asf/ambari/blob/6c0f9ace/ambari-server/src/test/java/org/apache/ambari/server/view/ViewURLStreamProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewURLStreamProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewURLStreamProviderTest.java
index e606820..af48c4b 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewURLStreamProviderTest.java
+++ b/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);
+ }
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/6c0f9ace/ambari-views/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-views/pom.xml b/ambari-views/pom.xml
index 8fcb5ae..59e7026 100644
--- a/ambari-views/pom.xml
+++ b/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>
http://git-wip-us.apache.org/repos/asf/ambari/blob/6c0f9ace/ambari-views/src/main/java/org/apache/ambari/view/URLConnectionProvider.java
----------------------------------------------------------------------
diff --git a/ambari-views/src/main/java/org/apache/ambari/view/URLConnectionProvider.java b/ambari-views/src/main/java/org/apache/ambari/view/URLConnectionProvider.java
new file mode 100644
index 0000000..d02f964
--- /dev/null
+++ b/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;
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/6c0f9ace/ambari-views/src/main/java/org/apache/ambari/view/ViewContext.java
----------------------------------------------------------------------
diff --git a/ambari-views/src/main/java/org/apache/ambari/view/ViewContext.java b/ambari-views/src/main/java/org/apache/ambari/view/ViewContext.java
index 2522486..c0cae80 100644
--- a/ambari-views/src/main/java/org/apache/ambari/view/ViewContext.java
+++ b/ambari-views/src/main/java/org/apache/ambari/view/ViewContext.java
@@ -151,6 +151,13 @@ public interface ViewContext {
public URLStreamProvider getURLStreamProvider();
/**
+ * Get a URL connection provider.
+ *
+ * @return a connection provider
+ */
+ public URLConnectionProvider getURLConnectionProvider();
+
+ /**
* Get an Ambari stream provider.
*
* @return an Ambari stream provider