View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * A file name that represents a 'generic' URI, as per RFC 2396. Consists of a scheme, userinfo (typically username and
25   * password), hostname, port, and path.
26   */
27  public class GenericFileName extends AbstractFileName {
28  
29      private final String userName;
30      private final String hostName;
31      private final int defaultPort;
32      private final String password;
33      private final int port;
34  
35      /**
36       * Constructs a new instance.
37       *
38       * @param scheme the scheme.
39       * @param hostName the host name.
40       * @param port the port.
41       * @param defaultPort the default port.
42       * @param userName the user name.
43       * @param password the password.
44       * @param path the absolute path, maybe empty or null.
45       * @param type the file type.
46       */
47      protected GenericFileName(final String scheme, final String hostName, final int port, final int defaultPort,
48          final String userName, final String password, final String path, final FileType type) {
49          super(scheme, path, type);
50          this.hostName = hostName;
51          this.defaultPort = defaultPort;
52          this.password = password;
53          this.userName = userName;
54          this.port = port > 0 ? port : defaultPort;
55      }
56  
57      /**
58       * Append the user credentials.
59       * <p>
60       * If anything was added, it will be '@' terminated.
61       * </p>
62       *
63       * @param buffer the string buffer to modify.
64       * @param addPassword flag if password should be added or replaced with placeholder (false).
65       */
66      protected void appendCredentials(final StringBuilder buffer, final boolean addPassword) {
67          if (!StringUtils.isEmpty(userName)) {
68              UriParser.appendEncodedRfc2396(buffer, userName, RFC2396.USERINFO_UNESCAPED);
69              if (password != null && !password.isEmpty()) {
70                  buffer.append(':');
71                  if (addPassword) {
72                      UriParser.appendEncodedRfc2396(buffer, password, RFC2396.USERINFO_UNESCAPED);
73                  } else {
74                      buffer.append("***");
75                  }
76              }
77              buffer.append('@');
78          }
79      }
80  
81      /**
82       * Builds the root URI for this file name.
83       */
84      @Override
85      protected void appendRootUri(final StringBuilder buffer, final boolean addPassword) {
86          buffer.append(getScheme());
87          buffer.append("://");
88          appendCredentials(buffer, addPassword);
89          buffer.append(hostName);
90          if (port != getDefaultPort()) {
91              buffer.append(':');
92              buffer.append(port);
93          }
94      }
95  
96      /**
97       * Create a FileName.
98       *
99       * @param absPath The absolute path.
100      * @param type The FileType.
101      * @return The created FileName.
102      */
103     @Override
104     public FileName createName(final String absPath, final FileType type) {
105         return new GenericFileName(getScheme(), hostName, port, defaultPort, userName, password, absPath, type);
106     }
107 
108     /**
109      * Returns the default port for this file name.
110      *
111      * @return The default port number.
112      */
113     public int getDefaultPort() {
114         return defaultPort;
115     }
116 
117     /**
118      * Returns the host name part of this name.
119      *
120      * @return The host name.
121      */
122     public String getHostName() {
123         return hostName;
124     }
125 
126     /**
127      * Returns the password part of this name.
128      *
129      * @return The password.
130      */
131     public String getPassword() {
132         return password;
133     }
134 
135     /**
136      * Returns the port part of this name.
137      *
138      * @return The port number.
139      */
140     public int getPort() {
141         return port;
142     }
143 
144     /**
145      * Returns the user name part of this name.
146      *
147      * @return The user name.
148      */
149     public String getUserName() {
150         return userName;
151     }
152 }