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      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       * Returns the user name part of this name.
48       *
49       * @return The user name.
50       */
51      public String getUserName() {
52          return userName;
53      }
54  
55      /**
56       * Returns the password part of this name.
57       *
58       * @return The password.
59       */
60      public String getPassword() {
61          return password;
62      }
63  
64      /**
65       * Returns the host name part of this name.
66       *
67       * @return The host name.
68       */
69      public String getHostName() {
70          return hostName;
71      }
72  
73      /**
74       * Returns the port part of this name.
75       *
76       * @return The port number.
77       */
78      public int getPort() {
79          return port;
80      }
81  
82      /**
83       * Returns the default port for this file name.
84       *
85       * @return The default port number.
86       */
87      public int getDefaultPort() {
88          return defaultPort;
89      }
90  
91      /**
92       * Create a FileName.
93       *
94       * @param absPath The absolute path.
95       * @param type The FileType.
96       * @return The created FileName.
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      * Builds the root URI for this file name.
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      * Append the user credentials.
120      * <p>
121      * If anything was added, it will be '@' terminated.
122      * </p>
123      *
124      * @param buffer the string buffer to modify.
125      * @param addPassword flag if password should be added or replaced with placeholder (false).
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 }