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.server.endpoint.adapter.method;
18  
19  import javax.xml.transform.Source;
20  
21  import org.springframework.core.MethodParameter;
22  import org.springframework.ws.WebServiceMessage;
23  import org.springframework.ws.context.MessageContext;
24  import org.springframework.ws.server.endpoint.annotation.RequestPayload;
25  
26  /**
27   * Abstract base class for {@link MethodArgumentResolver} and {@link MethodReturnValueHandler} implementations based on
28   * {@link Source}s.
29   *
30   * @author Arjen Poutsma
31   * @since 2.0
32   */
33  public abstract class AbstractPayloadSourceMethodProcessor extends AbstractPayloadMethodProcessor {
34  
35      // MethodArgumentResolver
36  
37      public final Object resolveArgument(MessageContext messageContext, MethodParameter parameter) throws Exception {
38          Source requestPayload = getRequestPayload(messageContext);
39          return requestPayload != null ? resolveRequestPayloadArgument(parameter, requestPayload) : null;
40      }
41  
42      /** Returns the request payload as {@code Source}. */
43      private Source getRequestPayload(MessageContext messageContext) {
44          WebServiceMessage request = messageContext.getRequest();
45          return request != null ? request.getPayloadSource() : null;
46      }
47  
48      /**
49       * Resolves the given parameter, annotated with {@link RequestPayload}, into a method argument.
50       *
51       * @param parameter      the parameter to resolve to an argument
52       * @param requestPayload the request payload
53       * @return the resolved argument. May be {@code null}.
54       * @throws Exception in case of errors
55       */
56      protected abstract Object resolveRequestPayloadArgument(MethodParameter parameter, Source requestPayload)
57              throws Exception;
58  
59      // MethodReturnValueHandler
60  
61      public final void handleReturnValue(MessageContext messageContext, MethodParameter returnType, Object returnValue)
62              throws Exception {
63          if (returnValue != null) {
64              Source responsePayload = createResponsePayload(returnType, returnValue);
65              if (responsePayload != null) {
66                  WebServiceMessage response = messageContext.getResponse();
67                  transform(responsePayload, response.getPayloadResult());
68              }
69          }
70      }
71  
72      /**
73       * Creates a response payload for the given return value.
74       *
75       * @param returnType  the return type to handle
76       * @param returnValue the return value to handle
77       * @return the response payload
78       * @throws Exception in case of errors
79       */
80      protected abstract Source createResponsePayload(MethodParameter returnType, Object returnValue) throws Exception;
81  
82  }