1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
44
45
46
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
70 public Message getRequestMessage() {
71 return requestMessage;
72 }
73
74
75 public Message getResponseMessage() {
76 return responseMessage;
77 }
78
79
80
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
93
94
95 public URI getUri() throws URISyntaxException {
96 return XmppTransportUtils.toUri(requestMessage);
97 }
98
99
100
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
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
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 }