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 java.net.URISyntaxException;
20  
21  import org.apache.commons.vfs2.FileName;
22  import org.apache.commons.vfs2.FileSystemException;
23  import org.apache.commons.vfs2.FileType;
24  import org.apache.commons.vfs2.util.URIUtils;
25  
26  /**
27   * Generic file name that represents a URL.
28   */
29  public class GenericURLFileName extends GenericFileName {
30  
31      private static final int BUFFER_SIZE = 250;
32  
33      private final String queryString;
34  
35      public GenericURLFileName(final String scheme, final String hostName, final int port, final int defaultPort,
36              final String userName, final String password, final String path, final FileType type,
37              final String queryString) {
38          super(scheme, hostName, port, defaultPort, userName, password, path, type);
39          this.queryString = queryString;
40      }
41  
42      /**
43       * Gets the query string.
44       *
45       * @return the query string part of the file name
46       */
47      public String getQueryString() {
48          return queryString;
49      }
50  
51      /**
52       * Gets the path and query string e.g. /path/servlet?param1=true.
53       *
54       * @return the path and its query string
55       */
56      public String getPathQuery() {
57          final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
58          sb.append(getPath());
59          sb.append("?");
60          sb.append(getQueryString());
61  
62          return sb.toString();
63      }
64  
65      /**
66       * Gets the path encoded suitable for url like file system e.g. (http, webdav).
67       *
68       * @param charset the charset used for the path encoding
69       * @return The encoded path.
70       * @throws URISyntaxException If an error occurs encoding the URI.
71       * @throws FileSystemException If some other error occurs.
72       */
73      public String getPathQueryEncoded(final String charset) throws URISyntaxException, FileSystemException {
74          if (getQueryString() == null) {
75              if (charset != null) {
76                  return URIUtils.encodePath(getPathDecoded(), charset);
77              } else {
78                  return URIUtils.encodePath(getPathDecoded());
79              }
80          }
81  
82          final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
83          if (charset != null) {
84              sb.append(URIUtils.encodePath(getPathDecoded(), charset));
85          } else {
86              sb.append(URIUtils.encodePath(getPathDecoded()));
87          }
88          sb.append("?");
89          sb.append(getQueryString());
90          return sb.toString();
91      }
92  
93      /**
94       * Creates 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 GenericURLFileName(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 URISyntaxException if an exception occurs encoding the URI.
132      */
133     public String getURIEncoded(final String charset) throws FileSystemException, URISyntaxException {
134         final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
135         appendRootUri(sb, true);
136         sb.append(getPathQueryEncoded(charset));
137         return sb.toString();
138     }
139 }