1 /*
2  * Copyright 2002-2014 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.soap.server.endpoint.mapping;
18
19 import java.lang.reflect.Method;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.springframework.core.annotation.AnnotationUtils;
24 import org.springframework.util.Assert;
25 import org.springframework.util.StringUtils;
26 import org.springframework.ws.context.MessageContext;
27 import org.springframework.ws.server.EndpointInterceptor;
28 import org.springframework.ws.server.EndpointInvocationChain;
29 import org.springframework.ws.server.endpoint.mapping.AbstractAnnotationMethodEndpointMapping;
30 import org.springframework.ws.soap.SoapMessage;
31 import org.springframework.ws.soap.server.SoapEndpointInvocationChain;
32 import org.springframework.ws.soap.server.SoapEndpointMapping;
33 import org.springframework.ws.soap.server.endpoint.annotation.SoapAction;
34 import org.springframework.ws.soap.server.endpoint.annotation.SoapActions;
35
36 /**
37  * Implementation of the {@link org.springframework.ws.server.EndpointMapping} interface that uses the {@link
38  * SoapAction} annotation to map methods to the request SOAPAction header.
39  *
40  * <p>Endpoints typically have the following form:
41  * <pre>
42  * &#64;Endpoint
43  * public class MyEndpoint{
44  *      &#64;SoapAction("http://springframework.org/spring-ws/SoapAction")
45  *      public Source doSomethingWithRequest() {
46  *         ...
47  *      }
48  * }
49  * </pre>
50  *
51  * @author Arjen Poutsma
52  * @since 1.0.0
53  */

54 public class SoapActionAnnotationMethodEndpointMapping extends AbstractAnnotationMethodEndpointMapping<String>
55         implements SoapEndpointMapping {
56
57     private String[] actorsOrRoles;
58
59     private boolean isUltimateReceiver = true;
60
61     @Override
62     public final void setActorOrRole(String actorOrRole) {
63         Assert.notNull(actorOrRole, "actorOrRole must not be null");
64         actorsOrRoles = new String[]{actorOrRole};
65     }
66
67     @Override
68     public final void setActorsOrRoles(String[] actorsOrRoles) {
69         Assert.notEmpty(actorsOrRoles, "actorsOrRoles must not be empty");
70         this.actorsOrRoles = actorsOrRoles;
71     }
72
73     @Override
74     public final void setUltimateReceiver(boolean ultimateReceiver) {
75         isUltimateReceiver = ultimateReceiver;
76     }
77
78     /**
79      * Creates a new {@code SoapEndpointInvocationChain} based on the given endpoint, and the set interceptors, and
80      * actors/roles.
81      *
82      * @param endpoint       the endpoint
83      * @param interceptors the endpoint interceptors
84      * @return the created invocation chain
85      * @see #setInterceptors(org.springframework.ws.server.EndpointInterceptor[])
86      * @see #setActorsOrRoles(String[])
87      */

88     @Override
89     protected final EndpointInvocationChain createEndpointInvocationChain(MessageContext messageContext,
90                                                                           Object endpoint,
91                                                                           EndpointInterceptor[] interceptors) {
92         return new SoapEndpointInvocationChain(endpoint, interceptors, actorsOrRoles, isUltimateReceiver);
93     }
94
95     @Override
96     protected String getLookupKeyForMessage(MessageContext messageContext) throws Exception {
97         if (messageContext.getRequest() instanceof SoapMessage) {
98             SoapMessage request = (SoapMessage) messageContext.getRequest();
99             String soapAction = request.getSoapAction();
100             if (StringUtils.hasLength(soapAction) && soapAction.charAt(0) == '"' &&
101                     soapAction.charAt(soapAction.length() - 1) == '"') {
102                 return soapAction.substring(1, soapAction.length() - 1);
103             }
104             else {
105                 return soapAction;
106             }
107         }
108         else {
109             return null;
110         }
111     }
112
113     @Override
114     protected String getLookupKeyForMethod(Method method) {
115         SoapAction soapAction = AnnotationUtils.findAnnotation(method, SoapAction.class);
116         return soapAction != null ? soapAction.value() : null;
117     }
118
119     @Override
120     protected List<String> getLookupKeysForMethod(Method method) {
121         List<String> result = new ArrayList<String>();
122
123         SoapActions soapActions = AnnotationUtils.findAnnotation(method,
124                 SoapActions.class);
125         if (soapActions != null) {
126             for (SoapAction soapAction : soapActions.value()) {
127                 result.add(soapAction.value());
128             }
129         }
130         else {
131             SoapAction soapAction = AnnotationUtils.findAnnotation(method, SoapAction.class);
132             if (soapAction != null) {
133                 result.add(soapAction.value());
134             }
135         }
136         return result;
137     }
138 }
139