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.Message;
023import org.apache.camel.impl.DefaultMessage;
024import org.apache.camel.spi.IdAware;
025import org.apache.camel.support.ServiceSupport;
026import org.apache.camel.util.AsyncProcessorHelper;
027import org.apache.camel.util.ExchangeHelper;
028import org.apache.camel.util.IOHelper;
029import org.apache.camel.util.ObjectHelper;
030
031/**
032 * A processor which converts the payload of the input message to be of the given type
033 * <p/>
034 * If the conversion fails an {@link org.apache.camel.InvalidPayloadException} is thrown.
035 *
036 * @version 
037 */
038public class ConvertBodyProcessor extends ServiceSupport implements AsyncProcessor, IdAware {
039    private String id;
040    private final Class<?> type;
041    private final String charset;
042
043    public ConvertBodyProcessor(Class<?> type) {
044        ObjectHelper.notNull(type, "type", this);
045        this.type = type;
046        this.charset = null;
047    }
048
049    public ConvertBodyProcessor(Class<?> type, String charset) {
050        ObjectHelper.notNull(type, "type", this);
051        this.type = type;
052        this.charset = IOHelper.normalizeCharset(charset);
053    }
054
055    @Override
056    public String toString() {
057        return "convertBodyTo[" + type.getCanonicalName() + "]";
058    }
059
060    public String getId() {
061        return id;
062    }
063
064    public void setId(String id) {
065        this.id = id;
066    }
067
068    public void process(Exchange exchange) throws Exception {
069        AsyncProcessorHelper.process(this, exchange);
070    }
071
072    @Override
073    public boolean process(Exchange exchange, AsyncCallback callback) {
074        boolean out = exchange.hasOut();
075        Message old = out ? exchange.getOut() : exchange.getIn();
076
077        if (old.getBody() == null) {
078            // only convert if the is a body
079            callback.done(true);
080            return true;
081        }
082
083        if (exchange.getException() != null) {
084            // do not convert if an exception has been thrown as if we attempt to convert and it also fails with a new
085            // exception then it will override the existing exception
086            callback.done(true);
087            return true;
088        }
089
090        if (charset != null) {
091            // override existing charset with configured charset as that is what the user
092            // have explicit configured and expects to be used
093            exchange.setProperty(Exchange.CHARSET_NAME, charset);
094        }
095        // use mandatory conversion
096        Object value;
097        try {
098            value = old.getMandatoryBody(type);
099        } catch (Throwable e) {
100            exchange.setException(e);
101            callback.done(true);
102            return true;
103        }
104
105        // create a new message container so we do not drag specialized message objects along
106        // but that is only needed if the old message is a specialized message
107        boolean copyNeeded = !(old.getClass().equals(DefaultMessage.class));
108
109        if (copyNeeded) {
110            Message msg = new DefaultMessage(exchange.getContext());
111            msg.copyFromWithNewBody(old, value);
112
113            // replace message on exchange
114            ExchangeHelper.replaceMessage(exchange, msg, false);
115        } else {
116            // no copy needed so set replace value directly
117            old.setBody(value);
118        }
119
120        // remove charset when we are done as we should not propagate that,
121        // as that can lead to double converting later on
122        if (charset != null) {
123            exchange.removeProperty(Exchange.CHARSET_NAME);
124        }
125
126        callback.done(true);
127        return true;
128    }
129
130    public Class<?> getType() {
131        return type;
132    }
133
134    public String getCharset() {
135        return charset;
136    }
137
138    @Override
139    protected void doStart() throws Exception {
140        // noop
141    }
142
143    @Override
144    protected void doStop() throws Exception {
145        // noop
146    }
147}