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.model;
018
019import java.nio.charset.Charset;
020import java.nio.charset.UnsupportedCharsetException;
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlRootElement;
025import javax.xml.bind.annotation.XmlTransient;
026
027import org.apache.camel.Processor;
028import org.apache.camel.processor.ConvertBodyProcessor;
029import org.apache.camel.spi.Metadata;
030import org.apache.camel.spi.RouteContext;
031
032/**
033 * Converts the message body to another type
034 */
035@Metadata(label = "eip,transformation")
036@XmlRootElement(name = "convertBodyTo")
037@XmlAccessorType(XmlAccessType.FIELD)
038public class ConvertBodyDefinition extends NoOutputDefinition<ConvertBodyDefinition> {
039    @XmlAttribute(required = true)
040    private String type;
041    @XmlAttribute
042    private String charset;
043    @XmlTransient
044    private Class<?> typeClass;
045
046    public ConvertBodyDefinition() {
047    }
048
049    public ConvertBodyDefinition(String type) {
050        setType(type);
051    }
052
053    public ConvertBodyDefinition(Class<?> typeClass) {
054        setTypeClass(typeClass);
055        setType(typeClass.getCanonicalName());
056    }
057
058    public ConvertBodyDefinition(Class<?> typeClass, String charset) {
059        setTypeClass(typeClass);
060        setType(typeClass.getCanonicalName());
061        setCharset(charset);
062    }
063
064    @Override
065    public String toString() {        
066        return "ConvertBodyTo[" + getType() + "]";
067    }
068
069    @Override
070    public String getLabel() {
071        return "convertBodyTo[" + getType() + "]";
072    }
073    
074    public static void validateCharset(String charset) throws UnsupportedCharsetException {
075        if (charset != null) {
076            if (Charset.isSupported(charset)) {
077                Charset.forName(charset);
078                return;
079            }
080        }
081        throw new UnsupportedCharsetException(charset);
082    }
083
084    @Override
085    public Processor createProcessor(RouteContext routeContext) throws Exception {
086        if (typeClass == null && type != null) {
087            typeClass = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(type);
088        }
089
090        // validate charset
091        if (charset != null) {
092            validateCharset(charset);
093        }
094
095        return new ConvertBodyProcessor(getTypeClass(), getCharset());
096    }
097
098    public String getType() {
099        return type;
100    }
101
102    /**
103     * The java type to convert to
104     */
105    public void setType(String type) {
106        this.type = type;
107    }
108
109    public Class<?> getTypeClass() {
110        return typeClass;
111    }
112
113    public void setTypeClass(Class<?> typeClass) {
114        this.typeClass = typeClass;
115    }
116
117    public String getCharset() {
118        return charset;
119    }
120
121    /**
122     * To use a specific charset when converting
123     */
124    public void setCharset(String charset) {
125        this.charset = charset;
126    }
127}