001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.provider;
018
019import org.apache.commons.vfs2.FileName;
020import org.apache.commons.vfs2.FileType;
021
022/**
023 * A file name that represents a 'generic' URI, as per RFC 2396.  Consists of
024 * a scheme, userinfo (typically username and password), hostname, port, and
025 * path.
026 */
027public class GenericFileName extends AbstractFileName
028{
029    private static final char[] USERNAME_RESERVED = {':', '@', '/'};
030    private static final char[] PASSWORD_RESERVED = {'@', '/', '?'};
031    private final String userName;
032    private final String hostName;
033    private final int defaultPort;
034    private final String password;
035    private final int port;
036
037    protected GenericFileName(final String scheme,
038                              final String hostName,
039                              final int port,
040                              final int defaultPort,
041                              final String userName,
042                              final String password,
043                              final String path,
044                              final FileType type
045    )
046    {
047        super(scheme, path, type);
048        this.hostName = hostName;
049        this.defaultPort = defaultPort;
050        this.password = password;
051        this.userName = userName;
052        if (port > 0)
053        {
054            this.port = port;
055        }
056        else
057        {
058            this.port = getDefaultPort();
059        }
060    }
061
062    /**
063     * Returns the user name part of this name.
064     * @return The user name.
065     */
066    public String getUserName()
067    {
068        return userName;
069    }
070
071    /**
072     * Returns the password part of this name.
073     * @return The password.
074     */
075    public String getPassword()
076    {
077        return password;
078    }
079
080    /**
081     * Returns the host name part of this name.
082     * @return The host name.
083     */
084    public String getHostName()
085    {
086        return hostName;
087    }
088
089    /**
090     * Returns the port part of this name.
091     * @return The port number.
092     */
093    public int getPort()
094    {
095        return port;
096    }
097
098    /**
099     * Returns the default port for this file name.
100     * @return The default port number.
101     */
102    public int getDefaultPort()
103    {
104        return defaultPort;
105    }
106
107    /**
108     * Create a FileName.
109     * @param absPath The absolute path.
110     * @param type The FileType.
111     * @return The created FileName.
112     */
113    @Override
114    public FileName createName(final String absPath, final FileType type)
115    {
116        return new GenericFileName(
117            getScheme(),
118            hostName,
119            port,
120            defaultPort,
121            userName,
122            password,
123            absPath,
124            type);
125    }
126
127    /**
128     * Builds the root URI for this file name.
129     */
130    @Override
131    protected void appendRootUri(final StringBuilder buffer, final boolean addPassword)
132    {
133        buffer.append(getScheme());
134        buffer.append("://");
135        appendCredentials(buffer, addPassword);
136        buffer.append(hostName);
137        if (port != getDefaultPort())
138        {
139            buffer.append(':');
140            buffer.append(port);
141        }
142    }
143
144    /**
145     * append the user credentials
146     */
147    protected void appendCredentials(final StringBuilder buffer, final boolean addPassword)
148    {
149        if (userName != null && userName.length() != 0)
150        {
151            UriParser.appendEncoded(buffer, userName, USERNAME_RESERVED);
152            if (password != null && password.length() != 0)
153            {
154                buffer.append(':');
155                if (addPassword)
156                {
157                    UriParser.appendEncoded(buffer, password, PASSWORD_RESERVED);
158                }
159                else
160                {
161                    buffer.append("***");
162                }
163            }
164            buffer.append('@');
165        }
166    }
167}