ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vbrodets...@apache.org
Subject git commit: AMBARI-4482: We should set content type(do encoding), for data in body. URLStreamProvider.processURL.(vbrodetskyi)
Date Mon, 03 Feb 2014 13:26:26 GMT
Updated Branches:
  refs/heads/trunk 20225db20 -> 6372db8bf


AMBARI-4482: We should set content type(do encoding), for data in body. URLStreamProvider.processURL.(vbrodetskyi)


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

Branch: refs/heads/trunk
Commit: 6372db8bf6e58cceacd1b1c0e7785dcbdac24b19
Parents: 20225db
Author: Vitaly Brodetskyi <vbrodetskyi@hortonworks.com>
Authored: Mon Feb 3 15:25:34 2014 +0200
Committer: Vitaly Brodetskyi <vbrodetskyi@hortonworks.com>
Committed: Mon Feb 3 15:25:34 2014 +0200

----------------------------------------------------------------------
 .../server/api/services/ProxyService.java       | 19 ++++++------
 .../controller/internal/URLStreamProvider.java  | 17 +++++++++--
 .../server/api/services/ProxyServiceTest.java   | 32 +++++++++++---------
 3 files changed, 42 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/6372db8b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ProxyService.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ProxyService.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ProxyService.java
index ce933a9..9e457c6 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ProxyService.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ProxyService.java
@@ -23,6 +23,7 @@ import org.apache.ambari.server.controller.internal.URLStreamProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
 import javax.ws.rs.Path;
 import javax.ws.rs.GET;
@@ -45,7 +46,7 @@ import java.util.Map;
 public class ProxyService {
 
   private static final int REPO_URL_CONNECT_TIMEOUT = 3000;
-  private static final int REPO_URL_READ_TIMEOUT = 500;
+  private static final int REPO_URL_READ_TIMEOUT = 1000;
   private static final int HTTP_ERROR_RANGE_START = 400;
 
   private static final String REQUEST_TYPE_GET = "GET";
@@ -59,32 +60,32 @@ public class ProxyService {
 
   @GET
   public Response processGetRequestForwarding(@Context HttpHeaders headers, @Context UriInfo
ui) {
-    return handleRequest(REQUEST_TYPE_GET, ui, null);
+    return handleRequest(REQUEST_TYPE_GET, ui, null, APPLICATION_FORM_URLENCODED);
   }
 
   @POST
-  public Response processPostRequestForwarding(String body, @Context HttpHeaders headers,
@Context UriInfo ui) {
-    return handleRequest(REQUEST_TYPE_POST, ui, body);
+  public Response processPostRequestForwarding(Object body, @Context HttpHeaders headers,
@Context UriInfo ui) {
+    return handleRequest(REQUEST_TYPE_POST, ui, body, headers.getMediaType().toString());
   }
 
   @PUT
-  public Response processPutRequestForwarding(String body, @Context HttpHeaders headers,
@Context UriInfo ui) {
-    return handleRequest(REQUEST_TYPE_PUT, ui, body);
+  public Response processPutRequestForwarding(Object body, @Context HttpHeaders headers,
@Context UriInfo ui) {
+    return handleRequest(REQUEST_TYPE_PUT, ui, body, headers.getMediaType().toString());
   }
 
   @DELETE
   public Response processDeleteRequestForwarding(@Context HttpHeaders headers, @Context UriInfo
ui) {
-    return handleRequest(REQUEST_TYPE_DELETE, ui, null);
+    return handleRequest(REQUEST_TYPE_DELETE, ui, null, APPLICATION_FORM_URLENCODED);
   }
 
-  private Response handleRequest(String requestType, UriInfo ui, String body) {
+  private Response handleRequest(String requestType, UriInfo ui, Object body, String mediaType)
{
     URLStreamProvider urlStreamProvider = new URLStreamProvider(REPO_URL_CONNECT_TIMEOUT,
                                                 REPO_URL_READ_TIMEOUT, null, null, null);
     List<String> urlsToForward = ui.getQueryParameters().get(QUERY_PARAMETER_URL);
     if (!urlsToForward.isEmpty()) {
       String url = urlsToForward.get(0);
       try {
-        HttpURLConnection connection = urlStreamProvider.processURL(url, requestType, body);
+        HttpURLConnection connection = urlStreamProvider.processURL(url, requestType, body,
mediaType);
         int responseCode = connection.getResponseCode();
         if (responseCode >= HTTP_ERROR_RANGE_START) {
           throw new WebApplicationException(connection.getResponseCode());

http://git-wip-us.apache.org/repos/asf/ambari/blob/6372db8b/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 2847c05..6b5681c 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
@@ -18,6 +18,8 @@
 
 package org.apache.ambari.server.controller.internal;
 
+import com.google.gson.Gson;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -26,6 +28,8 @@ import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
 import java.security.KeyStore;
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
 
 import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.SSLContext;
@@ -77,10 +81,10 @@ public class URLStreamProvider implements StreamProvider {
   
   @Override
   public InputStream readFrom(String spec, String requestMethod, String params) throws IOException
{
-    return processURL(spec, requestMethod, params).getInputStream();
+    return processURL(spec, requestMethod, params, APPLICATION_FORM_URLENCODED).getInputStream();
   }
 
-  public HttpURLConnection processURL(String spec, String requestMethod, String params) throws
IOException {
+  public HttpURLConnection processURL(String spec, String requestMethod, Object params, String
mediaType) throws IOException {
     if (LOG.isDebugEnabled()) {
       LOG.debug("readFrom spec:" + spec);
     }
@@ -98,9 +102,16 @@ public class URLStreamProvider implements StreamProvider {
     connection.setReadTimeout(readTimeout);
     connection.setDoOutput(true);
     connection.setRequestMethod(requestMethod);
+    connection.setRequestProperty("Content-Type", mediaType);
 
     if (params != null) {
-      connection.getOutputStream().write(params.getBytes());
+      String strParams;
+      if (mediaType.equals(APPLICATION_JSON)) {
+        strParams = new Gson().toJson(params);
+      } else {
+        strParams = (String)params;
+      }
+      connection.getOutputStream().write(strParams.getBytes());
     }
 
     int statusCode = connection.getResponseCode();

http://git-wip-us.apache.org/repos/asf/ambari/blob/6372db8b/ambari-server/src/test/java/org/apache/ambari/server/api/services/ProxyServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/ProxyServiceTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/api/services/ProxyServiceTest.java
index 057772b..3f219dd 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/ProxyServiceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/ProxyServiceTest.java
@@ -31,6 +31,8 @@ import org.powermock.api.easymock.PowerMock;
 import org.powermock.core.classloader.annotations.PrepareForTest;
 import org.powermock.modules.junit4.PowerMockRunner;
 
+import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
+import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED_TYPE;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
@@ -63,11 +65,11 @@ class ProxyServiceTest extends BaseServiceTest {
     InputStream is = new ByteArrayInputStream("test".getBytes());
     PowerMock.mockStatic(Response.class);
     expect(getUriInfo().getQueryParameters()).andReturn(queryParams);
-    expect(streamProviderMock.processURL("testurl", "GET", null)).andReturn(urlConnectionMock);
+    expect(streamProviderMock.processURL("testurl", "GET", null, APPLICATION_FORM_URLENCODED)).andReturn(urlConnectionMock);
     expect(urlConnectionMock.getResponseCode()).andReturn(200);
     expect(urlConnectionMock.getContentType()).andReturn("text/plain");
     expect(urlConnectionMock.getInputStream()).andReturn(is);
-    PowerMock.expectNew(URLStreamProvider.class, 3000, 500, null, null, null).andReturn(streamProviderMock);
+    PowerMock.expectNew(URLStreamProvider.class, 3000, 1000, null, null, null).andReturn(streamProviderMock);
     expect(Response.status(200)).andReturn(responseBuilderMock);
     expect(responseBuilderMock.entity(is)).andReturn(responseBuilderMock);
     expect(responseBuilderMock.type("text/plain")).andReturn(responseBuilderMock);
@@ -90,17 +92,18 @@ class ProxyServiceTest extends BaseServiceTest {
     InputStream is = new ByteArrayInputStream("test".getBytes());
     PowerMock.mockStatic(Response.class);
     expect(getUriInfo().getQueryParameters()).andReturn(queryParams);
-    expect(streamProviderMock.processURL("testurl", "POST", "testbody")).andReturn(urlConnectionMock);
+    expect(getHttpHeaders().getMediaType()).andReturn(APPLICATION_FORM_URLENCODED_TYPE);
+    expect(streamProviderMock.processURL("testurl", "POST", "testbody", APPLICATION_FORM_URLENCODED)).andReturn(urlConnectionMock);
     expect(urlConnectionMock.getResponseCode()).andReturn(200);
     expect(urlConnectionMock.getContentType()).andReturn("text/plain");
     expect(urlConnectionMock.getInputStream()).andReturn(is);
-    PowerMock.expectNew(URLStreamProvider.class, 3000, 500, null, null, null).andReturn(streamProviderMock);
+    PowerMock.expectNew(URLStreamProvider.class, 3000, 1000, null, null, null).andReturn(streamProviderMock);
     expect(Response.status(200)).andReturn(responseBuilderMock);
     expect(responseBuilderMock.entity(is)).andReturn(responseBuilderMock);
     expect(responseBuilderMock.type("text/plain")).andReturn(responseBuilderMock);
     expect(responseBuilderMock.build()).andReturn(responseMock);
     PowerMock.replay(streamProviderMock, URLStreamProvider.class, Response.class, responseBuilderMock);
-    replay(getUriInfo(), urlConnectionMock);
+    replay(getUriInfo(), urlConnectionMock, getHttpHeaders());
     Response resultForPostRequest = ps.processPostRequestForwarding("testbody", getHttpHeaders(),
getUriInfo());
     assertSame(resultForPostRequest, responseMock);
   }
@@ -117,17 +120,18 @@ class ProxyServiceTest extends BaseServiceTest {
     InputStream is = new ByteArrayInputStream("test".getBytes());
     PowerMock.mockStatic(Response.class);
     expect(getUriInfo().getQueryParameters()).andReturn(queryParams);
-    expect(streamProviderMock.processURL("testurl", "PUT", "testbody")).andReturn(urlConnectionMock);
+    expect(getHttpHeaders().getMediaType()).andReturn(APPLICATION_FORM_URLENCODED_TYPE);
+    expect(streamProviderMock.processURL("testurl", "PUT", "testbody", APPLICATION_FORM_URLENCODED)).andReturn(urlConnectionMock);
     expect(urlConnectionMock.getResponseCode()).andReturn(200);
     expect(urlConnectionMock.getContentType()).andReturn("text/plain");
     expect(urlConnectionMock.getInputStream()).andReturn(is);
-    PowerMock.expectNew(URLStreamProvider.class, 3000, 500, null, null, null).andReturn(streamProviderMock);
+    PowerMock.expectNew(URLStreamProvider.class, 3000, 1000, null, null, null).andReturn(streamProviderMock);
     expect(Response.status(200)).andReturn(responseBuilderMock);
     expect(responseBuilderMock.entity(is)).andReturn(responseBuilderMock);
     expect(responseBuilderMock.type("text/plain")).andReturn(responseBuilderMock);
     expect(responseBuilderMock.build()).andReturn(responseMock);
     PowerMock.replay(streamProviderMock, URLStreamProvider.class, Response.class, responseBuilderMock);
-    replay(getUriInfo(), urlConnectionMock);
+    replay(getUriInfo(), urlConnectionMock, getHttpHeaders());
     Response resultForPutRequest = ps.processPutRequestForwarding("testbody", getHttpHeaders(),
getUriInfo());
     assertSame(resultForPutRequest, responseMock);
   }
@@ -144,11 +148,11 @@ class ProxyServiceTest extends BaseServiceTest {
     InputStream is = new ByteArrayInputStream("test".getBytes());
     PowerMock.mockStatic(Response.class);
     expect(getUriInfo().getQueryParameters()).andReturn(queryParams);
-    expect(streamProviderMock.processURL("testurl", "DELETE", null)).andReturn(urlConnectionMock);
+    expect(streamProviderMock.processURL("testurl", "DELETE", null, APPLICATION_FORM_URLENCODED)).andReturn(urlConnectionMock);
     expect(urlConnectionMock.getResponseCode()).andReturn(200);
     expect(urlConnectionMock.getContentType()).andReturn("text/plain");
     expect(urlConnectionMock.getInputStream()).andReturn(is);
-    PowerMock.expectNew(URLStreamProvider.class, 3000, 500, null, null, null).andReturn(streamProviderMock);
+    PowerMock.expectNew(URLStreamProvider.class, 3000, 1000, null, null, null).andReturn(streamProviderMock);
     expect(Response.status(200)).andReturn(responseBuilderMock);
     expect(responseBuilderMock.entity(is)).andReturn(responseBuilderMock);
     expect(responseBuilderMock.type("text/plain")).andReturn(responseBuilderMock);
@@ -167,9 +171,9 @@ class ProxyServiceTest extends BaseServiceTest {
     MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
     queryParams.add("url","testurl");
     expect(getUriInfo().getQueryParameters()).andReturn(queryParams);
-    expect(streamProviderMock.processURL("testurl", "GET", null)).andReturn(urlConnectionMock);
+    expect(streamProviderMock.processURL("testurl", "GET", null, APPLICATION_FORM_URLENCODED)).andReturn(urlConnectionMock);
     expect(urlConnectionMock.getResponseCode()).andReturn(405).times(2);
-    PowerMock.expectNew(URLStreamProvider.class, 3000, 500, null, null, null).andReturn(streamProviderMock);
+    PowerMock.expectNew(URLStreamProvider.class, 3000, 1000, null, null, null).andReturn(streamProviderMock);
     PowerMock.replay(streamProviderMock, URLStreamProvider.class);
     replay(getUriInfo(), urlConnectionMock);
     ps.processGetRequestForwarding(getHttpHeaders(),getUriInfo());
@@ -187,11 +191,11 @@ class ProxyServiceTest extends BaseServiceTest {
     Map map = new Gson().fromJson(new InputStreamReader(new ByteArrayInputStream("{ \"test\":\"test\"
}".getBytes())), Map.class);
     PowerMock.mockStatic(Response.class);
     expect(getUriInfo().getQueryParameters()).andReturn(queryParams);
-    expect(streamProviderMock.processURL("testurl", "GET", null)).andReturn(urlConnectionMock);
+    expect(streamProviderMock.processURL("testurl", "GET", null, APPLICATION_FORM_URLENCODED)).andReturn(urlConnectionMock);
     expect(urlConnectionMock.getResponseCode()).andReturn(200);
     expect(urlConnectionMock.getContentType()).andReturn("application/json");
     expect(urlConnectionMock.getInputStream()).andReturn(new ByteArrayInputStream("{ \"test\":\"test\"
}".getBytes()));
-    PowerMock.expectNew(URLStreamProvider.class, 3000, 500, null, null, null).andReturn(streamProviderMock);
+    PowerMock.expectNew(URLStreamProvider.class, 3000, 1000, null, null, null).andReturn(streamProviderMock);
     expect(Response.status(200)).andReturn(responseBuilderMock);
     expect(responseBuilderMock.entity(map)).andReturn(responseBuilderMock);
     expect(responseBuilderMock.type("application/json")).andReturn(responseBuilderMock);


Mime
View raw message