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