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.client.support;
18  
19  import java.io.IOException;
20  import java.net.URI;
21  import java.net.URISyntaxException;
22  
23  import org.springframework.beans.factory.InitializingBean;
24  import org.springframework.util.Assert;
25  import org.springframework.ws.WebServiceMessageFactory;
26  import org.springframework.ws.transport.WebServiceConnection;
27  import org.springframework.ws.transport.WebServiceMessageSender;
28  import org.springframework.xml.transform.TransformerObjectSupport;
29  
30  /**
31   * Base class for <code>WebServiceTemplate</code> and other WS-accessing helpers. Defines common properties like the
32   * [email protected] WebServiceMessageFactory} and [email protected] WebServiceMessageSender}.
33   * <p/>
34   * Not intended to be used directly. See [email protected] org.springframework.ws.client.core.WebServiceTemplate}.
35   *
36   * @author Arjen Poutsma
37   * @see org.springframework.ws.client.core.WebServiceTemplate
38   * @since 1.0.0
39   */
40  public abstract class WebServiceAccessor extends TransformerObjectSupport implements InitializingBean {
41  
42      private WebServiceMessageFactory messageFactory;
43  
44      private WebServiceMessageSender[] messageSenders;
45  
46      /** Returns the message factory used for creating messages. */
47      public WebServiceMessageFactory getMessageFactory() {
48          return messageFactory;
49      }
50  
51      /** Sets the message factory used for creating messages. */
52      public void setMessageFactory(WebServiceMessageFactory messageFactory) {
53          this.messageFactory = messageFactory;
54      }
55  
56      /** Returns the message senders used for sending messages. */
57      public WebServiceMessageSender[] getMessageSenders() {
58          return messageSenders;
59      }
60  
61      /**
62       * Sets the single message sender used for sending messages.
63       * <p/>
64       * This message sender will be used to resolve an URI to a [email protected] WebServiceConnection}.
65       *
66       * @see #createConnection(URI)
67       */
68      public void setMessageSender(WebServiceMessageSender messageSender) {
69          Assert.notNull(messageSender, "'messageSender' must not be null");
70          messageSenders = new WebServiceMessageSender[]{messageSender};
71      }
72  
73      /**
74       * Sets the message senders used for sending messages.
75       * <p/>
76       * These message senders will be used to resolve an URI to a [email protected] WebServiceConnection}.
77       *
78       * @see #createConnection(URI)
79       */
80      public void setMessageSenders(WebServiceMessageSender[] messageSenders) {
81          Assert.notEmpty(messageSenders, "'messageSenders' must not be empty");
82          this.messageSenders = messageSenders;
83      }
84  
85      public void afterPropertiesSet() {
86          Assert.notNull(getMessageFactory(), "Property 'messageFactory' is required");
87          Assert.notEmpty(getMessageSenders(), "Property 'messageSenders' is required");
88      }
89  
90      /**
91       * Creates a connection to the given URI, or throws an exception when it cannot be resolved.
92       * <p/>
93       * Default implementation iterates over all configured [email protected] WebServiceMessageSender} objects, and calls [email protected]
94       * WebServiceMessageSender#supports(URI)} for each of them. If the sender supports the parameter URI, it creates a
95       * connection using [email protected] WebServiceMessageSender#createConnection(URI)} .
96       *
97       * @param uri the URI to open a connection to
98       * @return the created connection
99       * @throws IllegalArgumentException when the uri cannot be resolved
100      * @throws IOException              when an I/O error occurs
101      */
102     protected WebServiceConnection createConnection(URI uri) throws IOException {
103         Assert.notEmpty(getMessageSenders(), "Property 'messageSenders' is required");
104         WebServiceMessageSender[] messageSenders = getMessageSenders();
105         for (WebServiceMessageSender messageSender : messageSenders) {
106             if (messageSender.supports(uri)) {
107                 WebServiceConnection connection = messageSender.createConnection(uri);
108                 if (logger.isDebugEnabled()) {
109                     try {
110                         logger.debug("Opening [" + connection + "] to [" + connection.getUri() + "]");
111                     }
112                     catch (URISyntaxException e) {
113                         // ignore
114                     }
115                 }
116                 return connection;
117             }
118         }
119         throw new IllegalArgumentException("Could not resolve [" + uri + "] to a WebServiceMessageSender");
120     }
121 
122 }