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.AbstractReceiverConnection;
29 import org.springframework.ws.transport.xmpp.support.XmppTransportUtils;
30
31 import org.jivesoftware.smack.XMPPConnection;
32 import org.jivesoftware.smack.packet.Message;
33
34
35
36
37
38
39
40
41
42 public class XmppReceiverConnection extends AbstractReceiverConnection {
43
44 private final XMPPConnection connection;
45
46 private final Message requestMessage;
47
48 private Message responseMessage;
49
50 private String messageEncoding;
51
52 public XmppReceiverConnection(XMPPConnection connection, Message requestMessage) {
53 Assert.notNull(connection, "'connection' must not be null");
54 Assert.notNull(requestMessage, "'requestMessage' must not be null");
55 this.connection = connection;
56 this.requestMessage = requestMessage;
57 }
58
59
60 public Message getRequestMessage() {
61 return requestMessage;
62 }
63
64
65 public Message getResponseMessage() {
66 return responseMessage;
67 }
68
69
70
71
72
73 void setMessageEncoding(String messageEncoding) {
74 this.messageEncoding = messageEncoding;
75 }
76
77
78
79
80
81 public URI getUri() throws URISyntaxException {
82 return XmppTransportUtils.toUri(requestMessage);
83 }
84
85
86
87
88
89 public boolean hasError() {
90 return XmppTransportUtils.hasError(responseMessage);
91 }
92
93 public String getErrorMessage() {
94 return XmppTransportUtils.getErrorMessage(responseMessage);
95 }
96
97
98
99
100
101 @Override
102 protected Iterator<String> getRequestHeaderNames() throws IOException {
103 return XmppTransportUtils.getHeaderNames(requestMessage);
104 }
105
106 @Override
107 protected Iterator<String> getRequestHeaders(String name) throws IOException {
108 return XmppTransportUtils.getHeaders(requestMessage, name);
109 }
110
111 @Override
112 protected InputStream getRequestInputStream() throws IOException {
113 return new MessageInputStream(requestMessage, messageEncoding);
114 }
115
116
117
118
119
120 @Override
121 protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
122 responseMessage = new Message(requestMessage.getFrom(), Message.Type.chat);
123 responseMessage.setFrom(connection.getUser());
124 responseMessage.setThread(requestMessage.getThread());
125 }
126
127 @Override
128 protected void addResponseHeader(String name, String value) throws IOException {
129 XmppTransportUtils.addHeader(responseMessage, name, value);
130 }
131
132 @Override
133 protected OutputStream getResponseOutputStream() throws IOException {
134 return new MessageOutputStream(responseMessage, messageEncoding);
135 }
136
137 @Override
138 protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
139 connection.sendPacket(responseMessage);
140 }
141 }