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/05/13 13:18:38 UTC
ambari git commit: AMBARI-16656. Error while loading slider view:
Unable to initialize Slider view. (Gaurav Nagar via dipayanb)
Repository: ambari
Updated Branches:
refs/heads/trunk f5cfd9c4e -> a729e7c25
AMBARI-16656. Error while loading slider view: Unable to initialize Slider view. (Gaurav Nagar via dipayanb)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/a729e7c2
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/a729e7c2
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/a729e7c2
Branch: refs/heads/trunk
Commit: a729e7c25e97bf8b5dacbec4a234fbd448230a25
Parents: f5cfd9c
Author: Dipayan Bhowmick <di...@gmail.com>
Authored: Fri May 13 18:47:30 2016 +0530
Committer: Dipayan Bhowmick <di...@gmail.com>
Committed: Fri May 13 18:48:08 2016 +0530
----------------------------------------------------------------------
.../view/slider/rest/client/BaseHttpClient.java | 27 +++--
.../rest/client/URLStreamProviderBasicAuth.java | 105 +++++++++++++++++++
2 files changed, 122 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/a729e7c2/contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/BaseHttpClient.java
----------------------------------------------------------------------
diff --git a/contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/BaseHttpClient.java b/contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/BaseHttpClient.java
index 3d4a08d..ce30a10 100644
--- a/contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/BaseHttpClient.java
+++ b/contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/BaseHttpClient.java
@@ -75,6 +75,10 @@ public class BaseHttpClient {
return viewContext.getURLStreamProvider();
}
+ public URLStreamProviderBasicAuth getUrlStreamProviderBasicAuth() {
+ return new URLStreamProviderBasicAuth(getUrlStreamProvider(),getUserId(),getPassword());
+ }
+
public String getUrl() {
return url;
}
@@ -113,39 +117,42 @@ public class BaseHttpClient {
public JsonElement doGetJson(String url, String path) throws HttpException,
IOException {
- String response = null;
+ InputStream inputStream = null;
try {
Map<String, String> headers = new HashMap<String, String>();
if (isNeedsAuthentication()) {
- response = ambariApi.readFromAmbari(
+ inputStream = getUrlStreamProviderBasicAuth().readFrom(
url + path, "GET", (String) null, headers);
} else {
- response = IOUtils.toString(viewContext.getURLStreamProvider().readAsCurrent(
- url + path, "GET", (String) null, headers));
+ inputStream = getUrlStreamProvider().readAsCurrent(
+ url + path, "GET", (String) null, headers);
}
- } catch (Exception e) {
+ } catch (IOException e) {
logger.error("Error while reading from url " + url + path, e);
HttpException httpException = new HttpException(
e.getLocalizedMessage());
throw httpException;
}
- JsonElement jsonElement = new JsonParser().parse(response);
+ JsonElement jsonElement = new JsonParser().parse(new JsonReader(
+ new InputStreamReader(inputStream)));
return jsonElement;
}
public String doGet(String path) throws HttpException, IOException {
String response = null;
try {
+ InputStream inputStream = null;
if (isNeedsAuthentication()) {
- response = ambariApi.readFromAmbari(
+ inputStream = getUrlStreamProviderBasicAuth().readFrom(
getUrl() + path, "GET", (String) null,
new HashMap<String, String>());
} else {
- response = IOUtils.toString(viewContext.getURLStreamProvider().readAsCurrent(
+ inputStream = getUrlStreamProvider().readAsCurrent(
getUrl() + path, "GET", (String) null,
- new HashMap<String, String>()));
+ new HashMap<String, String>());
}
- } catch (Exception e) {
+ response = IOUtils.toString(inputStream);
+ } catch (IOException e) {
logger.error("Error while reading from url " + getUrl() + path, e);
HttpException httpException = new HttpException(
e.getLocalizedMessage());
http://git-wip-us.apache.org/repos/asf/ambari/blob/a729e7c2/contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/URLStreamProviderBasicAuth.java
----------------------------------------------------------------------
diff --git a/contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/URLStreamProviderBasicAuth.java b/contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/URLStreamProviderBasicAuth.java
new file mode 100644
index 0000000..2d32fd7
--- /dev/null
+++ b/contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/URLStreamProviderBasicAuth.java
@@ -0,0 +1,105 @@
+/**
+ * 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.slider.rest.client;
+
+import org.apache.ambari.view.URLStreamProvider;
+import org.apache.commons.codec.binary.Base64;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Wrapper for URLStreamProvider that adds authentication header.
+ */
+public class URLStreamProviderBasicAuth implements URLStreamProvider {
+ private URLStreamProvider urlStreamProvider;
+ private String username;
+ private String password;
+ private String requestedBy = "views";
+
+ public URLStreamProviderBasicAuth(URLStreamProvider urlStreamProvider, String username, String password) {
+ this.urlStreamProvider = urlStreamProvider;
+ this.username = username;
+ this.password = password;
+ }
+
+ /**
+ * X-Requested-By header value
+ * @param requestedBy value of X-Requested-By header
+ */
+ public void setRequestedBy(String requestedBy) {
+ this.requestedBy = requestedBy;
+ }
+
+ @Override
+ public InputStream readFrom(String url, String method, String data, Map<String, String> headers) throws IOException {
+ return urlStreamProvider.readFrom(url, method, data, addHeaders(headers));
+ }
+
+ @Override
+ public InputStream readFrom(String url, String method, InputStream data, Map<String, String> headers) throws IOException {
+ return urlStreamProvider.readFrom(url, method, data, addHeaders(headers));
+ }
+
+ @Override
+ public InputStream readAs(String url, String method, String data, Map<String, String> headers, String doAs) throws IOException {
+ return urlStreamProvider.readAs(url, method, data, addHeaders(headers), doAs);
+ }
+
+ @Override
+ public InputStream readAs(String url, String method, InputStream data, Map<String, String> headers, String doAs) throws IOException {
+ return urlStreamProvider.readAs(url, method, data, addHeaders(headers), doAs);
+ }
+
+ @Override
+ public InputStream readAsCurrent(String url, String method, String data, Map<String, String> headers) throws IOException {
+ return urlStreamProvider.readAsCurrent(url, method, data, addHeaders(headers));
+ }
+
+ @Override
+ public InputStream readAsCurrent(String url, String method, InputStream data, Map<String, String> headers) throws IOException {
+ return urlStreamProvider.readAsCurrent(url, method, data, addHeaders(headers));
+ }
+
+ private HashMap<String, String> addHeaders(Map<String, String> customHeaders) {
+ HashMap<String, String> newHeaders = new HashMap<String, String>();
+ if (customHeaders != null)
+ newHeaders.putAll(customHeaders);
+
+ if (urlStreamProvider != null) {
+ // basic auth is not needed for AmbariStreamProvider
+ addBasicAuthHeaders(newHeaders);
+ }
+ addRequestedByHeaders(newHeaders);
+ return newHeaders;
+ }
+
+ private void addRequestedByHeaders(HashMap<String, String> newHeaders) {
+ newHeaders.put("X-Requested-By", requestedBy);
+ }
+
+ private void addBasicAuthHeaders(HashMap<String, String> headers) {
+ String authString = username + ":" + password;
+ byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
+ String authStringEnc = new String(authEncBytes);
+
+ headers.put("Authorization", "Basic " + authStringEnc);
+ }
+}