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.vfs2.FileName;
20  import org.apache.commons.vfs2.FileType;
21  
22  /**
23   * A file name that represents a 'generic' URI, as per RFC 2396. Consists of a scheme, userinfo (typically username and
24   * password), hostname, port, and path.
25   */
26  public class GenericFileName extends AbstractFileName {
27      private static final char[] USERNAME_RESERVED = { ':', '@', '/' };
28      private static final char[] PASSWORD_RESERVED = { '@', '/', '?' };
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      protected GenericFileName(final String scheme, final String hostName, final int port, final int defaultPort,
36              final String userName, final String password, final String path, final FileType type) {
37          super(scheme, path, type);
38          this.hostName = hostName;
39          this.defaultPort = defaultPort;
40          this.password = password;
41          this.userName = userName;
42          if (port > 0) {
43              this.port = port;
44          } else {
45              this.port = getDefaultPort();
46          }
47      }
48  
49      /**
50       * Returns the user name part of this name.
51       *
52       * @return The user name.
53       */
54      public String getUserName() {
55          return userName;
56      }
57  
58      /**
59       * Returns the password part of this name.
60       *
61       * @return The password.
62       */
63      public String getPassword() {
64          return password;
65      }
66  
67      /**
68       * Returns the host name part of this name.
69       *
70       * @return The host name.
71       */
72      public String getHostName() {
73          return hostName;
74      }
75  
76      /**
77       * Returns the port part of this name.
78       *
79       * @return The port number.
80       */
81      public int getPort() {
82          return port;
83      }
84  
85      /**
86       * Returns the default port for this file name.
87       *
88       * @return The default port number.
89       */
90      public int getDefaultPort() {
91          return defaultPort;
92      }
93  
94      /**
95       * Create a FileName.
96       *
97       * @param absPath The absolute path.
98       * @param type The FileType.
99       * @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      * </p>
126      *
127      * @param buffer the string buffer to modify.
128      * @param addPassword flag if password should be added or replaced with placeholder (false).
129      */
130     protected void appendCredentials(final StringBuilder buffer, final boolean addPassword) {
131         if (userName != null && userName.length() != 0) {
132             UriParser.appendEncoded(buffer, userName, USERNAME_RESERVED);
133             if (password != null && password.length() != 0) {
134                 buffer.append(':');
135                 if (addPassword) {
136                     UriParser.appendEncoded(buffer, password, PASSWORD_RESERVED);
137                 } else {
138                     buffer.append("***");
139                 }
140             }
141             buffer.append('@');
142         }
143     }
144 }