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.util.Collections; 020 import java.util.List; 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.RuntimeCamelException; 030 import org.apache.camel.processor.ConvertBodyProcessor; 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 ProcessorDefinition<ProcessorDefinition> { 039 @XmlAttribute 040 private String type; 041 @XmlAttribute(required = false) 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 Processor createProcessor(RouteContext routeContext) throws Exception { 076 if (getTypeClass() == null) { 077 this.typeClass = routeContext.getCamelContext().getClassResolver().resolveClass(getType()); 078 if (getTypeClass() == null) { 079 throw new RuntimeCamelException("Cannot load the class with the class name: " + getType()); 080 } 081 } 082 083 return new ConvertBodyProcessor(getTypeClass(), getCharset()); 084 } 085 086 @Override 087 @SuppressWarnings("unchecked") 088 public List<ProcessorDefinition> getOutputs() { 089 return Collections.EMPTY_LIST; 090 } 091 092 public String getType() { 093 return type; 094 } 095 096 public void setType(String type) { 097 this.type = type; 098 } 099 100 public Class getTypeClass() { 101 return typeClass; 102 } 103 104 public void setTypeClass(Class typeClass) { 105 this.typeClass = typeClass; 106 } 107 108 public String getCharset() { 109 return charset; 110 } 111 112 public void setCharset(String charset) { 113 this.charset = charset; 114 } 115 116 }