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 java.io.ByteArrayOutputStream;
020
021import org.apache.camel.AsyncCallback;
022import org.apache.camel.AsyncProcessor;
023import org.apache.camel.CamelContext;
024import org.apache.camel.CamelContextAware;
025import org.apache.camel.Exchange;
026import org.apache.camel.Message;
027import org.apache.camel.Traceable;
028import org.apache.camel.converter.stream.CachedOutputStream;
029import org.apache.camel.spi.DataFormat;
030import org.apache.camel.spi.IdAware;
031import org.apache.camel.support.ServiceSupport;
032import org.apache.camel.util.AsyncProcessorHelper;
033import org.apache.camel.util.ObjectHelper;
034import org.apache.camel.util.ServiceHelper;
035
036/**
037 * Marshals the body of the incoming message using the given
038 * <a href="http://camel.apache.org/data-format.html">data format</a>
039 *
040 * @version
041 */
042public class MarshalProcessor extends ServiceSupport implements AsyncProcessor, Traceable, CamelContextAware, IdAware {
043    private String id;
044    private CamelContext camelContext;
045    private final DataFormat dataFormat;
046
047    public MarshalProcessor(DataFormat dataFormat) {
048        this.dataFormat = dataFormat;
049    }
050
051    public void process(Exchange exchange) throws Exception {
052        AsyncProcessorHelper.process(this, exchange);
053    }
054
055    public boolean process(Exchange exchange, AsyncCallback callback) {
056        ObjectHelper.notNull(dataFormat, "dataFormat");
057
058        // if stream caching is enabled then use that so we can stream accordingly
059        // for example to overflow to disk for big streams
060        CachedOutputStream cos;
061        ByteArrayOutputStream os;
062        if (exchange.getContext().getStreamCachingStrategy().isEnabled()) {
063            cos = new CachedOutputStream(exchange);
064            os = null;
065        } else {
066            cos = null;
067            os = new ByteArrayOutputStream();
068        }
069
070        Message in = exchange.getIn();
071        Object body = in.getBody();
072
073        // lets setup the out message before we invoke the dataFormat
074        // so that it can mutate it if necessary
075        Message out = exchange.getOut();
076        out.copyFrom(in);
077
078        try {
079            if (cos != null) {
080                dataFormat.marshal(exchange, body, cos);
081                out.setBody(cos.newStreamCache());
082            } else {
083                dataFormat.marshal(exchange, body, os);
084                byte[] data = os.toByteArray();
085                out.setBody(data);
086            }
087        } catch (Throwable e) {
088            // remove OUT message, as an exception occurred
089            exchange.setOut(null);
090            exchange.setException(e);
091        }
092
093        callback.done(true);
094        return true;
095    }
096
097    @Override
098    public String toString() {
099        return "Marshal[" + dataFormat + "]";
100    }
101
102    public String getTraceLabel() {
103        return "marshal[" + dataFormat + "]";
104    }
105
106    public String getId() {
107        return id;
108    }
109
110    public void setId(String id) {
111        this.id = id;
112    }
113
114    public CamelContext getCamelContext() {
115        return camelContext;
116    }
117
118    public void setCamelContext(CamelContext camelContext) {
119        this.camelContext = camelContext;
120    }
121
122    @Override
123    protected void doStart() throws Exception {
124        // inject CamelContext on data format
125        if (dataFormat instanceof CamelContextAware) {
126            ((CamelContextAware) dataFormat).setCamelContext(camelContext);
127        }
128        // add dataFormat as service which will also start the service
129        // (false => we and handling the lifecycle of the dataFormat)
130        getCamelContext().addService(dataFormat, false);
131    }
132
133    @Override
134    protected void doStop() throws Exception {
135        ServiceHelper.stopService(dataFormat);
136        getCamelContext().removeService(dataFormat);
137    }
138}