View Javadoc

1   /*
2    * Copyright 2005-2012 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.http;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.springframework.web.servlet.HandlerAdapter;
23  import org.springframework.web.servlet.ModelAndView;
24  import org.springframework.ws.InvalidXmlException;
25  import org.springframework.ws.transport.WebServiceConnection;
26  import org.springframework.ws.transport.WebServiceMessageReceiver;
27  import org.springframework.ws.transport.support.WebServiceMessageReceiverObjectSupport;
28  
29  /**
30   * Adapter to use the {@link WebServiceMessageReceiver} interface with the generic {@link
31   * org.springframework.web.servlet.DispatcherServlet}. Requires a {@link org.springframework.ws.WebServiceMessageFactory}
32   * which is used to convert the incoming <code>HttpServletRequest</code> into a <code>WebServiceMessage</code>, and
33   * passes that context to the mapped <code>WebServiceMessageReceiver</code>. If a response is created, that is sent via
34   * the <code>HttpServletResponse</code>.
35   * <p/>
36   * Note that the <code>MessageDispatcher</code> implements the <code>WebServiceMessageReceiver</code> interface,
37   * enabling this adapter to function as a gateway to further message handling logic.
38   *
39   * @author Arjen Poutsma
40   * @see #setMessageFactory(org.springframework.ws.WebServiceMessageFactory)
41   * @see org.springframework.ws.transport.WebServiceMessageReceiver
42   * @see org.springframework.ws.WebServiceMessageFactory
43   * @see org.springframework.ws.server.MessageDispatcher
44   * @since 1.0.0
45   */
46  public class WebServiceMessageReceiverHandlerAdapter extends WebServiceMessageReceiverObjectSupport
47          implements HandlerAdapter {
48  
49      public long getLastModified(HttpServletRequest request, Object handler) {
50          return -1L;
51      }
52  
53      public ModelAndView handle(HttpServletRequest httpServletRequest,
54                                 HttpServletResponse httpServletResponse,
55                                 Object handler) throws Exception {
56          if (HttpTransportConstants.METHOD_POST.equals(httpServletRequest.getMethod())) {
57              WebServiceConnection connection = new HttpServletConnection(httpServletRequest, httpServletResponse);
58              try {
59                  handleConnection(connection, (WebServiceMessageReceiver) handler);
60              }
61              catch (InvalidXmlException ex) {
62                  handleInvalidXmlException(httpServletRequest, httpServletResponse, handler, ex);
63              }
64          }
65          else {
66              handleNonPostMethod(httpServletRequest, httpServletResponse, handler);
67          }
68          return null;
69      }
70  
71      public boolean supports(Object handler) {
72          return handler instanceof WebServiceMessageReceiver;
73      }
74  
75      /**
76       * Template method that is invoked when the request method is not {@code POST}. Called from {@link
77       * #handle(HttpServletRequest, HttpServletResponse, Object)}.
78       * <p/>
79       * Default implementation set the response status to 405: Method Not Allowed. Can be overridden in subclasses.
80       *
81       * @param httpServletRequest  current HTTP request
82       * @param httpServletResponse current HTTP response
83       * @param handler             current handler
84       */
85      protected void handleNonPostMethod(HttpServletRequest httpServletRequest,
86                                         HttpServletResponse httpServletResponse,
87                                         Object handler) throws Exception {
88          httpServletResponse.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
89      }
90  
91      /**
92       * Template method that is invoked when parsing the request results in a {@link InvalidXmlException}. Called from
93       * {@link #handle(HttpServletRequest, HttpServletResponse, Object)}.
94       * <p/>
95       * Default implementation set the response status to 400: Bad Request. Can be overridden in subclasses.
96       *
97       * @param httpServletRequest  current HTTP request
98       * @param httpServletResponse current HTTP response
99       * @param handler             current handler
100      * @param ex                  the invalid XML exception that resulted in this method being called
101      */
102     protected void handleInvalidXmlException(HttpServletRequest httpServletRequest,
103                                              HttpServletResponse httpServletResponse,
104                                              Object handler,
105                                              InvalidXmlException ex) throws Exception {
106         httpServletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
107     }
108 
109 }