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