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 */ 017package org.apache.camel.processor; 018 019import org.apache.camel.AsyncCallback; 020import org.apache.camel.AsyncProcessor; 021import org.apache.camel.Exchange; 022import org.apache.camel.Expression; 023import org.apache.camel.Message; 024import org.apache.camel.Traceable; 025import org.apache.camel.impl.DefaultMessage; 026import org.apache.camel.support.ServiceSupport; 027import org.apache.camel.util.AsyncProcessorHelper; 028import org.apache.camel.util.ExchangeHelper; 029import org.apache.camel.util.ObjectHelper; 030 031/** 032 * A processor which sets the body on the OUT message with an {@link Expression} 033 */ 034public class TransformProcessor extends ServiceSupport implements AsyncProcessor, Traceable { 035 private final Expression expression; 036 037 public TransformProcessor(Expression expression) { 038 ObjectHelper.notNull(expression, "expression", this); 039 this.expression = expression; 040 } 041 042 public void process(Exchange exchange) throws Exception { 043 AsyncProcessorHelper.process(this, exchange); 044 } 045 046 public boolean process(Exchange exchange, AsyncCallback callback) { 047 try { 048 Object newBody = expression.evaluate(exchange, Object.class); 049 050 if (exchange.getException() != null) { 051 // the expression threw an exception so we should break-out 052 callback.done(true); 053 return true; 054 } 055 056 boolean out = exchange.hasOut(); 057 Message old = out ? exchange.getOut() : exchange.getIn(); 058 059 // create a new message container so we do not drag specialized message objects along 060 // but that is only needed if the old message is a specialized message 061 boolean copyNeeded = !(old.getClass().equals(DefaultMessage.class)); 062 063 if (copyNeeded) { 064 Message msg = new DefaultMessage(); 065 msg.copyFrom(old); 066 msg.setBody(newBody); 067 068 // replace message on exchange (must set as OUT) 069 ExchangeHelper.replaceMessage(exchange, msg, true); 070 } else { 071 // no copy needed so set replace value directly 072 old.setBody(newBody); 073 074 // but the message must be on OUT 075 if (!exchange.hasOut()) { 076 exchange.setOut(exchange.getIn()); 077 } 078 } 079 080 } catch (Throwable e) { 081 exchange.setException(e); 082 } 083 084 callback.done(true); 085 return true; 086 } 087 088 @Override 089 public String toString() { 090 return "Transform(" + expression + ")"; 091 } 092 093 public String getTraceLabel() { 094 return "transform[" + expression + "]"; 095 } 096 097 @Override 098 protected void doStart() throws Exception { 099 // noop 100 } 101 102 @Override 103 protected void doStop() throws Exception { 104 // noop 105 } 106}