You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by js...@apache.org on 2006/10/10 16:41:15 UTC

svn commit: r454756 - in /incubator/servicemix/trunk/servicemix-xmpp: ./ src/main/java/org/apache/servicemix/jabber/ src/main/java/org/apache/servicemix/xmpp/ src/test/java/org/apache/servicemix/jabber/ src/test/java/org/apache/servicemix/xmpp/ src/tes...

Author: jstrachan
Date: Tue Oct 10 07:41:14 2006
New Revision: 454756

URL: http://svn.apache.org/viewvc?view=rev&rev=454756
Log:
refactored from jabber->xmpp

Added:
    incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/
    incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/GroupChatEndpoint.java
    incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/PrivateChatEndpoint.java
    incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPComponent.java
    incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPEndpoint.java
    incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPListenerException.java
    incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPMarshaler.java
    incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/package.html
    incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/
    incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/DynamicEndpointTest.java
    incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/SpringComponentTest.java
    incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/UriConfigurationTest.java
Removed:
    incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/jabber/
    incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/jabber/
    incubator/servicemix/trunk/servicemix-xmpp/src/test/resources/org/apache/servicemix/
Modified:
    incubator/servicemix/trunk/servicemix-xmpp/pom.xml
    incubator/servicemix/trunk/servicemix-xmpp/src/test/resources/spring-no-endpoints.xml
    incubator/servicemix/trunk/servicemix-xmpp/src/test/resources/spring.xml

Modified: incubator/servicemix/trunk/servicemix-xmpp/pom.xml
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/pom.xml?view=diff&rev=454756&r1=454755&r2=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/pom.xml (original)
+++ incubator/servicemix/trunk/servicemix-xmpp/pom.xml Tue Oct 10 07:41:14 2006
@@ -97,8 +97,9 @@
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
           <excludes>
-            <!--  TODO - need an embedded Jabber server to be able to properly test -->
-            <exclude>**/*Test.*</exclude>
+            <!--  These integration tests require a Jabber server -->
+            <exclude>**/DynamicEndpointTest.*</exclude>
+            <exclude>**/SpringComponentTest.*</exclude>
           </excludes>
         </configuration>
       </plugin>

Added: incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/GroupChatEndpoint.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/GroupChatEndpoint.java?view=auto&rev=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/GroupChatEndpoint.java (added)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/GroupChatEndpoint.java Tue Oct 10 07:41:14 2006
@@ -0,0 +1,97 @@
+/*
+ * 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.servicemix.xmpp;
+
+import org.jivesoftware.smack.GroupChat;
+import org.jivesoftware.smack.packet.Message;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.servicedesc.ServiceEndpoint;
+
+/**
+ * Represents a group chat endpoint
+ *
+ * @version $Revision: $
+ * @org.apache.xbean.XBean element="groupChatEndpoint"
+ */
+public class GroupChatEndpoint extends XMPPEndpoint {
+
+    private GroupChat chat;
+    private String room;
+
+
+    public GroupChatEndpoint() {
+    }
+
+    public GroupChatEndpoint(XMPPComponent component, ServiceEndpoint serviceEndpoint) {
+        super(component, serviceEndpoint);
+    }
+
+    public GroupChatEndpoint(XMPPComponent component, ServiceEndpoint serviceEndpoint, String room) {
+        super(component, serviceEndpoint);
+        this.room = room;
+    }
+
+    public void start() throws Exception {
+        super.start();
+        if (chat == null) {
+            if (room == null) {
+                throw new IllegalArgumentException("No room property specified");
+            }
+            chat = getConnection().createGroupChat(room);
+        }
+    }
+
+    public void stop() throws Exception {
+        if (chat != null) {
+            chat.leave();
+            chat = null;
+        }
+        super.stop();
+    }
+
+    // Properties
+    //-------------------------------------------------------------------------
+    public GroupChat getChat() {
+        return chat;
+    }
+
+    public void setChat(GroupChat chat) {
+        this.chat = chat;
+    }
+
+    public String getRoom() {
+        return room;
+    }
+
+    public void setRoom(String room) {
+        this.room = room;
+    }
+
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+    protected void processInOnly(MessageExchange exchange, NormalizedMessage normalizedMessage) throws Exception {
+        Message message = chat.createMessage();
+        message.setTo(room);
+        message.setFrom(getUser());
+        getMarshaler().fromNMS(message, exchange, normalizedMessage);
+        chat.sendMessage(message);
+        done(exchange);
+    }
+}

Added: incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/PrivateChatEndpoint.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/PrivateChatEndpoint.java?view=auto&rev=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/PrivateChatEndpoint.java (added)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/PrivateChatEndpoint.java Tue Oct 10 07:41:14 2006
@@ -0,0 +1,105 @@
+/*
+ * 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.servicemix.xmpp;
+
+import org.jivesoftware.smack.Chat;
+import org.jivesoftware.smack.packet.Message;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.servicedesc.ServiceEndpoint;
+import java.net.URI;
+
+/**
+ * Represents an endpoint for chatting to a single individual
+ *
+ * @version $Revision: $
+ * @org.apache.xbean.XBean element="privateChatEndpoint"
+ */
+public class PrivateChatEndpoint extends XMPPEndpoint {
+
+    private Chat chat;
+    private String participant;
+
+    public PrivateChatEndpoint() {
+    }
+
+    public PrivateChatEndpoint(XMPPComponent component, ServiceEndpoint serviceEndpoint) {
+        super(component, serviceEndpoint);
+    }
+
+    public void start() throws Exception {
+        super.start();
+        if (chat == null) {
+            String participant = getParticipant();
+            if (participant == null) {
+                throw new IllegalArgumentException("No participant property specified");
+            }
+            chat = getConnection().createChat(participant);
+        }
+    }
+
+    public void stop() throws Exception {
+        chat = null;
+        super.stop();
+    }
+
+
+    // Properties
+    //-------------------------------------------------------------------------
+    public Chat getChat() {
+        return chat;
+    }
+
+    public void setChat(Chat chat) {
+        this.chat = chat;
+    }
+
+    public String getParticipant() {
+        return participant;
+    }
+
+    public void setParticipant(String participant) {
+        this.participant = participant;
+    }
+
+
+    public void setUri(URI uri) {
+        super.setUri(uri);
+        String path = uri.getPath();
+        if (path != null) {
+            // lets strip the leading slash to make an XMPP resource
+            if (path.startsWith("/")) {
+                path = path.substring(1);
+            }
+            setParticipant(path);
+        }
+    }
+
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+    protected void processInOnly(MessageExchange exchange, NormalizedMessage normalizedMessage) throws Exception {
+        Message message = chat.createMessage();
+        getMarshaler().fromNMS(message, exchange, normalizedMessage);
+        message.setTo(getParticipant());
+        message.setFrom(getUser());
+        message.setThread(exchange.getExchangeId());
+        message.setType(Message.Type.NORMAL);
+        chat.sendMessage(message);
+    }
+}

Added: incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPComponent.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPComponent.java?view=auto&rev=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPComponent.java (added)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPComponent.java Tue Oct 10 07:41:14 2006
@@ -0,0 +1,140 @@
+/*
+ * 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.servicemix.xmpp;
+
+import org.apache.activemq.util.URISupport;
+import org.apache.servicemix.common.DefaultComponent;
+import org.apache.servicemix.common.Endpoint;
+import org.apache.servicemix.common.ResolvedEndpoint;
+import org.apache.servicemix.jbi.util.IntrospectionSupport;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanFactoryAware;
+import org.w3c.dom.DocumentFragment;
+
+import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.xml.namespace.QName;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * A JBI component for binding POJOs to the JBI bus which work directly off of the JBI messages
+ * without requiring any SOAP Processing. If you require support for SOAP, JAX-WS, JSR-181 then you
+ * should use the servicemix-jsr181 module instead.
+ *
+ * @version $Revision: $
+ * @org.apache.xbean.XBean element="component" description="POJO Component"
+ */
+public class XMPPComponent extends DefaultComponent implements BeanFactoryAware {
+
+    public final static String EPR_URI = "urn:servicemix:xmpp";
+    public final static QName EPR_SERVICE = new QName(EPR_URI, "XMPPComponent");
+    public final static String EPR_NAME = "epr";
+
+    private XMPPEndpoint[] endpoints;
+    private BeanFactory beanFactory;
+    private String user;
+    private String password;
+
+
+    public XMPPEndpoint[] getEndpoints() {
+        return endpoints;
+    }
+
+    public void setEndpoints(XMPPEndpoint[] endpoints) {
+        this.endpoints = endpoints;
+    }
+
+    public String getUser() {
+        return user;
+    }
+
+    public void setUser(String user) {
+        this.user = user;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public BeanFactory getBeanFactory() {
+        return beanFactory;
+    }
+
+    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
+        this.beanFactory = beanFactory;
+    }
+
+    public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) {
+        return ResolvedEndpoint.resolveEndpoint(epr, EPR_URI, EPR_NAME, EPR_SERVICE, "xmpp:");
+    }
+
+    protected List getConfiguredEndpoints() {
+        return asList(getEndpoints());
+    }
+
+    protected Class[] getEndpointClasses() {
+        return new Class[]{XMPPEndpoint.class};
+    }
+
+    protected QName getEPRServiceName() {
+        return EPR_SERVICE;
+    }
+
+    protected Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception {
+        XMPPEndpoint endpoint = createEndpoint(ep);
+        endpoint.activate();
+        return endpoint;
+    }
+
+    /**
+     * A factory method for creating endpoints from a service endpoint
+     * which is public so that it can be easily unit tested
+     */
+    public XMPPEndpoint createEndpoint(ServiceEndpoint ep) throws URISyntaxException {
+        URI uri = new URI(ep.getEndpointName());
+        Map map = URISupport.parseQuery(uri.getQuery());
+
+        XMPPEndpoint endpoint = null;
+
+        // TODO how do we decide whether to use group or private chat from the URI in a nicer way?
+        String room = (String) map.get("room");
+        if (room != null) {
+            endpoint = new GroupChatEndpoint(this, ep, room);
+        }
+        else {
+            endpoint = new PrivateChatEndpoint(this, ep);
+        }
+
+        IntrospectionSupport.setProperties(endpoint, map);
+        IntrospectionSupport.setProperties(endpoint.getMarshaler(), map, "marshal.");
+
+        // TODO
+        //endpoint.setRole(MessageExchange.Role.PROVIDER);
+
+        endpoint.setUri(uri);
+
+        return endpoint;
+    }
+
+}

Added: incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPEndpoint.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPEndpoint.java?view=auto&rev=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPEndpoint.java (added)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPEndpoint.java Tue Oct 10 07:41:14 2006
@@ -0,0 +1,255 @@
+/*
+ * 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.servicemix.xmpp;
+
+import org.apache.servicemix.common.ProviderEndpoint;
+import org.apache.servicemix.common.ServiceUnit;
+import org.jivesoftware.smack.AccountManager;
+import org.jivesoftware.smack.*;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.filter.PacketFilter;
+import org.jivesoftware.smack.packet.Packet;
+import org.jivesoftware.smack.packet.Presence;
+import org.jivesoftware.smack.packet.RosterPacket;
+import org.jivesoftware.smack.packet.Message;
+
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.servicedesc.ServiceEndpoint;
+import java.net.URI;
+import java.util.Iterator;
+
+/**
+ * Represents a base XMPP endpoint
+ *
+ * @version $Revision: $
+ */
+public abstract class XMPPEndpoint extends ProviderEndpoint implements PacketListener {
+    private XMPPMarshaler marshaler = new XMPPMarshaler();
+    private XMPPConnection connection;
+    private String host;
+    private int port;
+    private String user;
+    private String password;
+    private String resource = "ServiceMix";
+    private boolean login = true;
+    private PacketFilter filter;
+    private boolean createAccount;
+
+    public XMPPEndpoint() {
+    }
+
+    public XMPPEndpoint(XMPPComponent component, ServiceEndpoint serviceEndpoint) {
+        super(component, serviceEndpoint);
+        init(component);
+    }
+
+    public void stop() throws Exception {
+        super.stop();
+        if (connection != null) {
+            connection.close();
+            connection = null;
+        }
+    }
+
+    public void start() throws Exception {
+        super.start();
+        if (connection == null) {
+            if (port > 0) {
+                connection = new XMPPConnection(host, port);
+            }
+            else {
+                connection = new XMPPConnection(host);
+            }
+        }
+        getConnection().addPacketListener(this, filter);
+        if (login && !connection.isAuthenticated()) {
+            if (user != null) {
+                logger.info("Logging in to Jabber as user: " + user + " on connection: " + connection);
+                if (password == null) {
+                    logger.warn("No password configured for user: " + user);
+                }
+
+                if (createAccount) {
+                    AccountManager accountManager = new AccountManager(connection);
+                    accountManager.createAccount(user, password);
+                }
+                if (resource != null) {
+                    connection.login(user, password, resource);
+                }
+                else {
+                    connection.login(user, password);
+                }
+            }
+            else {
+                logger.info("Logging in anonymously to Jabber on connection: " + connection);
+                connection.loginAnonymously();
+            }
+
+            // now lets send a presence
+
+            connection.sendPacket(new Presence(Presence.Type.AVAILABLE));
+        }
+    }
+
+
+    public void setServiceUnit(ServiceUnit serviceUnit) {
+        super.setServiceUnit(serviceUnit);
+        init((XMPPComponent) serviceUnit.getComponent());
+    }
+
+    public void processPacket(Packet packet) {
+        try {
+            System.out.println("Received packet: " + packet);
+            Iterator iter = packet.getPropertyNames();
+            while (iter.hasNext()) {
+                String property = (String) iter.next();
+                System.out.println("Packet header: " + property + " value: " + packet.getProperty(property));
+            }
+            if (packet instanceof Message) {
+                Message message = (Message) packet;
+                System.out.println("Received message: " + message + " with " + message.getBody());
+
+            }
+            else if (packet instanceof RosterPacket) {
+                RosterPacket rosterPacket = (RosterPacket) packet;
+                System.out.println("Roster packet with : " + rosterPacket.getRosterItemCount());
+                Iterator rosterItems = rosterPacket.getRosterItems();
+                while (rosterItems.hasNext()) {
+                    Object item = rosterItems.next();
+                    System.out.println("Roster item: " + item);
+                }
+
+            }
+            InOnly exchange = getExchangeFactory().createInOnlyExchange();
+            NormalizedMessage in = exchange.createMessage();
+            exchange.setInMessage(in);
+            marshaler.toNMS(in, packet);
+            System.out.println("Exchange: " + exchange);
+            //send(exchange);
+        }
+        catch (MessagingException e) {
+            throw new XMPPListenerException(e, packet);
+        }
+    }
+
+    // Properties
+    //-------------------------------------------------------------------------
+
+    /**
+     * Configures the endpoint from a URI
+     */
+    public void setUri(URI uri) {
+        setHost(uri.getHost());
+        setPort(uri.getPort());
+        if (uri.getUserInfo() != null) {
+            setUser(uri.getUserInfo());
+        }
+    }
+
+    public XMPPConnection getConnection() {
+        return connection;
+    }
+
+    public void setConnection(XMPPConnection connection) {
+        this.connection = connection;
+    }
+
+    public String getHost() {
+        return host;
+    }
+
+    public void setHost(String host) {
+        this.host = host;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public void setPort(int port) {
+        this.port = port;
+    }
+
+    public XMPPMarshaler getMarshaler() {
+        return marshaler;
+    }
+
+    public void setMarshaler(XMPPMarshaler marshaler) {
+        this.marshaler = marshaler;
+    }
+
+    public String getUser() {
+        return user;
+    }
+
+    public void setUser(String user) {
+        this.user = user;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public String getResource() {
+        return resource;
+    }
+
+    public void setResource(String resource) {
+        this.resource = resource;
+    }
+
+    public boolean isLogin() {
+        return login;
+    }
+
+    public void setLogin(boolean login) {
+        this.login = login;
+    }
+
+    public boolean isCreateAccount() {
+        return createAccount;
+    }
+
+    public void setCreateAccount(boolean createAccount) {
+        this.createAccount = createAccount;
+    }
+
+    public PacketFilter getFilter() {
+        return filter;
+    }
+
+    public void setFilter(PacketFilter filter) {
+        this.filter = filter;
+    }
+
+
+    protected void init(XMPPComponent component) {
+        if (user == null) {
+            user = component.getUser();
+        }
+        if (password == null) {
+            password = component.getPassword();
+        }
+    }
+
+}

Added: incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPListenerException.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPListenerException.java?view=auto&rev=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPListenerException.java (added)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPListenerException.java Tue Oct 10 07:41:14 2006
@@ -0,0 +1,37 @@
+/*
+ * 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.servicemix.xmpp;
+
+import org.jivesoftware.smack.packet.Packet;
+
+/**
+ * A runtime exception thrown when processing inbound XMPP packets.
+ *
+ * @version $Revision: 426415 $
+ */
+public class XMPPListenerException extends RuntimeException {
+    private Packet packet;
+
+    public XMPPListenerException(Exception cause, Packet packet) {
+        super(cause.getMessage() + ". Was processing packet: " + packet, cause);
+        this.packet = packet;
+    }
+
+    public Packet getPacket() {
+        return packet;
+    }
+}

Added: incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPMarshaler.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPMarshaler.java?view=auto&rev=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPMarshaler.java (added)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/XMPPMarshaler.java Tue Oct 10 07:41:14 2006
@@ -0,0 +1,194 @@
+/*
+ * 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.servicemix.xmpp;
+
+import org.apache.servicemix.jbi.jaxp.SourceMarshaler;
+import org.jivesoftware.smack.packet.Message;
+import org.jivesoftware.smack.packet.Packet;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.namespace.QName;
+import java.util.Date;
+import java.util.Iterator;
+
+/**
+ * Marshals Jabber messages into and out of NMS messages
+ *
+ * @version $Revision: 429277 $
+ */
+public class XMPPMarshaler {
+    private SourceMarshaler sourceMarshaler;
+    private String messageBodyOpenTag = "<message>";
+    private String messageBodyCloseTag = "</message>";
+
+    public XMPPMarshaler() {
+        this(new SourceMarshaler());
+    }
+
+    public XMPPMarshaler(SourceMarshaler sourceMarshaler) {
+        this.sourceMarshaler = sourceMarshaler;
+    }
+
+    /**
+     * Marshals the Jabber message into an NMS message
+     *
+     * @throws javax.jbi.messaging.MessagingException
+     *
+     */
+    public void toNMS(NormalizedMessage normalizedMessage, Packet packet) throws MessagingException {
+        addNmsProperties(normalizedMessage, packet);
+        if (packet instanceof Message) {
+            Message message = (Message) packet;
+            String text = message.getBody();
+            if (text != null) {
+                Source source = sourceMarshaler.asSource(messageBodyOpenTag + text + messageBodyCloseTag);
+                normalizedMessage.setContent(source);
+            }
+        }
+
+        // lets add the packet to the NMS
+        normalizedMessage.setProperty("org.apache.servicemix.xmpp.packet", packet);
+    }
+
+    /**
+     * Marshals from the Jabber message to the normalized message
+     *
+     * @param message
+     * @param exchange
+     * @param normalizedMessage @throws javax.xml.transform.TransformerException
+     */
+    public void fromNMS(Message message, MessageExchange exchange, NormalizedMessage normalizedMessage) throws TransformerException {
+        // lets create a text message
+        String xml = messageAsString(normalizedMessage);
+        message.setBody(xml);
+        addJabberProperties(message, exchange, normalizedMessage);
+    }
+
+    // Properties
+    //-------------------------------------------------------------------------
+    /**
+     * @deprecated use getSourceMarshaler instead
+     */
+    public SourceMarshaler getSourceMarshaller() {
+        return sourceMarshaler;
+    }
+
+    /**
+     * @deprecated use setSourceMashaler instead
+     */
+    public void setSourceMarshaller(SourceMarshaler sourceMarshaler) {
+        this.sourceMarshaler = sourceMarshaler;
+    }
+
+    /**
+     * @return the sourceMarshaler
+     */
+    public SourceMarshaler getSourceMarshaler() {
+        return sourceMarshaler;
+    }
+
+    /**
+     * @param sourceMarshaler the sourceMarshaler to set
+     */
+    public void setSourceMarshaler(SourceMarshaler sourceMarshaler) {
+        this.sourceMarshaler = sourceMarshaler;
+    }
+
+    public String getMessageBodyOpenTag() {
+        return messageBodyOpenTag;
+    }
+
+    /**
+     * Sets the XML open tag used to wrap inbound Jabber text messages
+     */
+    public void setMessageBodyOpenTag(String messageBodyOpenTag) {
+        this.messageBodyOpenTag = messageBodyOpenTag;
+    }
+
+    public String getMessageBodyCloseTag() {
+        return messageBodyCloseTag;
+    }
+
+    /**
+     * Sets the XML close tag used to wrap inbound Jabber text messages
+     */
+    public void setMessageBodyCloseTag(String messageBodyCloseTag) {
+        this.messageBodyCloseTag = messageBodyCloseTag;
+    }
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+
+    /**
+     * Converts the inbound message to a String that can be sent
+     */
+    protected String messageAsString(NormalizedMessage normalizedMessage) throws TransformerException {
+        return sourceMarshaler.asString(normalizedMessage.getContent());
+    }
+
+    /**
+     * Appends properties on the NMS to the JMS Message
+     */
+    protected void addJabberProperties(Message message, MessageExchange exchange, NormalizedMessage normalizedMessage) {
+        for (Iterator iter = normalizedMessage.getPropertyNames().iterator(); iter.hasNext();) {
+            String name = (String) iter.next();
+            Object value = normalizedMessage.getProperty(name);
+            if (shouldIncludeHeader(normalizedMessage, name, value)) {
+                message.setProperty(name, value);
+            }
+        }
+        message.setProperty("exchangeId", exchange.getExchangeId());
+        setProperty(message, "interface", exchange.getInterfaceName());
+        setProperty(message, "operation", exchange.getOperation());
+        setProperty(message, "service", exchange.getService());
+        //message.setProperty("pattern", exchange.getPattern());
+        //message.setProperty("role", exchange.getRole());
+        ServiceEndpoint endpoint = exchange.getEndpoint();
+        if (endpoint != null) {
+            message.setProperty("endpointName", endpoint.getEndpointName());
+        }
+    }
+
+    protected void setProperty(Message message, String name, QName qName) {
+        if (qName != null) {
+            message.setProperty(name, qName.toString());
+        }
+    }
+
+    protected void addNmsProperties(NormalizedMessage normalizedMessage, Packet message) {
+        Iterator iter = message.getPropertyNames();
+        while (iter.hasNext()) {
+            String name = (String) iter.next();
+            Object value = message.getProperty(name);
+            normalizedMessage.setProperty(name, value);
+        }
+    }
+
+    /**
+     * Decides whether or not the given header should be included in the JMS message.
+     * By default this includes all suitable typed values
+     */
+    protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String name, Object value) {
+        return value instanceof String || value instanceof Number || value instanceof Date;
+    }
+
+}

Added: incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/package.html
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/package.html?view=auto&rev=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/package.html (added)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/main/java/org/apache/servicemix/xmpp/package.html Tue Oct 10 07:41:14 2006
@@ -0,0 +1,28 @@
+<!--
+
+    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.
+
+-->
+<html>
+<head>
+</head>
+<body>
+
+Component for <a href="http://jabber.org/">Jabber</a> using the <a href="http://xmlpp.org/">XMPP</a> protocol via the
+<a href="http://www.jivesoftware.org/smack/">Smack</a> Java client
+
+</body>
+</html>

Added: incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/DynamicEndpointTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/DynamicEndpointTest.java?view=auto&rev=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/DynamicEndpointTest.java (added)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/DynamicEndpointTest.java Tue Oct 10 07:41:14 2006
@@ -0,0 +1,66 @@
+/*
+ * 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.servicemix.xmpp;
+
+import org.apache.servicemix.client.DefaultServiceMixClient;
+import org.apache.servicemix.client.ServiceMixClient;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.tck.SpringTestSupport;
+import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.servicedesc.ServiceEndpoint;
+
+public class DynamicEndpointTest extends SpringTestSupport {
+
+    public void testSendingToDynamicEndpoint() throws Exception {
+        ServiceMixClient client = new DefaultServiceMixClient(jbi);
+
+        ServiceEndpoint se = client.resolveEndpointReference("xmpp://im.google.com/room");
+        assertNotNull("We should find a service endpoint!", se);
+
+        InOnly exchange = client.createInOnlyExchange();
+        exchange.setEndpoint(se);
+        exchange.getInMessage().setContent(new StringSource("<hello>world</hello>"));
+        client.sendSync(exchange);
+
+        assertExchangeWorked(exchange);
+    }
+
+    protected void assertExchangeWorked(MessageExchange me) throws Exception {
+        if (me.getStatus() == ExchangeStatus.ERROR) {
+            if (me.getError() != null) {
+                throw me.getError();
+            }
+            else {
+                fail("Received ERROR status");
+            }
+        }
+        else if (me.getFault() != null) {
+            fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
+        }
+    }
+
+    protected AbstractXmlApplicationContext createBeanFactory() {
+        return new ClassPathXmlApplicationContext("spring-no-endpoints.xml");
+    }
+
+}

Added: incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/SpringComponentTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/SpringComponentTest.java?view=auto&rev=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/SpringComponentTest.java (added)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/SpringComponentTest.java Tue Oct 10 07:41:14 2006
@@ -0,0 +1,95 @@
+/*
+ * 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.servicemix.xmpp;
+
+import org.apache.servicemix.client.DefaultServiceMixClient;
+import org.apache.servicemix.client.ServiceMixClient;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.tck.SpringTestSupport;
+import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.namespace.QName;
+import java.net.InetAddress;
+
+public class SpringComponentTest extends SpringTestSupport {
+
+    public static void main(String[] args) {
+        try {
+            SpringComponentTest test = new SpringComponentTest();
+            test.setUp();
+            test.testSendingToStaticEndpoint();
+
+            System.out.println();
+            System.out.println("Waiting for incoming messages");
+            System.out.println("Press any key to terminate");
+            System.out.println();
+
+            System.in.read();
+
+            test.tearDown();
+        }
+        catch (Exception e) {
+            System.err.println("Caught: " + e);
+            e.printStackTrace();
+        }
+
+    }
+    public void testSendingToStaticEndpoint() throws Exception {
+        ServiceMixClient client = new DefaultServiceMixClient(jbi);
+        InOnly me = client.createInOnlyExchange();
+        me.setService(new QName("urn:test", "service"));
+        NormalizedMessage message = me.getInMessage();
+
+        message.setProperty("name", "cheese");
+        message.setContent(new StringSource("<hello>world</hello>"));
+
+        client.sendSync(me);
+        assertExchangeWorked(me);
+    }
+
+
+    protected void setUp() throws Exception {
+        String host = InetAddress.getLocalHost().getHostName();
+        System.setProperty("host", host);
+        super.setUp();
+    }
+
+    protected void assertExchangeWorked(MessageExchange me) throws Exception {
+        if (me.getStatus() == ExchangeStatus.ERROR) {
+            if (me.getError() != null) {
+                throw me.getError();
+            }
+            else {
+                fail("Received ERROR status");
+            }
+        }
+        else if (me.getFault() != null) {
+            fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
+        }
+    }
+
+    protected AbstractXmlApplicationContext createBeanFactory() {
+        return new ClassPathXmlApplicationContext("spring.xml");
+    }
+
+}

Added: incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/UriConfigurationTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/UriConfigurationTest.java?view=auto&rev=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/UriConfigurationTest.java (added)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/test/java/org/apache/servicemix/xmpp/UriConfigurationTest.java Tue Oct 10 07:41:14 2006
@@ -0,0 +1,42 @@
+/*
+ * 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.servicemix.xmpp;
+
+import junit.framework.TestCase;
+import org.apache.servicemix.common.ResolvedEndpoint;
+
+import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.xml.namespace.QName;
+
+/**
+ * @version $Revision: $
+ */
+public class UriConfigurationTest extends TestCase {
+
+
+    public void testUriConfiguration() throws Exception {
+        ServiceEndpoint serviceEndpoint = new ResolvedEndpoint("xmpp://servicemix-user@localhost/test-user@localhost", new QName("test"));
+
+        XMPPComponent component = new XMPPComponent();
+        PrivateChatEndpoint endpoint = (PrivateChatEndpoint) component.createEndpoint(serviceEndpoint);
+        assertNotNull("Should have created an endpoint", endpoint);
+
+        assertEquals("localhost", endpoint.getHost());
+        assertEquals("servicemix-user", endpoint.getUser());
+        assertEquals("test-user@localhost", endpoint.getParticipant());
+    }
+}

Modified: incubator/servicemix/trunk/servicemix-xmpp/src/test/resources/spring-no-endpoints.xml
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/test/resources/spring-no-endpoints.xml?view=diff&rev=454756&r1=454755&r2=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/test/resources/spring-no-endpoints.xml (original)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/test/resources/spring-no-endpoints.xml Tue Oct 10 07:41:14 2006
@@ -18,7 +18,7 @@
 
 -->
 <beans xmlns:sm="http://servicemix.apache.org/config/1.0"
-       xmlns:jabber="http://servicemix.apache.org/jabber/1.0"
+       xmlns:xmpp="http://servicemix.apache.org/xmpp/1.0"
        xmlns:test="urn:test">
 
   <sm:container id="jbi" embedded="true" createMBeanServer="false">
@@ -27,7 +27,7 @@
 
       <sm:activationSpec>
         <sm:component>
-          <jabber:component/>
+          <xmpp:component/>
         </sm:component>
       </sm:activationSpec>
 

Modified: incubator/servicemix/trunk/servicemix-xmpp/src/test/resources/spring.xml
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-xmpp/src/test/resources/spring.xml?view=diff&rev=454756&r1=454755&r2=454756
==============================================================================
--- incubator/servicemix/trunk/servicemix-xmpp/src/test/resources/spring.xml (original)
+++ incubator/servicemix/trunk/servicemix-xmpp/src/test/resources/spring.xml Tue Oct 10 07:41:14 2006
@@ -18,7 +18,7 @@
 
 -->
 <beans xmlns:sm="http://servicemix.apache.org/config/1.0"
-       xmlns:jabber="http://servicemix.apache.org/jabber/1.0"
+       xmlns:xmpp="http://servicemix.apache.org/xmpp/1.0"
        xmlns:test="urn:test">
   
   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
@@ -30,19 +30,19 @@
       <sm:activationSpec>
         <sm:component>
 
-          <jabber:component user="servicemix-user" password="servicemix-pwd">
+          <xmpp:component user="servicemix-user" password="servicemix-pwd">
 
-            <jabber:endpoints>
+            <xmpp:endpoints>
 
-              <jabber:privateChatEndpoint service="test:service" endpoint="endpoint"
-                                          uri="jabber://${host}/test-user@${host}"/>
+              <xmpp:privateChatEndpoint service="test:service" endpoint="endpoint"
+                                          uri="xmpp://${host}/test-user@${host}"/>
 
 
-              <!--<jabber:groupChatEndpoint service="test:groupChat" endpoint="endpoint2"-->
-                                        <!--uri="jabber://${host}" room="conference"/>-->
+              <!--<xmpp:groupChatEndpoint service="test:groupChat" endpoint="endpoint2"-->
+                                        <!--uri="xmpp://${host}" room="conference"/>-->
 
-            </jabber:endpoints>
-          </jabber:component>
+            </xmpp:endpoints>
+          </xmpp:component>
         </sm:component>
       </sm:activationSpec>