labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ber...@apache.org
Subject svn commit: r601223 - in /labs/vysper/src: main/config/ main/java/org/apache/vysper/mina/ main/java/org/apache/vysper/mina/codec/ main/java/org/apache/vysper/spring/ main/java/org/apache/vysper/xmpp/applicationdomains/base/ main/java/org/apache/vysper/...
Date Wed, 05 Dec 2007 06:42:33 GMT
Author: berndf
Date: Tue Dec  4 22:42:30 2007
New Revision: 601223

URL: http://svn.apache.org/viewvc?rev=601223&view=rev
Log:
[vysper] TLS is work in progress 
adding TLS infrastructure: trust manager, certificate, ssl context, start tls in mina
created own stanza handler for TLS

Added:
    labs/vysper/src/main/config/bogus_mina_tls.cert   (with props)
    labs/vysper/src/main/java/org/apache/vysper/spring/ResourceBasedTLSContextFactory.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/security/
    labs/vysper/src/main/java/org/apache/vysper/xmpp/security/AbstractTLSContextFactory.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/security/BogusTrustManagerFactory.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/security/TLSContextFactory.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/security/TrustManagerFactory.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/server/AbstractSessionContext.java
      - copied, changed from r542950, labs/vysper/src/main/java/org/apache/vysper/xmpp/server/DefaultSessionContext.java
    labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/starttls/
    labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/starttls/handler/
    labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/starttls/handler/StartTLSHandlerTestCase.java
      - copied, changed from r542950, labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/IQHandlerTestCase.java
Removed:
    labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/base/handler/StartTLSHandler.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/server/DefaultSessionContext.java
Modified:
    labs/vysper/src/main/config/spring-config.xml
    labs/vysper/src/main/java/org/apache/vysper/mina/MinaBackedSessionContext.java
    labs/vysper/src/main/java/org/apache/vysper/mina/XmppIoHandlerAdapter.java
    labs/vysper/src/main/java/org/apache/vysper/mina/codec/StanzaWriterProtocolEncoder.java
    labs/vysper/src/main/java/org/apache/vysper/mina/codec/XMPPProtocolCodecFactory.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/base/BaseStreamStanzaDictionary.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/base/handler/StreamStartHandler.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/starttls/handler/StartTLSHandler.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/StanzaHandlerLookup.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/worker/StartedProtocolWorker.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/server/DefaultServerRuntimeContext.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/server/ServerRuntimeContext.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/server/SessionContext.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/writer/DenseStanzaLogRenderer.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLElementVerifier.java
    labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java

Added: labs/vysper/src/main/config/bogus_mina_tls.cert
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/config/bogus_mina_tls.cert?rev=601223&view=auto
==============================================================================
Binary file - no diff available.

Propchange: labs/vysper/src/main/config/bogus_mina_tls.cert
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: labs/vysper/src/main/config/spring-config.xml
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/config/spring-config.xml?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/config/spring-config.xml (original)
+++ labs/vysper/src/main/config/spring-config.xml Tue Dec  4 22:42:30 2007
@@ -20,6 +20,16 @@
 
 <beans>
 
+    <!-- The SSL configuration -->
+
+     <bean id="bogusTrustManagerFactory" class="org.apache.vysper.xmpp.security.BogusTrustManagerFactory"/>
+    
+    <bean id="tlsContextFactory" class="org.apache.vysper.spring.ResourceBasedTLSContextFactory">
+        <constructor-arg value="file:src/main/config/bogus_mina_tls.cert"/>
+        <property name="password" value="boguspw" />
+        <property name="trustManagerFactory"><bean class="org.apache.vysper.xmpp.security.BogusTrustManagerFactory" /></property>
+    </bean>
+
     <!--
         Vysper Server singletons
     -->
@@ -45,6 +55,7 @@
                 <bean class="org.apache.vysper.xmpp.applicationdomains.starttls.StartTLSStanzaDictionary"/>
             </list>
         </constructor-arg>
+        <property name="tlsContextFactory" ref="tlsContextFactory" />
     </bean>
     
     <!--
@@ -64,26 +75,9 @@
         </map>
       </property>
     </bean>
-
-    <!-- The SSL configuration -->
-    <!--
-     <bean id="keyStore" class="org.apache.mina.integration.spring.ssl.KeyStoreFactoryBean">
-       <property name="resource" value="classpath:org/apache/mina/example/echoserver/ssl/bogus.cert"/>
-       <property name="password" value="boguspw"/>
-     </bean>
-  
-     <bean id="bogusTrustManagerFactory" class="org.apache.mina.integration.spring.ssl.BogusTrustManagerFactory"/>
-  
-     <bean id="sslContext"  class="org.apache.mina.integration.spring.ssl.SSLContextFactoryBean">
-       <property name="protocol" value="TLS"/>
-       <property name="keyManagerFactoryAlgorithm" value="SunX509"/>
-       <property name="keyManagerFactoryKeyStore"><ref local="keyStore"/></property>
-       <property name="keyManagerFactoryKeyStorePassword" value="boguspw"/>
-       <property name="trustManagerFactory"><ref local="bogusTrustManagerFactory"/></property>
-     </bean>
-    -->
     
-    <bean id="xmppCodec" class="org.apache.vysper.mina.codec.XMPPProtocolCodecFactory"/>
+    <bean id="xmppCodec" class="org.apache.vysper.mina.codec.XMPPProtocolCodecFactory">
+    </bean>
 
     <bean id="filterChainBuilder" class="org.apache.mina.integration.spring.DefaultIoFilterChainBuilderFactoryBean">
       <property name="filters">

Modified: labs/vysper/src/main/java/org/apache/vysper/mina/MinaBackedSessionContext.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/mina/MinaBackedSessionContext.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/mina/MinaBackedSessionContext.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/mina/MinaBackedSessionContext.java Tue Dec  4 22:42:30 2007
@@ -1,18 +1,38 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.mina;
 
 import org.apache.mina.common.IoSession;
+import org.apache.mina.common.TrafficMask;
+import org.apache.mina.filter.SSLFilter;
 import org.apache.vysper.mina.codec.StanzaWriteInfo;
 import org.apache.vysper.xmpp.protocol.SessionStateHolder;
-import org.apache.vysper.xmpp.server.DefaultSessionContext;
+import org.apache.vysper.xmpp.server.AbstractSessionContext;
 import org.apache.vysper.xmpp.server.ServerRuntimeContext;
+import org.apache.vysper.xmpp.server.SessionState;
 import org.apache.vysper.xmpp.stanza.Stanza;
 import org.apache.vysper.xmpp.writer.StanzaWriter;
 
 /**
  */
-public class MinaBackedSessionContext extends DefaultSessionContext implements StanzaWriter {
+public class MinaBackedSessionContext extends AbstractSessionContext implements StanzaWriter {
     private IoSession minaSession;
     private boolean openingStanzaWritten = false;
+    private boolean switchToTLS = false;
 
     public MinaBackedSessionContext(
             ServerRuntimeContext serverRuntimeContext, 
@@ -20,13 +40,30 @@
             IoSession minaSession) {
         super(serverRuntimeContext, sessionStateHolder);
         this.minaSession = minaSession;
+        sessionStateHolder.setState(SessionState.INITIATED); // connection established
     }
 
     public StanzaWriter getResponseWriter() {
         return this;
     }
 
+    public void switchToTLS() {
+        switchToTLS = true;
+    }
+
     public void write(Stanza stanza) {
+
+        if (switchToTLS) {
+            minaSession.setTrafficMask(TrafficMask.WRITE);
+            SSLFilter filter = new SSLFilter(getServerRuntimeContext().getSslContext());
+            filter.setUseClientMode(true);
+            minaSession.getFilterChain().addFirst("sslFilter", filter);
+            minaSession.setAttribute(SSLFilter.DISABLE_ENCRYPTION_ONCE, Boolean.TRUE);
+            minaSession.setAttribute(SSLFilter.USE_NOTIFICATION, Boolean.TRUE);
+            minaSession.setTrafficMask(TrafficMask.ALL);
+            switchToTLS = false;
+        }
+
         minaSession.write(new StanzaWriteInfo(stanza, !openingStanzaWritten));
         openingStanzaWritten = true;
     }

Modified: labs/vysper/src/main/java/org/apache/vysper/mina/XmppIoHandlerAdapter.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/mina/XmppIoHandlerAdapter.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/mina/XmppIoHandlerAdapter.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/mina/XmppIoHandlerAdapter.java Tue Dec  4 22:42:30 2007
@@ -31,6 +31,7 @@
 public class XmppIoHandlerAdapter implements IoHandler {
     
     public static final String ATTRIBUTE_VYSPER_SESSION = "vysperSession";
+    public static final String ATTRIBUTE_VYSPER_SESSIONSTATEHOLDER = "vysperSessionStateHolder";
 
     private ServerRuntimeContext serverRuntimeContext;
     
@@ -47,7 +48,7 @@
         Stanza stanza = (Stanza) message;
 
         SessionContext session = extractSession(ioSession);
-        protocolWorker.processStanza(session, stanza, new SessionStateHolder());
+        protocolWorker.processStanza(session, stanza, (SessionStateHolder)ioSession.getAttribute(ATTRIBUTE_VYSPER_SESSIONSTATEHOLDER));
     }
 
     private SessionContext extractSession(IoSession ioSession) {
@@ -59,8 +60,10 @@
     }
 
     public void sessionCreated(IoSession ioSession) throws Exception {
-        SessionContext sessionContext = new MinaBackedSessionContext(serverRuntimeContext, new SessionStateHolder(), ioSession);
+        SessionStateHolder stateHolder = new SessionStateHolder();
+        SessionContext sessionContext = new MinaBackedSessionContext(serverRuntimeContext, stateHolder, ioSession);
         ioSession.setAttribute(ATTRIBUTE_VYSPER_SESSION, sessionContext);
+        ioSession.setAttribute(ATTRIBUTE_VYSPER_SESSIONSTATEHOLDER, stateHolder);
     }
 
     public void sessionOpened(IoSession ioSession) throws Exception {

Modified: labs/vysper/src/main/java/org/apache/vysper/mina/codec/StanzaWriterProtocolEncoder.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/mina/codec/StanzaWriterProtocolEncoder.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/mina/codec/StanzaWriterProtocolEncoder.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/mina/codec/StanzaWriterProtocolEncoder.java Tue Dec  4 22:42:30 2007
@@ -18,15 +18,13 @@
 
 import org.apache.mina.common.ByteBuffer;
 import org.apache.mina.common.IoSession;
-import org.apache.mina.common.TrafficMask;
-import org.apache.mina.filter.SSLFilter;
 import org.apache.mina.filter.codec.ProtocolEncoder;
 import org.apache.mina.filter.codec.ProtocolEncoderOutput;
 import org.apache.vysper.charset.CharsetUtil;
-import org.apache.vysper.xmpp.writer.StanzaWriter;
+import org.apache.vysper.xmpp.stanza.Stanza;
 import org.apache.vysper.xmpp.writer.DenseStanzaLogRenderer;
+import org.apache.vysper.xmpp.writer.StanzaWriter;
 import org.apache.vysper.xmpp.xmlfragment.Renderer;
-import org.apache.vysper.xmpp.stanza.Stanza;
 
 import java.nio.charset.CharsetEncoder;
 
@@ -34,7 +32,6 @@
  * connects MINA low level protocol and session stanza writer
  */
 public class StanzaWriterProtocolEncoder implements ProtocolEncoder {
-    
 
     public void encode(IoSession ioSession, Object o, ProtocolEncoderOutput protocolEncoderOutput) throws Exception {
         if (!(o instanceof StanzaWriteInfo)) {
@@ -57,23 +54,10 @@
         protocolEncoderOutput.write(byteBuffer);
     }
 
-    public void switchToTLS(IoSession session) {
-        session.setTrafficMask(TrafficMask.WRITE);
-        //session.writeSMTPResponse();
-        SSLFilter filter = new SSLFilter(null /*sslContext*/);
-        //session.resetState();
-        session.getFilterChain().addFirst("sslFilter", filter);
-        session.setTrafficMask(TrafficMask.ALL);
-    }
-    
     public void dispose(IoSession ioSession) throws Exception {
-
+        System.err.println("unhandled StanzaWriterProtocolEncoder.dispose()");        
     }
 
-    public void close() {
-
-    }
-    
     public CharsetEncoder getSessionEncoder() {
         return CharsetUtil.UTF8_ENCODER; // todo get session dependent encoder
     }

Modified: labs/vysper/src/main/java/org/apache/vysper/mina/codec/XMPPProtocolCodecFactory.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/mina/codec/XMPPProtocolCodecFactory.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/mina/codec/XMPPProtocolCodecFactory.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/mina/codec/XMPPProtocolCodecFactory.java Tue Dec  4 22:42:30 2007
@@ -23,19 +23,12 @@
 /**
  */
 public class XMPPProtocolCodecFactory implements ProtocolCodecFactory {
-    private StanzaWriterProtocolEncoder encoder;
-    private ProtocolDecoder decoder;
-
-    public XMPPProtocolCodecFactory() {
-        encoder = new StanzaWriterProtocolEncoder();
-        decoder = new XMLStreamTokenizer();
-    }
 
     public ProtocolEncoder getEncoder() throws Exception {
-        return encoder;
+        return new StanzaWriterProtocolEncoder();
     }
 
     public ProtocolDecoder getDecoder() throws Exception {
-        return decoder;
+        return new XMLStreamTokenizer();
     }
 }

Added: labs/vysper/src/main/java/org/apache/vysper/spring/ResourceBasedTLSContextFactory.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/spring/ResourceBasedTLSContextFactory.java?rev=601223&view=auto
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/spring/ResourceBasedTLSContextFactory.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/spring/ResourceBasedTLSContextFactory.java Tue Dec  4 22:42:30 2007
@@ -0,0 +1,38 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.spring;
+
+import org.apache.vysper.xmpp.security.AbstractTLSContextFactory;
+import org.springframework.core.io.Resource;
+
+import java.io.InputStream;
+import java.io.IOException;
+
+/**
+ */
+public class ResourceBasedTLSContextFactory extends AbstractTLSContextFactory {
+
+    private Resource certificateResource = null;
+
+    public ResourceBasedTLSContextFactory(Resource certificateResource) {
+    this.certificateResource = certificateResource;
+}
+
+    protected InputStream getCertificateInputStream() throws IOException {
+        return certificateResource.getInputStream();
+    }
+}

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/base/BaseStreamStanzaDictionary.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/base/BaseStreamStanzaDictionary.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/base/BaseStreamStanzaDictionary.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/base/BaseStreamStanzaDictionary.java Tue Dec  4 22:42:30 2007
@@ -17,13 +17,12 @@
 
 package org.apache.vysper.xmpp.applicationdomains.base;
 
-import org.apache.vysper.xmpp.protocol.NamespaceURIs;
-import org.apache.vysper.xmpp.protocol.NamespaceHandlerDictionary;
-import org.apache.vysper.xmpp.applicationdomains.base.handler.StreamStartHandler;
-import org.apache.vysper.xmpp.applicationdomains.starttls.handler.StartTLSHandler;
 import org.apache.vysper.xmpp.applicationdomains.base.handler.IQHandler;
 import org.apache.vysper.xmpp.applicationdomains.base.handler.MessageHandler;
 import org.apache.vysper.xmpp.applicationdomains.base.handler.PresenceHandler;
+import org.apache.vysper.xmpp.applicationdomains.base.handler.StreamStartHandler;
+import org.apache.vysper.xmpp.protocol.NamespaceHandlerDictionary;
+import org.apache.vysper.xmpp.protocol.NamespaceURIs;
 
 /**
  * handler for base stanzas from RFC3920 (xmpp core)<br/>
@@ -34,7 +33,6 @@
     public BaseStreamStanzaDictionary() {
         super(NamespaceURIs.HTTP_ETHERX_JABBER_ORG_STREAMS);
         register(new StreamStartHandler());
-        register(new StartTLSHandler());
         register(new IQHandler());
         register(new MessageHandler());
         register(new PresenceHandler());

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/base/handler/StreamStartHandler.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/base/handler/StreamStartHandler.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/base/handler/StreamStartHandler.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/base/handler/StreamStartHandler.java Tue Dec  4 22:42:30 2007
@@ -56,6 +56,10 @@
         if (clientCall && serverCall) serverCall = false; // silently ignore ambigous attributes
         if (serverCall) sessionContext.setServerToServer(); else sessionContext.setClientToServer();
 
+        if (sessionStateHolder.getState() != SessionState.INITIATED) {
+            return respondUnsupportedStanzaType("unexpected stream start");
+        } 
+        
         // http://etherx.jabber.org/streams cannot be ommitted
         if (!jabberNamespace) {
             return respondIllegalNamespaceError("namespace is mandatory: " + NamespaceURIs.HTTP_ETHERX_JABBER_ORG_STREAMS);
@@ -156,6 +160,14 @@
     private ResponseStanzaContainer respondIllegalNamespaceError(String descriptiveText) {
         return new ResponseStanzaContainerImpl(
                     ServerErrorResponses.getInstance().getError(StreamErrorCondition.INVALID_NAMESPACE,
+                                                        null,
+                                                        descriptiveText,
+                                                        null));
+    }
+
+    private ResponseStanzaContainer respondUnsupportedStanzaType(String descriptiveText) {
+        return new ResponseStanzaContainerImpl(
+                    ServerErrorResponses.getInstance().getError(StreamErrorCondition.UNSUPPORTED_STANZA_TYPE,
                                                         null,
                                                         descriptiveText,
                                                         null));

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/starttls/handler/StartTLSHandler.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/starttls/handler/StartTLSHandler.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/starttls/handler/StartTLSHandler.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/applicationdomains/starttls/handler/StartTLSHandler.java Tue Dec  4 22:42:30 2007
@@ -49,13 +49,16 @@
         if (!tlsNamespace) {
             return respondTLSFailure();
         }
-
+        if (sessionStateHolder.getState() != SessionState.STARTED) {
+            return respondTLSFailure();
+        }
+        
         Stanza responseStanza = new ServerResponses().getTLSProceed();
 
         // if all is correct, go to next phase
         sessionStateHolder.setState(SessionState.ENCRYPTION_STARTED);
 
-        // TODO switch session to TLS mode!
+        sessionContext.switchToTLS();
         
         return new ResponseStanzaContainerImpl(responseStanza);
     }

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/StanzaHandlerLookup.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/StanzaHandlerLookup.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/StanzaHandlerLookup.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/StanzaHandlerLookup.java Tue Dec  4 22:42:30 2007
@@ -61,7 +61,7 @@
         else {
             // this is not a core stanza (RFC3920),
             // could be a test, or custom extension, so we delegate
-            return getFirstMatchingHandler(stanza);
+            return getHandlerForElement(stanza, stanza);
         }
     }
 
@@ -89,28 +89,23 @@
     }
 
     /**
-     * tries to find the handler by using the xmlElements namespace in the first place
+     * tries to find the handler by trying
+     * 1. value of xmlElement's XMLNS attribute, if unique
+     * 1. xmlElements namespace, if the element name has a namespace prefix 
      */
     private StanzaHandler getHandlerForElement(Stanza stanza, XMLElement xmlElement) {
-        String name = xmlElement.getName();
-        String namespace = xmlElement.getNamespace();
+        
+        // TODO review this method if it checks the right things
+        String namespace = xmlElement.getVerifier().getUniqueXMLNSValue();
         NamespaceHandlerDictionary namespaceHandlerDictionary = dictionaries.get(namespace);
-        if (namespaceHandlerDictionary == null) return null;
 
-        return namespaceHandlerDictionary.get(stanza);
-    }
+        if (namespaceHandlerDictionary == null) {
+            namespace = xmlElement.getNamespace();
+            namespaceHandlerDictionary = dictionaries.get(namespace);
+        } 
+        if (namespaceHandlerDictionary != null) return namespaceHandlerDictionary.get(stanza); 
 
-    /**
-     * returns the first handler which signals he can process the stanza
-     */
-    private StanzaHandler getFirstMatchingHandler(Stanza stanza) {
-        Collection<NamespaceHandlerDictionary> namespaceHandlerDictionaries = dictionaries.values();
-        for (NamespaceHandlerDictionary namespaceHandlerDictionary : namespaceHandlerDictionaries) {
-            StanzaHandler stanzaHandler = namespaceHandlerDictionary.get(stanza);
-            if (stanzaHandler != null) return stanzaHandler;
-        }
         return null;
     }
-
 
 }

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/worker/StartedProtocolWorker.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/worker/StartedProtocolWorker.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/worker/StartedProtocolWorker.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/worker/StartedProtocolWorker.java Tue Dec  4 22:42:30 2007
@@ -17,6 +17,7 @@
 
 package org.apache.vysper.xmpp.protocol.worker;
 
+import org.apache.vysper.xmpp.applicationdomains.starttls.handler.StartTLSHandler;
 import org.apache.vysper.xmpp.protocol.ResponseWriter;
 import org.apache.vysper.xmpp.protocol.SessionStateHolder;
 import org.apache.vysper.xmpp.protocol.StanzaHandler;
@@ -33,7 +34,7 @@
     }
 
     protected boolean checkState(SessionContext sessionContext, SessionStateHolder sessionStateHolder, Stanza stanza, StanzaHandler stanzaHandler, ResponseWriter responseWriter) {
-        // TODO allow authenticating dictionary
+        if (stanzaHandler instanceof StartTLSHandler) return true;
         responseWriter.writeStreamNotOpenedError(sessionContext); // TODO remove this temporary solution
         return false;
     }

Added: labs/vysper/src/main/java/org/apache/vysper/xmpp/security/AbstractTLSContextFactory.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/security/AbstractTLSContextFactory.java?rev=601223&view=auto
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/security/AbstractTLSContextFactory.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/security/AbstractTLSContextFactory.java Tue Dec  4 22:42:30 2007
@@ -0,0 +1,96 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.security;
+
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.GeneralSecurityException;
+import java.security.KeyStore;
+import java.security.Security;
+
+/**
+ * derived from MINA's BogusSSLContextFactory.
+ * see http://svn.apache.org/viewvc/mina/branches/1.0/example/src/main/java/org/apache/mina/example/echoserver/ssl/BogusSSLContextFactory.java?view=markup 
+ */
+public abstract class AbstractTLSContextFactory implements TLSContextFactory {
+
+    private static final String PROTOCOL = "TLS";
+    private static final String KEY_MANAGER_FACTORY_ALGORITHM;
+
+    static {
+        String algorithm = Security.getProperty( "ssl.KeyManagerFactory.algorithm" );
+        if( algorithm == null ) algorithm = "SunX509";
+        KEY_MANAGER_FACTORY_ALGORITHM = algorithm;
+    }
+
+    private SSLContext sslContext = null;
+
+    protected String password = null;
+    
+    protected TrustManagerFactory trustManagerFactory = null;
+    
+    // NOTE: The keystore was generated using keytool:
+    //   keytool -genkey -alias bogus -keysize 512 -validity 3650
+    //           -keyalg RSA -dname "CN=bogus.com, OU=XXX CA,
+    //               O=BogusTrustManagerFactory Inc, L=Stockholm, S=Stockholm, C=SE"
+    //           -keypass boguspw -storepass boguspw -keystore bogus.cert
+
+    abstract protected InputStream getCertificateInputStream() throws IOException;
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public void setTrustManagerFactory(TrustManagerFactory trustManagerFactory) {
+        this.trustManagerFactory = trustManagerFactory;
+    }
+
+    public SSLContext getSSLContext() throws GeneralSecurityException, IOException {
+        if (sslContext == null) sslContext = createSSLContext();
+        return sslContext;
+    }
+
+     private SSLContext createSSLContext() throws GeneralSecurityException, IOException {
+         // Create keystore
+         KeyStore ks = KeyStore.getInstance("JKS");
+         InputStream in = null;
+         try {
+             in = getCertificateInputStream();
+             ks.load( in, password.toCharArray() );
+         } finally {
+             if( in != null ) {
+                 try {
+                     in.close();
+                 }
+                 catch( IOException ignored ) { ; }
+             }
+         }
+
+         // Set up key manager factory to use our key store
+         KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEY_MANAGER_FACTORY_ALGORITHM );
+         kmf.init(ks, password.toCharArray());
+
+         // Initialize the SSLContext to work with our key managers.
+         SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
+         sslContext.init(kmf.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
+
+         return sslContext;
+     }
+
+}

Added: labs/vysper/src/main/java/org/apache/vysper/xmpp/security/BogusTrustManagerFactory.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/security/BogusTrustManagerFactory.java?rev=601223&view=auto
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/security/BogusTrustManagerFactory.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/security/BogusTrustManagerFactory.java Tue Dec  4 22:42:30 2007
@@ -0,0 +1,78 @@
+/*
+ *  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.security;
+
+import java.security.InvalidAlgorithmParameterException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.ManagerFactoryParameters;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactorySpi;
+import javax.net.ssl.X509TrustManager;
+
+/**
+ * BogusTrustManagerFactory trust manager factory. Creates BogusX509TrustManager
+ * 
+ * nearly verbose copy from project MINA.
+ * see http://svn.apache.org/viewvc/mina/branches/1.0/example/src/main/java/org/apache/mina/example/echoserver/ssl/BogusTrustManagerFactory.java?view=markup
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ */
+class BogusTrustManagerFactory extends TrustManagerFactorySpi implements TrustManagerFactory {
+
+    static final X509TrustManager X509 = new X509TrustManager() {
+        public void checkClientTrusted(X509Certificate[] x509Certificates,
+                String s) throws CertificateException {
+        }
+
+        public void checkServerTrusted(X509Certificate[] x509Certificates,
+                String s) throws CertificateException {
+        }
+
+        public X509Certificate[] getAcceptedIssuers() {
+            return new X509Certificate[0];
+        }
+    };
+
+    private static final TrustManager[] X509_MANAGERS = new TrustManager[] { X509 };
+
+    public BogusTrustManagerFactory() {
+    }
+
+    protected TrustManager[] engineGetTrustManagers() {
+        return X509_MANAGERS;
+    }
+
+    protected void engineInit(KeyStore keystore) throws KeyStoreException {
+        // noop
+    }
+
+    protected void engineInit(ManagerFactoryParameters managerFactoryParameters)
+            throws InvalidAlgorithmParameterException {
+        // noop
+    }
+    
+    public TrustManager[] getTrustManagers() {
+        return X509_MANAGERS;
+    }
+}
\ No newline at end of file

Added: labs/vysper/src/main/java/org/apache/vysper/xmpp/security/TLSContextFactory.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/security/TLSContextFactory.java?rev=601223&view=auto
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/security/TLSContextFactory.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/security/TLSContextFactory.java Tue Dec  4 22:42:30 2007
@@ -0,0 +1,29 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.security;
+
+import javax.net.ssl.SSLContext;
+import java.security.GeneralSecurityException;
+import java.io.IOException;
+
+/**
+ */
+public interface TLSContextFactory {
+    
+    SSLContext getSSLContext() throws GeneralSecurityException, IOException;
+    
+}

Added: labs/vysper/src/main/java/org/apache/vysper/xmpp/security/TrustManagerFactory.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/security/TrustManagerFactory.java?rev=601223&view=auto
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/security/TrustManagerFactory.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/security/TrustManagerFactory.java Tue Dec  4 22:42:30 2007
@@ -0,0 +1,25 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.security;
+
+import javax.net.ssl.TrustManager;
+
+/**
+ */
+public interface TrustManagerFactory {
+    TrustManager[] getTrustManagers();
+}

Copied: labs/vysper/src/main/java/org/apache/vysper/xmpp/server/AbstractSessionContext.java (from r542950, labs/vysper/src/main/java/org/apache/vysper/xmpp/server/DefaultSessionContext.java)
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/server/AbstractSessionContext.java?p2=labs/vysper/src/main/java/org/apache/vysper/xmpp/server/AbstractSessionContext.java&p1=labs/vysper/src/main/java/org/apache/vysper/xmpp/server/DefaultSessionContext.java&r1=542950&r2=601223&rev=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/server/DefaultSessionContext.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/server/AbstractSessionContext.java Tue Dec  4 22:42:30 2007
@@ -24,7 +24,7 @@
 /**
  * provides default session context behavior
  */
-public class DefaultSessionContext implements SessionContext {
+public abstract class AbstractSessionContext implements SessionContext {
 
     protected ServerRuntimeContext serverRuntimeContext;
     protected String sessionId;
@@ -35,7 +35,7 @@
     private Entity initiatingEntity;
     private boolean serverToServer = false;
 
-    public DefaultSessionContext(ServerRuntimeContext serverRuntimeContext, SessionStateHolder sessionStateHolder) {
+    public AbstractSessionContext(ServerRuntimeContext serverRuntimeContext, SessionStateHolder sessionStateHolder) {
         this.serverRuntimeContext = serverRuntimeContext;
         sessionId = serverRuntimeContext.getNextSessionId();
         serverEntity = serverRuntimeContext.getServerEnitity();

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/server/DefaultServerRuntimeContext.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/server/DefaultServerRuntimeContext.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/server/DefaultServerRuntimeContext.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/server/DefaultServerRuntimeContext.java Tue Dec  4 22:42:30 2007
@@ -25,7 +25,9 @@
 import org.apache.vysper.xmpp.protocol.StanzaHandler;
 import org.apache.vysper.xmpp.protocol.StanzaHandlerLookup;
 import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.security.TLSContextFactory;
 
+import javax.net.ssl.SSLContext;
 import java.util.List;
 import java.util.UUID;
 
@@ -37,6 +39,8 @@
     private Entity serverEntity;
     private StanzaRelay stanzaRelay;
     private ServerFeatures serverFeatures = new ServerFeatures();
+    private SSLContext sslContext = null;
+
 
     public DefaultServerRuntimeContext(Entity serverEntity, StanzaRelay stanzaRelay) {
         this.serverEntity = serverEntity;
@@ -87,5 +91,17 @@
         for (NamespaceHandlerDictionary dictionary : dictionaries) {
             addDictionary(dictionary);
         }
+    }
+
+    public void setTlsContextFactory(TLSContextFactory tlsContextFactory) {
+        try {
+            sslContext = tlsContextFactory.getSSLContext();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public SSLContext getSslContext() {
+        return sslContext;
     }
 }

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/server/ServerRuntimeContext.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/server/ServerRuntimeContext.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/server/ServerRuntimeContext.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/server/ServerRuntimeContext.java Tue Dec  4 22:42:30 2007
@@ -17,10 +17,12 @@
 
 package org.apache.vysper.xmpp.server;
 
-import org.apache.vysper.xmpp.protocol.StanzaHandler;
-import org.apache.vysper.xmpp.stanza.Stanza;
 import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.delivery.StanzaRelay;
+import org.apache.vysper.xmpp.protocol.StanzaHandler;
+import org.apache.vysper.xmpp.stanza.Stanza;
+
+import javax.net.ssl.SSLContext;
 
 /**
  * provides each session with server-global data
@@ -37,4 +39,6 @@
     StanzaRelay getStanzaRelay();
     
     ServerFeatures getServerFeatures();
+
+    SSLContext getSslContext();
 }

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/server/SessionContext.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/server/SessionContext.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/server/SessionContext.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/server/SessionContext.java Tue Dec  4 22:42:30 2007
@@ -57,4 +57,9 @@
     void endSession();
 
     Entity getServerJID();
+
+    /**
+     * signals the underlying transport to handle TLS handshake
+     */
+    void switchToTLS();
 }

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/writer/DenseStanzaLogRenderer.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/writer/DenseStanzaLogRenderer.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/writer/DenseStanzaLogRenderer.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/writer/DenseStanzaLogRenderer.java Tue Dec  4 22:42:30 2007
@@ -17,23 +17,76 @@
 package org.apache.vysper.xmpp.writer;
 
 import org.apache.vysper.xmpp.xmlfragment.XMLElement;
+import org.apache.vysper.xmpp.xmlfragment.XMLSemanticError;
+import org.apache.vysper.xmpp.xmlfragment.XMLText;
+import org.apache.vysper.xmpp.xmlfragment.Attribute;
 
 /**
  * renders only reduced digest stanza information for logging output
  */
 public class DenseStanzaLogRenderer {
     private static final String ELEMENT_SEPARATOR = ".";
+    private static final String ATTR_QUOTE = "'";
+    private static final String EQUALS = "=";
 
     public static String render(XMLElement stanza) {
         StringBuilder stringBuilder = new StringBuilder();
         String outerName = stanza.getName();
         stringBuilder.append(outerName);
         XMLElement firstInnerElement = stanza.getFirstInnerElement();
+
+        if ("message".equals(outerName)) {
+            renderMessage(stringBuilder, stanza, firstInnerElement);
+        } else if ("error".equals(outerName)) {
+            renderError(stringBuilder, stanza, firstInnerElement);
+        } else if ("presence".equals(outerName)) {
+            renderPresence(stringBuilder, stanza, firstInnerElement);
+        }
+        
+        return stringBuilder.toString();
+    }
+
+    private static void renderError(StringBuilder stringBuilder, XMLElement stanza, XMLElement firstInnerElement) {
+        if (firstInnerElement == null) {
+            stringBuilder.append(ELEMENT_SEPARATOR);
+            stringBuilder.append("???");
+            return;
+        }
+        
+        stringBuilder.append(ELEMENT_SEPARATOR);
+        stringBuilder.append(firstInnerElement.getName());
+    }
+
+    private static void renderPresence(StringBuilder stringBuilder, XMLElement stanza, XMLElement firstInnerElement) {
+        renderAttribute(stringBuilder, stanza, "from");
+        renderAttribute(stringBuilder, stanza, "type");
+    }
+
+    private static void renderAttribute(StringBuilder stringBuilder, XMLElement element, String attributeName) {
+        Attribute attribute = element.getAttribute(attributeName);
+        if (attribute != null) {
+            stringBuilder.append(ELEMENT_SEPARATOR);
+            stringBuilder.append(attributeName).append(EQUALS);
+            stringBuilder.append(ATTR_QUOTE).append(attribute.getValue()).append(ATTR_QUOTE);
+        }
+    }
+
+    private static void renderMessage(StringBuilder stringBuilder, XMLElement stanza, XMLElement firstInnerElement) {
         if (firstInnerElement != null) {
             stringBuilder.append(ELEMENT_SEPARATOR);
             stringBuilder.append(firstInnerElement.getName());
+
+            String firstInnerName = firstInnerElement.getName();
+            if ("body".equals(firstInnerName)) {
+                stringBuilder.append(ELEMENT_SEPARATOR);
+                XMLText xmlText = null;
+                try {
+                    xmlText = firstInnerElement.getSingleInnerText();
+                    if (xmlText != null) stringBuilder.append(xmlText.getText());
+                } catch (XMLSemanticError xmlSemanticError) {
+                    stringBuilder.append("???");
+                }
+            } 
         }
-        
-        return stringBuilder.toString();
     }
 }

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLElementVerifier.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLElementVerifier.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLElementVerifier.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/xmlfragment/XMLElementVerifier.java Tue Dec  4 22:42:30 2007
@@ -69,15 +69,32 @@
 
     public boolean namespacePresent(String namespaceURI) {
         for (Attribute attribute : element.getAttributes()) {
-            if ((attribute.getName().equalsIgnoreCase(NamespaceAttribute.XMLNS)
-                 || attribute.getName().startsWith(NamespaceAttribute.XMLNS_COLON))
-                && attribute.getValue().equals(namespaceURI)) {
+            if (isNamespaceAttribute(attribute) && attribute.getValue().equals(namespaceURI)) {
                 return true;
             }
         }
         return false; // not present
     }
 
+    private boolean isNamespaceAttribute(Attribute attribute) {
+        return (attribute.getName().equalsIgnoreCase(NamespaceAttribute.XMLNS)
+                 || attribute.getName().startsWith(NamespaceAttribute.XMLNS_COLON));
+    }
+
+    public String getUniqueXMLNSValue() {
+        Attribute found = null;
+        for (Attribute attribute : element.getAttributes()) {
+            if (isNamespaceAttribute(attribute)) {
+                if (found != null) return null; // not unique
+                else {
+                    found = attribute;
+                }
+            }
+        }
+        if (found == null) return null;
+        return found.getValue();
+    }
+    
     public boolean toAttributeEquals(String toValue) {
         return attributeEquals("to", toValue);
     }

Copied: labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/starttls/handler/StartTLSHandlerTestCase.java (from r542950, labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/IQHandlerTestCase.java)
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/starttls/handler/StartTLSHandlerTestCase.java?p2=labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/starttls/handler/StartTLSHandlerTestCase.java&p1=labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/IQHandlerTestCase.java&r1=542950&r2=601223&rev=601223&view=diff
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/base/handler/IQHandlerTestCase.java (original)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/applicationdomains/starttls/handler/StartTLSHandlerTestCase.java Tue Dec  4 22:42:30 2007
@@ -15,23 +15,21 @@
  * permissions and limitations under the License.                      *
  ***********************************************************************/
 
-package org.apache.vysper.xmpp.applicationdomains.base.handler;
+package org.apache.vysper.xmpp.applicationdomains.starttls.handler;
 
+import junit.framework.TestCase;
 import org.apache.vysper.xmpp.protocol.NamespaceURIs;
 import org.apache.vysper.xmpp.protocol.ResponseStanzaContainer;
 import org.apache.vysper.xmpp.protocol.SessionStateHolder;
+import org.apache.vysper.xmpp.server.SessionState;
 import org.apache.vysper.xmpp.server.TestSessionContext;
-import org.apache.vysper.xmpp.stanza.IQStanza;
-import org.apache.vysper.xmpp.stanza.IQStanzaType;
 import org.apache.vysper.xmpp.stanza.Stanza;
 import org.apache.vysper.xmpp.stanza.StanzaBuilder;
-import org.apache.vysper.xmpp.stanza.XMPPCoreStanzaVerifier;
 import org.apache.vysper.xmpp.xmlfragment.XMLElementVerifier;
-import junit.framework.TestCase;
 
 /**
  */
-public class IQHandlerTestCase extends TestCase {
+public class StartTLSHandlerTestCase extends TestCase {
     private TestSessionContext sessionContext;
 
     private SessionStateHolder sessionStateHolder = new SessionStateHolder();
@@ -41,121 +39,63 @@
         sessionContext = new TestSessionContext(sessionStateHolder);
     }
 
-    public void testMissingToInServerCall() {
-        StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_SERVER);
-        stanzaBuilder.addAttribute("type", "get");
-        // missing stanzaBuilder.addAttribute("to", "test@example.com");
-        stanzaBuilder.addAttribute("id", "anyway");
-        stanzaBuilder.startInnerElement("inner").endInnerElement();
+    public void testAppropriateSessionState() {
+        StanzaBuilder stanzaBuilder = new StanzaBuilder("starttls");
+        stanzaBuilder.addNamespaceAttribute(NamespaceURIs.URN_IETF_PARAMS_XML_NS_XMPP_TLS);
+        Stanza starttlsStanza = stanzaBuilder.getFinalStanza();
 
         TestSessionContext sessionContext = this.sessionContext;
         sessionContext.setServerToServer();
 
-        TestIQHandler iqHandler = new TestIQHandler();
-        ResponseStanzaContainer responseStanzaContainer = iqHandler.execute(stanzaBuilder.getFinalStanza(), sessionContext, null);
-        Stanza responseStanza = responseStanzaContainer.getResponseStanza();
+        sessionContext.setSessionState(SessionState.INITIATED);
+        Stanza responseStanza = executeStartTLSHandler(starttlsStanza, sessionContext);
         XMLElementVerifier verifier = responseStanza.getVerifier();
-        assertTrue("error", verifier.nameEquals("error"));
-    }
-
-    public void testMissingID() {
-        StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
-        stanzaBuilder.addAttribute("type", "get");
-        assertIQError(stanzaBuilder.getFinalStanza());
-    }
-
-    public void testDoNotRespondToErrorWithError() {
-        StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
-        stanzaBuilder.addAttribute("type", "error");
-        Stanza stanza = stanzaBuilder.getFinalStanza(); // this stanza has no ID
-
-        IQHandler iqHandler = new IQHandler();
-        ResponseStanzaContainer responseStanzaContainer = iqHandler.execute(stanza, sessionContext, null);
+        assertTrue("session state to low failure", verifier.nameEquals("failure"));
+        assertFalse("tls init", sessionContext.isSwitchToTLSCalled());
+                  
+        sessionContext.setSessionState(SessionState.ENCRYPTION_STARTED);
+        responseStanza = executeStartTLSHandler(starttlsStanza, sessionContext);
+        verifier = responseStanza.getVerifier();
+        assertTrue("session state too high failure", verifier.nameEquals("failure"));
+        assertFalse("tls init", sessionContext.isSwitchToTLSCalled());
+
+        sessionContext.setSessionState(SessionState.STARTED);
+        responseStanza = executeStartTLSHandler(starttlsStanza, sessionContext);
+        verifier = responseStanza.getVerifier();
+        assertTrue("session state ready", verifier.nameEquals("proceed"));
+        assertEquals("session stat is encryption started", SessionState.ENCRYPTION_STARTED, sessionStateHolder.getState());
+        assertTrue("tls init", sessionContext.isSwitchToTLSCalled());
+    }
+
+    private Stanza executeStartTLSHandler(Stanza starttlsStanza, TestSessionContext sessionContext) {
+        StartTLSHandler startTLSHandler = new StartTLSHandler();
+        ResponseStanzaContainer responseStanzaContainer = startTLSHandler.execute(starttlsStanza, sessionContext, sessionStateHolder);
         Stanza responseStanza = responseStanzaContainer.getResponseStanza();
-        XMLElementVerifier verifier = responseStanza.getVerifier();
-        assertTrue("error", verifier.nameEquals("error")); // response is _not_ IQ stanza
-    }
-
-    private void assertIQError(Stanza stanza) {
-        TestIQHandler iqHandler = new TestIQHandler();
-        ResponseStanzaContainer responseStanzaContainer = iqHandler.execute(stanza, sessionContext, null);
-        Stanza responseStanza = responseStanzaContainer.getResponseStanza();
-        XMLElementVerifier verifier = responseStanza.getVerifier();
-        assertTrue("iq", verifier.nameEquals("iq"));
-        assertTrue("error type", verifier.attributeEquals("type", IQStanzaType.ERROR.value()));
-        assertTrue("iq-error", verifier.subElementPresent("error"));
-    }
-
-    public void testMissingType() {
-        StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
-        stanzaBuilder.addAttribute("id", "1");
-        // missing: stanzaBuilder.addAttribute("type", "get");
-        assertIQError(stanzaBuilder.getFinalStanza());
+        return responseStanza;
     }
+    
+    public void testNamespace() {
+        sessionContext.setSessionState(SessionState.STARTED);
+        
+        StanzaBuilder stanzaBuilder = new StanzaBuilder("starttls");
+        stanzaBuilder.addNamespaceAttribute(NamespaceURIs.HTTP_ETHERX_JABBER_ORG_STREAMS);
+        Stanza wrongNSStanza = stanzaBuilder.getFinalStanza();
+
+        stanzaBuilder = new StanzaBuilder("starttls");
+        stanzaBuilder.addNamespaceAttribute(NamespaceURIs.URN_IETF_PARAMS_XML_NS_XMPP_TLS);
+        Stanza correctNSStanza = stanzaBuilder.getFinalStanza();
 
-    public void testUnsupportedType() {
-        StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
-        stanzaBuilder.addAttribute("id", "1");
-        stanzaBuilder.addAttribute("type", "bogus");
-        assertIQError(stanzaBuilder.getFinalStanza());
-    }
-
-    public void testGetAndSetSubelements() {
-        // get and set must have exactly one subelement
-
-        String type = "get";
-        assertAnySub(type);  // test with zero
-        assertNotTwoSubs(type); // test with 2
-
-        type = "set";
-        assertAnySub(type);
-        assertNotTwoSubs(type);
-    }
-
-    public void testResultSubelements() {
-        // result must have zero or one subelements
-        String type = "result";
-        assertNotTwoSubs(type); // test with two
-    }
-
-    private void assertNotTwoSubs(String type) {
-        StanzaBuilder stanzaTwoSubs = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
-        stanzaTwoSubs.addAttribute("id", "1");
-        stanzaTwoSubs.addAttribute("type", type);
-        stanzaTwoSubs.startInnerElement("firstSub").endInnerElement();
-        stanzaTwoSubs.startInnerElement("secondSub").endInnerElement();
-        assertIQError(stanzaTwoSubs.getFinalStanza());
-    }
-
-    private void assertAnySub(String type) {
-        StanzaBuilder stanzaNoSub = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
-        stanzaNoSub.addAttribute("id", "1");
-        stanzaNoSub.addAttribute("type", type);
-        assertIQError(stanzaNoSub.getFinalStanza());
-    }
+        TestSessionContext sessionContext = this.sessionContext;
 
-    public void testGet() {
-        StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", NamespaceURIs.JABBER_CLIENT);
-        stanzaBuilder.addAttribute("id", "1");
-        stanzaBuilder.addAttribute("type", "get");
-        stanzaBuilder.startInnerElement("getRequest").endInnerElement();
-
-        TestIQHandler iqHandler = new TestIQHandler();
-        ResponseStanzaContainer responseStanzaContainer = iqHandler.execute(stanzaBuilder.getFinalStanza(), sessionContext, null);
-        IQStanza incomingStanza = iqHandler.getIncomingStanza();
-
-        XMPPCoreStanzaVerifier verifier = incomingStanza.getCoreVerifier();
-        assertTrue("iq", verifier.nameEquals("iq"));
-        assertTrue("iq-id", verifier.attributeEquals("id", "1"));
-        assertTrue("iq-type-get", verifier.attributeEquals("type", "get"));
+        Stanza responseStanza = executeStartTLSHandler(wrongNSStanza, sessionContext);
+        XMLElementVerifier verifier = responseStanza.getVerifier();
+        assertTrue("namespace wrong failure", verifier.nameEquals("failure"));
+        assertFalse("tls init", sessionContext.isSwitchToTLSCalled());
 
-        // response is "result"
-        Stanza responseStanza = responseStanzaContainer.getResponseStanza();
-        XMLElementVerifier responseVerifier = responseStanza.getVerifier();
-        assertTrue("iq", responseVerifier.nameEquals("iq"));
-        assertTrue("iq-id", responseVerifier.attributeEquals("id", "1"));
-        assertTrue("iq-type-result", responseVerifier.attributeEquals("type", "result"));
+        responseStanza = executeStartTLSHandler(correctNSStanza, sessionContext);
+        verifier = responseStanza.getVerifier();
+        assertTrue("namespace correct proceed", verifier.nameEquals("proceed"));
+        assertTrue("tls init", sessionContext.isSwitchToTLSCalled());
     }
-
-}
-
+    
+}
\ No newline at end of file

Modified: labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java?rev=601223&r1=601222&r2=601223&view=diff
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java (original)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/server/TestSessionContext.java Tue Dec  4 22:42:30 2007
@@ -26,10 +26,11 @@
 /**
  * makes response available for testing
  */
-public class TestSessionContext extends DefaultSessionContext implements StanzaWriter {
+public class TestSessionContext extends AbstractSessionContext implements StanzaWriter {
 
     private Stanza recordedResponse = null;
     private boolean closed = false;
+    private boolean switchToTLSCalled;
 
     public TestSessionContext(SessionStateHolder sessionStateHolder) {
         this(new DefaultServerRuntimeContext(new EntityImpl(null, "test", null), new RecordingStanzaRelay()), sessionStateHolder);
@@ -61,5 +62,13 @@
 
     public void setSessionState(SessionState sessionState) {
         this.sessionStateHolder.setState(sessionState);
+    }
+
+    public void switchToTLS() {
+        switchToTLSCalled = true;
+    }
+
+    public boolean isSwitchToTLSCalled() {
+        return switchToTLSCalled;
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org


Mime
View raw message