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.Expression;
024 import org.apache.camel.impl.DefaultComponent;
025 import org.apache.camel.spi.Language;
026 import org.apache.camel.util.ObjectHelper;
027 import org.apache.camel.util.ResourceHelper;
028
029 /**
030 * The <a href="http://camel.apache.org/language-component.html">language component</a> to send
031 * {@link org.apache.camel.Exchange}s to a given language and have the script being executed.
032 *
033 * @version
034 */
035 public class LanguageComponent extends DefaultComponent {
036
037 protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
038 String name = ObjectHelper.before(remaining, ":");
039 String script = ObjectHelper.after(remaining, ":");
040 // no script then remaining is the language name
041 if (name == null && script == null) {
042 name = remaining;
043 }
044
045 if (ObjectHelper.isEmpty(name)) {
046 throw new IllegalArgumentException("Illegal syntax. Name of language not given in uri: " + uri);
047 }
048 Language language = getCamelContext().resolveLanguage(name);
049
050 Expression expression = null;
051 String resourceUri = null;
052 if (script != null) {
053 if (ResourceHelper.hasScheme(script)) {
054 // the script is a uri for a resource
055 resourceUri = script;
056 } else {
057 // the script is provided as text in the uri, so decode to utf-8
058 expression = language.createExpression(URLDecoder.decode(script, "UTF-8"));
059 }
060 }
061
062 return new LanguageEndpoint(uri, this, language, expression, resourceUri);
063 }
064 }