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 * @deprecated Use {@link GenericURLFileName} as it doesn't depend on HTTP Client v3 API directly.
028 */
029@Deprecated
030public class URLFileName extends GenericFileName {
031
032    private static final int BUFFER_SIZE = 250;
033
034    private final String queryString;
035
036    /**
037     * Constructs a new instance.
038     *
039     * @param scheme The host scheme.
040     * @param hostName The host name or IP address.
041     * @param port The host port.
042     * @param defaultPort The default host port.
043     * @param userName The user name.
044     * @param password The user password.
045     * @param path The host path.
046     * @param type The file type on the host.
047     * @param queryString The query after the path.
048     */
049    public URLFileName(final String scheme, final String hostName, final int port, final int defaultPort,
050            final String userName, final String password, final String path, final FileType type,
051            final String queryString) {
052        super(scheme, hostName, port, defaultPort, userName, password, path, type);
053        this.queryString = queryString;
054    }
055
056    /**
057     * Create a FileName.
058     *
059     * @param absPath The absolute path.
060     * @param type The FileType.
061     * @return The FileName
062     */
063    @Override
064    public FileName createName(final String absPath, final FileType type) {
065        return new URLFileName(getScheme(), getHostName(), getPort(), getDefaultPort(), getUserName(), getPassword(),
066                absPath, type, getQueryString());
067    }
068
069    /**
070     * Appends query string to the URI.
071     *
072     * @return the URI
073     */
074    @Override
075    protected String createURI() {
076        if (getQueryString() != null) {
077            final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
078            sb.append(super.createURI());
079            sb.append("?");
080            sb.append(getQueryString());
081
082            return sb.toString();
083        }
084
085        return super.createURI();
086    }
087
088    /**
089     * Gets the path and query string e.g. /path/servlet?param1=true.
090     *
091     * @return the path and its query string
092     */
093    public String getPathQuery() {
094        final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
095        sb.append(getPath());
096        sb.append("?");
097        sb.append(getQueryString());
098
099        return sb.toString();
100    }
101
102    /**
103     * Gets the path encoded suitable for url like file system e.g. (http, webdav).
104     *
105     * @param charset the charset used for the path encoding
106     * @return The encoded path.
107     * @throws URIException If an error occurs encoding the URI.
108     * @throws FileSystemException If some other error occurs.
109     */
110    public String getPathQueryEncoded(final String charset) throws URIException, FileSystemException {
111        if (getQueryString() == null) {
112            if (charset != null) {
113                return URIUtil.encodePath(getPathDecoded(), charset);
114            }
115            return URIUtil.encodePath(getPathDecoded());
116        }
117
118        final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
119        if (charset != null) {
120            sb.append(URIUtil.encodePath(getPathDecoded(), charset));
121        } else {
122            sb.append(URIUtil.encodePath(getPathDecoded()));
123        }
124        sb.append("?");
125        sb.append(getQueryString());
126        return sb.toString();
127    }
128
129    /**
130     * Gets the query string.
131     *
132     * @return the query string part of the file name
133     */
134    public String getQueryString() {
135        return queryString;
136    }
137
138    /**
139     * Encodes a URI.
140     *
141     * @param charset The character set.
142     * @return The encoded URI
143     * @throws FileSystemException if some other exception occurs.
144     * @throws URIException if an exception occurs encoding the URI.
145     */
146    public String getURIEncoded(final String charset) throws FileSystemException, URIException {
147        final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
148        appendRootUri(sb, true);
149        sb.append(getPathQueryEncoded(charset));
150        return sb.toString();
151    }
152}