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