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 java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import javax.xml.transform.Source;
22  import javax.xml.transform.TransformerException;
23  import javax.xml.transform.stream.StreamResult;
24  
25  import org.springframework.core.MethodParameter;
26  import org.springframework.util.Assert;
27  import org.springframework.ws.server.endpoint.annotation.RequestPayload;
28  import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
29  import org.springframework.xml.transform.TransformerObjectSupport;
30  
31  /**
32   * Abstract base class for {@link MethodArgumentResolver} and {@link MethodReturnValueHandler} implementations based on
33   * {@link RequestPayload} and {@link ResponsePayload} annotations.
34   *
35   * @author Arjen Poutsma
36   * @since 2.0
37   */
38  public abstract class AbstractPayloadMethodProcessor extends TransformerObjectSupport
39          implements MethodArgumentResolver, MethodReturnValueHandler {
40  
41      // MethodArgumentResolver
42  
43      /**
44       * {@inheritDoc}
45       * <p/>
46       * This implementation gets checks if the given parameter is annotated with {@link RequestPayload}, and invokes
47       * {@link #supportsRequestPayloadParameter(org.springframework.core.MethodParameter)} afterwards.
48       */
49      public final boolean supportsParameter(MethodParameter parameter) {
50          Assert.isTrue(parameter.getParameterIndex() >= 0, "Parameter index larger smaller than 0");
51          if (parameter.getParameterAnnotation(RequestPayload.class) == null) {
52              return false;
53          }
54          else {
55              return supportsRequestPayloadParameter(parameter);
56          }
57      }
58  
59      /**
60       * Indicates whether the given {@linkplain MethodParameter method parameter}, annotated with {@link RequestPayload},
61       * is supported by this resolver.
62       *
63       * @param parameter the method parameter to check
64       * @return {@code true} if this resolver supports the supplied parameter; {@code false} otherwise
65       */
66      protected abstract boolean supportsRequestPayloadParameter(MethodParameter parameter);
67  
68      // MethodReturnValueHandler
69      
70      /**
71       * {@inheritDoc}
72       * <p/>
73       * This implementation gets checks if the method of the given return type is annotated with {@link ResponsePayload},
74       * and invokes {@link #supportsResponsePayloadReturnType(org.springframework.core.MethodParameter)} afterwards.
75       */
76      public final boolean supportsReturnType(MethodParameter returnType) {
77          Assert.isTrue(returnType.getParameterIndex() == -1, "Parameter index is not -1");
78          if (returnType.getMethodAnnotation(ResponsePayload.class) == null) {
79              return false;
80          }
81          else {
82              return supportsResponsePayloadReturnType(returnType);
83          }
84      }
85  
86      /**
87       * Indicates whether the given {@linkplain MethodParameter method return type}, annotated with {@link
88       * ResponsePayload}, is supported.
89       *
90       * @param returnType the method parameter to check
91       * @return {@code true} if this resolver supports the supplied return type; {@code false} otherwise
92       */
93      protected abstract boolean supportsResponsePayloadReturnType(MethodParameter returnType);
94  
95      /**
96       * Converts the given source to a byte array input stream.
97       *
98       * @param source the source to convert
99       * @return the input stream
100      * @throws javax.xml.transform.TransformerException in case of transformation errors
101      */
102     protected ByteArrayInputStream convertToByteArrayInputStream(Source source) throws TransformerException {
103         ByteArrayOutputStream bos = new ByteArrayOutputStream();
104         transform(source, new StreamResult(bos));
105         return new ByteArrayInputStream(bos.toByteArray());
106     }
107 }