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.support;
18  
19  import java.net.URISyntaxException;
20  
21  import org.springframework.beans.factory.InitializingBean;
22  import org.springframework.util.Assert;
23  import org.springframework.ws.FaultAwareWebServiceMessage;
24  import org.springframework.ws.NoEndpointFoundException;
25  import org.springframework.ws.WebServiceMessage;
26  import org.springframework.ws.WebServiceMessageFactory;
27  import org.springframework.ws.context.DefaultMessageContext;
28  import org.springframework.ws.context.MessageContext;
29  import org.springframework.ws.transport.EndpointAwareWebServiceConnection;
30  import org.springframework.ws.transport.FaultAwareWebServiceConnection;
31  import org.springframework.ws.transport.WebServiceConnection;
32  import org.springframework.ws.transport.WebServiceMessageReceiver;
33  import org.springframework.ws.transport.context.DefaultTransportContext;
34  import org.springframework.ws.transport.context.TransportContext;
35  import org.springframework.ws.transport.context.TransportContextHolder;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * Convenience base class for server-side transport objects. Contains a [email protected] WebServiceMessageFactory}, and has
42   * methods for handling incoming [email protected] WebServiceConnection}s.
43   *
44   * @author Arjen Poutsma
45   * @see #handleConnection
46   * @since 1.0.0
47   */
48  public abstract class WebServiceMessageReceiverObjectSupport implements InitializingBean {
49  
50      /** Logger available to subclasses. */
51      protected final Log logger = LogFactory.getLog(getClass());
52  
53      private WebServiceMessageFactory messageFactory;
54  
55      /** Returns the <code>WebServiceMessageFactory</code>. */
56      public WebServiceMessageFactory getMessageFactory() {
57          return messageFactory;
58      }
59  
60      /** Sets the <code>WebServiceMessageFactory</code>. */
61      public void setMessageFactory(WebServiceMessageFactory messageFactory) {
62          this.messageFactory = messageFactory;
63      }
64  
65      public void afterPropertiesSet() throws Exception {
66          Assert.notNull(messageFactory, "messageFactory is required");
67      }
68  
69      /**
70       * Handles an incoming connection by [email protected] WebServiceConnection#receive(WebServiceMessageFactory) receving} a
71       * message from it, passing it to the [email protected] WebServiceMessageReceiver#receive(MessageContext) receiver}, and [email protected]
72       * WebServiceConnection#send(WebServiceMessage) sending} the response (if any).
73       * <p/>
74       * Stores the given connection in the [email protected] TransportContext}.
75       *
76       * @param connection the incoming connection
77       * @param receiver   the handler of the message, typically a [email protected] org.springframework.ws.server.MessageDispatcher}
78       */
79      protected final void handleConnection(WebServiceConnection connection, WebServiceMessageReceiver receiver)
80              throws Exception {
81          logUri(connection);
82          TransportContext previousTransportContext = TransportContextHolder.getTransportContext();
83          TransportContextHolder.setTransportContext(new DefaultTransportContext(connection));
84  
85          try {
86              WebServiceMessage request = connection.receive(getMessageFactory());
87              MessageContext messageContext = new DefaultMessageContext(request, getMessageFactory());
88              receiver.receive(messageContext);
89              if (messageContext.hasResponse()) {
90                  WebServiceMessage response = messageContext.getResponse();
91                  if (response instanceof FaultAwareWebServiceMessage &&
92                          connection instanceof FaultAwareWebServiceConnection) {
93                      FaultAwareWebServiceMessage faultResponse = (FaultAwareWebServiceMessage) response;
94                      FaultAwareWebServiceConnection faultConnection = (FaultAwareWebServiceConnection) connection;
95                      faultConnection.setFault(faultResponse.hasFault());
96                  }
97                  connection.send(messageContext.getResponse());
98              }
99          }
100         catch (NoEndpointFoundException ex) {
101             if (connection instanceof EndpointAwareWebServiceConnection) {
102                 ((EndpointAwareWebServiceConnection) connection).endpointNotFound();
103             }
104         }
105         finally {
106             TransportUtils.closeConnection(connection);
107             TransportContextHolder.setTransportContext(previousTransportContext);
108         }
109     }
110 
111     private void logUri(WebServiceConnection connection) {
112         if (logger.isDebugEnabled()) {
113             try {
114                 logger.debug("Accepting incoming [" + connection + "] at [" + connection.getUri() + "]");
115             }
116             catch (URISyntaxException e) {
117                 // ignore
118             }
119         }
120     }
121 
122 }