1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider;
18
19 import org.apache.commons.lang3.StringUtils;
20 import org.apache.commons.vfs2.FileName;
21 import org.apache.commons.vfs2.FileType;
22
23
24
25
26
27 public class GenericFileName extends AbstractFileName {
28 private static final char[] USERNAME_RESERVED = { ':', '@', '/' };
29 private static final char[] PASSWORD_RESERVED = { '@', '/', '?' };
30 private final String userName;
31 private final String hostName;
32 private final int defaultPort;
33 private final String password;
34 private final int port;
35
36 protected GenericFileName(final String scheme, final String hostName, final int port, final int defaultPort,
37 final String userName, final String password, final String path, final FileType type) {
38 super(scheme, path, type);
39 this.hostName = hostName;
40 this.defaultPort = defaultPort;
41 this.password = password;
42 this.userName = userName;
43 this.port = port > 0 ? port : defaultPort;
44 }
45
46
47
48
49
50
51 public String getUserName() {
52 return userName;
53 }
54
55
56
57
58
59
60 public String getPassword() {
61 return password;
62 }
63
64
65
66
67
68
69 public String getHostName() {
70 return hostName;
71 }
72
73
74
75
76
77
78 public int getPort() {
79 return port;
80 }
81
82
83
84
85
86
87 public int getDefaultPort() {
88 return defaultPort;
89 }
90
91
92
93
94
95
96
97
98 @Override
99 public FileName createName(final String absPath, final FileType type) {
100 return new GenericFileName(getScheme(), hostName, port, defaultPort, userName, password, absPath, type);
101 }
102
103
104
105
106 @Override
107 protected void appendRootUri(final StringBuilder buffer, final boolean addPassword) {
108 buffer.append(getScheme());
109 buffer.append("://");
110 appendCredentials(buffer, addPassword);
111 buffer.append(hostName);
112 if (port != getDefaultPort()) {
113 buffer.append(':');
114 buffer.append(port);
115 }
116 }
117
118
119
120
121
122
123
124
125
126
127 protected void appendCredentials(final StringBuilder buffer, final boolean addPassword) {
128 if (!StringUtils.isEmpty(userName)) {
129 UriParser.appendEncoded(buffer, userName, USERNAME_RESERVED);
130 if (password != null && !password.isEmpty()) {
131 buffer.append(':');
132 if (addPassword) {
133 UriParser.appendEncoded(buffer, password, PASSWORD_RESERVED);
134 } else {
135 buffer.append("***");
136 }
137 }
138 buffer.append('@');
139 }
140 }
141 }