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