View Javadoc

1   /*
2    * Copyright 2007 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;
18  
19  import java.lang.reflect.Method;
20  import javax.xml.transform.Source;
21  
22  import org.springframework.ws.WebServiceMessage;
23  import org.springframework.ws.context.MessageContext;
24  import org.springframework.ws.server.MessageDispatcher;
25  import org.springframework.ws.server.endpoint.MethodEndpoint;
26  import org.springframework.ws.soap.server.SoapMessageDispatcher;
27  
28  /**
29   * Adapter that supports endpoint methods that use marshalling. Supports methods with the following signature:
30   * <pre>
31   * void handleMyMessage(Source request);
32   * </pre>
33   * or
34   * <pre>
35   * Source handleMyMessage(Source request);
36   * </pre>
37   * I.e. methods that take a single {@link Source} parameter, and return either <code>void</code> or a {@link Source}.
38   * The method can have any name, as long as it is mapped by an {@link org.springframework.ws.server.EndpointMapping}.
39   * <p/>
40   * This adapter is registered by default by the {@link MessageDispatcher} and {@link SoapMessageDispatcher}.
41   *
42   * @author Arjen Poutsma
43   * @since 1.0.0
44   */
45  public class PayloadMethodEndpointAdapter extends AbstractMethodEndpointAdapter {
46  
47      protected boolean supportsInternal(MethodEndpoint methodEndpoint) {
48          Method method = methodEndpoint.getMethod();
49          return (Void.TYPE.isAssignableFrom(method.getReturnType()) ||
50                  Source.class.isAssignableFrom(method.getReturnType())) && method.getParameterTypes().length == 1 &&
51                  Source.class.isAssignableFrom(method.getParameterTypes()[0]);
52  
53      }
54  
55      protected void invokeInternal(MessageContext messageContext, MethodEndpoint methodEndpoint) throws Exception {
56          Source requestSource = messageContext.getRequest().getPayloadSource();
57          Object result = methodEndpoint.invoke(new Object[]{requestSource});
58          if (result != null) {
59              Source responseSource = (Source) result;
60              WebServiceMessage response = messageContext.getResponse();
61              transform(responseSource, response.getPayloadResult());
62          }
63      }
64  }