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.file.remote; 018 019 import java.net.URI; 020 import java.util.Map; 021 022 import org.apache.camel.CamelContext; 023 import org.apache.camel.RuntimeCamelException; 024 import org.apache.camel.impl.DefaultComponent; 025 import org.apache.commons.net.ftp.FTPClientConfig; 026 027 public class RemoteFileComponent extends DefaultComponent<RemoteFileExchange> { 028 private RemoteFileConfiguration configuration; 029 030 public RemoteFileComponent() { 031 this.configuration = new RemoteFileConfiguration(); 032 } 033 034 public RemoteFileComponent(RemoteFileConfiguration configuration) { 035 this.configuration = configuration; 036 } 037 038 public RemoteFileComponent(CamelContext context) { 039 super(context); 040 this.configuration = new RemoteFileConfiguration(); 041 } 042 043 public String toString() { 044 return "RemoteFileComponent"; 045 } 046 047 public static RemoteFileComponent remoteFileComponent() { 048 return new RemoteFileComponent(); 049 } 050 051 protected RemoteFileEndpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception { 052 RemoteFileConfiguration config = getConfiguration().copy(); 053 054 // get the base uri part before the options as they can be non URI valid such as the expression using $ chars 055 // and the URI constructor will regard $ as an illegal character and we dont want to enforce end users to 056 // to espace the $ for the expression (file language) 057 String baseUri = uri; 058 if (uri.indexOf("?") != -1) { 059 baseUri = uri.substring(0, uri.indexOf("?")); 060 } 061 config.configure(new URI(baseUri)); 062 063 // lets make sure we copy the configuration as each endpoint can 064 // customize its own version 065 final RemoteFileEndpoint endpoint; 066 if ("ftp".equals(config.getProtocol())) { 067 endpoint = new FtpEndpoint(uri, this, config); 068 } else if ("sftp".equals(config.getProtocol())) { 069 endpoint = new SftpEndpoint(uri, this, config); 070 } else { 071 throw new RuntimeCamelException("Unsupported protocol: " + config.getProtocol()); 072 } 073 074 configureFTPClientConfig(parameters, endpoint); 075 setProperties(endpoint.getConfiguration(), parameters); 076 return endpoint; 077 } 078 079 private void configureFTPClientConfig(Map parameters, RemoteFileEndpoint endpoint) { 080 // lookup client config in registry if provided 081 String ref = getAndRemoveParameter(parameters, "ftpClientConfig", String.class); 082 if (ref != null) { 083 FTPClientConfig ftpClientConfig = this.getCamelContext().getRegistry().lookup(ref, FTPClientConfig.class); 084 if (ftpClientConfig == null) { 085 throw new IllegalArgumentException("FTPClientConfig " + ref + " not found in registry."); 086 } 087 endpoint.getConfiguration().setFtpClientConfig(ftpClientConfig); 088 } 089 } 090 091 public RemoteFileConfiguration getConfiguration() { 092 return configuration; 093 } 094 095 public void setConfiguration(RemoteFileConfiguration configuration) { 096 this.configuration = configuration; 097 } 098 }