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;
018
019 import org.apache.camel.Exchange;
020 import org.slf4j.Logger;
021
022 import static org.apache.camel.util.ExchangeHelper.hasExceptionBeenHandledByErrorHandler;
023
024 /**
025 * Helper for processing {@link org.apache.camel.Exchange} in a
026 * <a href="http://camel.apache.org/pipes-and-filters.html">pipeline</a>.
027 *
028 * @version
029 */
030 public final class PipelineHelper {
031
032 private PipelineHelper() {
033 }
034
035 /**
036 * Should we continue processing the exchange?
037 *
038 * @param exchange the next exchange
039 * @param message a message to use when logging that we should not continue processing
040 * @param log a logger
041 * @return <tt>true</tt> to continue processing, <tt>false</tt> to break out, for example if an exception occurred.
042 */
043 public static boolean continueProcessing(Exchange exchange, String message, Logger log) {
044 // check for error if so we should break out
045 boolean exceptionHandled = hasExceptionBeenHandledByErrorHandler(exchange);
046 if (exchange.isFailed() || exchange.isRollbackOnly() || exceptionHandled) {
047 // We need to write a warning message when the exception and fault message be set at the same time
048 if (exchange.hasOut() && exchange.getOut().isFault() && exchange.getException() != null) {
049 StringBuilder sb = new StringBuilder();
050 sb.append("Message exchange has failed: " + message + " for exchange: ").append(exchange);
051 sb.append(" Warning: Both fault and exception exists on the exchange, its best practice to only set one of them.");
052 sb.append(" Exception: ").append(exchange.getException());
053 sb.append(" Fault: ").append(exchange.getOut());
054 if (exceptionHandled) {
055 sb.append(" Handled by the error handler.");
056 }
057 log.warn(sb.toString());
058 }
059 // The Exchange.ERRORHANDLED_HANDLED property is only set if satisfactory handling was done
060 // by the error handler. It's still an exception, the exchange still failed.
061 if (log.isDebugEnabled()) {
062 StringBuilder sb = new StringBuilder();
063 sb.append("Message exchange has failed: " + message + " for exchange: ").append(exchange);
064 if (exchange.isRollbackOnly()) {
065 sb.append(" Marked as rollback only.");
066 }
067 if (exchange.getException() != null) {
068 sb.append(" Exception: ").append(exchange.getException());
069 }
070 if (exchange.hasOut() && exchange.getOut().isFault()) {
071 sb.append(" Fault: ").append(exchange.getOut());
072 }
073 if (exceptionHandled) {
074 sb.append(" Handled by the error handler.");
075 }
076 log.debug(sb.toString());
077 }
078
079 return false;
080 }
081
082 // check for stop
083 Object stop = exchange.getProperty(Exchange.ROUTE_STOP);
084 if (stop != null) {
085 boolean doStop = exchange.getContext().getTypeConverter().convertTo(Boolean.class, exchange, stop);
086 if (doStop) {
087 log.debug("ExchangeId: {} is marked to stop routing: {}", exchange.getExchangeId(), exchange);
088 return false;
089 }
090 }
091
092 return true;
093 }
094
095 }