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 */
017package org.apache.camel.component.language;
018
019import java.io.InputStream;
020
021import org.apache.camel.CamelExchangeException;
022import org.apache.camel.Exchange;
023import org.apache.camel.Expression;
024import org.apache.camel.impl.DefaultProducer;
025import org.apache.camel.util.IOHelper;
026import org.apache.camel.util.ResourceHelper;
027import org.apache.camel.util.ServiceHelper;
028
029/**
030 * Language producer.
031 *
032 * @version 
033 */
034public class LanguageProducer extends DefaultProducer {
035
036    public LanguageProducer(LanguageEndpoint endpoint) {
037        super(endpoint);
038    }
039
040    public void process(Exchange exchange) throws Exception {
041        String script = null;
042
043        // is there a custom expression in the header?
044        Expression exp = exchange.getIn().getHeader(Exchange.LANGUAGE_SCRIPT, Expression.class);
045        if (exp == null) {
046            script = exchange.getIn().getHeader(Exchange.LANGUAGE_SCRIPT, String.class);
047            if (script != null) {
048                // the script may be a file: so resolve it before using
049                script = getEndpoint().resolveScript(script);
050                exp = getEndpoint().getLanguage().createExpression(script);
051            }
052        }
053        // if not fallback to use expression from endpoint
054        if (exp == null && getEndpoint().isCacheScript()) {
055            exp = getEndpoint().getExpression();
056        }
057
058        // the script can be a resource from the endpoint,
059        // or refer to a resource itself
060        // or just be a plain string
061        InputStream is = null;
062
063        // fallback and use resource uri from endpoint
064        if (exp == null) {
065            script = getEndpoint().getScript();
066
067            if (script == null && getEndpoint().getResourceUri() == null) {
068                // no script to execute
069                throw new CamelExchangeException("No script to evaluate", exchange);
070            }
071
072            if (script == null) {
073                is = getEndpoint().getResourceAsInputStream();
074            } else if (ResourceHelper.hasScheme(script)) {
075                is = ResourceHelper.resolveMandatoryResourceAsInputStream(getEndpoint().getCamelContext().getClassResolver(), script);
076            }
077
078            if (is != null && !getEndpoint().isBinary()) {
079                try {
080                    script = getEndpoint().getCamelContext().getTypeConverter().convertTo(String.class, exchange, is);
081                } finally {
082                    IOHelper.close(is);
083                }
084            }
085        }
086
087        // if we have a text based script then use and evaluate it
088        if (script != null) {
089            // create the expression from the script
090            exp = getEndpoint().getLanguage().createExpression(script);
091            // expression was resolved from resource
092            getEndpoint().setContentResolvedFromResource(true);
093            // if we cache then set this as expression on endpoint so we don't re-create it again
094            if (getEndpoint().isCacheScript()) {
095                getEndpoint().setExpression(exp);
096            }
097        }
098
099        // the result is either the result of the expression or the input stream as-is because its binary content
100        Object result;
101        if (exp != null) {
102            try {
103                result = exp.evaluate(exchange, Object.class);
104                log.debug("Evaluated expression as: {} with: {}", result, exchange);
105            } finally {
106                if (!getEndpoint().isCacheScript()) {
107                    // some languages add themselves as a service which we then need to remove if we are not cached
108                    ServiceHelper.stopService(exp);
109                    getEndpoint().getCamelContext().removeService(exp);
110                }
111            }
112        } else {
113            // use the result as-is
114            result = is;
115        }
116
117        // set message body if transform is enabled
118        if (getEndpoint().isTransform()) {
119            if (exchange.hasOut()) {
120                exchange.getOut().setBody(result);
121            } else {
122                exchange.getIn().setBody(result);
123            }
124        }
125    }
126
127    @Override
128    public LanguageEndpoint getEndpoint() {
129        return (LanguageEndpoint) super.getEndpoint();
130    }
131}