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