View Javadoc
1   /*
2    * Copyright 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  package org.openehealth.ipf.commons.ihe.ws;
17  
18  import java.util.List;
19  
20  import org.apache.cxf.interceptor.Interceptor;
21  import org.apache.cxf.interceptor.InterceptorProvider;
22  import org.apache.cxf.message.Exchange;
23  import org.apache.cxf.message.Message;
24  
25  /**
26   * Helper methods for handling user-defined custom interceptors.
27   * @author Dmytro Rud
28   */
29  abstract public class InterceptorUtils {
30  
31      private InterceptorUtils() {
32          throw new IllegalStateException("Cannot instantiate utility class");
33      }
34  
35      
36      public static void copyInterceptorsFromProvider(
37              InterceptorProvider source, 
38              InterceptorProvider target) 
39      {
40          if (source != null) {
41              copyInterceptorsFromList(source.getInInterceptors(), 
42                                       target.getInInterceptors());
43              copyInterceptorsFromList(source.getInFaultInterceptors(), 
44                                       target.getInFaultInterceptors());
45              copyInterceptorsFromList(source.getOutInterceptors(), 
46                                       target.getOutInterceptors());
47              copyInterceptorsFromList(source.getOutFaultInterceptors(), 
48                                       target.getOutFaultInterceptors());
49          }
50      }
51  
52      private static void copyInterceptorsFromList(
53              List<Interceptor<? extends Message>> source, 
54              List<Interceptor<? extends Message>> target) 
55      {
56          if (source != null) {
57              target.addAll(source);
58          }
59      }
60  
61  
62      /**
63       * Searches for a property in all available contexts
64       * associated with the given SOAP message.
65       *
66       * @param message
67       *      CXF message.
68       * @param propertyName
69       *      name of the property.
70       * @param <T>
71       *      type of the property.
72       * @return
73       *      property value, or <code>null</code> when not found.
74       */
75      public static <T> T findContextualProperty(Message message, String propertyName) {
76          Exchange exchange = message.getExchange();
77          Message[] messages = new Message[] {
78                  message,
79                  exchange.getInMessage(),
80                  exchange.getOutMessage(),
81                  exchange.getInFaultMessage(),
82                  exchange.getOutFaultMessage()
83          };
84  
85          for (Message m : messages) {
86              if (m != null) {
87                  T t  = (T) m.getContextualProperty(propertyName);
88                  if (t != null) {
89                      return t;
90                  }
91              }
92          }
93          return null;
94      }
95  
96  }