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, is);
063 } finally {
064 IOHelper.close(is);
065 }
066 // create the expression from the script
067 exp = getEndpoint().getLanguage().createExpression(script);
068 // if we cache then set this as expression on endpoint so we don't re-create it again
069 if (getEndpoint().isContentCache()) {
070 getEndpoint().setExpression(exp);
071 }
072 } else {
073 // no script to execute
074 throw new CamelExchangeException("No script to evaluate", exchange);
075 }
076 }
077
078 ObjectHelper.notNull(exp, "expression");
079
080 Object result = exp.evaluate(exchange, Object.class);
081 log.debug("Evaluated expression as: {} with: {}", result, exchange);
082
083 // set message body if transform is enabled
084 if (getEndpoint().isTransform()) {
085 if (exchange.hasOut()) {
086 exchange.getOut().setBody(result);
087 } else {
088 exchange.getIn().setBody(result);
089 }
090 }
091 }
092
093 @Override
094 public LanguageEndpoint getEndpoint() {
095 return (LanguageEndpoint) super.getEndpoint();
096 }
097 }