1 package ch
.cyberduck
.core
;
4 * Copyright (c) 2002-2011 David Kocher. All rights reserved.
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
;
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();
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
) {
80 // Order determines list in connection dropdown
81 final Local bundled
= LocalFactory
.get(PreferencesFactory
.get().getProperty("application.profiles.path"));
82 if(bundled
.exists()) {
84 for(Local f
: bundled
.list().filter(new Filter
<Local
>() {
86 public boolean accept(final Local file
) {
87 return "cyberduckprofile".equals(FilenameUtils
.getExtension(file
.getName()));
90 final Profile profile
= ProfileReaderFactory
.get().read(f
);
91 if(null == profile
.getProtocol()) {
94 if(log
.isInfoEnabled()) {
95 log
.info(String
.format("Adding bundled protocol %s", profile
));
97 // Replace previous possibly disable protocol in Preferences
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()) {
110 for(Local profile
: library
.list().filter(new Filter
<Local
>() {
112 public boolean accept(final Local file
) {
113 return "cyberduckprofile".equals(FilenameUtils
.getExtension(file
.getName()));
116 final Profile protocol
= ProfileReaderFactory
.get().read(profile
);
117 if(null == protocol
) {
120 if(log
.isInfoEnabled()) {
121 log
.info(String
.format("Adding thirdparty protocol %s", protocol
));
123 // Replace previous possibly disable protocol in Preferences
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
) {
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
);
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
) {
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
)) {
175 for(Protocol protocol
: protocols
) {
176 if(String
.valueOf(protocol
.hashCode()).equals(identifier
)) {
180 for(Protocol protocol
: protocols
) {
181 for(String scheme
: protocol
.getSchemes()) {
182 if(scheme
.equals(identifier
)) {
187 log
.warn(String
.format("Unknown protocol with identifier %s", identifier
));
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
)) {
203 log
.warn(String
.format("Unknown scheme %s", scheme
));
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
+ "://")) {