View Javadoc
1   /*
2    * Copyright 2009 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  package org.openehealth.ipf.commons.ihe.ws.cxf;
17  
18  import org.apache.cxf.binding.soap.SoapHeader;
19  import org.apache.cxf.binding.soap.SoapMessage;
20  import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
21  import org.apache.cxf.ext.logging.LoggingOutInterceptor;
22  import org.apache.cxf.headers.Header;
23  import org.apache.cxf.interceptor.Fault;
24  import org.apache.cxf.phase.Phase;
25  import org.apache.cxf.ws.addressing.soap.MAPCodec;
26  
27  import javax.xml.namespace.QName;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /**
32   * CXF Interceptor to tag SOAP headers with an {@code mustUnderstand} flag. 
33   * @author Jens Riemschneider
34   */
35  public class MustUnderstandDecoratorInterceptor extends AbstractSoapInterceptor {
36      private final List<QName> mustUnderstandHeaders = new ArrayList<>();
37  
38      /**
39       * Constructs the interceptor.
40       */
41      public MustUnderstandDecoratorInterceptor() {
42          super(Phase.WRITE);
43          addAfter(MAPCodec.class.getName());
44          addBefore(LoggingOutInterceptor.class.getName());
45      }
46  
47      @Override
48      public void handleMessage(SoapMessage message) throws Fault {
49          List<Header> headers = message.getHeaders();
50          for (QName name : mustUnderstandHeaders) {
51              mustUnderstand(headers, name);
52          }
53      }
54  
55      /**
56       * Adds a set of SOAP headers which are to be flagged 
57       * with {@code mustUnderstand}.
58       *  
59       * @param headers
60       *          the headers to flag, represented as Strings 
61       */
62      public void addHeaders(List<String> headers) {
63          for (String header : headers) {
64              int namespaceEnd = header.indexOf('}');
65              String namespace = header.substring(1, namespaceEnd);
66              String name = header.substring(namespaceEnd + 1);
67              mustUnderstandHeaders.add(new QName(namespace, name));
68          }
69      }
70  
71      /**
72       * Adds an item to the list of SOAP headers which  
73       * are to be flagged with {@code mustUnderstand}.
74       *  
75       * @param header
76       *          the header to flag.
77       */
78      public void addHeader(QName header) {
79          mustUnderstandHeaders.add(header);
80      }
81      
82      
83      private Header getHeader(List<Header> headers, QName name) {
84          for (Header header : headers) {
85              if (header.getName().equals(name)) {
86                  return header;
87              }
88          }
89          return null;
90      }
91  
92      private void mustUnderstand(List<Header> headers, QName name) {
93          Header header = getHeader(headers, name);
94          if (header == null) {
95              return;
96          }
97  
98          if (header instanceof SoapHeader) {
99              SoapHeader soapHeader = (SoapHeader) header;
100             soapHeader.setMustUnderstand(true);
101             return;
102         }
103 
104         headers.remove(header);
105         SoapHeader newHeader = new SoapHeader(name, header.getObject());
106         newHeader.setMustUnderstand(true);
107         headers.add(newHeader);
108     }
109 }