[jboss-remoting-commits] JBoss Remoting SVN: r4017 - in remoting3/trunk: http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection and 1 other directory.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Fri Apr 18 17:37:37 EDT 2008


Author: david.lloyd at jboss.com
Date: 2008-04-18 17:37:37 -0400 (Fri, 18 Apr 2008)
New Revision: 4017

Added:
   remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/AbstractHttpUrlChannel.java
   remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpUrlChannel.java
   remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpsUrlChannel.java
Removed:
   remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpUrlChannel.java
Modified:
   remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/CookieClientSession.java
Log:
More progress towards HTTP client connection

Modified: remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/CookieClientSession.java
===================================================================
--- remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/CookieClientSession.java	2008-04-18 21:37:17 UTC (rev 4016)
+++ remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/CookieClientSession.java	2008-04-18 21:37:37 UTC (rev 4017)
@@ -38,7 +38,7 @@
      * @param domain the request domain
      * @param path the request path (sans file, with trailing slash)
      * @param secureRequest {@code true} if the request will use the {@code https} protocol
-     * @return the cookie header value
+     * @return the cookie header value, or {@code null} if there's no cookies to set
      */
     public String getCookieHeader(String domain, String path, boolean secureRequest) {
         final SortedMap<Cookie.Key, Cookie> sortedValidatedCookies = new TreeMap<Cookie.Key, Cookie>();

Copied: remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/AbstractHttpUrlChannel.java (from rev 4015, remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpUrlChannel.java)
===================================================================
--- remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/AbstractHttpUrlChannel.java	                        (rev 0)
+++ remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/AbstractHttpUrlChannel.java	2008-04-18 21:37:37 UTC (rev 4017)
@@ -0,0 +1,204 @@
+package org.jboss.cx.remoting.http.urlconnection;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.List;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import org.jboss.cx.remoting.http.cookie.CookieClientSession;
+import org.jboss.cx.remoting.http.spi.AbstractIncomingHttpMessage;
+import org.jboss.cx.remoting.http.spi.OutgoingHttpMessage;
+import org.jboss.cx.remoting.http.spi.RemotingHttpSessionContext;
+import org.jboss.cx.remoting.log.Logger;
+import org.jboss.cx.remoting.util.AbstractOutputStreamByteMessageOutput;
+import org.jboss.cx.remoting.util.ByteMessageInput;
+import org.jboss.cx.remoting.util.InputStreamByteMessageInput;
+import org.jboss.cx.remoting.util.IoUtil;
+
+/**
+ *
+ */
+public abstract class AbstractHttpUrlChannel {
+
+    private static final Logger log = Logger.getLogger(AbstractHttpUrlChannel.class);
+
+    private final CookieClientSession cookieClientSession = new CookieClientSession();
+
+    protected AbstractHttpUrlChannel() {
+    }
+
+    // Configuration
+
+    private int concurrentRequests = 2;
+    private int connectTimeout = 5000;
+    private int readTimeout = 5000;
+    private URL connectUrl;
+
+    public int getConcurrentRequests() {
+        return concurrentRequests;
+    }
+
+    public void setConcurrentRequests(final int concurrentRequests) {
+        this.concurrentRequests = concurrentRequests;
+    }
+
+    public int getConnectTimeout() {
+        return connectTimeout;
+    }
+
+    public void setConnectTimeout(final int connectTimeout) {
+        this.connectTimeout = connectTimeout;
+    }
+
+    public int getReadTimeout() {
+        return readTimeout;
+    }
+
+    public void setReadTimeout(final int readTimeout) {
+        this.readTimeout = readTimeout;
+    }
+
+    public URL getConnectUrl() {
+        return connectUrl;
+    }
+
+    public void setConnectUrl(final URL connectUrl) {
+        this.connectUrl = connectUrl;
+    }
+
+    // Dependencies
+
+    private RemotingHttpSessionContext sessionContext;
+    private Executor executor;
+
+    public RemotingHttpSessionContext getSessionContext() {
+        return sessionContext;
+    }
+
+    public void setSessionContext(final RemotingHttpSessionContext sessionContext) {
+        this.sessionContext = sessionContext;
+    }
+
+    public Executor getExecutor() {
+        return executor;
+    }
+
+    public void setExecutor(final Executor executor) {
+        this.executor = executor;
+    }
+
+    // Lifecycle
+
+    private ExecutorService executorService;
+
+    public void create() {
+        if (executor == null) {
+            executor = executorService = Executors.newFixedThreadPool(concurrentRequests);
+        }
+        if (connectUrl == null) {
+            throw new NullPointerException("connectUrl is null");
+        }
+        if (sessionContext == null) {
+            throw new NullPointerException("sessionContext is null");
+        }
+    }
+
+    public void start() {
+
+    }
+
+    public void stop() {
+
+    }
+
+    public void destroy() {
+        try {
+
+        } finally {
+            if (executorService != null) {
+                executorService.shutdown();
+            }
+        }
+        executor = executorService = null;
+        sessionContext = null;
+    }
+
+    // Interface
+
+    protected void handleRequest(final URL connectUrl) {
+        final RemotingHttpSessionContext sessionContext = getSessionContext();
+        final OutgoingHttpMessage message = sessionContext.getOutgoingHttpMessage();
+        try {
+            final HttpURLConnection httpConnection = intializeConnection(connectUrl);
+            try {
+                httpConnection.connect();
+                final OutputStream outputStream = httpConnection.getOutputStream();
+                try {
+                    message.writeMessageData(new AbstractOutputStreamByteMessageOutput(outputStream) {
+                        public void commit() throws IOException {
+                        }
+                    });
+                    // now read the reply
+                    final String responseMessage = httpConnection.getResponseMessage();
+                    log.trace("HTTP server sent back a response message: %s", responseMessage);
+                    final List<String> setCookies = httpConnection.getHeaderFields().get("Set-Cookie");
+                    for (String s : setCookies) {
+                        cookieClientSession.handleSetCookieHeader(s, connectUrl.getHost(), connectUrl.getPath());
+                    }
+                    final InputStream inputStream = httpConnection.getInputStream();
+                    try {
+                        sessionContext.processInboundMessage(new AbstractIncomingHttpMessage() {
+                            public ByteMessageInput getMessageData() throws IOException {
+                                return new InputStreamByteMessageInput(inputStream, -1);
+                            }
+                        });
+                    } finally {
+                        IoUtil.closeSafely(inputStream);
+                    }
+                } finally {
+                    IoUtil.closeSafely(outputStream);
+                }
+            } catch (IOException e) {
+                // probably a HTTP error occurred, so let's consume it
+                try {
+                    final InputStream errorStream = httpConnection.getErrorStream();
+                    if (errorStream != null) try {
+                        // consume & discard the error stream
+                        while (errorStream.read() > -1);
+                        errorStream.close();
+                    } finally {
+                        IoUtil.closeSafely(errorStream);
+                    } else {
+                        log.trace(e, "Connection failed but there is no error stream");
+                    }
+                } catch (IOException e2) {
+                    log.trace(e2, "Error consuming the error stream from remote URL '%s'", connectUrl);
+                }
+                // todo - need a backoff timer to prevent a storm of HTTP errors.  Or perhaps the session should be torn down.
+            }
+        } catch (IOException e) {
+            log.trace(e, "Error establishing connection");
+        }
+    }
+
+    protected HttpURLConnection intializeConnection(final URL connectUrl) throws IOException {
+        final HttpURLConnection httpConnection = (HttpURLConnection) connectUrl.openConnection();
+        httpConnection.setDoInput(true);
+        httpConnection.setDoOutput(true);
+        httpConnection.setDefaultUseCaches(false);
+        httpConnection.setUseCaches(false);
+        httpConnection.setInstanceFollowRedirects(false);
+        httpConnection.setConnectTimeout(getConnectTimeout());
+        httpConnection.setReadTimeout(getReadTimeout());
+        httpConnection.setRequestMethod("POST"); // All remoting requests are POST
+        final String cookieHeader = cookieClientSession.getCookieHeader(connectUrl.getHost(), connectUrl.getPath(), false);
+        if (cookieHeader != null) {
+            httpConnection.setRequestProperty("Cookie", cookieHeader);
+        }
+        return httpConnection;
+    }
+}

Deleted: remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpUrlChannel.java
===================================================================
--- remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpUrlChannel.java	2008-04-18 21:37:17 UTC (rev 4016)
+++ remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpUrlChannel.java	2008-04-18 21:37:37 UTC (rev 4017)
@@ -1,141 +0,0 @@
-package org.jboss.cx.remoting.http.urlconnection;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentMap;
-import org.jboss.cx.remoting.http.spi.AbstractIncomingHttpMessage;
-import org.jboss.cx.remoting.http.spi.OutgoingHttpMessage;
-import org.jboss.cx.remoting.http.spi.RemotingHttpSessionContext;
-import org.jboss.cx.remoting.log.Logger;
-import org.jboss.cx.remoting.util.ByteMessageInput;
-import org.jboss.cx.remoting.util.ByteMessageOutput;
-import org.jboss.cx.remoting.util.CollectionUtil;
-
-/**
- *
- */
-public final class HttpUrlChannel {
-
-    private static final Logger log = Logger.getLogger(HttpUrlChannel.class);
-
-    private final ConcurrentMap<String, String> cookies = CollectionUtil.synchronizedMap(new LinkedHashMap<String, String>());
-
-    private RemotingHttpSessionContext sessionContext;
-
-    public RemotingHttpSessionContext getSessionContext() {
-        return sessionContext;
-    }
-
-    public void setSessionContext(final RemotingHttpSessionContext sessionContext) {
-        this.sessionContext = sessionContext;
-    }
-
-    public void doIt() {
-        final URL connectUrl = null;
-        new Runnable() {
-            public void run() {
-                for (;;) {
-                    final OutgoingHttpMessage message = sessionContext.getOutgoingHttpMessage();
-                    HttpURLConnection httpConnection = null;
-                    try {
-                        httpConnection = (HttpURLConnection) connectUrl.openConnection();
-                        httpConnection.setDoInput(true);
-                        httpConnection.setDoOutput(true);
-                        httpConnection.setDefaultUseCaches(false);
-                        httpConnection.setUseCaches(false);
-                        httpConnection.setInstanceFollowRedirects(false);
-//                        httpURLConnection.setConnectTimeout();
-//                        httpURLConnection.setReadTimeout();
-                        httpConnection.setRequestMethod("POST"); // All remoting requests are POST
-                        for (Map.Entry<String, String> entry : cookies.entrySet()) {
-//                            httpConnection.setRequestProperty();
-//                            entry.getKey()
-                        }
-                        httpConnection.connect();
-                        final OutputStream outputStream = httpConnection.getOutputStream();
-                        message.writeMessageData(new ByteMessageOutput() {
-
-                            public void write(final int b) throws IOException {
-                                outputStream.write(b);
-                            }
-
-                            public void write(final byte[] b) throws IOException {
-                                outputStream.write(b);
-                            }
-
-                            public void write(final byte[] b, final int offs, final int len) throws IOException {
-                                outputStream.write(b, offs, len);
-                            }
-
-                            public void commit() throws IOException {
-                            }
-
-                            public int getBytesWritten() throws IOException {
-                                throw new UnsupportedOperationException("getBytesWritten()");
-                            }
-
-                            public void close() throws IOException {
-                                outputStream.close();
-                            }
-
-                            public void flush() throws IOException {
-                                outputStream.flush();
-                            }
-                        });
-                        // now read the reply
-                        final List<String> setCookies = httpConnection.getHeaderFields().get("Set-Cookie");
-                        final InputStream inputStream = httpConnection.getInputStream();
-                        sessionContext.processInboundMessage(new AbstractIncomingHttpMessage() {
-                            public ByteMessageInput getMessageData() throws IOException {
-                                return new ByteMessageInput() {
-
-                                    public int read() throws IOException {
-                                        return inputStream.read();
-                                    }
-
-                                    public int read(final byte[] data) throws IOException {
-                                        return inputStream.read(data);
-                                    }
-
-                                    public int read(final byte[] data, final int offs, final int len) throws IOException {
-                                        return inputStream.read(data, offs, len);
-                                    }
-
-                                    public int remaining() {
-                                        throw new UnsupportedOperationException("remaining()");
-                                    }
-
-                                    public void close() throws IOException {
-                                        inputStream.close();
-                                    }
-                                };
-                            }
-                        });
-                    } catch (IOException e) {
-                        // probably a HTTP error occurred, so let's consume it
-                        try {
-                            if (httpConnection != null) {
-                                final int responseCode = httpConnection.getResponseCode();
-                                log.trace("Got error response code %d from remote URL '%s'", Integer.valueOf(responseCode), connectUrl);
-                                final InputStream errorStream = httpConnection.getErrorStream();
-                                // consume & discard the error stream
-                                while (errorStream.read() > -1);
-                                errorStream.close();
-                            }
-                        } catch (IOException e2) {
-                            log.trace("Error consuming the error stream from remote URL '%s'", connectUrl);
-                        }
-                        // todo - need a backoff timer to prevent a storm of HTTP errors.  Or perhaps the session should be torn down.
-                    }
-                }
-            }
-        };
-
-    }
-}

Added: remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpUrlChannel.java
===================================================================
--- remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpUrlChannel.java	                        (rev 0)
+++ remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpUrlChannel.java	2008-04-18 21:37:37 UTC (rev 4017)
@@ -0,0 +1,16 @@
+package org.jboss.cx.remoting.http.urlconnection;
+
+/**
+ *
+ */
+public final class HttpUrlChannel extends AbstractHttpUrlChannel {
+
+    // lifecycle
+
+    public void create() {
+        final String protocol = getConnectUrl().getProtocol();
+        if (! "http".equals(protocol)) {
+            throw new IllegalArgumentException("Cannot use " + HttpUrlChannel.class.getName() + " for protocol \"" + protocol + "\"");
+        }
+    }
+}

Added: remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpsUrlChannel.java
===================================================================
--- remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpsUrlChannel.java	                        (rev 0)
+++ remoting3/trunk/http-urlconnection/src/main/java/org/jboss/cx/remoting/http/urlconnection/HttpsUrlChannel.java	2008-04-18 21:37:37 UTC (rev 4017)
@@ -0,0 +1,81 @@
+package org.jboss.cx.remoting.http.urlconnection;
+
+import java.io.IOException;
+import java.net.URL;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLSocketFactory;
+
+/**
+ *
+ */
+public final class HttpsUrlChannel extends AbstractHttpUrlChannel {
+
+    // Configuration
+
+    private HostnameVerifier hostnameVerifier;
+    private SSLSocketFactory sslSocketFactory;
+
+    public HostnameVerifier getHostnameVerifier() {
+        return hostnameVerifier;
+    }
+
+    public void setHostnameVerifier(final HostnameVerifier hostnameVerifier) {
+        this.hostnameVerifier = hostnameVerifier;
+    }
+
+    public SSLSocketFactory getSslSocketFactory() {
+        return sslSocketFactory;
+    }
+
+    public void setSslSocketFactory(final SSLSocketFactory sslSocketFactory) {
+        this.sslSocketFactory = sslSocketFactory;
+    }
+
+    // Dependencies
+
+    
+
+    // Lifecycle
+
+    public void create() {
+        super.create();
+        if (hostnameVerifier == null) {
+            hostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
+        }
+        if (sslSocketFactory == null) {
+            sslSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
+        }
+        final String protocol = getConnectUrl().getProtocol();
+        if (! "https".equals(protocol)) {
+            throw new IllegalArgumentException("Cannot use " + HttpsUrlChannel.class.getName() + " for protocol \"" + protocol + "\"");
+        }
+    }
+
+    public void start() {
+        super.start();
+    }
+
+    public void stop() {
+        super.stop();
+    }
+
+    public void destroy() {
+        try {
+            super.destroy();
+        } finally {
+            hostnameVerifier = null;
+            sslSocketFactory = null;
+        }
+    }
+
+    // Interface
+
+    protected HttpsURLConnection intializeConnection(final URL connectUrl) throws IOException {
+        final HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super.intializeConnection(connectUrl);
+        httpsURLConnection.setHostnameVerifier(hostnameVerifier);
+        httpsURLConnection.setSSLSocketFactory(sslSocketFactory);
+        return httpsURLConnection;
+    }
+}




More information about the jboss-remoting-commits mailing list