View Javadoc

1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.transport.xmpp;
18  
19  import java.io.IOException;
20  import java.net.URI;
21  import java.util.UUID;
22  
23  import org.springframework.beans.factory.InitializingBean;
24  import org.springframework.util.Assert;
25  import org.springframework.ws.transport.WebServiceConnection;
26  import org.springframework.ws.transport.WebServiceMessageSender;
27  import org.springframework.ws.transport.xmpp.support.XmppTransportUtils;
28  
29  import org.jivesoftware.smack.XMPPConnection;
30  
31  /**
32   * {@link WebServiceMessageSender} implementation that uses XMPP {@link org.jivesoftware.smack.packet.Message}s.
33   * Requires a {@link #setConnection(org.jivesoftware.smack.XMPPConnection) connection}to be set.
34   * <p/>
35   * This message sender supports URI's of the following format: <blockquote> <tt><b>xmpp:</b></tt><i>to</i> </blockquote>
36   * The <i>to</i> represents a Jabber ID.
37   *
38   * @author Gildas Cuisinier
39   * @author Arjen Poutsma
40   * @since 2.0
41   */
42  public class XmppMessageSender implements WebServiceMessageSender, InitializingBean {
43  
44      /** Default timeout for receive operations: -1 indicates a blocking receive without timeout. */
45      public static final long DEFAULT_RECEIVE_TIMEOUT = -1;
46  
47      /** Default encoding used to read from and write to {@link org.jivesoftware.smack.packet.Message} messages. */
48      public static final String DEFAULT_MESSAGE_ENCODING = "UTF-8";
49  
50      private long receiveTimeout = DEFAULT_RECEIVE_TIMEOUT;
51  
52      private String messageEncoding = DEFAULT_MESSAGE_ENCODING;
53  
54      private XMPPConnection connection;
55  
56      /** Sets the {@code XMPPConnection}. Setting this property is required. */
57      public void setConnection(XMPPConnection connection) {
58          this.connection = connection;
59      }
60  
61      /**
62       * Set the timeout to use for receive calls. The default is -1, which means no timeout.
63       *
64       * @see org.jivesoftware.smack.PacketCollector#nextResult(long)
65       */
66      public void setReceiveTimeout(long receiveTimeout) {
67          this.receiveTimeout = receiveTimeout;
68      }
69  
70      /**
71       * Sets the encoding used to read from {@link org.jivesoftware.smack.packet.Message} object. Defaults to
72       * <code>UTF-8</code>.
73       */
74      public void setMessageEncoding(String messageEncoding) {
75          this.messageEncoding = messageEncoding;
76      }
77  
78      public void afterPropertiesSet() throws Exception {
79          Assert.notNull(connection, "'connection' is required");
80      }
81  
82      public WebServiceConnection createConnection(URI uri) throws IOException {
83          String to = XmppTransportUtils.getTo(uri);
84          String thread = createThread();
85          XmppSenderConnection connection = new XmppSenderConnection(this.connection, to, thread);
86          connection.setReceiveTimeout(receiveTimeout);
87          connection.setMessageEncoding(messageEncoding);
88          return connection;
89      }
90  
91      public boolean supports(URI uri) {
92          return uri.getScheme().equals(XmppTransportConstants.XMPP_URI_SCHEME);
93      }
94  
95      protected String createThread() {
96          return UUID.randomUUID().toString();
97      }
98  }