View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.ksb.impl.cxf.interceptors;
17  
18  import org.apache.cxf.interceptor.Fault;
19  import org.apache.cxf.message.Message;
20  import org.apache.cxf.phase.AbstractPhaseInterceptor;
21  import org.apache.cxf.phase.Phase;
22  import org.kuali.rice.core.api.util.collect.CollectionUtils;
23  
24  import java.util.List;
25  
26  /**
27   * A CXF Interceptor that binds itself to the USER_LOGICAL phase to be used on inbound
28   * messages.  This interceptor is invoked in the interceptor chain after unmarshalling
29   * from XML to Java has occurred.  The role of this interceptor is to ensure that any
30   * Collection (and specifically List, Set, or Map) used in a @WebMethod is ultimately of the
31   * expected immutable type returned from the local service.
32   */
33  @SuppressWarnings("unused")
34  public class ImmutableCollectionsInInterceptor extends AbstractPhaseInterceptor<Message> {
35  
36      /**
37       * Instantiates an ImmutableCollectionsInInterceptor and adds it to the USER_LOGICAL phase.
38       */
39      public ImmutableCollectionsInInterceptor() {
40          super(Phase.USER_LOGICAL);
41      }
42  
43      @Override
44      public void handleMessage(final Message message) throws Fault {
45          try {
46              List contents = message.getContent(List.class);
47              if (contents != null) {
48                  for (Object o : contents) {
49                      CollectionUtils.makeUnmodifiableAndNullSafe(o);
50                  }
51              }
52          } catch (IllegalAccessException e) {
53              throw new Fault(e);
54          }
55      }
56  }