001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.ksb.impl.cxf.interceptors;
017    
018    import org.apache.cxf.interceptor.Fault;
019    import org.apache.cxf.message.Message;
020    import org.apache.cxf.phase.AbstractPhaseInterceptor;
021    import org.apache.cxf.phase.Phase;
022    import org.kuali.rice.core.api.util.collect.CollectionUtils;
023    
024    import java.util.List;
025    
026    /**
027     * A CXF Interceptor that binds itself to the USER_LOGICAL phase to be used on inbound
028     * messages.  This interceptor is invoked in the interceptor chain after unmarshalling
029     * from XML to Java has occurred.  The role of this interceptor is to ensure that any
030     * Collection (and specifically List, Set, or Map) used in a @WebMethod is ultimately of the
031     * expected immutable type returned from the local service.
032     */
033    @SuppressWarnings("unused")
034    public class ImmutableCollectionsInInterceptor extends AbstractPhaseInterceptor<Message> {
035    
036        /**
037         * Instantiates an ImmutableCollectionsInInterceptor and adds it to the USER_LOGICAL phase.
038         */
039        public ImmutableCollectionsInInterceptor() {
040            super(Phase.USER_LOGICAL);
041        }
042    
043        @Override
044        public void handleMessage(final Message message) throws Fault {
045            try {
046                List contents = message.getContent(List.class);
047                if (contents != null) {
048                    for (Object o : contents) {
049                        CollectionUtils.makeUnmodifiableAndNullSafe(o);
050                    }
051                }
052            } catch (IllegalAccessException e) {
053                throw new Fault(e);
054            }
055        }
056    }