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.vfs.provider;
    18
    19  import org.apache.commons.vfs.FileName;
    20  import org.apache.commons.vfs.FileType;
    21
    22  /***
    23   * A file name that represents a 'generic' URI, as per RFC 2396.  Consists of
    24   * a scheme, userinfo (typically username and password), hostname, port, and
    25   * path.
    26   *
    27   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
    28   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    29   */
    30  public class GenericFileName extends AbstractFileName
    31  {
    32      private final String userName;
    33      private final String hostName;
    34      private final int defaultPort;
    35      private final String password;
    36      private final int port;
    37      private static final char[] USERNAME_RESERVED = {':', '@', '/'};
    38      private static final char[] PASSWORD_RESERVED = {'@', '/', '?'};
    39
    40      protected GenericFileName(final String scheme,
    41                                final String hostName,
    42                                final int port,
    43                                final int defaultPort,
    44                                final String userName,
    45                                final String password,
    46                                final String path,
    47                                final FileType type
    48      )
    49      {
    50          super(scheme, path, type);
    51          this.hostName = hostName;
    52          this.defaultPort = defaultPort;
    53          this.password = password;
    54          this.userName = userName;
    55          if (port > 0)
    56          {
    57              this.port = port;
    58          }
    59          else
    60          {
    61              this.port = getDefaultPort();
    62          }
    63      }
    64
    65      /***
    66       * Returns the user name part of this name.
    67       */
    68      public String getUserName()
    69      {
    70          return userName;
    71      }
    72
    73      /***
    74       * Returns the password part of this name.
    75       */
    76      public String getPassword()
    77      {
    78          return password;
    79      }
    80
    81      /***
    82       * Returns the host name part of this name.
    83       */
    84      public String getHostName()
    85      {
    86          return hostName;
    87      }
    88
    89      /***
    90       * Returns the port part of this name.
    91       */
    92      public int getPort()
    93      {
    94          return port;
    95      }
    96
    97      /***
    98       * Returns the default port for this file name.
    99       */
    100     public int getDefaultPort()
    101     {
    102         return defaultPort;
    103     }
    104
    105     public FileName createName(String absPath, FileType type)
    106     {
    107         return new GenericFileName(
    108             getScheme(),
    109             hostName,
    110             port,
    111             defaultPort,
    112             userName,
    113             password,
    114             absPath,
    115             type);
    116     }
    117
    118     /***
    119      * Builds the root URI for this file name.
    120      */
    121     protected void appendRootUri(final StringBuffer buffer, boolean addPassword)
    122     {
    123         buffer.append(getScheme());
    124         buffer.append("://");
    125         appendCredentials(buffer, addPassword);
    126         buffer.append(hostName);
    127         if (port != getDefaultPort())
    128         {
    129             buffer.append(':');
    130             buffer.append(port);
    131         }
    132     }
    133
    134     /***
    135      * append the user credentials
    136      */
    137     protected void appendCredentials(StringBuffer buffer, boolean addPassword)
    138     {
    139         if (userName != null && userName.length() != 0)
    140         {
    141             UriParser.appendEncoded(buffer, userName, USERNAME_RESERVED);
    142             if (password != null && password.length() != 0)
    143             {
    144                 buffer.append(':');
    145                 if (addPassword)
    146                 {
    147                     UriParser.appendEncoded(buffer, password, PASSWORD_RESERVED);
    148                 }
    149                 else
    150                 {
    151                     buffer.append("*****");
    152                 }
    153             }
    154             buffer.append('@');
    155         }
    156     }
    157 }