[jboss-remoting-commits] JBoss Remoting SVN: r4022 - in remoting3/trunk: http/src/main/java/org/jboss/cx/remoting/http/cookie and 2 other directories.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Fri Apr 18 23:29:11 EDT 2008


Author: david.lloyd at jboss.com
Date: 2008-04-18 23:29:10 -0400 (Fri, 18 Apr 2008)
New Revision: 4022

Added:
   remoting3/trunk/http-se6/src/main/java/org/jboss/cx/remoting/http/se6/SunHttpServerChannel.java
Removed:
   remoting3/trunk/http-se6/src/main/java/org/jboss/cx/remoting/http/se6/ServerInstance.java
Modified:
   remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/HttpProtocolSupport.java
   remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/CookieParser.java
   remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/SimpleCookieParser.java
   remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/spi/RemotingHttpServerContext.java
Log:
javase6 http server channel

Modified: remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/HttpProtocolSupport.java
===================================================================
--- remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/HttpProtocolSupport.java	2008-04-19 00:51:04 UTC (rev 4021)
+++ remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/HttpProtocolSupport.java	2008-04-19 03:29:10 UTC (rev 4022)
@@ -100,12 +100,12 @@
     private final ConcurrentMap<String, RemotingHttpSession> sessionMap = CollectionUtil.concurrentWeakValueMap();
 
     public String generateSessionId() {
-        final byte[] bytes = new byte[32];
+        final byte[] bytes = new byte[16];
         StringBuilder builder = new StringBuilder(bytes.length * 2);
         random.nextBytes(bytes);
         for (byte b : bytes) {
-            builder.append(Character.digit(b >>> 4 & 15, 16));
-            builder.append(Character.digit(b & 15, 16));
+            builder.append(Character.forDigit(b >>> 4 & 15, 16));
+            builder.append(Character.forDigit(b & 15, 16));
         }
         return builder.toString();
     }

Modified: remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/CookieParser.java
===================================================================
--- remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/CookieParser.java	2008-04-19 00:51:04 UTC (rev 4021)
+++ remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/CookieParser.java	2008-04-19 03:29:10 UTC (rev 4022)
@@ -1,8 +1,12 @@
 package org.jboss.cx.remoting.http.cookie;
 
+import java.util.List;
+
 /**
  *
  */
 public interface CookieParser {
     Cookie parseSetCookie(String setCookie, String defaultDomain, String defaultPath);
+
+    List<Cookie> parseCookie(String cookie);
 }

Modified: remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/SimpleCookieParser.java
===================================================================
--- remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/SimpleCookieParser.java	2008-04-19 00:51:04 UTC (rev 4021)
+++ remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/cookie/SimpleCookieParser.java	2008-04-19 03:29:10 UTC (rev 4022)
@@ -4,7 +4,10 @@
 import java.text.SimpleDateFormat;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.List;
 import org.jboss.cx.remoting.log.Logger;
+import org.jboss.cx.remoting.util.CollectionUtil;
+import static org.jboss.cx.remoting.util.CollectionUtil.arrayList;
 
 /**
  *
@@ -81,4 +84,20 @@
         }
         return new Cookie(name, value, path, domain, expires, secure);
     }
+
+    public List<Cookie> parseCookie(final String cookie) {
+        if (cookie == null) {
+            throw new NullPointerException("cookie is null");
+        }
+        List<Cookie> cookieList = arrayList();
+        final Matcher matcher = PAIR_PATTERN.matcher(cookie);
+        while (matcher.find()) {
+            final String name = matcher.group(1);
+            final String value = matcher.group(2);
+            if (name != null && value != null) {
+                cookieList.add(new Cookie(name, value, "/", ".unknown.local", -1L, false));
+            }
+        }
+        return cookieList;
+    }
 }

Modified: remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/spi/RemotingHttpServerContext.java
===================================================================
--- remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/spi/RemotingHttpServerContext.java	2008-04-19 00:51:04 UTC (rev 4021)
+++ remoting3/trunk/http/src/main/java/org/jboss/cx/remoting/http/spi/RemotingHttpServerContext.java	2008-04-19 03:29:10 UTC (rev 4022)
@@ -4,5 +4,5 @@
  *
  */
 public interface RemotingHttpServerContext {
-    RemotingHttpSessionContext locateSession(String remotingSessionId);
+    RemotingHttpChannelContext processUnsolicitedInboundMessage(IncomingHttpMessage incomingHttpMessage);
 }

Deleted: remoting3/trunk/http-se6/src/main/java/org/jboss/cx/remoting/http/se6/ServerInstance.java
===================================================================
--- remoting3/trunk/http-se6/src/main/java/org/jboss/cx/remoting/http/se6/ServerInstance.java	2008-04-19 00:51:04 UTC (rev 4021)
+++ remoting3/trunk/http-se6/src/main/java/org/jboss/cx/remoting/http/se6/ServerInstance.java	2008-04-19 03:29:10 UTC (rev 4022)
@@ -1,140 +0,0 @@
-package org.jboss.cx.remoting.http.se6;
-
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.util.concurrent.Executor;
-
-import com.sun.net.httpserver.BasicAuthenticator;
-import com.sun.net.httpserver.HttpContext;
-import com.sun.net.httpserver.HttpExchange;
-import com.sun.net.httpserver.HttpHandler;
-import com.sun.net.httpserver.HttpServer;
-
-/**
- *
- */
-public final class ServerInstance {
-    private final HttpServer httpServer;
-    private final InetAddress localAddress;
-    private final int localPort;
-
-    public ServerInstance(String context, HttpServer httpServer) {
-        this.httpServer = httpServer;
-        final HttpContext httpContext = httpServer.createContext(context, new MyHttpHandler());
-        httpContext.setAuthenticator(new BasicAuthenticator("Remote Access") {
-            public boolean checkCredentials(final String user, final String password) {
-                final char[] passwordChars = password.toCharArray();
-
-                // todo - use endpoint callbacks
-                return false;
-            }
-        });
-        final InetSocketAddress socketAddress = httpServer.getAddress();
-        localAddress = socketAddress.getAddress();
-        localPort = socketAddress.getPort();
-    }
-
-    public ServerInstance(String context, InetSocketAddress address, Executor executor) throws IOException {
-        this(context, HttpServer.create(address, 0));
-        httpServer.setExecutor(executor);
-    }
-
-    public void start() {
-        httpServer.start();
-    }
-
-    public void stop() {
-        // todo - magic #
-        httpServer.stop(30);
-    }
-
-    private class MyHttpHandler implements HttpHandler {
-        public void handle(final HttpExchange httpExchange) throws IOException {
-//            final URI requestURI = httpExchange.getRequestURI();
-//            final Headers requestHeaders = httpExchange.getRequestHeaders();
-//            final InetSocketAddress inetSocketAddress = httpExchange.getRemoteAddress();
-//            final InetAddress remoteAddress = inetSocketAddress.getAddress();
-//            final int remotePort = inetSocketAddress.getPort();
-//            RemotingHttpSessionContext httpSessionContext = null; // todo locate
-//            httpSessionContext.queueMessage(new AbstractIncomingHttpMessage() {
-//                public ByteMessageInput getMessageData() {
-//                    final InputStream inputStream = httpExchange.getRequestBody();
-//                    return new ByteMessageInput() {
-//                        public int read() throws IOException {
-//                            return inputStream.read();
-//                        }
-//
-//                        public int read(byte[] data) throws IOException {
-//                            return inputStream.read(data);
-//                        }
-//
-//                        public int read(byte[] data, int offs, int len) throws IOException {
-//                            return inputStream.read(data, offs, len);
-//                        }
-//
-//                        public int remaining() {
-//                            return -1;
-//                        }
-//
-//                        public void close() throws IOException {
-//                            inputStream.close();
-//                        }
-//                    };
-//                }
-//            });
-//            // todo - WAIT untit the input stream is consumed? or - just don't close the output until the input is done
-//            // todo - consume all of input stream
-//            OutgoingHttpMessage httpReply = null;
-//            try {
-//                // todo - magic # - timeout should be configurable
-//                httpReply = httpSessionContext.getNextMessage(8000L);
-//            } catch (InterruptedException e) {
-//                Thread.currentThread().interrupt();
-//            }
-//            if (httpReply == null) {
-//                // send empty OK
-//                httpExchange.sendResponseHeaders(200, 0);
-//            } else {
-//                // send reply
-//                final Headers responseHeaders = httpExchange.getResponseHeaders();
-//                for (final String name : httpReply.getHeaderNames()) {
-//                    for (final String value : httpReply.getHeaderValues(name)) {
-//                        responseHeaders.set(name, value);
-//                    }
-//                }
-//                httpExchange.sendResponseHeaders(200, 0); // todo - preset response size?
-//                final OutputStream outputStream = httpExchange.getResponseBody();
-//                httpReply.writeMessageData(new ByteMessageOutput() {
-//                    public void write(int b) throws IOException {
-//                        outputStream.write(b);
-//                    }
-//
-//                    public void write(byte[] b) throws IOException {
-//                        outputStream.write(b);
-//                    }
-//
-//                    public void write(byte[] b, int offs, int len) throws IOException {
-//                        outputStream.write(b, offs, len);
-//                    }
-//
-//                    public void commit() throws IOException {
-//                    }
-//
-//                    public int getBytesWritten() throws IOException {
-//                        return -1;
-//                    }
-//
-//                    public void close() throws IOException {
-//                        outputStream.close();
-//                    }
-//
-//                    public void flush() throws IOException {
-//                        outputStream.flush();
-//                    }
-//                });
-//            }
-//            httpExchange.close();
-        }
-    }
-}

Added: remoting3/trunk/http-se6/src/main/java/org/jboss/cx/remoting/http/se6/SunHttpServerChannel.java
===================================================================
--- remoting3/trunk/http-se6/src/main/java/org/jboss/cx/remoting/http/se6/SunHttpServerChannel.java	                        (rev 0)
+++ remoting3/trunk/http-se6/src/main/java/org/jboss/cx/remoting/http/se6/SunHttpServerChannel.java	2008-04-19 03:29:10 UTC (rev 4022)
@@ -0,0 +1,172 @@
+package org.jboss.cx.remoting.http.se6;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.List;
+import java.util.concurrent.ConcurrentMap;
+import org.jboss.cx.remoting.http.AbstractHttpChannel;
+import org.jboss.cx.remoting.http.HttpProtocolSupport;
+import org.jboss.cx.remoting.http.cookie.Cookie;
+import org.jboss.cx.remoting.http.cookie.CookieParser;
+import org.jboss.cx.remoting.http.spi.AbstractIncomingHttpMessage;
+import org.jboss.cx.remoting.http.spi.OutgoingHttpMessage;
+import org.jboss.cx.remoting.http.spi.RemotingHttpChannelContext;
+import org.jboss.cx.remoting.http.spi.RemotingHttpServerContext;
+import org.jboss.cx.remoting.util.AbstractOutputStreamByteMessageOutput;
+import org.jboss.cx.remoting.util.ByteMessageInput;
+import org.jboss.cx.remoting.util.CollectionUtil;
+import org.jboss.cx.remoting.util.InputStreamByteMessageInput;
+import org.jboss.cx.remoting.util.IoUtil;
+
+import com.sun.net.httpserver.Headers;
+import com.sun.net.httpserver.HttpContext;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+
+/**
+ *
+ */
+public final class SunHttpServerChannel extends AbstractHttpChannel implements HttpHandler {
+
+    public SunHttpServerChannel() {
+    }
+
+    // Configuration
+
+    private CookieParser cookieParser;
+
+    public CookieParser getCookieParser() {
+        return cookieParser;
+    }
+
+    public void setCookieParser(final CookieParser cookieParser) {
+        this.cookieParser = cookieParser;
+    }
+
+    // Dependencies
+
+    private HttpProtocolSupport protocolSupport;
+    private RemotingHttpServerContext serverContext;
+    private HttpContext httpContext;
+
+    public HttpProtocolSupport getProtocolSupport() {
+        return protocolSupport;
+    }
+
+    public void setProtocolSupport(final HttpProtocolSupport protocolSupport) {
+        this.protocolSupport = protocolSupport;
+    }
+
+    public RemotingHttpServerContext getServerContext() {
+        return serverContext;
+    }
+
+    public void setServerContext(final RemotingHttpServerContext serverContext) {
+        this.serverContext = serverContext;
+    }
+
+    public HttpContext getHttpContext() {
+        return httpContext;
+    }
+
+    public void setHttpContext(final HttpContext httpContext) {
+        this.httpContext = httpContext;
+    }
+
+    // Lifecycle
+
+    public void create() {
+        if (serverContext == null) {
+            throw new NullPointerException("serverContext is null");
+        }
+    }
+
+    public void start() {
+        httpContext.setHandler(this);
+    }
+
+    public void stop() {
+        httpContext.setHandler(new HttpHandler() {
+            public void handle(final HttpExchange exchange) throws IOException {
+                throw new IOException("Context is not available");
+            }
+        });
+    }
+
+    public void destroy() {
+        serverContext = null;
+        httpContext = null;
+    }
+
+    // Implementation
+
+    private final ConcurrentMap<String, RemotingHttpChannelContext> sessions = CollectionUtil.concurrentMap();
+
+    public void handle(final HttpExchange exchange) throws IOException {
+        // it could be a non-https exchange (in the case of a separate SSL frontend)
+        final boolean secure = "https".equals(exchange.getProtocol());
+        final Headers requestHeader = exchange.getRequestHeaders();
+        final List<String> cookieHeaders = requestHeader.get("Cookie");
+        int parkTimeout = -1;
+        String sessionId = null;
+        for (String cookieString : cookieHeaders) {
+            final List<Cookie> cookies = cookieParser.parseCookie(cookieString);
+            for (Cookie cookie : cookies) {
+                if ("Park-Timeout".equals(cookie.getName())) {
+                    try {
+                        parkTimeout = Integer.parseInt(cookie.getValue());
+                    } catch (NumberFormatException e) {
+                        // oh well
+                    }
+                } else if ("JSESSIONID".equals(cookie.getName())) {
+                    sessionId = cookie.getValue();
+                }
+            }
+        }
+        final boolean needToSetSession;
+        RemotingHttpChannelContext context = sessions.get(sessionId);
+        final InputStream inputStream = exchange.getRequestBody();
+        try {
+            final AbstractIncomingHttpMessage incomingMessage = new AbstractIncomingHttpMessage() {
+                public ByteMessageInput getMessageData() throws IOException {
+                    return new InputStreamByteMessageInput(inputStream, -1);
+                }
+            };
+            if (context == null) {
+                needToSetSession = true;
+                context = serverContext.processUnsolicitedInboundMessage(incomingMessage);
+            } else {
+                needToSetSession = false;
+                context.processInboundMessage(incomingMessage);
+            }
+        } finally {
+            IoUtil.closeSafely(inputStream);
+        }
+        if (needToSetSession) {
+            final StringBuilder setCookieBuilder = new StringBuilder(60);
+            setCookieBuilder.append("JSESSIONID=");
+            for (;;) {
+                String jsessionid = protocolSupport.generateSessionId();
+                if (sessions.putIfAbsent(jsessionid, context) == null) {
+                    setCookieBuilder.append(jsessionid);
+                    break;
+                }
+            }
+            if (secure) {
+                setCookieBuilder.append("; secure");
+            }
+            exchange.getResponseHeaders().set("Set-Cookie", setCookieBuilder.toString());
+        }
+        final OutgoingHttpMessage outgoingMessage = context.waitForOutgoingHttpMessage(parkTimeout);
+        final OutputStream outputStream = exchange.getResponseBody();
+        try {
+            outgoingMessage.writeMessageData(new AbstractOutputStreamByteMessageOutput(outputStream) {
+                public void commit() throws IOException {
+                }
+            });
+        } finally {
+            IoUtil.closeSafely(outputStream);
+        }
+    }
+}




More information about the jboss-remoting-commits mailing list