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