Author: ngn
Date: Sun May 30 21:32:36 2010
New Revision: 949587

URL: http://svn.apache.org/viewvc?rev=949587&view=rev
Log:
Adding first stab at a BOSH servlet (VYSPER-205). By Bogdan Pistol

Added:
    
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshServlet.java
    mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/
    
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/crossdomain.xml
    
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/keystore
   (with props)
    
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/keystore-README.txt
Removed:
    
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshIoHandler.java
Modified:
    mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/pom.xml
    
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshEndpoint.java
    
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/ServerMain.java

Modified: mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/pom.xml
URL: 
http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/pom.xml?rev=949587&r1=949586&r2=949587&view=diff
==============================================================================
--- mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/pom.xml (original)
+++ mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/pom.xml Sun May 30 
21:32:36 2010
@@ -41,11 +41,11 @@
       <artifactId>vysper-core</artifactId>
     </dependency>
     
-       <dependency>
-         <groupId>org.eclipse.jetty</groupId>
-         <artifactId>jetty-server</artifactId>
-         <version>7.1.1.v20100517</version>
-       </dependency>
+    <dependency>
+      <groupId>org.eclipse.jetty</groupId>
+      <artifactId>jetty-servlet</artifactId>
+      <version>7.1.3.v20100526</version>
+    </dependency>
 
     <!-- Runtime dependencies -->
     <dependency>

Modified: 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshEndpoint.java
URL: 
http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshEndpoint.java?rev=949587&r1=949586&r2=949587&view=diff
==============================================================================
--- 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshEndpoint.java
 (original)
+++ 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshEndpoint.java
 Sun May 30 21:32:36 2010
@@ -23,12 +23,20 @@ import java.io.IOException;
 
 import org.apache.vysper.xmpp.server.Endpoint;
 import org.apache.vysper.xmpp.server.ServerRuntimeContext;
+import org.eclipse.jetty.server.Connector;
 import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.nio.SelectChannelConnector;
+import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Allows HTTP clients to communicate through the Bosh protocol 
(http://xmpp.org/extensions/xep-0124.html)
+ * Allows HTTP clients to communicate via the BOSH protocol with Vysper.
+ * <p>
+ * See http://xmpp.org/extensions/xep-0124.html and
+ * http://xmpp.org/extensions/xep-0206.html
  *
  * @author The Apache MINA Project ([email protected])
  */
@@ -42,28 +50,97 @@ public class BoshEndpoint implements End
 
     private Server server;
 
+    private boolean isSSLEnabled;
+    
+    private String sslKeystorePath;
+    
+    private String sslKeystorePassword;
+    
+    private String flashCrossDomainPolicy;
+
     public void setServerRuntimeContext(
             ServerRuntimeContext serverRuntimeContext) {
         this.serverRuntimeContext = serverRuntimeContext;
     }
 
+    /**
+     * Setter for the listen port
+     * @param port
+     */
     public void setPort(int port) {
         this.port = port;
     }
-
-    public void setSslEnabled(boolean value) {
-        // TODO:
+    
+    /**
+     * Configures the SSL keystore and the keystore password.
+     * <p>
+     * These parameters are required if SSL is enabled.
+     * The password is used both for accessing the keystore and for recovering
+     * the key from the keystore. The unique password is a limitation, you
+     * cannot use different passwords for the keystore and for the key.
+     * 
+     * @param keystorePath the path to the Java keystore
+     * @param password the password used as the keystore password and also used
+     * when recovering the key from the keystore
+     */
+    public void setSSLCertificateInfo(String keystorePath, String password) {
+        sslKeystorePath = keystorePath;
+        sslKeystorePassword = password;
     }
 
+    /**
+     * Enables/disables SSL for this endpoint.
+     * <p>
+     * If SSL is enabled it requires SSL certificate information that can be
+     * configured with {...@link #setSSLCertificateInfo(String, String)}
+     * @param value
+     */
+    public void setSSLEnabled(boolean value) {
+        isSSLEnabled = value;
+    }
+    
+    /**
+     * Setter for the Flash cross-domain policy file location
+     * @param policyPath
+     */
+    public void setFlashCrossDomainPolicy(String policyPath) {
+        flashCrossDomainPolicy = policyPath;
+    }
+    
+    /**
+     * @throws IOException 
+     * @throws RuntimeException a wrapper of the possible
+     * {...@link java.lang.Exception} that Jetty can throw at start-up
+     */
     public void start() throws IOException {
-        server = new Server(port);
-        BoshIoHandler boshIoHandler = new BoshIoHandler();
-        boshIoHandler.setServerRuntimeContext(serverRuntimeContext);
-        server.setHandler(boshIoHandler);
+        server = new Server();
+        
+        Connector connector;
+        if (isSSLEnabled) {
+            SslSelectChannelConnector sslConnector = new 
SslSelectChannelConnector();
+            sslConnector.setKeystore(sslKeystorePath);
+            sslConnector.setPassword(sslKeystorePassword);
+            sslConnector.setKeyPassword(sslKeystorePassword);
+            connector = sslConnector;
+        } else {
+            connector = new SelectChannelConnector();
+        }
+        connector.setPort(port);
+        server.setConnectors(new Connector[] { connector });
+        
+        ServletContextHandler context = new ServletContextHandler(
+                ServletContextHandler.SESSIONS);
+        context.setContextPath("/");
+        server.setHandler(context);
+        
+        BoshServlet boshServlet = new BoshServlet();
+        boshServlet.setServerRuntimeContext(serverRuntimeContext);
+        boshServlet.setFlashCrossDomainPolicy(flashCrossDomainPolicy);
+        context.addServlet(new ServletHolder(boshServlet), "/");
+        
         try {
             server.start();
         } catch (Exception e) {
-            // TODO IOException(Exception) is only Java 1.6, so throwing a 
RuntimeException for now
             throw new RuntimeException(e);
         }
     }

Added: 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshServlet.java
URL: 
http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshServlet.java?rev=949587&view=auto
==============================================================================
--- 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshServlet.java
 (added)
+++ 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshServlet.java
 Sun May 30 21:32:36 2010
@@ -0,0 +1,141 @@
+/*
+ *  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.vysper.xmpp.extension.xep0124;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Random;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.vysper.xmpp.server.ServerRuntimeContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Handles BOSH requests from HTTP clients.
+ *
+ * @author The Apache MINA Project ([email protected])
+ */
+public class BoshServlet extends HttpServlet {
+
+    private static final long serialVersionUID = 1979722775762481476L;
+
+    private static final String FLASH_CROSS_DOMAIN_POLICY_URI = 
"/crossdomain.xml";
+
+    private static final String HTML_CONTENT_TYPE = "text/html; charset=UTF-8";
+
+    private static final String XML_CONTENT_TYPE = "text/xml; charset=UTF-8";
+
+    private static final String INFO_GET = "This is an XMPP BOSH connection 
manager, you need to use a compatible BOSH client to use its services!";
+
+    private final Logger logger = LoggerFactory.getLogger(BoshServlet.class);
+
+    private ServerRuntimeContext serverRuntimeContext;
+
+    private ByteArrayOutputStream flashCrossDomainPolicy;
+
+    /**
+     * Setter for the {...@link ServerRuntimeContext}
+     * @param serverRuntimeContext
+     */
+    public void setServerRuntimeContext(
+            ServerRuntimeContext serverRuntimeContext) {
+        this.serverRuntimeContext = serverRuntimeContext;
+    }
+
+    /**
+     * Configures the Flash cross-domain policy
+     * @param policyPath
+     * @throws IOException 
+     */
+    public void setFlashCrossDomainPolicy(String policyPath) throws 
IOException {
+        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
+                policyPath));
+        flashCrossDomainPolicy = new ByteArrayOutputStream();
+        byte[] buf = new byte[1024];
+        for (;;) {
+            int i = bis.read(buf);
+            if (i == -1) {
+                break;
+            }
+            flashCrossDomainPolicy.write(buf, 0, i);
+        }
+        bis.close();
+    }
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+            throws ServletException, IOException {
+        if (FLASH_CROSS_DOMAIN_POLICY_URI.equals(req.getRequestURI())) {
+            resp.setContentType(XML_CONTENT_TYPE);
+            flashCrossDomainPolicy.writeTo(resp.getOutputStream());
+        } else {
+            resp.setContentType(HTML_CONTENT_TYPE);
+            resp.getWriter().println(INFO_GET);
+        }
+        resp.flushBuffer();
+    }
+
+    @Override
+    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
+            throws ServletException, IOException {
+        BufferedReader reader = req.getReader();
+
+        char[] buf = new char[1024];
+        StringBuilder sb = new StringBuilder();
+
+        for (;;) {
+            int n = reader.read(buf);
+            if (n == -1) {
+                break;
+            }
+            sb.append(buf, 0, n);
+        }
+
+        String body = sb.toString();
+        logger.debug("BOSH CM received : {}", body);
+
+        // test if this is the first request (kind of a hack - should be 
parsing XML)
+        if (body.indexOf("sid=") == -1) {
+            // initial request
+            String sid = Long.toString(new Random().nextLong(), 16);
+            resp.setContentType(XML_CONTENT_TYPE);
+            resp.getWriter()
+                    .print("<body xmlns='http://jabber.org/protocol/httpbind' 
wait='60' inactivity='60' polling='5' requests='2' hold='1' maxpause='120' 
sid='");
+            resp.getWriter().print(sid);
+            resp.getWriter().print("' ver='1.6' from='vysper.org'/>");
+            resp.flushBuffer();
+            return;
+        }
+
+        // session exists
+        // not handled yet, TODO
+        resp.setContentType(XML_CONTENT_TYPE);
+        resp.flushBuffer();
+    }
+
+}

Modified: 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/ServerMain.java
URL: 
http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/ServerMain.java?rev=949587&r1=949586&r2=949587&view=diff
==============================================================================
--- 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/ServerMain.java
 (original)
+++ 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/ServerMain.java
 Sun May 30 21:32:36 2010
@@ -85,9 +85,17 @@ public class ServerMain {
         }
 
         XMPPServer server = new XMPPServer("vysper.org");
+        
         server.addEndpoint(new TCPEndpoint());
+        
         BoshEndpoint boshEndpoint = new BoshEndpoint();
+        boshEndpoint.setFlashCrossDomainPolicy("src/main/resources/" +
+                       "crossdomain.xml");
+//        boshEndpoint.setSSLEnabled(true);
+//        boshEndpoint.setSSLCertificateInfo("src/main/resources/keystore",
+//                "password");
         server.addEndpoint(boshEndpoint);
+        
         //server.addEndpoint(new StanzaSessionFactory());
         server.setStorageProviderRegistry(providerRegistry);
 

Added: 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/crossdomain.xml
URL: 
http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/crossdomain.xml?rev=949587&view=auto
==============================================================================
--- 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/crossdomain.xml
 (added)
+++ 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/crossdomain.xml
 Sun May 30 21:32:36 2010
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+  <!--
+    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.
+  -->
+<!DOCTYPE cross-domain-policy SYSTEM 
"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd";>
+<cross-domain-policy>
+  <!-- 
+       Flash cross-domain policy file for allowing everything.  If you need 
more
+       information on these, please see:
+       http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html
+  -->
+  <site-control permitted-cross-domain-policies="all"/>
+  <allow-access-from domain="*" />
+  <allow-http-request-headers-from domain="*" headers="*" />
+</cross-domain-policy>
\ No newline at end of file

Added: 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/keystore
URL: 
http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/keystore?rev=949587&view=auto
==============================================================================
Binary file - no diff available.

Propchange: 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/keystore
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/keystore-README.txt
URL: 
http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/keystore-README.txt?rev=949587&view=auto
==============================================================================
--- 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/keystore-README.txt
 (added)
+++ 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/resources/keystore-README.txt
 Sun May 30 21:32:36 2010
@@ -0,0 +1,34 @@
+The keystore contains a private key and its associated self-signed certificate
+that can be used by SSL. This keystore is provided to be used only for testing 
this software.
+In non-test environments it is highly recommended to use real certificates 
signed by a Certification Authority (CA).
+
+The keystore was generated with the following command:
+
+       $ keytool -keystore keystore -alias jetty -genkey -keyalg RSA
+       Enter keystore password: password
+       Re-enter new password: password
+       What is your first and last name?
+         [Unknown]: vysper.org
+       What is the name of your organizational unit?
+         [Unknown]:
+       What is the name of your organization?
+         [Unknown]:  Vysper
+       What is the name of your City or Locality?
+         [Unknown]:
+       What is the name of your State or Province?
+         [Unknown]:
+       What is the two-letter country code for this unit?
+         [Unknown]:
+       Is CN=vysper.org, OU=Unknown, O=Vysper, L=Unknown, ST=Unknown, 
C=Unknown correct?
+         [no]: yes
+       Enter key password for <jetty>
+               (RETURN if same as keystore password):
+
+The password for the keystore and for the key is "password" and the Common 
Name (CN) is "vysper.org".
+The CN must be the domain name used by the clients to connect to the Vysper 
server (otherwise
+the browser will warn the user that the certificate is issued for another 
domain name)
+
+Using the self-signed certificate (from the keystore) will make the web 
browser warn you that the certificate if self-signed,
+but for testing purposes you can add an exception for the browser to accept it.
+
+More information about keytool: 
http://java.sun.com/javase/6/docs/technotes/tools/windows/keytool.html
\ No newline at end of file


Reply via email to