Add session to cache with both hostname and address.
[cyberduck.git] / source / ch / cyberduck / core / ProtocolFactory.java
blob55437277fc0d4c87e28338c3a5d772bc5d7ef1f1
1 package ch.cyberduck.core;
3 /*
4 * Copyright (c) 2002-2011 David Kocher. All rights reserved.
6 * http://cyberduck.ch/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * Bug fixes, suggestions and comments should be sent to:
19 * dkocher@cyberduck.ch
22 import ch.cyberduck.core.azure.AzureProtocol;
23 import ch.cyberduck.core.dav.DAVProtocol;
24 import ch.cyberduck.core.dav.DAVSSLProtocol;
25 import ch.cyberduck.core.exception.AccessDeniedException;
26 import ch.cyberduck.core.ftp.FTPProtocol;
27 import ch.cyberduck.core.ftp.FTPTLSProtocol;
28 import ch.cyberduck.core.gstorage.GoogleStorageProtocol;
29 import ch.cyberduck.core.irods.IRODSProtocol;
30 import ch.cyberduck.core.openstack.SwiftProtocol;
31 import ch.cyberduck.core.preferences.PreferencesFactory;
32 import ch.cyberduck.core.s3.S3Protocol;
33 import ch.cyberduck.core.sftp.SFTPProtocol;
35 import org.apache.commons.io.FilenameUtils;
36 import org.apache.commons.lang3.StringUtils;
37 import org.apache.log4j.Logger;
39 import java.util.ArrayList;
40 import java.util.LinkedHashSet;
41 import java.util.List;
42 import java.util.Set;
44 /**
45 * @version $Id$
47 public final class ProtocolFactory {
48 private static final Logger log = Logger.getLogger(ProtocolFactory.class);
50 public static final Protocol FTP = new FTPProtocol();
51 public static final Protocol FTP_TLS = new FTPTLSProtocol();
52 public static final Protocol SFTP = new SFTPProtocol();
53 public static final Protocol S3_SSL = new S3Protocol();
54 public static final Protocol WEBDAV = new DAVProtocol();
55 public static final Protocol WEBDAV_SSL = new DAVSSLProtocol();
56 public static final Protocol SWIFT = new SwiftProtocol();
57 public static final Protocol GOOGLESTORAGE_SSL = new GoogleStorageProtocol();
58 public static final Protocol AZURE = new AzureProtocol();
59 public static final Protocol IRODS = new IRODSProtocol();
61 /**
62 * Ordered list of supported protocols.
64 private static final Set<Protocol> protocols
65 = new LinkedHashSet<Protocol>();
67 private ProtocolFactory() {
71 public static void register() {
72 register(FTP, FTP_TLS, SFTP, WEBDAV, WEBDAV_SSL, SWIFT, S3_SSL, GOOGLESTORAGE_SSL, AZURE, IRODS);
75 public static void register(Protocol... protocols) {
76 // Order determines list in connection dropdown
77 for(Protocol protocol : protocols) {
78 register(protocol);
80 // Order determines list in connection dropdown
81 final Local bundled = LocalFactory.get(PreferencesFactory.get().getProperty("application.profiles.path"));
82 if(bundled.exists()) {
83 try {
84 for(Local f : bundled.list().filter(new Filter<Local>() {
85 @Override
86 public boolean accept(final Local file) {
87 return "cyberduckprofile".equals(FilenameUtils.getExtension(file.getName()));
89 })) {
90 final Profile profile = ProfileReaderFactory.get().read(f);
91 if(null == profile.getProtocol()) {
92 continue;
94 if(log.isInfoEnabled()) {
95 log.info(String.format("Adding bundled protocol %s", profile));
97 // Replace previous possibly disable protocol in Preferences
98 register(profile);
101 catch(AccessDeniedException e) {
102 log.warn(String.format("Failure reading collection %s %s", bundled, e.getMessage()));
105 // Load thirdparty protocols
106 final Local library = LocalFactory.get(PreferencesFactory.get().getProperty("application.support.path"),
107 PreferencesFactory.get().getProperty("profiles.folder.name"));
108 if(library.exists()) {
109 try {
110 for(Local profile : library.list().filter(new Filter<Local>() {
111 @Override
112 public boolean accept(final Local file) {
113 return "cyberduckprofile".equals(FilenameUtils.getExtension(file.getName()));
115 })) {
116 final Profile protocol = ProfileReaderFactory.get().read(profile);
117 if(null == protocol) {
118 continue;
120 if(log.isInfoEnabled()) {
121 log.info(String.format("Adding thirdparty protocol %s", protocol));
123 // Replace previous possibly disable protocol in Preferences
124 register(protocol);
127 catch(AccessDeniedException e) {
128 log.warn(String.format("Failure reading collection %s %s", library, e.getMessage()));
133 public static void register(final Protocol p) {
134 protocols.remove(p);
135 protocols.add(p);
139 * @return List of protocols
141 public static List<Protocol> getEnabledProtocols() {
142 final List<Protocol> enabled = new ArrayList<Protocol>();
143 for(Protocol protocol : protocols) {
144 if(protocol.isEnabled()) {
145 enabled.add(protocol);
148 return enabled;
152 * @param port Default port
153 * @return The standard protocol for this port number
155 public static Protocol getDefaultProtocol(final int port) {
156 for(Protocol protocol : getEnabledProtocols()) {
157 if(protocol.getDefaultPort() == port) {
158 return protocol;
161 log.warn(String.format("Cannot find default protocol for port %d", port));
162 return forName(PreferencesFactory.get().getProperty("connection.protocol.default"));
166 * @param identifier Provider name or hash code of protocol
167 * @return Matching protocol or null if no match
169 public static Protocol forName(final String identifier) {
170 for(Protocol protocol : protocols) {
171 if(protocol.getProvider().equals(identifier)) {
172 return protocol;
175 for(Protocol protocol : protocols) {
176 if(String.valueOf(protocol.hashCode()).equals(identifier)) {
177 return protocol;
180 for(Protocol protocol : protocols) {
181 for(String scheme : protocol.getSchemes()) {
182 if(scheme.equals(identifier)) {
183 return protocol;
187 log.warn(String.format("Unknown protocol with identifier %s", identifier));
188 return null;
192 * @param scheme Protocol scheme
193 * @return Standard protocol for this scheme. This is ambigous
195 public static Protocol forScheme(final String scheme) {
196 for(Protocol protocol : getEnabledProtocols()) {
197 for(int k = 0; k < protocol.getSchemes().length; k++) {
198 if(protocol.getSchemes()[k].equals(scheme)) {
199 return protocol;
203 log.warn(String.format("Unknown scheme %s", scheme));
204 return null;
208 * @param str Determine if URL can be handled by a registered protocol
209 * @return True if known URL
211 public static boolean isURL(final String str) {
212 if(StringUtils.isNotBlank(str)) {
213 for(Protocol protocol : getEnabledProtocols()) {
214 for(String scheme : protocol.getSchemes()) {
215 if(str.startsWith(scheme + "://")) {
216 return true;
221 return false;