001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.processor.interceptor;
018    
019    import java.util.List;
020    
021    import org.apache.camel.AsyncCallback;
022    import org.apache.camel.AsyncProcessor;
023    import org.apache.camel.Exchange;
024    import org.apache.camel.NoTypeConversionAvailableException;
025    import org.apache.camel.Processor;
026    import org.apache.camel.converter.stream.StreamCache;
027    import org.apache.camel.model.InterceptorRef;
028    import org.apache.camel.model.InterceptorType;
029    import org.apache.camel.processor.DelegateProcessor;
030    import org.apache.camel.util.AsyncProcessorHelper;
031    import org.apache.camel.util.MessageHelper;
032    
033    /**
034     * {@link DelegateProcessor} that converts a message into a re-readable format
035     */
036    public class StreamCachingInterceptor extends DelegateProcessor implements AsyncProcessor {
037    
038        public StreamCachingInterceptor() {
039            super();
040        }
041    
042        public StreamCachingInterceptor(Processor processor) {
043            this();
044            setProcessor(processor);
045        }
046    
047        @Override
048        public String toString() {
049            return "StreamCachingInterceptor(" + processor + ")";
050        }
051    
052        /**
053         * Remove the {@link StreamCachingInterceptor} type of interceptor from the given list of interceptors
054         *
055         * @param interceptors the list of interceptors
056         */
057        public static void noStreamCaching(List<InterceptorType> interceptors) {
058            for (int i = 0; i < interceptors.size(); i++) {
059                InterceptorType interceptor = interceptors.get(i);
060                if (interceptor instanceof InterceptorRef
061                    && ((InterceptorRef)interceptor).getInterceptor() instanceof StreamCachingInterceptor) {
062                    interceptors.remove(interceptor);
063                }
064            }
065        }
066        
067        @Override
068        public void process(Exchange exchange) throws Exception {
069            AsyncProcessorHelper.process(this, exchange);
070        }
071    
072        public boolean process(Exchange exchange, AsyncCallback callback) {
073            try {
074                StreamCache newBody = exchange.getIn().getBody(StreamCache.class);
075                if (newBody != null) {
076                    exchange.getIn().setBody(newBody);
077                }
078                MessageHelper.resetStreamCache(exchange.getIn());
079            } catch (NoTypeConversionAvailableException ex) {
080                // ignore if in is not of StreamCache type
081            }
082            return proceed(exchange, callback);
083        } 
084    
085        public boolean proceed(Exchange exchange, AsyncCallback callback) {        
086            if (getProcessor() instanceof AsyncProcessor) {            
087                return ((AsyncProcessor) getProcessor()).process(exchange, callback);
088            } else {
089                try {
090                    getProcessor().process(exchange);
091                } catch (Throwable e) {
092                    exchange.setException(e);
093                }
094                // false means processing of the exchange asynchronously,
095                callback.done(true);
096                return true;
097            }
098        }
099    }