Repository: ambari Updated Branches: refs/heads/trunk 824bc8b6a -> 2e0fd3d89
AMBARI-11170 - Views : AmbariStreamProvider and URLStreamProvider should accept body as Object (tbeerbower) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/2e0fd3d8 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/2e0fd3d8 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/2e0fd3d8 Branch: refs/heads/trunk Commit: 2e0fd3d897f807f3086d7390d1c0af6fc3704fad Parents: 824bc8b Author: tbeerbower <[email protected]> Authored: Tue May 19 16:28:32 2015 -0400 Committer: tbeerbower <[email protected]> Committed: Tue May 19 16:28:55 2015 -0400 ---------------------------------------------------------------------- .../controller/internal/URLStreamProvider.java | 61 +++++++++--- .../apache/ambari/server/utils/HTTPUtils.java | 2 +- .../server/view/HttpImpersonatorImpl.java | 2 +- .../server/view/ViewAmbariStreamProvider.java | 22 ++++- .../server/view/ViewURLStreamProvider.java | 67 +++++++++---- .../internal/URLStreamProviderTest.java | 2 +- .../ambari/server/proxy/ProxyServiceTest.java | 10 +- .../view/ViewAmbariStreamProviderTest.java | 40 +++++++- .../server/view/ViewURLStreamProviderTest.java | 98 +++++++++++++++++++- .../ambari/view/AmbariStreamProvider.java | 23 ++++- .../apache/ambari/view/URLStreamProvider.java | 48 ++++++++++ 11 files changed, 324 insertions(+), 51 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/2e0fd3d8/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/URLStreamProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/URLStreamProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/URLStreamProvider.java index 1853021..1a8b085 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/URLStreamProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/URLStreamProvider.java @@ -65,30 +65,31 @@ public class URLStreamProvider implements StreamProvider { /** * Provide the connection timeout for the underlying connection. - * + * * @param connectionTimeout * time, in milliseconds, to attempt a connection * @param readTimeout * the read timeout in milliseconds * @param configuration configuration holding TrustStore information */ - public URLStreamProvider(int connectionTimeout, int readTimeout, - ComponentSSLConfiguration configuration) { + public URLStreamProvider(int connectionTimeout, int readTimeout, + ComponentSSLConfiguration configuration) { this(connectionTimeout, readTimeout, configuration.getTruststorePath(), configuration.getTruststorePassword(), configuration.getTruststoreType()); } + /** * Provide the connection timeout for the underlying connection. - * + * * @param connectionTimeout * time, in milliseconds, to attempt a connection * @param readTimeout * the read timeout in milliseconds */ public URLStreamProvider(int connectionTimeout, int readTimeout, String path, - String password, String type) { + String password, String type) { this.connTimeout = connectionTimeout; this.readTimeout = readTimeout; @@ -118,14 +119,50 @@ public class URLStreamProvider implements StreamProvider { * * @param spec the String to parse as a URL * @param requestMethod the HTTP method (GET,POST,PUT,etc.). - * @param params the body of the request; may be null + * @param body the body of the request; may be null + * @param headers the headers of the request; may be null + * + * @return a URL connection + * + * @throws IOException if the URL connection can not be established + */ + public HttpURLConnection processURL(String spec, String requestMethod, String body, Map<String, List<String>> headers) + throws IOException { + + return processURL(spec, requestMethod, body == null ? null : body.getBytes(), headers); + } + + /** + * Get a URL connection from the given 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 a URL connection * * @throws IOException if the URL connection can not be established */ - public HttpURLConnection processURL(String spec, String requestMethod, Object params, Map<String, List<String>> headers) + public HttpURLConnection processURL(String spec, String requestMethod, InputStream body, Map<String, List<String>> headers) + throws IOException { + + return processURL(spec, requestMethod, body == null ? null : IOUtils.toByteArray(body), headers); + } + + /** + * Get a URL connection from the given 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 a URL connection + * + * @throws IOException if the URL connection can not be established + */ + public HttpURLConnection processURL(String spec, String requestMethod, byte[] body, Map<String, List<String>> headers) throws IOException { if (LOG.isDebugEnabled()) { LOG.debug("readFrom spec:" + spec); @@ -164,14 +201,8 @@ public class URLStreamProvider implements StreamProvider { } } - if (params != null) { - byte[] info; - if (params instanceof InputStream) { - info = IOUtils.toByteArray((InputStream)params); - } else { - info = ((String)params).getBytes(); - } - connection.getOutputStream().write(info); + if (body != null) { + connection.getOutputStream().write(body); } int statusCode = connection.getResponseCode(); http://git-wip-us.apache.org/repos/asf/ambari/blob/2e0fd3d8/ambari-server/src/main/java/org/apache/ambari/server/utils/HTTPUtils.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/utils/HTTPUtils.java b/ambari-server/src/main/java/org/apache/ambari/server/utils/HTTPUtils.java index 89d3f14..cfb7128 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/utils/HTTPUtils.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/utils/HTTPUtils.java @@ -50,7 +50,7 @@ public class HTTPUtils { Map<String, List<String>> headers = new HashMap<String, List<String>>(); - HttpURLConnection connection = urlStreamProvider.processURL(url, "GET", null, headers); + HttpURLConnection connection = urlStreamProvider.processURL(url, "GET", (String) null, headers); int responseCode = connection.getResponseCode(); InputStream resultInputStream = null; http://git-wip-us.apache.org/repos/asf/ambari/blob/2e0fd3d8/ambari-server/src/main/java/org/apache/ambari/server/view/HttpImpersonatorImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/HttpImpersonatorImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/view/HttpImpersonatorImpl.java index fe4e180..7dd4947 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/view/HttpImpersonatorImpl.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/view/HttpImpersonatorImpl.java @@ -137,7 +137,7 @@ public class HttpImpersonatorImpl implements HttpImpersonator { Map<String, List<String>> headers = new HashMap<String, List<String>>(); headers.put(impersonatorSetting.getDoAsParamName(), new ArrayList<String>() {{add(impersonatorSetting.getUsername()); }} ); - HttpURLConnection connection = urlStreamProvider.processURL(url, requestType, null, headers); + HttpURLConnection connection = urlStreamProvider.processURL(url, requestType, (String) null, headers); int responseCode = connection.getResponseCode(); InputStream resultInputStream; http://git-wip-us.apache.org/repos/asf/ambari/blob/2e0fd3d8/ambari-server/src/main/java/org/apache/ambari/server/view/ViewAmbariStreamProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewAmbariStreamProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewAmbariStreamProvider.java index d8448b8..d36f562 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewAmbariStreamProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewAmbariStreamProvider.java @@ -22,6 +22,7 @@ import org.apache.ambari.server.controller.AmbariManagementController; import org.apache.ambari.server.controller.AmbariSessionManager; import org.apache.ambari.server.controller.internal.URLStreamProvider; import org.apache.ambari.view.AmbariStreamProvider; +import org.apache.commons.io.IOUtils; import java.io.IOException; import java.io.InputStream; @@ -72,10 +73,23 @@ public class ViewAmbariStreamProvider implements AmbariStreamProvider { // ----- AmbariStreamProvider ----------------------------------------------- @Override - public InputStream readFrom(String path, String requestMethod, String params, Map<String, String> headers, - boolean useAmbariSession) - throws IOException { + public InputStream readFrom(String path, String requestMethod, String body, Map<String, String> headers, + boolean useAmbariSession) throws IOException { + return getInputStream(path, requestMethod, headers, useAmbariSession, body.getBytes()); + } + + @Override + public InputStream readFrom(String path, String requestMethod, InputStream body, Map<String, String> headers, + boolean useAmbariSession) throws IOException { + + return getInputStream(path, requestMethod, headers, useAmbariSession, IOUtils.toByteArray(body)); + } + + + // ----- helper methods ---------------------------------------------------- + private InputStream getInputStream(String path, String requestMethod, Map<String, String> headers, + boolean useAmbariSession, byte[] body) throws IOException { // add the Ambari session cookie to the given headers if (useAmbariSession) { String sessionId = ambariSessionManager.getCurrentSessionId(); @@ -102,7 +116,7 @@ public class ViewAmbariStreamProvider implements AmbariStreamProvider { } return streamProvider.processURL(controller.getAmbariServerURI(path.startsWith("/") ? path : "/" + path), - requestMethod, params, headerMap).getInputStream(); + requestMethod, body, headerMap).getInputStream(); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/2e0fd3d8/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 0b81e04..32c130d 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 @@ -21,6 +21,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.ViewContext; +import org.apache.commons.io.IOUtils; import java.io.IOException; import java.io.InputStream; @@ -70,25 +71,50 @@ public class ViewURLStreamProvider implements org.apache.ambari.view.URLStreamPr @Override public InputStream readFrom(String spec, String requestMethod, String body, Map<String, String> headers) 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, body, headerMap); - - int responseCode = connection.getResponseCode(); + return getInputStream(spec, requestMethod, headers, body.getBytes()); + } - return responseCode >= ProxyService.HTTP_ERROR_RANGE_START ? - connection.getErrorStream() : connection.getInputStream(); + @Override + public InputStream readFrom(String spec, String requestMethod, InputStream body, Map<String, String> headers) + throws IOException { + return getInputStream(spec, requestMethod, headers, IOUtils.toByteArray(body)); } + @Override public InputStream readAs(String spec, String requestMethod, String body, Map<String, String> headers, String userName) throws IOException { + return readFrom(spec, requestMethod, body, addDoAsHeader(spec, headers, userName)); + } + + @Override + public InputStream readAs(String spec, String requestMethod, InputStream body, Map<String, String> headers, + String userName) throws IOException { + return readFrom(spec, requestMethod, body, addDoAsHeader(spec, headers, userName)); + } + + + @Override + public InputStream readAsCurrent(String spec, String requestMethod, String body, Map<String, String> headers) + throws IOException { + + return readAs(spec, requestMethod, body, headers, viewContext.getUsername()); + } + + @Override + public InputStream readAsCurrent(String spec, String requestMethod, InputStream body, Map<String, String> headers) + throws IOException { + + return readAs(spec, requestMethod, body, headers, viewContext.getUsername()); + } + + + // ----- helper methods ---------------------------------------------------- + + // add the "do as" header + private Map<String, String> addDoAsHeader(String spec, Map<String, String> headers, String userName) { if (spec.toLowerCase().contains(DO_AS_PARAM)) { throw new IllegalArgumentException("URL cannot contain \"" + DO_AS_PARAM + "\" parameter."); } @@ -100,15 +126,24 @@ public class ViewURLStreamProvider implements org.apache.ambari.view.URLStreamPr } headers.put(DO_AS_PARAM, userName); - - return readFrom(spec, requestMethod, body, headers); + return headers; } - @Override - public InputStream readAsCurrent(String spec, String requestMethod, String body, Map<String, String> headers) + // 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())); + } - return readAs(spec, requestMethod, body, headers, viewContext.getUsername()); + HttpURLConnection connection = streamProvider.processURL(spec, requestMethod, info, headerMap); + + int responseCode = connection.getResponseCode(); + + return responseCode >= ProxyService.HTTP_ERROR_RANGE_START ? + connection.getErrorStream() : connection.getInputStream(); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/2e0fd3d8/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/URLStreamProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/URLStreamProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/URLStreamProviderTest.java index ebed4db..7268ff2 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/URLStreamProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/URLStreamProviderTest.java @@ -68,7 +68,7 @@ public class URLStreamProviderTest { replay(urlStreamProvider, connection, appCookieManager); - Assert.assertEquals(connection, urlStreamProvider.processURL("spec", "GET", null, headerMap)); + Assert.assertEquals(connection, urlStreamProvider.processURL("spec", "GET", (String) null, headerMap)); verify(urlStreamProvider, connection, appCookieManager); } http://git-wip-us.apache.org/repos/asf/ambari/blob/2e0fd3d8/ambari-server/src/test/java/org/apache/ambari/server/proxy/ProxyServiceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/proxy/ProxyServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/proxy/ProxyServiceTest.java index 8ad8889..ba8f01e 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/proxy/ProxyServiceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/proxy/ProxyServiceTest.java @@ -82,7 +82,7 @@ class ProxyServiceTest extends BaseServiceTest { expect(getUriInfo().getRequestUri()).andReturn(uriMock); expect(getUriInfo().getQueryParameters()).andReturn(queryParams); expect(uriMock.getQuery()).andReturn("url=testurl"); - expect(streamProviderMock.processURL("testurl", "GET", null, headerParamsToForward)).andReturn(urlConnectionMock); + expect(streamProviderMock.processURL("testurl", "GET", (InputStream) null, headerParamsToForward)).andReturn(urlConnectionMock); expect(urlConnectionMock.getResponseCode()).andReturn(200); expect(urlConnectionMock.getContentType()).andReturn("text/plain"); expect(urlConnectionMock.getInputStream()).andReturn(is); @@ -198,7 +198,7 @@ class ProxyServiceTest extends BaseServiceTest { expect(getUriInfo().getRequestUri()).andReturn(uriMock); expect(getUriInfo().getQueryParameters()).andReturn(queryParams); expect(uriMock.getQuery()).andReturn("url=testurl"); - expect(streamProviderMock.processURL("testurl", "DELETE", null, headerParamsToForward)).andReturn(urlConnectionMock); + expect(streamProviderMock.processURL("testurl", "DELETE", (InputStream) null, headerParamsToForward)).andReturn(urlConnectionMock); expect(urlConnectionMock.getResponseCode()).andReturn(200); expect(urlConnectionMock.getContentType()).andReturn("text/plain"); expect(urlConnectionMock.getInputStream()).andReturn(is); @@ -236,7 +236,7 @@ class ProxyServiceTest extends BaseServiceTest { expect(getUriInfo().getRequestUri()).andReturn(uriMock); expect(getUriInfo().getQueryParameters()).andReturn(queryParams); expect(uriMock.getQuery()).andReturn("url=testurl"); - expect(streamProviderMock.processURL("testurl", "GET", null, headerParamsToForward)).andReturn(urlConnectionMock); + expect(streamProviderMock.processURL("testurl", "GET", (InputStream) null, headerParamsToForward)).andReturn(urlConnectionMock); expect(urlConnectionMock.getResponseCode()).andReturn(400).times(2); expect(urlConnectionMock.getContentType()).andReturn("text/plain"); expect(urlConnectionMock.getErrorStream()).andReturn(es); @@ -274,7 +274,7 @@ class ProxyServiceTest extends BaseServiceTest { expect(getUriInfo().getRequestUri()).andReturn(uriMock); expect(getUriInfo().getQueryParameters()).andReturn(queryParams); expect(uriMock.getQuery()).andReturn("url=testurl"); - expect(streamProviderMock.processURL("testurl", "GET", null, headerParamsToForward)).andReturn(urlConnectionMock); + expect(streamProviderMock.processURL("testurl", "GET", (InputStream) null, headerParamsToForward)).andReturn(urlConnectionMock); expect(urlConnectionMock.getResponseCode()).andReturn(200); expect(urlConnectionMock.getContentType()).andReturn("application/json"); expect(urlConnectionMock.getInputStream()).andReturn(new ByteArrayInputStream("{ \"test\":\"test\" }".getBytes())); @@ -314,7 +314,7 @@ class ProxyServiceTest extends BaseServiceTest { expect(urlConnectionMock.getInputStream()).andReturn(is); PowerMock.expectNew(URLStreamProvider.class, 20000, 15000, null, null, null).andReturn(streamProviderMock); expect(streamProviderMock.processURL("http://server:8188/ws/v1/timeline/HIVE_QUERY_ID?fields=events,primary" + - "filters&limit=10&primaryFilter=user:hiveuser1", "GET", null, headerParamsToForward)).andReturn(urlConnectionMock); + "filters&limit=10&primaryFilter=user:hiveuser1", "GET", (InputStream) null, headerParamsToForward)).andReturn(urlConnectionMock); PowerMock.replay(streamProviderMock, URLStreamProvider.class); replay(getUriInfo(), urlConnectionMock, getHttpHeaders()); ps.processGetRequestForwarding(getHttpHeaders(),getUriInfo()); http://git-wip-us.apache.org/repos/asf/ambari/blob/2e0fd3d8/ambari-server/src/test/java/org/apache/ambari/server/view/ViewAmbariStreamProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewAmbariStreamProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewAmbariStreamProviderTest.java index 94f5e86..d424a29 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewAmbariStreamProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewAmbariStreamProviderTest.java @@ -24,6 +24,7 @@ import org.apache.ambari.server.controller.internal.URLStreamProvider; import org.junit.Assert; import org.junit.Test; +import java.io.ByteArrayInputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.util.Collections; @@ -31,7 +32,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import static org.easymock.EasyMock.aryEq; import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.eq; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; @@ -59,7 +62,7 @@ public class ViewAmbariStreamProviderTest { expect(sessionManager.getSessionCookie()).andReturn("AMBARISESSIONID"); expect(controller.getAmbariServerURI("/spec")).andReturn("http://c6401.ambari.apache.org:8080/spec"); - expect(streamProvider.processURL("http://c6401.ambari.apache.org:8080/spec", "requestMethod", "params", headerMap)).andReturn(urlConnection); + expect(streamProvider.processURL(eq("http://c6401.ambari.apache.org:8080/spec"), eq("requestMethod"), aryEq("params".getBytes()), eq(headerMap))).andReturn(urlConnection); expect(urlConnection.getInputStream()).andReturn(inputStream); replay(streamProvider, sessionManager, controller, urlConnection, inputStream); @@ -70,4 +73,39 @@ public class ViewAmbariStreamProviderTest { verify(streamProvider, sessionManager, urlConnection, inputStream); } + + @Test + public void testReadFromNew() throws Exception { + URLStreamProvider streamProvider = createNiceMock(URLStreamProvider.class); + AmbariSessionManager sessionManager = createNiceMock(AmbariSessionManager.class); + AmbariManagementController controller = createNiceMock(AmbariManagementController.class); + + HttpURLConnection urlConnection = createNiceMock(HttpURLConnection.class); + InputStream inputStream = createNiceMock(InputStream.class); + + InputStream body = new ByteArrayInputStream("params".getBytes()); + + Map<String, String> headers = new HashMap<String, String>(); + headers.put("header", "headerValue"); + headers.put("Cookie", "FOO=bar"); + + Map<String, List<String>> headerMap = new HashMap<String, List<String>>(); + headerMap.put("header", Collections.singletonList("headerValue")); + headerMap.put("Cookie", Collections.singletonList("FOO=bar; AMBARISESSIONID=abcdefg")); + + expect(sessionManager.getCurrentSessionId()).andReturn("abcdefg"); + expect(sessionManager.getSessionCookie()).andReturn("AMBARISESSIONID"); + + expect(controller.getAmbariServerURI("/spec")).andReturn("http://c6401.ambari.apache.org:8080/spec"); + expect(streamProvider.processURL(eq("http://c6401.ambari.apache.org:8080/spec"), eq("requestMethod"), aryEq("params".getBytes()), eq(headerMap))).andReturn(urlConnection); + expect(urlConnection.getInputStream()).andReturn(inputStream); + + replay(streamProvider, sessionManager, controller, urlConnection, inputStream); + + ViewAmbariStreamProvider viewAmbariStreamProvider = new ViewAmbariStreamProvider(streamProvider, sessionManager, controller); + + Assert.assertEquals(inputStream, viewAmbariStreamProvider.readFrom("spec", "requestMethod", body, headers, true)); + + verify(streamProvider, sessionManager, urlConnection, inputStream); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/2e0fd3d8/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 4a265e7..908e63c 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 @@ -23,6 +23,7 @@ import org.apache.ambari.view.ViewContext; import org.junit.Assert; import org.junit.Test; +import java.io.ByteArrayInputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.util.Collections; @@ -30,7 +31,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import static org.easymock.EasyMock.aryEq; import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.eq; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; @@ -51,7 +54,7 @@ public class ViewURLStreamProviderTest { 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(streamProvider.processURL(eq("spec"), eq("requestMethod"), aryEq("params".getBytes()), eq(headerMap))).andReturn(urlConnection); expect(urlConnection.getInputStream()).andReturn(inputStream); replay(streamProvider, urlConnection, inputStream); @@ -79,7 +82,7 @@ public class ViewURLStreamProviderTest { headerMap.put("header", Collections.singletonList("headerValue")); headerMap.put("doAs", Collections.singletonList("joe")); - expect(streamProvider.processURL("spec", "requestMethod", "params", headerMap)).andReturn(urlConnection); + expect(streamProvider.processURL(eq("spec"), eq("requestMethod"), aryEq("params".getBytes()), eq(headerMap))).andReturn(urlConnection); expect(urlConnection.getInputStream()).andReturn(inputStream); replay(streamProvider, urlConnection, inputStream); @@ -107,7 +110,7 @@ public class ViewURLStreamProviderTest { headerMap.put("header", Collections.singletonList("headerValue")); headerMap.put("doAs", Collections.singletonList("joe")); - expect(streamProvider.processURL("spec", "requestMethod", "params", headerMap)).andReturn(urlConnection); + expect(streamProvider.processURL(eq("spec"), eq("requestMethod"), aryEq("params".getBytes()), eq(headerMap))).andReturn(urlConnection); expect(urlConnection.getInputStream()).andReturn(inputStream); expect(viewContext.getUsername()).andReturn("joe").anyTimes(); @@ -119,4 +122,93 @@ public class ViewURLStreamProviderTest { verify(streamProvider, urlConnection, inputStream, viewContext); } + + @Test + public void testReadFromInputStream() throws Exception { + + URLStreamProvider streamProvider = createNiceMock(URLStreamProvider.class); + HttpURLConnection urlConnection = createNiceMock(HttpURLConnection.class); + InputStream inputStream = createNiceMock(InputStream.class); + ViewContext viewContext = createNiceMock(ViewContext.class); + + InputStream body = new ByteArrayInputStream("params".getBytes()); + + 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); + expect(urlConnection.getInputStream()).andReturn(inputStream); + + replay(streamProvider, urlConnection, inputStream); + + ViewURLStreamProvider viewURLStreamProvider = new ViewURLStreamProvider(viewContext, streamProvider); + + Assert.assertEquals(inputStream, viewURLStreamProvider.readFrom("spec", "requestMethod", body, headers)); + + verify(streamProvider, urlConnection, inputStream); + } + + @Test + public void testReadAsInputStream() throws Exception { + + URLStreamProvider streamProvider = createNiceMock(URLStreamProvider.class); + HttpURLConnection urlConnection = createNiceMock(HttpURLConnection.class); + InputStream inputStream = createNiceMock(InputStream.class); + ViewContext viewContext = createNiceMock(ViewContext.class); + + InputStream body = new ByteArrayInputStream("params".getBytes()); + + Map<String, String> headers = new HashMap<String, String>(); + headers.put("header", "headerValue"); + headers.put("doAs", "joe"); + + Map<String, List<String>> headerMap = new HashMap<String, List<String>>(); + headerMap.put("header", Collections.singletonList("headerValue")); + headerMap.put("doAs", Collections.singletonList("joe")); + + expect(streamProvider.processURL(eq("spec"), eq("requestMethod"), aryEq("params".getBytes()), eq(headerMap))).andReturn(urlConnection); + expect(urlConnection.getInputStream()).andReturn(inputStream); + + replay(streamProvider, urlConnection, inputStream); + + ViewURLStreamProvider viewURLStreamProvider = new ViewURLStreamProvider(viewContext, streamProvider); + + Assert.assertEquals(inputStream, viewURLStreamProvider.readAs("spec", "requestMethod", body, headers, "joe")); + + verify(streamProvider, urlConnection, inputStream); + } + + @Test + public void testReadAsCurrentInputStream() throws Exception { + + URLStreamProvider streamProvider = createNiceMock(URLStreamProvider.class); + HttpURLConnection urlConnection = createNiceMock(HttpURLConnection.class); + InputStream inputStream = createNiceMock(InputStream.class); + ViewContext viewContext = createNiceMock(ViewContext.class); + + InputStream body = new ByteArrayInputStream("params".getBytes()); + + Map<String, String> headers = new HashMap<String, String>(); + headers.put("header", "headerValue"); + headers.put("doAs", "joe"); + + Map<String, List<String>> headerMap = new HashMap<String, List<String>>(); + headerMap.put("header", Collections.singletonList("headerValue")); + headerMap.put("doAs", Collections.singletonList("joe")); + + expect(streamProvider.processURL(eq("spec"), eq("requestMethod"), aryEq("params".getBytes()), eq(headerMap))).andReturn(urlConnection); + expect(urlConnection.getInputStream()).andReturn(inputStream); + expect(viewContext.getUsername()).andReturn("joe").anyTimes(); + + replay(streamProvider, urlConnection, inputStream, viewContext); + + ViewURLStreamProvider viewURLStreamProvider = new ViewURLStreamProvider(viewContext, streamProvider); + + Assert.assertEquals(inputStream, viewURLStreamProvider.readAsCurrent("spec", "requestMethod", body, headers)); + + verify(streamProvider, urlConnection, inputStream, viewContext); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/2e0fd3d8/ambari-views/src/main/java/org/apache/ambari/view/AmbariStreamProvider.java ---------------------------------------------------------------------- diff --git a/ambari-views/src/main/java/org/apache/ambari/view/AmbariStreamProvider.java b/ambari-views/src/main/java/org/apache/ambari/view/AmbariStreamProvider.java index 0f8367a..ce91676 100644 --- a/ambari-views/src/main/java/org/apache/ambari/view/AmbariStreamProvider.java +++ b/ambari-views/src/main/java/org/apache/ambari/view/AmbariStreamProvider.java @@ -31,7 +31,7 @@ public interface AmbariStreamProvider { * * @param path the String to parse as an Ambari endpoint (e.g. /api/v1/users) * @param requestMethod the HTTP method (GET,POST,PUT,etc.). - * @param params the body of the request; may be null + * @param body the body of the request; may be null * @param headers the headers of the request; may be null * @param useAmbariSession indicates that the current Ambari session cookie should be set for the request * @@ -39,7 +39,22 @@ public interface AmbariStreamProvider { * * @throws java.io.IOException if an error occurred connecting to the server */ - public InputStream readFrom(String path, String requestMethod, String params, - Map<String, String> headers, boolean useAmbariSession) - throws IOException; + public InputStream readFrom(String path, String requestMethod, String body, + Map<String, String> headers, boolean useAmbariSession) throws IOException; + + /** + * Read from the input stream specified by the given path on the Ambari server. + * + * @param path the String to parse as an Ambari endpoint (e.g. /api/v1/users) + * @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 useAmbariSession indicates that the current Ambari session cookie should be set for the request + * + * @return the input stream + * + * @throws java.io.IOException if an error occurred connecting to the server + */ + public InputStream readFrom(String path, String requestMethod, InputStream body, + Map<String, String> headers, boolean useAmbariSession) throws IOException; } http://git-wip-us.apache.org/repos/asf/ambari/blob/2e0fd3d8/ambari-views/src/main/java/org/apache/ambari/view/URLStreamProvider.java ---------------------------------------------------------------------- diff --git a/ambari-views/src/main/java/org/apache/ambari/view/URLStreamProvider.java b/ambari-views/src/main/java/org/apache/ambari/view/URLStreamProvider.java index 2b667e4..f98f09a 100644 --- a/ambari-views/src/main/java/org/apache/ambari/view/URLStreamProvider.java +++ b/ambari-views/src/main/java/org/apache/ambari/view/URLStreamProvider.java @@ -42,6 +42,21 @@ public interface URLStreamProvider { throws IOException; /** + * Read from the input stream 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 input stream + * + * @throws IOException if an error occurred connecting to the server + */ + public InputStream readFrom(String spec, String requestMethod, InputStream body, Map<String, String> headers) + throws IOException; + + /** * Read from the input stream specified by the given URL spec as the given user. This method sets the * "doAs" user header to impersonate the user over the request. * @@ -60,6 +75,23 @@ public interface URLStreamProvider { throws IOException; /** + * Read from the input stream 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 input stream + * + * @throws IOException if an error occurred connecting to the server + */ + public InputStream readAs(String spec, String requestMethod, InputStream body, Map<String, String> headers, + String userName) throws IOException; + + /** * Read from the input stream specified by the given URL spec as the current user. This method sets the * "doAs" user header to impersonate the user over the request. * @@ -75,4 +107,20 @@ public interface URLStreamProvider { public InputStream readAsCurrent(String spec, String requestMethod, String body, Map<String, String> headers) throws IOException; + /** + * Read from the input stream 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 input stream + * + * @throws IOException if an error occurred connecting to the server + */ + public InputStream readAsCurrent(String spec, String requestMethod, InputStream body, Map<String, String> headers) + throws IOException; + }
