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.StreamCache;
027 import org.apache.camel.model.AbstractInterceptorDefinition;
028 import org.apache.camel.model.InterceptorDefinition;
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<AbstractInterceptorDefinition> interceptors) {
058 for (int i = 0; i < interceptors.size(); i++) {
059 AbstractInterceptorDefinition interceptor = interceptors.get(i);
060 if (interceptor instanceof InterceptorDefinition
061 && ((InterceptorDefinition)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
083 return proceed(exchange, callback);
084 }
085
086 public boolean proceed(Exchange exchange, AsyncCallback callback) {
087 if (getProcessor() instanceof AsyncProcessor) {
088 return ((AsyncProcessor) getProcessor()).process(exchange, callback);
089 } else {
090 try {
091 getProcessor().process(exchange);
092 } catch (Exception e) {
093 exchange.setException(e);
094 }
095 // false means processing of the exchange asynchronously,
096 callback.done(true);
097 return true;
098 }
099 }
100 }