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