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.vfs2.provider;
18  
19  import org.apache.commons.httpclient.URIException;
20  import org.apache.commons.httpclient.util.URIUtil;
21  import org.apache.commons.vfs2.FileName;
22  import org.apache.commons.vfs2.FileSystemException;
23  import org.apache.commons.vfs2.FileType;
24  
25  /**
26   * A file name that represents URL.
27   * @deprecated Use {@link GenericURLFileName} as it doesn't depend on HTTP Client v3 API directly.
28   */
29  @Deprecated
30  public class URLFileName extends GenericFileName {
31  
32      private static final int BUFFER_SIZE = 250;
33  
34      private final String queryString;
35  
36      /**
37       * Constructs a new instance.
38       *
39       * @param scheme The host scheme.
40       * @param hostName The host name or IP address.
41       * @param port The host port.
42       * @param defaultPort The default host port.
43       * @param userName The user name.
44       * @param password The user password.
45       * @param path The host path.
46       * @param type The file type on the host.
47       * @param queryString The query after the path.
48       */
49      public URLFileName(final String scheme, final String hostName, final int port, final int defaultPort,
50              final String userName, final String password, final String path, final FileType type,
51              final String queryString) {
52          super(scheme, hostName, port, defaultPort, userName, password, path, type);
53          this.queryString = queryString;
54      }
55  
56      /**
57       * Create a FileName.
58       *
59       * @param absPath The absolute path.
60       * @param type The FileType.
61       * @return The FileName
62       */
63      @Override
64      public FileName createName(final String absPath, final FileType type) {
65          return new URLFileName(getScheme(), getHostName(), getPort(), getDefaultPort(), getUserName(), getPassword(),
66                  absPath, type, getQueryString());
67      }
68  
69      /**
70       * Appends query string to the URI.
71       *
72       * @return the URI
73       */
74      @Override
75      protected String createURI() {
76          if (getQueryString() != null) {
77              final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
78              sb.append(super.createURI());
79              sb.append("?");
80              sb.append(getQueryString());
81  
82              return sb.toString();
83          }
84  
85          return super.createURI();
86      }
87  
88      /**
89       * Gets the path and query string e.g. /path/servlet?param1=true.
90       *
91       * @return the path and its query string
92       */
93      public String getPathQuery() {
94          final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
95          sb.append(getPath());
96          sb.append("?");
97          sb.append(getQueryString());
98  
99          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 }