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.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   * Implementation of {@link org.springframework.ws.transport.WebServiceConnection} that is used for server-side XMPP
36   * access. Exposes a {@link Message} request and response message.
37   *
38   * @author Gildas Cuisinier
39   * @author Arjen Poutsma
40   * @since 2.0
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      /** Returns the request message for this connection. */
60      public Message getRequestMessage() {
61          return requestMessage;
62      }
63  
64      /** Returns the response message, if any, for this connection. */
65      public Message getResponseMessage() {
66          return responseMessage;
67      }
68  
69      /*
70       * Package-friendly setters
71       */
72  
73      void setMessageEncoding(String messageEncoding) {
74          this.messageEncoding = messageEncoding;
75      }
76  
77      /*
78      * URI
79      */
80  
81      public URI getUri() throws URISyntaxException {
82          return XmppTransportUtils.toUri(requestMessage);
83      }
84  
85      /*
86      * Errors
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       * Receiving
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      * Sending
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 }