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.InputStream; 020 021 import org.apache.camel.CamelExchangeException; 022 import org.apache.camel.Exchange; 023 import org.apache.camel.Expression; 024 import org.apache.camel.impl.DefaultProducer; 025 import org.apache.camel.util.IOHelper; 026 import org.apache.camel.util.ObjectHelper; 027 028 /** 029 * Language producer. 030 * 031 * @version 032 */ 033 public class LanguageProducer extends DefaultProducer { 034 035 public LanguageProducer(LanguageEndpoint endpoint) { 036 super(endpoint); 037 } 038 039 public void process(Exchange exchange) throws Exception { 040 // is there a custom expression in the header? 041 Expression exp = exchange.getIn().getHeader(Exchange.LANGUAGE_SCRIPT, Expression.class); 042 if (exp == null) { 043 String script = exchange.getIn().getHeader(Exchange.LANGUAGE_SCRIPT, String.class); 044 if (script != null) { 045 // the script may be a file: so resolve it before using 046 script = getEndpoint().resolveScript(script); 047 exp = getEndpoint().getLanguage().createExpression(script); 048 } 049 } 050 // if not fallback to use expression from endpoint 051 if (exp == null) { 052 exp = getEndpoint().getExpression(); 053 } 054 055 // fallback and use resource uri from endpoint 056 if (exp == null) { 057 if (getEndpoint().getResourceUri() != null) { 058 // load the resource 059 String script; 060 InputStream is = getEndpoint().getResourceAsInputStream(); 061 try { 062 script = getEndpoint().getCamelContext().getTypeConverter().convertTo(String.class, exchange, is); 063 } finally { 064 IOHelper.close(is); 065 } 066 // create the expression from the script 067 exp = getEndpoint().getLanguage().createExpression(script); 068 // expression was resolved from resource 069 getEndpoint().setContentResolvedFromResource(true); 070 // if we cache then set this as expression on endpoint so we don't re-create it again 071 if (getEndpoint().isContentCache()) { 072 getEndpoint().setExpression(exp); 073 } 074 } else { 075 // no script to execute 076 throw new CamelExchangeException("No script to evaluate", exchange); 077 } 078 } 079 080 ObjectHelper.notNull(exp, "expression"); 081 082 Object result = exp.evaluate(exchange, Object.class); 083 log.debug("Evaluated expression as: {} with: {}", result, exchange); 084 085 // set message body if transform is enabled 086 if (getEndpoint().isTransform()) { 087 if (exchange.hasOut()) { 088 exchange.getOut().setBody(result); 089 } else { 090 exchange.getIn().setBody(result); 091 } 092 } 093 } 094 095 @Override 096 public LanguageEndpoint getEndpoint() { 097 return (LanguageEndpoint) super.getEndpoint(); 098 } 099 }