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: */
018: package org.apache.ivy.plugins.repository.vfs;
019:
020: import org.apache.ivy.Ivy;
021:
022: public class VfsURI {
023: private String host;
024:
025: private String passwd;
026:
027: private String path;
028:
029: private String scheme;
030:
031: private String user;
032:
033: // VFS Schemes
034: public static final String SCHEME_CIFS = "smb";
035:
036: public static final String SCHEME_FILE = "file";
037:
038: public static final String SCHEME_FTP = "ftp";
039:
040: public static final String SCHEME_HTTP = "http";
041:
042: public static final String SCHEME_HTTPS = "https";
043:
044: public static final String SCHEME_SFTP = "sftp";
045:
046: public static final String SCHEME_WEBDAV = "webdav";
047:
048: public static final String[] SUPPORTED_SCHEMES = new String[] {
049: // add other schemes here if other can be tested on your machine
050: SCHEME_FILE };
051:
052: /**
053: * Create a set of valid VFS URIs for the file access protocol
054: *
055: * @param resourcePath
056: * relative path (from the base repo) to the resource to be accessed
057: * @return
058: */
059: public static VfsURI vfsURIFactory(String scheme, String resource,
060: Ivy ivy) {
061: VfsURI vfsURI = null;
062: if (scheme.equals(SCHEME_CIFS)) {
063: vfsURI = new VfsURI(SCHEME_CIFS, ivy
064: .getVariable(VfsTestHelper.PROP_VFS_USER_ID), ivy
065: .getVariable(VfsTestHelper.PROP_VFS_USER_PASSWD),
066: ivy.getVariable(VfsTestHelper.PROP_VFS_HOST),
067: ivy.getVariable(VfsTestHelper.PROP_VFS_SAMBA_REPO)
068: + "/" + resource);
069: } else if (scheme.equals(SCHEME_FILE)) {
070: vfsURI = new VfsURI(SCHEME_FILE, null, null, null,
071: VfsTestHelper.CWD + "/"
072: + VfsTestHelper.TEST_REPO_DIR + "/"
073: + resource);
074: } else if (scheme.equals(SCHEME_FTP)) {
075: vfsURI = new VfsURI(SCHEME_FTP, ivy
076: .getVariable(VfsTestHelper.PROP_VFS_USER_ID), ivy
077: .getVariable(VfsTestHelper.PROP_VFS_USER_PASSWD),
078: ivy.getVariable(VfsTestHelper.PROP_VFS_HOST),
079: VfsTestHelper.CWD + "/"
080: + VfsTestHelper.TEST_REPO_DIR + "/"
081: + resource);
082: } else if (scheme.equals(SCHEME_SFTP)) {
083: vfsURI = new VfsURI(SCHEME_SFTP, ivy
084: .getVariable(VfsTestHelper.PROP_VFS_USER_ID), ivy
085: .getVariable(VfsTestHelper.PROP_VFS_USER_PASSWD),
086: ivy.getVariable(VfsTestHelper.PROP_VFS_HOST),
087: VfsTestHelper.CWD + "/"
088: + VfsTestHelper.TEST_REPO_DIR + "/"
089: + resource);
090: }
091: return vfsURI;
092: }
093:
094: /**
095: * Create a wellformed VFS resource identifier
096: *
097: * @param scheme
098: * the name of the scheme used to acces the resource
099: * @param user
100: * a user name. May be <code>null</code>
101: * @param passwd
102: * a passwd. May be <code>null</code>
103: * @param host
104: * a host identifier. May be <code>null</code>
105: * @param path
106: * a scheme spacific path to a resource
107: */
108: public VfsURI(String scheme, String user, String passwd,
109: String host, String path) {
110: this .scheme = scheme.trim();
111:
112: if (user != null) {
113: this .user = user.trim();
114: } else {
115: this .user = null;
116: }
117:
118: if (passwd != null) {
119: this .passwd = passwd.trim();
120: } else {
121: this .passwd = null;
122: }
123:
124: if (host != null) {
125: this .host = host.trim();
126: } else {
127: this .host = null;
128: }
129:
130: this .path = normalizePath(path);
131: }
132:
133: /**
134: * Return a well-formed VFS Resource identifier
135: *
136: * @return <code>String<code> representing a well formed VFS resource identifier
137: */
138: public String getVfsURI() {
139: StringBuffer uri = new StringBuffer();
140: uri.append(this .scheme + "://");
141:
142: // not all resource identifiers include user/passwd specifiers
143: if (user != null && user.trim().length() > 0) {
144: uri.append(this .user + ":");
145:
146: if (passwd != null && passwd.trim().length() > 0) {
147: this .passwd = passwd.trim();
148: } else {
149: this .passwd = "";
150: }
151: uri.append(this .passwd + "@");
152: }
153:
154: // not all resource identifiers include a host specifier
155: if (host != null && host.trim().length() > 0) {
156: this .host = host.trim();
157: uri.append(this .host);
158: }
159:
160: uri.append(this .path);
161: return uri.toString();
162: }
163:
164: /**
165: * Convert a resource path to the format required for a VFS resource identifier
166: *
167: * @param path
168: * <code>String</code> path to the resource
169: * @return <code>String</code> representing a normalized resource path
170: */
171: private String normalizePath(String path) {
172: // all backslashes replaced with forward slashes
173: String normalizedPath = path.replaceAll("\\\\", "/");
174:
175: // collapse multiple instance of forward slashes to single slashes
176: normalizedPath = normalizedPath.replaceAll("//+", "/");
177:
178: // ensure that our path starts with a forward slash
179: if (!normalizedPath.startsWith("/")) {
180: normalizedPath = "/" + normalizedPath;
181: }
182:
183: return normalizedPath.trim();
184: }
185:
186: public String toString() {
187: return getVfsURI();
188: }
189:
190: public String getScheme() {
191: return scheme;
192: }
193: }
|