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 */ 017package org.apache.commons.vfs2.provider; 018 019import org.apache.commons.vfs2.FileName; 020import org.apache.commons.vfs2.FileType; 021 022/** 023 * A file name that represents a 'generic' URI, as per RFC 2396. Consists of a scheme, userinfo (typically username and 024 * password), hostname, port, and path. 025 */ 026public class GenericFileName extends AbstractFileName { 027 private static final char[] USERNAME_RESERVED = { ':', '@', '/' }; 028 private static final char[] PASSWORD_RESERVED = { '@', '/', '?' }; 029 private final String userName; 030 private final String hostName; 031 private final int defaultPort; 032 private final String password; 033 private final int port; 034 035 protected GenericFileName(final String scheme, final String hostName, final int port, final int defaultPort, 036 final String userName, final String password, final String path, final FileType type) { 037 super(scheme, path, type); 038 this.hostName = hostName; 039 this.defaultPort = defaultPort; 040 this.password = password; 041 this.userName = userName; 042 if (port > 0) { 043 this.port = port; 044 } else { 045 this.port = getDefaultPort(); 046 } 047 } 048 049 /** 050 * Returns the user name part of this name. 051 * 052 * @return The user name. 053 */ 054 public String getUserName() { 055 return userName; 056 } 057 058 /** 059 * Returns the password part of this name. 060 * 061 * @return The password. 062 */ 063 public String getPassword() { 064 return password; 065 } 066 067 /** 068 * Returns the host name part of this name. 069 * 070 * @return The host name. 071 */ 072 public String getHostName() { 073 return hostName; 074 } 075 076 /** 077 * Returns the port part of this name. 078 * 079 * @return The port number. 080 */ 081 public int getPort() { 082 return port; 083 } 084 085 /** 086 * Returns the default port for this file name. 087 * 088 * @return The default port number. 089 */ 090 public int getDefaultPort() { 091 return defaultPort; 092 } 093 094 /** 095 * Create a FileName. 096 * 097 * @param absPath The absolute path. 098 * @param type The FileType. 099 * @return The created FileName. 100 */ 101 @Override 102 public FileName createName(final String absPath, final FileType type) { 103 return new GenericFileName(getScheme(), hostName, port, defaultPort, userName, password, absPath, type); 104 } 105 106 /** 107 * Builds the root URI for this file name. 108 */ 109 @Override 110 protected void appendRootUri(final StringBuilder buffer, final boolean addPassword) { 111 buffer.append(getScheme()); 112 buffer.append("://"); 113 appendCredentials(buffer, addPassword); 114 buffer.append(hostName); 115 if (port != getDefaultPort()) { 116 buffer.append(':'); 117 buffer.append(port); 118 } 119 } 120 121 /** 122 * Append the user credentials. 123 * <p> 124 * If anything was added, it will be '@' terminated. 125 * 126 * @param buffer the string buffer to modify. 127 * @param addPassword flag if password should be added or replaced with placeholder (false). 128 */ 129 protected void appendCredentials(final StringBuilder buffer, final boolean addPassword) { 130 if (userName != null && userName.length() != 0) { 131 UriParser.appendEncoded(buffer, userName, USERNAME_RESERVED); 132 if (password != null && password.length() != 0) { 133 buffer.append(':'); 134 if (addPassword) { 135 UriParser.appendEncoded(buffer, password, PASSWORD_RESERVED); 136 } else { 137 buffer.append("***"); 138 } 139 } 140 buffer.append('@'); 141 } 142 } 143}