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.support;
18  
19  import java.net.URI;
20  import java.net.URISyntaxException;
21  import java.util.Collections;
22  import java.util.Iterator;
23  
24  import org.springframework.util.Assert;
25  import org.springframework.ws.transport.xmpp.XmppTransportConstants;
26  
27  import org.jivesoftware.smack.packet.Message;
28  
29  /**
30   * Collection of utility methods to work with Mail transports.
31   *
32   * @author Arjen Poutsma
33   * @since 2.0
34   */
35  public abstract class XmppTransportUtils {
36  
37      private XmppTransportUtils() {
38      }
39  
40      /**
41       * Converts the given XMPP destination into a <code>xmpp</code> URI.
42       */
43      public static URI toUri(Message requestMessage) throws URISyntaxException {
44          return new URI(XmppTransportConstants.XMPP_URI_SCHEME, requestMessage.getTo(), null);
45      }
46  
47      public static String getTo(URI uri) {
48          return uri.getSchemeSpecificPart();
49      }
50  
51      public static boolean hasError(Message message) {
52          return message != null && Message.Type.error.equals(message.getType());
53      }
54  
55      public static String getErrorMessage(Message message) {
56          if (message == null || !Message.Type.error.equals(message.getType())) {
57              return null;
58          }
59          else {
60              return message.getBody();
61          }
62      }
63  
64      public static void addHeader(Message message, String name, String value) {
65          message.setProperty(name, value);        
66      }
67  
68      public static Iterator<String> getHeaderNames(Message message) {
69          Assert.notNull(message, "'message' must not be null");
70          return message.getPropertyNames().iterator();
71      }
72  
73      public static Iterator<String> getHeaders(Message message, String name) {
74          Assert.notNull(message, "'message' must not be null");
75          String value = message.getProperty(name).toString();
76          if (value != null) {
77              return Collections.singletonList(value).iterator();
78          }
79          else {
80              return Collections.<String>emptyList().iterator();
81          }
82      }
83  
84  }