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.httpclient.URIException;
020import org.apache.commons.httpclient.util.URIUtil;
021import org.apache.commons.vfs2.FileName;
022import org.apache.commons.vfs2.FileSystemException;
023import org.apache.commons.vfs2.FileType;
024
025/**
026 * A file name that represents URL.
027 */
028public class URLFileName extends GenericFileName
029{
030    private static final int BUFFER_SIZE = 250;
031
032    private final String queryString;
033
034    public URLFileName(final String scheme,
035                       final String hostName,
036                       final int port,
037                       final int defaultPort,
038                       final String userName,
039                       final String password,
040                       final String path,
041                       final FileType type,
042                       final String queryString)
043    {
044        super(scheme, hostName, port, defaultPort, userName, password, path, type);
045        this.queryString = queryString;
046    }
047
048    /**
049     * Get the query string.
050     *
051     * @return the query string part of the filename
052     */
053    public String getQueryString()
054    {
055        return queryString;
056    }
057
058    /**
059     * Get the path and query string e.g. /path/servlet?param1=true.
060     *
061     * @return the path and its query string
062     */
063    public String getPathQuery()
064    {
065        final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
066        sb.append(getPath());
067        sb.append("?");
068        sb.append(getQueryString());
069
070        return sb.toString();
071    }
072
073    /**
074     * Get the path encoded suitable for url like filesystem e.g. (http, webdav).
075     *
076     * @param charset the charset used for the path encoding
077     * @return The encoded path.
078     * @throws URIException If an error occurs encoding the URI.
079     * @throws FileSystemException If some other error occurs.
080     */
081    public String getPathQueryEncoded(final String charset) throws URIException, FileSystemException
082    {
083        if (getQueryString() == null)
084        {
085            if (charset != null)
086            {
087                return URIUtil.encodePath(getPathDecoded(), charset);
088            }
089            else
090            {
091                return URIUtil.encodePath(getPathDecoded());
092            }
093        }
094
095        final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
096        if (charset != null)
097        {
098            sb.append(URIUtil.encodePath(getPathDecoded(), charset));
099        }
100        else
101        {
102            sb.append(URIUtil.encodePath(getPathDecoded()));
103        }
104        sb.append("?");
105        sb.append(getQueryString());
106        return sb.toString();
107    }
108
109    /**
110     * Create a FileName.
111     * @param absPath The absolute path.
112     * @param type The FileType.
113     * @return The FileName
114     */
115    @Override
116    public FileName createName(final String absPath, final FileType type)
117    {
118        return new URLFileName(getScheme(),
119            getHostName(),
120            getPort(),
121            getDefaultPort(),
122            getUserName(),
123            getPassword(),
124            absPath,
125            type,
126            getQueryString());
127    }
128
129    /**
130     * Append query string to the uri.
131     *
132     * @return the uri
133     */
134    @Override
135    protected String createURI()
136    {
137        if (getQueryString() != null)
138        {
139            final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
140            sb.append(super.createURI());
141            sb.append("?");
142            sb.append(getQueryString());
143
144            return sb.toString();
145        }
146
147        return super.createURI();
148    }
149
150    /**
151     * Encode a URI.
152     * @param charset The character set.
153     * @return The encoded URI
154     * @throws FileSystemException if some other exception occurs.
155     * @throws URIException if an exception occurs encoding the URI.
156     */
157    public String getURIEncoded(final String charset) throws FileSystemException, URIException
158    {
159        final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
160        appendRootUri(sb, true);
161        sb.append(getPathQueryEncoded(charset));
162        return sb.toString();
163    }
164}