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.commons.vfs.provider;
018:
019: import org.apache.commons.vfs.FileName;
020: import org.apache.commons.vfs.FileType;
021:
022: /**
023: * A file name that represents a 'generic' URI, as per RFC 2396. Consists of
024: * a scheme, userinfo (typically username and password), hostname, port, and
025: * path.
026: *
027: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
028: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
029: */
030: public class GenericFileName extends AbstractFileName {
031: private final String userName;
032: private final String hostName;
033: private final int defaultPort;
034: private final String password;
035: private final int port;
036: private static final char[] USERNAME_RESERVED = { ':', '@', '/' };
037: private static final char[] PASSWORD_RESERVED = { '@', '/', '?' };
038:
039: protected GenericFileName(final String scheme,
040: final String hostName, final int port,
041: final int defaultPort, final String userName,
042: final String password, final String path,
043: final FileType type) {
044: super (scheme, path, type);
045: this .hostName = hostName;
046: this .defaultPort = defaultPort;
047: this .password = password;
048: this .userName = userName;
049: if (port > 0) {
050: this .port = port;
051: } else {
052: this .port = getDefaultPort();
053: }
054: }
055:
056: /**
057: * Returns the user name part of this name.
058: */
059: public String getUserName() {
060: return userName;
061: }
062:
063: /**
064: * Returns the password part of this name.
065: */
066: public String getPassword() {
067: return password;
068: }
069:
070: /**
071: * Returns the host name part of this name.
072: */
073: public String getHostName() {
074: return hostName;
075: }
076:
077: /**
078: * Returns the port part of this name.
079: */
080: public int getPort() {
081: return port;
082: }
083:
084: /**
085: * Returns the default port for this file name.
086: */
087: public int getDefaultPort() {
088: return defaultPort;
089: }
090:
091: public FileName createName(String absPath, FileType type) {
092: return new GenericFileName(getScheme(), hostName, port,
093: defaultPort, userName, password, absPath, type);
094: }
095:
096: /**
097: * Builds the root URI for this file name.
098: */
099: protected void appendRootUri(final StringBuffer buffer,
100: boolean addPassword) {
101: buffer.append(getScheme());
102: buffer.append("://");
103: appendCredentials(buffer, addPassword);
104: buffer.append(hostName);
105: if (port != getDefaultPort()) {
106: buffer.append(':');
107: buffer.append(port);
108: }
109: }
110:
111: /**
112: * append the user credentials
113: */
114: protected void appendCredentials(StringBuffer buffer,
115: boolean addPassword) {
116: if (userName != null && userName.length() != 0) {
117: UriParser
118: .appendEncoded(buffer, userName, USERNAME_RESERVED);
119: if (password != null && password.length() != 0) {
120: buffer.append(':');
121: if (addPassword) {
122: UriParser.appendEncoded(buffer, password,
123: PASSWORD_RESERVED);
124: } else {
125: buffer.append("*****");
126: }
127: }
128: buffer.append('@');
129: }
130: }
131: }
|