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