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.component.language; 018 019 import java.io.IOException; 020 import java.io.InputStream; 021 import java.io.UnsupportedEncodingException; 022 import java.net.URLEncoder; 023 024 import org.apache.camel.Component; 025 import org.apache.camel.Consumer; 026 import org.apache.camel.Expression; 027 import org.apache.camel.Processor; 028 import org.apache.camel.Producer; 029 import org.apache.camel.RuntimeCamelException; 030 import org.apache.camel.component.ResourceEndpoint; 031 import org.apache.camel.spi.Language; 032 import org.apache.camel.util.IOHelper; 033 import org.apache.camel.util.ObjectHelper; 034 import org.apache.camel.util.ResourceHelper; 035 036 /** 037 * Language endpoint. 038 * 039 * @version 040 */ 041 public class LanguageEndpoint extends ResourceEndpoint { 042 private Language language; 043 private Expression expression; 044 private String languageName; 045 private String script; 046 private boolean transform = true; 047 private boolean contentResolvedFromResource; 048 049 public LanguageEndpoint() { 050 // enable cache by default 051 setContentCache(true); 052 } 053 054 public LanguageEndpoint(String endpointUri, Component component, Language language, Expression expression, String resourceUri) { 055 super(endpointUri, component, resourceUri); 056 this.language = language; 057 this.expression = expression; 058 // enable cache by default 059 setContentCache(true); 060 } 061 062 public Producer createProducer() throws Exception { 063 ObjectHelper.notNull(getCamelContext(), "CamelContext", this); 064 065 if (language == null && languageName != null) { 066 language = getCamelContext().resolveLanguage(languageName); 067 } 068 069 ObjectHelper.notNull(language, "language", this); 070 if (expression == null && script != null) { 071 script = resolveScript(script); 072 expression = language.createExpression(script); 073 } 074 075 return new LanguageProducer(this); 076 } 077 078 public Consumer createConsumer(Processor processor) throws Exception { 079 throw new RuntimeCamelException("Cannot consume to a LanguageEndpoint: " + getEndpointUri()); 080 } 081 082 /** 083 * Resolves the script. 084 * 085 * @param script script or uri for a script to load 086 * @return the script 087 * @throws IOException is thrown if error loading the script 088 */ 089 protected String resolveScript(String script) throws IOException { 090 String answer; 091 if (ResourceHelper.hasScheme(script)) { 092 InputStream is = loadResource(script); 093 answer = getCamelContext().getTypeConverter().convertTo(String.class, is); 094 IOHelper.close(is); 095 } else { 096 answer = script; 097 } 098 099 return answer; 100 } 101 102 public boolean isSingleton() { 103 return true; 104 } 105 106 @Override 107 protected String createEndpointUri() { 108 String s = script; 109 try { 110 s = URLEncoder.encode(s, "UTF-8"); 111 } catch (UnsupportedEncodingException e) { 112 // ignore 113 } 114 return languageName + ":" + s; 115 } 116 117 public Language getLanguage() { 118 return language; 119 } 120 121 public Expression getExpression() { 122 if (isContentResolvedFromResource() && isContentCacheCleared()) { 123 return null; 124 } 125 return expression; 126 } 127 128 public void setExpression(Expression expression) { 129 this.expression = expression; 130 } 131 132 public boolean isTransform() { 133 return transform; 134 } 135 136 /** 137 * Whether or not the result of the script should be used as message body. 138 * <p/> 139 * This options is default <tt>true</tt>. 140 * 141 * @param transform <tt>true</tt> to use result as new message body, <tt>false</tt> to keep the existing message body 142 */ 143 public void setTransform(boolean transform) { 144 this.transform = transform; 145 } 146 147 /** 148 * Sets the name of the language to use 149 * 150 * @param languageName the name of the language 151 */ 152 public void setLanguageName(String languageName) { 153 this.languageName = languageName; 154 } 155 156 /** 157 * Sets the script to execute 158 * 159 * @param script the script 160 */ 161 public void setScript(String script) { 162 this.script = script; 163 } 164 165 public boolean isContentResolvedFromResource() { 166 return contentResolvedFromResource; 167 } 168 169 public void setContentResolvedFromResource(boolean contentResolvedFromResource) { 170 this.contentResolvedFromResource = contentResolvedFromResource; 171 } 172 }