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