You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2016/12/03 16:56:52 UTC

tomee git commit: reapplying 7.x http ejbd client fixes

Repository: tomee
Updated Branches:
  refs/heads/tomee-1.7.x ea8347ac0 -> cc31914c9


reapplying 7.x http ejbd client fixes


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

Branch: refs/heads/tomee-1.7.x
Commit: cc31914c989878d455e62464a93acdbd3c96b21a
Parents: ea8347a
Author: rmannibucau <rm...@apache.org>
Authored: Sat Dec 3 17:56:42 2016 +0100
Committer: rmannibucau <rm...@apache.org>
Committed: Sat Dec 3 17:56:42 2016 +0100

----------------------------------------------------------------------
 .../java/org/apache/openejb/client/Client.java  |  4 +++
 .../openejb/client/HttpConnectionFactory.java   | 35 ++++++++++++++++----
 .../org/apache/openejb/client/JNDIContext.java  | 10 ++++--
 3 files changed, 39 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/cc31914c/server/openejb-client/src/main/java/org/apache/openejb/client/Client.java
----------------------------------------------------------------------
diff --git a/server/openejb-client/src/main/java/org/apache/openejb/client/Client.java b/server/openejb-client/src/main/java/org/apache/openejb/client/Client.java
index 4cc352a..d8c74b1 100644
--- a/server/openejb-client/src/main/java/org/apache/openejb/client/Client.java
+++ b/server/openejb-client/src/main/java/org/apache/openejb/client/Client.java
@@ -26,6 +26,7 @@ import org.apache.openejb.client.event.RetryingRequest;
 import org.apache.openejb.client.event.ServerAdded;
 import org.apache.openejb.client.event.ServerRemoved;
 
+import javax.naming.AuthenticationException;
 import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
@@ -264,6 +265,9 @@ public class Client {
                 in = conn.getInputStream();
 
             } catch (final IOException e) {
+                if (AuthenticationException.class.isInstance(e.getCause())) {
+                    throw e.getCause();
+                }
                 throw newIOException("Cannot open input stream to server: ", e);
             }
 

http://git-wip-us.apache.org/repos/asf/tomee/blob/cc31914c/server/openejb-client/src/main/java/org/apache/openejb/client/HttpConnectionFactory.java
----------------------------------------------------------------------
diff --git a/server/openejb-client/src/main/java/org/apache/openejb/client/HttpConnectionFactory.java b/server/openejb-client/src/main/java/org/apache/openejb/client/HttpConnectionFactory.java
index 78b1928..15d55ae 100644
--- a/server/openejb-client/src/main/java/org/apache/openejb/client/HttpConnectionFactory.java
+++ b/server/openejb-client/src/main/java/org/apache/openejb/client/HttpConnectionFactory.java
@@ -16,6 +16,7 @@
  */
 package org.apache.openejb.client;
 
+import javax.naming.AuthenticationException;
 import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.SSLSocketFactory;
 import java.io.IOException;
@@ -25,8 +26,6 @@ import java.net.HttpURLConnection;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.security.KeyManagementException;
-import java.security.NoSuchAlgorithmException;
 import java.util.Map;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentHashMap;
@@ -74,7 +73,10 @@ public class HttpConnectionFactory implements ConnectionFactory {
                 throw new IllegalArgumentException("Invalid uri " + uri.toString(), e);
             }
 
-            httpURLConnection = (HttpURLConnection) url.openConnection();
+            final String authorization = params.get("authorization");
+
+            httpURLConnection = (HttpURLConnection) (authorization == null ?
+                    url : new URL(stripQuery(url.toExternalForm(), "authorization"))).openConnection();
             httpURLConnection.setDoOutput(true);
 
             final int timeout;
@@ -89,19 +91,23 @@ public class HttpConnectionFactory implements ConnectionFactory {
             if (params.containsKey("readTimeout")) {
                 httpURLConnection.setReadTimeout(Integer.parseInt(params.get("readTimeout")));
             }
+            if (authorization != null) {
+                httpURLConnection.setRequestProperty("Authorization", authorization);
+            }
 
             if (params.containsKey("sslKeyStore") || params.containsKey("sslTrustStore")) {
                 try {
                     SSLSocketFactory sslSocketFactory = socketFactoryMap.get(uri);
                     if (sslSocketFactory == null) {
                         sslSocketFactory = new SSLContextBuilder(params).build().getSocketFactory();
-                        socketFactoryMap.put(uri, sslSocketFactory);
+                        final SSLSocketFactory existing = socketFactoryMap.putIfAbsent(uri, sslSocketFactory);
+                        if (existing != null) {
+                            sslSocketFactory = existing;
+                        }
                     }
 
                     ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(sslSocketFactory);
-                } catch (final NoSuchAlgorithmException e) {
-                    throw new ClientRuntimeException(e.getMessage(), e);
-                } catch (final KeyManagementException e) {
+                } catch (final Exception e) {
                     throw new ClientRuntimeException(e.getMessage(), e);
                 }
             }
@@ -113,6 +119,18 @@ public class HttpConnectionFactory implements ConnectionFactory {
             }
         }
 
+        private String stripQuery(final String url, final String param) {
+            String result = url;
+            do {
+                final int h = result.indexOf(param + '=');
+                final int end = result.indexOf('&', h);
+                if (h <= 0) {
+                    return result;
+                }
+                result = result.substring(0, h - 1) + (end < 0 ? "" : result.substring(end + 1, result.length()));
+            } while (true);
+        }
+
         @Override
         public void discard() {
             try {
@@ -175,6 +193,9 @@ public class HttpConnectionFactory implements ConnectionFactory {
         @Override
         public InputStream getInputStream() throws IOException {
             if (inputStream == null) {
+                if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
+                    throw new IOException(new AuthenticationException());
+                }
                 inputStream = httpURLConnection.getInputStream();
             }
             return inputStream;

http://git-wip-us.apache.org/repos/asf/tomee/blob/cc31914c/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
----------------------------------------------------------------------
diff --git a/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java b/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
index 852ebae..c73cf68 100644
--- a/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
+++ b/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
@@ -428,9 +428,13 @@ public class JNDIContext implements InitialContextFactory, Context {
         try {
             res = request(req);
         } catch (Exception e) {
-            if (e instanceof RemoteException && e.getCause() instanceof ConnectException) {
-                e = (Exception) e.getCause();
-                throw (ServiceUnavailableException) new ServiceUnavailableException("Cannot lookup '" + name + "'.").initCause(e);
+            if (e instanceof RemoteException) {
+                if (e.getCause() instanceof ConnectException) {
+                    e = (Exception) e.getCause();
+                    throw (ServiceUnavailableException) new ServiceUnavailableException("Cannot lookup '" + name + "'.").initCause(e);
+                } else if (AuthenticationException.class.isInstance(e.getCause())) {
+                    throw AuthenticationException.class.cast(e.getCause());
+                }
             }
             throw (NamingException) new NamingException("Cannot lookup '" + name + "'.").initCause(e);
         }