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.net.URLDecoder;
020    import java.util.Map;
021    
022    import org.apache.camel.Endpoint;
023    import org.apache.camel.impl.UriEndpointComponent;
024    import org.apache.camel.spi.Language;
025    import org.apache.camel.util.ObjectHelper;
026    import org.apache.camel.util.ResourceHelper;
027    
028    /**
029     * The <a href="http://camel.apache.org/language-component.html">language component</a> to send
030     * {@link org.apache.camel.Exchange}s to a given language and have the script being executed.
031     *
032     * @version 
033     */
034    public class LanguageComponent extends UriEndpointComponent {
035    
036        public static final String RESOURCE = "resource:";
037    
038        public LanguageComponent() {
039            super(LanguageEndpoint.class);
040        }
041    
042        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
043            String name = ObjectHelper.before(remaining, ":");
044            String script = ObjectHelper.after(remaining, ":");
045            // no script then remaining is the language name
046            if (name == null && script == null) {
047                name = remaining;
048            }
049    
050            if (ObjectHelper.isEmpty(name)) {
051                throw new IllegalArgumentException("Illegal syntax. Name of language not given in uri: " + uri);
052            }
053            Language language = getCamelContext().resolveLanguage(name);
054    
055            String resourceUri = null;
056            String resource = script;
057            if (resource != null) {
058                if (resource.startsWith(RESOURCE)) {
059                    resource = resource.substring(RESOURCE.length());
060                }
061                if (ResourceHelper.hasScheme(resource)) {
062                    // the script is a uri for a resource
063                    resourceUri = resource;
064                    // then the script should be null
065                    script = null;
066                } else {
067                    // the script is provided as text in the uri, so decode to utf-8
068                    script = URLDecoder.decode(script, "UTF-8");
069                    // then the resource should be null
070                    resourceUri = null;
071                }
072            }
073    
074            LanguageEndpoint endpoint = new LanguageEndpoint(uri, this, language, null, resourceUri);
075            endpoint.setScript(script);
076            setProperties(endpoint, parameters);
077            return endpoint;
078        }
079    
080    }