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.InputStream;
020import java.util.Iterator;
021
022import org.apache.camel.AsyncCallback;
023import org.apache.camel.AsyncProcessor;
024import org.apache.camel.CamelContext;
025import org.apache.camel.CamelContextAware;
026import org.apache.camel.Exchange;
027import org.apache.camel.Message;
028import org.apache.camel.RuntimeCamelException;
029import org.apache.camel.Traceable;
030import org.apache.camel.spi.DataFormat;
031import org.apache.camel.support.ServiceSupport;
032import org.apache.camel.util.AsyncProcessorHelper;
033import org.apache.camel.util.IOHelper;
034import org.apache.camel.util.ObjectHelper;
035import org.apache.camel.util.ServiceHelper;
036
037/**
038 * Unmarshals the body of the incoming message using the given
039 * <a href="http://camel.apache.org/data-format.html">data format</a>
040 *
041 * @version 
042 */
043public class UnmarshalProcessor extends ServiceSupport implements AsyncProcessor, Traceable, CamelContextAware {
044    private CamelContext camelContext;
045    private final DataFormat dataFormat;
046
047    public UnmarshalProcessor(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        InputStream stream = null;
059        Object result = null;
060        try {
061            stream = exchange.getIn().getMandatoryBody(InputStream.class);
062
063            // lets setup the out message before we invoke the dataFormat so that it can mutate it if necessary
064            Message out = exchange.getOut();
065            out.copyFrom(exchange.getIn());
066
067            result = dataFormat.unmarshal(exchange, stream);
068            if (result instanceof Exchange) {
069                if (result != exchange) {
070                    // it's not allowed to return another exchange other than the one provided to dataFormat
071                    throw new RuntimeCamelException("The returned exchange " + result + " is not the same as " + exchange + " provided to the DataFormat");
072                }
073            } else if (result instanceof Message) {
074                // the dataformat has probably set headers, attachments, etc. so let's use it as the outbound payload
075                exchange.setOut((Message) result);
076            } else {
077                out.setBody(result);
078            }
079        } catch (Exception e) {
080            // remove OUT message, as an exception occurred
081            exchange.setOut(null);
082            exchange.setException(e);
083        } finally {
084            // The Iterator will close the stream itself
085            if (!(result instanceof Iterator)) {
086                IOHelper.close(stream, "input stream");
087            }
088        }
089        callback.done(true);
090        return true;
091    }
092
093    public String toString() {
094        return "Unmarshal[" + dataFormat + "]";
095    }
096
097    public String getTraceLabel() {
098        return "unmarshal[" + dataFormat + "]";
099    }
100
101    public CamelContext getCamelContext() {
102        return camelContext;
103    }
104
105    public void setCamelContext(CamelContext camelContext) {
106        this.camelContext = camelContext;
107    }
108
109    @Override
110    protected void doStart() throws Exception {
111        // inject CamelContext on data format
112        if (dataFormat instanceof CamelContextAware) {
113            ((CamelContextAware) dataFormat).setCamelContext(camelContext);
114        }
115        ServiceHelper.startService(dataFormat);
116    }
117
118    @Override
119    protected void doStop() throws Exception {
120        ServiceHelper.stopService(dataFormat);
121    }
122
123}