View Javadoc

1   /*
2    * Copyright 2005-2011 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.interceptor;
18  
19  import javax.xml.namespace.QName;
20  import javax.xml.transform.TransformerException;
21  
22  import org.springframework.util.Assert;
23  import org.springframework.util.StringUtils;
24  import org.springframework.ws.WebServiceMessage;
25  import org.springframework.ws.server.EndpointInterceptor;
26  import org.springframework.ws.server.endpoint.support.PayloadRootUtils;
27  import org.springframework.xml.transform.TransformerHelper;
28  
29  /**
30   * Implementation of the {@link org.springframework.ws.soap.server.SmartSoapEndpointInterceptor
31   * SmartSoapEndpointInterceptor} interface that only intercepts requests that have a specified namespace URI or local
32   * part (or both) as payload root.
33   *
34   * @author Arjen Poutsma
35   * @since 2.0
36   */
37  public class PayloadRootSmartSoapEndpointInterceptor extends DelegatingSmartSoapEndpointInterceptor {
38  
39      private TransformerHelper transformerHelper = new TransformerHelper();
40  
41      private final String namespaceUri;
42  
43      private final String localPart;
44  
45      public PayloadRootSmartSoapEndpointInterceptor(EndpointInterceptor delegate,
46                                                     String namespaceUri,
47                                                     String localPart) {
48          super(delegate);
49          Assert.hasLength(namespaceUri, "namespaceUri can not be empty");
50          this.namespaceUri = namespaceUri;
51          this.localPart = localPart;
52      }
53  
54      public void setTransformerHelper(TransformerHelper transformerHelper) {
55          this.transformerHelper = transformerHelper;
56      }
57  
58      @Override
59      protected boolean shouldIntercept(WebServiceMessage request, Object endpoint) {
60          try {
61              QName payloadRootName = PayloadRootUtils.getPayloadRootQName(request.getPayloadSource(), transformerHelper);
62              if (!namespaceUri.equals(payloadRootName.getNamespaceURI())) {
63                  return false;
64              }
65              return !StringUtils.hasLength(localPart) || localPart.equals(payloadRootName.getLocalPart());
66  
67          }
68          catch (TransformerException e) {
69              return false;
70          }
71      }
72  }