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; 021 022import javax.xml.bind.annotation.XmlAccessType; 023import javax.xml.bind.annotation.XmlAccessorType; 024import javax.xml.bind.annotation.XmlAttribute; 025import javax.xml.bind.annotation.XmlRootElement; 026import javax.xml.bind.annotation.XmlTransient; 027 028import org.apache.camel.Processor; 029import org.apache.camel.processor.ConvertBodyProcessor; 030import org.apache.camel.spi.Metadata; 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.getCanonicalName()); 057 } 058 059 public ConvertBodyDefinition(Class<?> typeClass, String charset) { 060 setTypeClass(typeClass); 061 setType(typeClass.getCanonicalName()); 062 setCharset(charset); 063 } 064 065 @Override 066 public String toString() { 067 return "ConvertBodyTo[" + getType() + "]"; 068 } 069 070 @Override 071 public String getShortName() { 072 return "convertBodyTo"; 073 } 074 075 @Override 076 public String getLabel() { 077 return "convertBodyTo[" + getType() + "]"; 078 } 079 080 public static void validateCharset(String charset) throws UnsupportedCharsetException { 081 if (charset != null) { 082 if (Charset.isSupported(charset)) { 083 Charset.forName(charset); 084 return; 085 } 086 } 087 throw new UnsupportedCharsetException(charset); 088 } 089 090 @Override 091 public Processor createProcessor(RouteContext routeContext) throws Exception { 092 if (typeClass == null && type != null) { 093 typeClass = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(type); 094 } 095 096 // validate charset 097 if (charset != null) { 098 validateCharset(charset); 099 } 100 101 return new ConvertBodyProcessor(getTypeClass(), getCharset()); 102 } 103 104 public String getType() { 105 return type; 106 } 107 108 /** 109 * The java type to convert to 110 */ 111 public void setType(String type) { 112 this.type = type; 113 } 114 115 public Class<?> getTypeClass() { 116 return typeClass; 117 } 118 119 public void setTypeClass(Class<?> typeClass) { 120 this.typeClass = typeClass; 121 } 122 123 public String getCharset() { 124 return charset; 125 } 126 127 /** 128 * To use a specific charset when converting 129 */ 130 public void setCharset(String charset) { 131 this.charset = charset; 132 } 133}