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