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.io.InputStream;
21  import java.io.OutputStream;
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  import java.util.Iterator;
25  
26  import org.springframework.util.Assert;
27  import org.springframework.ws.WebServiceMessage;
28  import org.springframework.ws.transport.AbstractSenderConnection;
29  import org.springframework.ws.transport.xmpp.support.XmppTransportUtils;
30  
31  import org.jivesoftware.smack.PacketCollector;
32  import org.jivesoftware.smack.XMPPConnection;
33  import org.jivesoftware.smack.filter.AndFilter;
34  import org.jivesoftware.smack.filter.PacketFilter;
35  import org.jivesoftware.smack.filter.PacketTypeFilter;
36  import org.jivesoftware.smack.filter.ThreadFilter;
37  import org.jivesoftware.smack.packet.Message;
38  import org.jivesoftware.smack.packet.Packet;
39  
40  /**
41   * Implementation of {@link org.springframework.ws.transport.WebServiceConnection} that is used for client-side XMPP
42   * access. Exposes a {@link Message} request and response message.
43   *
44   * @author Gildas Cuisinier
45   * @author Arjen Poutsma
46   * @since 2.0
47   */
48  public class XmppSenderConnection extends AbstractSenderConnection {
49  
50      private final Message requestMessage;
51  
52      private final XMPPConnection connection;
53  
54      private Message responseMessage;
55  
56      private String messageEncoding;
57  
58      private long receiveTimeout;
59  
60      protected XmppSenderConnection(XMPPConnection connection, String to, String thread) {
61          Assert.notNull(connection, "'connection' must not be null");
62          Assert.hasLength(to, "'to' must not be empty");
63          Assert.hasLength(thread, "'thread' must not be empty");
64          this.connection = connection;
65          this.requestMessage = new Message(to, Message.Type.chat);
66          this.requestMessage.setThread(thread);
67      }
68  
69      /** Returns the request message for this connection. */
70      public Message getRequestMessage() {
71          return requestMessage;
72      }
73  
74      /** Returns the response message, if any, for this connection. */
75      public Message getResponseMessage() {
76          return responseMessage;
77      }
78  
79      /*
80       * Package-friendly setters
81       */
82  
83      void setMessageEncoding(String messageEncoding) {
84          this.messageEncoding = messageEncoding;
85      }
86  
87      void setReceiveTimeout(long receiveTimeout) {
88          this.receiveTimeout = receiveTimeout;
89      }
90  
91      /*
92      * URI
93      */
94  
95      public URI getUri() throws URISyntaxException {
96          return XmppTransportUtils.toUri(requestMessage);
97      }
98  
99      /*
100      * Errors
101      */
102 
103     public boolean hasError() {
104         return XmppTransportUtils.hasError(responseMessage);
105     }
106 
107     public String getErrorMessage() {
108         return XmppTransportUtils.getErrorMessage(responseMessage);
109     }
110 
111     /*
112      * Sending
113      */
114 
115     @Override
116     protected void addRequestHeader(String name, String value) {
117         XmppTransportUtils.addHeader(requestMessage, name, value);
118     }
119 
120     @Override
121     protected OutputStream getRequestOutputStream() throws IOException {
122         return new MessageOutputStream(requestMessage, messageEncoding);
123     }
124 
125     @Override
126     protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
127         requestMessage.setFrom(connection.getUser());
128         connection.sendPacket(requestMessage);
129     }
130 
131     /*
132      * Receiving
133      */
134 
135     @Override
136     protected void onReceiveBeforeRead() throws IOException {
137         PacketFilter packetFilter = createPacketFilter();
138 
139         PacketCollector collector = connection.createPacketCollector(packetFilter);
140         Packet packet = receiveTimeout >= 0 ? collector.nextResult(receiveTimeout) : collector.nextResult();
141         if (packet instanceof Message) {
142             responseMessage = (Message) packet;
143         }
144         else if (packet != null) {
145             throw new IllegalArgumentException(
146                     "Wrong packet type: [" + packet.getClass() + "]. Only Messages can be handled.");
147         }
148     }
149 
150     private PacketFilter createPacketFilter() {
151         AndFilter andFilter = new AndFilter();
152         andFilter.addFilter(new PacketTypeFilter(Message.class));
153         andFilter.addFilter(new ThreadFilter(requestMessage.getThread()));
154         return andFilter;
155     }
156 
157     @Override
158     protected boolean hasResponse() throws IOException {
159         return responseMessage != null;
160     }
161 
162     @Override
163     protected Iterator<String> getResponseHeaderNames() {
164         return XmppTransportUtils.getHeaderNames(responseMessage);
165     }
166 
167     @Override
168     protected Iterator<String> getResponseHeaders(String name) throws IOException {
169         return XmppTransportUtils.getHeaders(responseMessage, name);
170     }
171 
172     @Override
173     protected InputStream getResponseInputStream() throws IOException {
174         return new MessageInputStream(responseMessage, messageEncoding);
175     }
176 
177 }