You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by db...@apache.org on 2016/06/30 16:28:35 UTC

ambari git commit: AMBARI-17493. TEZ UI throws URISyntaxException when search by dag name with special characters. (dipayanb)

Repository: ambari
Updated Branches:
  refs/heads/trunk 48a8a072d -> b29ad444d


AMBARI-17493. TEZ UI throws URISyntaxException when search by dag name with special characters. (dipayanb)


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

Branch: refs/heads/trunk
Commit: b29ad444d2c31df8e407120805fc9e937e0fb8d0
Parents: 48a8a07
Author: Dipayan Bhowmick <di...@gmail.com>
Authored: Thu Jun 30 14:52:35 2016 +0530
Committer: Dipayan Bhowmick <di...@gmail.com>
Committed: Thu Jun 30 21:58:02 2016 +0530

----------------------------------------------------------------------
 contrib/views/tez/pom.xml                       | 24 ++++++++
 .../ambari/view/tez/rest/AtsProxyResource.java  |  2 +-
 .../ambari/view/tez/rest/RMProxyResource.java   |  2 +-
 .../view/tez/rest/RMRedirectResource.java       |  2 +-
 .../ambari/view/tez/utils/ProxyHelper.java      | 25 ++++-----
 .../ambari/view/tez/utils/ProxyHelperTest.java  | 58 ++++++++++++++++++++
 6 files changed, 97 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/b29ad444/contrib/views/tez/pom.xml
----------------------------------------------------------------------
diff --git a/contrib/views/tez/pom.xml b/contrib/views/tez/pom.xml
index 0534f79..30d1143 100644
--- a/contrib/views/tez/pom.xml
+++ b/contrib/views/tez/pom.xml
@@ -232,6 +232,30 @@
     </dependency>
 
     <dependency>
+      <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpclient</artifactId>
+      <version>4.5.1</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpcore</artifactId>
+      <version>4.4.3</version>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.easymock</groupId>
+      <artifactId>easymock</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
       <groupId>org.apache.ambari.contrib.views</groupId>
       <artifactId>ambari-views-utils</artifactId>
       <version>2.0.0.0-SNAPSHOT</version>

http://git-wip-us.apache.org/repos/asf/ambari/blob/b29ad444/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/AtsProxyResource.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/AtsProxyResource.java b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/AtsProxyResource.java
index 1dd822e..9f88893 100644
--- a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/AtsProxyResource.java
+++ b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/AtsProxyResource.java
@@ -42,6 +42,6 @@ public class AtsProxyResource extends BaseProxyResource {
   @Override
   public String getProxyUrl(String endpoint, MultivaluedMap<String, String> queryParams) {
     String atsUrl = viewController.getActiveATSUrl();
-    return String.format("%s/%s%s", atsUrl, endpoint, proxyHelper.getQueryParamsString(queryParams));
+    return proxyHelper.getProxyUrl(atsUrl, endpoint, queryParams);
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/b29ad444/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMProxyResource.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMProxyResource.java b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMProxyResource.java
index d3901c9..e3b3688 100644
--- a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMProxyResource.java
+++ b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMProxyResource.java
@@ -42,6 +42,6 @@ public class RMProxyResource extends BaseProxyResource {
   @Override
   public String getProxyUrl(String endpoint, MultivaluedMap<String, String> queryParams) {
     String activeRMUrl = viewController.getActiveRMUrl();
-    return String.format("%s/%s%s", activeRMUrl, endpoint, proxyHelper.getQueryParamsString(queryParams));
+    return proxyHelper.getProxyUrl(activeRMUrl, endpoint, queryParams);
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/b29ad444/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMRedirectResource.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMRedirectResource.java b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMRedirectResource.java
index e217884..99faf88 100644
--- a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMRedirectResource.java
+++ b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMRedirectResource.java
@@ -41,6 +41,6 @@ public class RMRedirectResource extends BaseRedirectionResource {
   @Override
   public String getProxyUrl(String endpoint, MultivaluedMap<String, String> queryParams) {
     String activeRMUrl = viewController.getActiveRMUrl();
-    return String.format("%s/%s%s", activeRMUrl, endpoint, proxyHelper.getQueryParamsString(queryParams));
+    return proxyHelper.getProxyUrl(activeRMUrl, endpoint, queryParams);
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/b29ad444/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/utils/ProxyHelper.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/utils/ProxyHelper.java b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/utils/ProxyHelper.java
index 08e371f..08eaa16 100644
--- a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/utils/ProxyHelper.java
+++ b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/utils/ProxyHelper.java
@@ -24,6 +24,7 @@ import org.apache.ambari.view.URLConnectionProvider;
 import org.apache.ambari.view.ViewContext;
 import org.apache.ambari.view.tez.exceptions.TezWebAppException;
 import org.apache.commons.io.IOUtils;
+import org.apache.http.client.utils.URIBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -32,6 +33,7 @@ import javax.ws.rs.core.Response;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
+import java.net.URISyntaxException;
 import java.util.Map;
 import java.util.Set;
 
@@ -79,21 +81,18 @@ public class ProxyHelper {
     }
   }
 
-  public String getQueryParamsString(MultivaluedMap<String, String> queryParameters) {
+  public String getProxyUrl(String baseUrl, String endPoint, MultivaluedMap<String, String> queryParameters) {
     Set<String> keySet = queryParameters.keySet();
-    StringBuilder builder = new StringBuilder();
-    if(keySet.size() > 0)
-      builder.append("?");
-
-    int count = 0;
-    for(String key: keySet) {
-      builder.append(key);
-      builder.append("=");
-      builder.append(queryParameters.getFirst(key));
-      if(count < keySet.size() - 1) {
-        builder.append("&");
+    URIBuilder builder;
+    try {
+      builder = new URIBuilder(baseUrl + "/" + endPoint);
+      for(String key: keySet) {
+        builder.addParameter(key, queryParameters.getFirst(key));
       }
+      return builder.build().toString();
+    } catch (URISyntaxException e) {
+      LOG.error("Failed to build a URL from the baseUrl: {} and endPoint: {}. Exception: {}", baseUrl, endPoint, e);
+      throw new TezWebAppException("Failed to build a URL from the baseUrl:" + baseUrl + "and endPoint: " + endPoint, e);
     }
-    return builder.toString();
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/b29ad444/contrib/views/tez/src/test/java/org/apache/ambari/view/tez/utils/ProxyHelperTest.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/test/java/org/apache/ambari/view/tez/utils/ProxyHelperTest.java b/contrib/views/tez/src/test/java/org/apache/ambari/view/tez/utils/ProxyHelperTest.java
new file mode 100644
index 0000000..2160848
--- /dev/null
+++ b/contrib/views/tez/src/test/java/org/apache/ambari/view/tez/utils/ProxyHelperTest.java
@@ -0,0 +1,58 @@
+/**
+ * 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.tez.utils;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.tez.exceptions.TezWebAppException;
+import org.junit.Test;
+
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+
+import static org.easymock.EasyMock.createNiceMock;
+import static org.junit.Assert.assertEquals;
+
+public class ProxyHelperTest {
+  @Test
+  public void shouldBuildURLWithNoQueryParameters() throws Exception {
+    ViewContext context = createNiceMock(ViewContext.class);
+    ProxyHelper helper = new ProxyHelper(context);
+    assertEquals("http://abc.com/", helper.getProxyUrl("http://abc.com", "", new MultivaluedHashMap<String, String>()));
+    assertEquals("http://abc.com/test/abcd", helper.getProxyUrl("http://abc.com", "test/abcd", new MultivaluedHashMap<String, String>()));
+  }
+
+  @Test
+  public void shouldBuildURLWithQueryParametersHavingSpecialCharacters() throws Exception {
+    ViewContext context = createNiceMock(ViewContext.class);
+    ProxyHelper helper = new ProxyHelper(context);
+    MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
+    map.putSingle("data", "abcd/efgh");
+    assertEquals("http://abc.com/test/abcd?data=abcd%2Fefgh", helper.getProxyUrl("http://abc.com", "test/abcd", map));
+    map.putSingle("data", "abcd efgh");
+    assertEquals("http://abc.com/test/abcd?data=abcd+efgh", helper.getProxyUrl("http://abc.com", "test/abcd", map));
+  }
+
+  @Test(expected = TezWebAppException.class)
+  public void shouldThrowExceptionIfWrongUrl() throws Exception {
+    ViewContext context = createNiceMock(ViewContext.class);
+    ProxyHelper helper = new ProxyHelper(context);
+    helper.getProxyUrl("####", "", new MultivaluedHashMap<String, String>());
+  }
+
+}
\ No newline at end of file