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      public URLFileName(final String scheme, final String hostName, final int port, final int defaultPort,
37              final String userName, final String password, final String path, final FileType type,
38              final String queryString) {
39          super(scheme, hostName, port, defaultPort, userName, password, path, type);
40          this.queryString = queryString;
41      }
42  
43      /**
44       * Gets the query string.
45       *
46       * @return the query string part of the file name
47       */
48      public String getQueryString() {
49          return queryString;
50      }
51  
52      /**
53       * Gets the path and query string e.g. /path/servlet?param1=true.
54       *
55       * @return the path and its query string
56       */
57      public String getPathQuery() {
58          final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
59          sb.append(getPath());
60          sb.append("?");
61          sb.append(getQueryString());
62  
63          return sb.toString();
64      }
65  
66      /**
67       * Gets the path encoded suitable for url like file system e.g. (http, webdav).
68       *
69       * @param charset the charset used for the path encoding
70       * @return The encoded path.
71       * @throws URIException If an error occurs encoding the URI.
72       * @throws FileSystemException If some other error occurs.
73       */
74      public String getPathQueryEncoded(final String charset) throws URIException, FileSystemException {
75          if (getQueryString() == null) {
76              if (charset != null) {
77                  return URIUtil.encodePath(getPathDecoded(), charset);
78              }
79              return URIUtil.encodePath(getPathDecoded());
80          }
81  
82          final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
83          if (charset != null) {
84              sb.append(URIUtil.encodePath(getPathDecoded(), charset));
85          } else {
86              sb.append(URIUtil.encodePath(getPathDecoded()));
87          }
88          sb.append("?");
89          sb.append(getQueryString());
90          return sb.toString();
91      }
92  
93      /**
94       * Create a FileName.
95       *
96       * @param absPath The absolute path.
97       * @param type The FileType.
98       * @return The FileName
99       */
100     @Override
101     public FileName createName(final String absPath, final FileType type) {
102         return new URLFileName(getScheme(), getHostName(), getPort(), getDefaultPort(), getUserName(), getPassword(),
103                 absPath, type, getQueryString());
104     }
105 
106     /**
107      * Appends query string to the uri.
108      *
109      * @return the uri
110      */
111     @Override
112     protected String createURI() {
113         if (getQueryString() != null) {
114             final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
115             sb.append(super.createURI());
116             sb.append("?");
117             sb.append(getQueryString());
118 
119             return sb.toString();
120         }
121 
122         return super.createURI();
123     }
124 
125     /**
126      * Encodes a URI.
127      *
128      * @param charset The character set.
129      * @return The encoded URI
130      * @throws FileSystemException if some other exception occurs.
131      * @throws URIException if an exception occurs encoding the URI.
132      */
133     public String getURIEncoded(final String charset) throws FileSystemException, URIException {
134         final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
135         appendRootUri(sb, true);
136         sb.append(getPathQueryEncoded(charset));
137         return sb.toString();
138     }
139 }