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 org.apache.camel.Exchange;
020 import org.apache.camel.Message;
021 import org.apache.camel.Processor;
022 import org.apache.camel.impl.DefaultMessage;
023 import org.apache.camel.support.ServiceSupport;
024 import org.apache.camel.util.IOHelper;
025 import org.apache.camel.util.ObjectHelper;
026
027 /**
028 * A processor which converts the payload of the input message to be of the given type
029 * <p/>
030 * If the conversion fails an {@link org.apache.camel.InvalidPayloadException} is thrown.
031 *
032 * @version
033 */
034 public class ConvertBodyProcessor extends ServiceSupport implements Processor {
035 private final Class<?> type;
036 private final String charset;
037
038 public ConvertBodyProcessor(Class<?> type) {
039 ObjectHelper.notNull(type, "type", this);
040 this.type = type;
041 this.charset = null;
042 }
043
044 public ConvertBodyProcessor(Class<?> type, String charset) {
045 ObjectHelper.notNull(type, "type", this);
046 this.type = type;
047 this.charset = IOHelper.normalizeCharset(charset);
048 }
049
050 @Override
051 public String toString() {
052 return "convertBodyTo[" + type.getCanonicalName() + "]";
053 }
054
055 public void process(Exchange exchange) throws Exception {
056 Message in = exchange.getIn();
057 if (in.getBody() == null) {
058 // only convert if the is a body
059 return;
060 }
061
062 if (charset != null) {
063 // override existing charset with configured charset as that is what the user
064 // have explicit configured and expects to be used
065 exchange.setProperty(Exchange.CHARSET_NAME, charset);
066 }
067 // use mandatory conversion
068 Object value = in.getMandatoryBody(type);
069
070 // create a new message container so we do not drag specialized message objects along
071 Message msg = new DefaultMessage();
072 msg.copyFrom(in);
073 msg.setBody(value);
074
075 if (exchange.getPattern().isOutCapable()) {
076 exchange.setOut(msg);
077 } else {
078 exchange.setIn(msg);
079 }
080
081 // remove charset when we are done as we should not propagate that,
082 // as that can lead to double converting later on
083 if (charset != null) {
084 exchange.removeProperty(Exchange.CHARSET_NAME);
085 }
086 }
087
088 public Class<?> getType() {
089 return type;
090 }
091
092 @Override
093 protected void doStart() throws Exception {
094 // noop
095 }
096
097 @Override
098 protected void doStop() throws Exception {
099 // noop
100 }
101 }