Public and Private Locate entries
[aaf/authz.git] / cadi / client / src / main / java / org / onap / aaf / cadi / http / HClient.java
index 1d3d954..daca47d 100644 (file)
@@ -40,6 +40,7 @@ import org.onap.aaf.cadi.SecuritySetter;
 import org.onap.aaf.cadi.client.EClient;
 import org.onap.aaf.cadi.client.Future;
 import org.onap.aaf.cadi.client.Rcli;
+import org.onap.aaf.cadi.util.FixURIinfo;
 import org.onap.aaf.misc.env.APIException;
 import org.onap.aaf.misc.env.Data;
 import org.onap.aaf.misc.env.Data.TYPE;
@@ -109,31 +110,30 @@ public class HClient implements EClient<HttpURLConnection> {
 
     @Override
     public void send() throws APIException {
-        try {
-            // Build URL from given URI plus current Settings
-            if(uri.getPath()==null) {
-                throw new APIException("Invalid URL entered for HClient");
-            }
-            StringBuilder pi=null;
-            if(pathinfo!=null) { // additional pathinfo
-                pi = new StringBuilder(uri.getPath());
-                if(!pathinfo.startsWith("/")) {
-                    pi.append('/');
-                }
-                pi.append(pathinfo);
+        // Build URL from given URI plus current Settings
+        if (uri.getPath()==null) {
+            throw new APIException("Invalid URL entered for HClient");
+        }
+        StringBuilder pi=null;
+        if (pathinfo!=null) { // additional pathinfo
+            pi = new StringBuilder(uri.getPath());
+            if (!pathinfo.startsWith("/")) {
+                pi.append('/');
             }
-            URI sendURI = new URI(
+            pi.append(pathinfo);
+        }
+               URI sendURI = null;
+        try {
+            sendURI = new URI(
                     uri.getScheme(),
-                    uri.getUserInfo(),
-                    uri.getHost(),
-                    uri.getPort(),
+                    uri.getAuthority(),
                     pi==null?uri.getPath():pi.toString(),
                     query==null?uri.getQuery():query,
                     fragment==null?uri.getFragment():fragment
                     );
             huc = getConnection(sendURI, pi);
             huc.setRequestMethod(meth);
-            if(ss!=null) {
+            if (ss!=null) {
                 ss.setSecurity(huc); 
             }
             if (headers != null)
@@ -149,11 +149,17 @@ public class HClient implements EClient<HttpURLConnection> {
                 transfer.transfer(huc.getOutputStream());
             }
             // TODO other settings? There's a bunch here.
+        } catch (APIException e) {
+               throw e;
         } catch (Exception e) {
-            throw new APIException(e);
+               if(sendURI==null) {
+                       throw new APIException("Cannot connect to Root URI: " + uri.toString(),e);
+               } else {
+                       throw new APIException("Cannot connect to " + sendURI.toString() + "(Root URI: " + uri.toString() +')',e);
+               }
         } finally { // ensure all these are reset after sends
             meth=pathinfo=null;
-            if(headers!=null) {
+            if (headers!=null) {
                 headers.clear();
             }
             pathinfo = query = fragment = "";
@@ -171,9 +177,7 @@ public class HClient implements EClient<HttpURLConnection> {
     protected HttpURLConnection getConnection(URI uri, StringBuilder pi) throws IOException, URISyntaxException {
         URL url = new URI(
                 uri.getScheme(), 
-                uri.getUserInfo(),
-                uri.getHost(), 
-                uri.getPort(), 
+                uri.getAuthority(),
                 pi==null?uri.getPath():pi.toString(), 
                 query,
                 fragment).toURL();
@@ -200,7 +204,7 @@ public class HClient implements EClient<HttpURLConnection> {
                 huc.setReadTimeout(timeout);
                 respCode = huc.getResponseCode();
                 ss.setLastResponse(respCode);
-                if(evalInfo(huc)) {
+                if (evalInfo(huc)) {
                     return true;
                 } else {
                     extractError();
@@ -216,13 +220,13 @@ public class HClient implements EClient<HttpURLConnection> {
         private void extractError() {
             InputStream is = huc.getErrorStream();
             try {
-                if(is==null) {
+                if (is==null) {
                     is = huc.getInputStream();
                 }
-                if(is!=null) {
+                if (is!=null) {
                 errContent = new StringBuilder();
                 int c;
-                    while((c=is.read())>=0) {
+                    while ((c=is.read())>=0) {
                         errContent.append((char)c);
                     }
                 }
@@ -274,7 +278,7 @@ public class HClient implements EClient<HttpURLConnection> {
         }
     
         public void close() {
-            if(huc!=null) {
+            if (huc!=null) {
                 huc.disconnect();
             }
         }
@@ -382,12 +386,12 @@ public class HClient implements EClient<HttpURLConnection> {
                 int read;
                 InputStream is;
                 OutputStream os = resp.getOutputStream();
-                if(respCode==expected) {
+                if (respCode==expected) {
                     is = huc.getInputStream();
                     // reuse Buffers
                     Pooled<byte[]> pbuff = Rcli.buffPool.get();
                     try { 
-                        while((read=is.read(pbuff.content))>=0) {
+                        while ((read=is.read(pbuff.content))>=0) {
                             os.write(pbuff.content,0,read);
                         }
                     } finally {
@@ -396,14 +400,14 @@ public class HClient implements EClient<HttpURLConnection> {
                     return true;
                 } else {
                     is = huc.getErrorStream();
-                    if(is==null) {
+                    if (is==null) {
                         is = huc.getInputStream();
                     }
-                    if(is!=null) {
+                    if (is!=null) {
                         errContent = new StringBuilder();
                         Pooled<byte[]> pbuff = Rcli.buffPool.get();
                         try { 
-                            while((read=is.read(pbuff.content))>=0) {
+                            while ((read=is.read(pbuff.content))>=0) {
                                 os.write(pbuff.content,0,read);
                             }
                         } finally {