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 org.apache.camel.CamelException;
020 import org.apache.camel.Exchange;
021 import org.apache.camel.Processor;
022 import org.apache.camel.processor.DelegateProcessor;
023
024 public class HandleFaultInterceptor extends DelegateProcessor {
025
026 public HandleFaultInterceptor() {
027 super();
028 }
029
030 public HandleFaultInterceptor(Processor processor) {
031 this();
032 setProcessor(processor);
033 }
034
035 @Override
036 public String toString() {
037 return "HandleFaultInterceptor[" + processor + "]";
038 }
039
040 @Override
041 public void process(Exchange exchange) throws Exception {
042 if (processor == null) {
043 return;
044 }
045
046 try {
047 processor.process(exchange);
048 } catch (Exception e) {
049 exchange.setException(e);
050 }
051
052 handleFault(exchange);
053 }
054
055 /**
056 * Handles the fault message by converting it to an Exception
057 */
058 protected void handleFault(Exchange exchange) {
059 // Take the fault message out before we keep on going
060 if (exchange.hasFault()) {
061 final Object faultBody = exchange.getFault().getBody();
062 if (faultBody != null && exchange.getException() == null) {
063 // remove fault as we are converting it to an exception
064 exchange.setFault(null);
065 if (faultBody instanceof Exception) {
066 exchange.setException((Exception) faultBody);
067 } else {
068 // wrap it in an exception
069 exchange.setException(new CamelException(faultBody.toString()));
070 }
071 }
072 }
073 }
074
075 }