1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.provider;
18
19 import org.apache.commons.httpclient.URIException;
20 import org.apache.commons.httpclient.util.URIUtil;
21 import org.apache.commons.vfs.FileName;
22 import org.apache.commons.vfs.FileSystemException;
23 import org.apache.commons.vfs.FileType;
24
25 public class URLFileName extends GenericFileName
26 {
27 private final String queryString;
28
29 public URLFileName(final String scheme,
30 final String hostName,
31 final int port,
32 final int defaultPort,
33 final String userName,
34 final String password,
35 final String path,
36 final FileType type,
37 final String queryString)
38 {
39 super(scheme, hostName, port, defaultPort, userName, password, path, type);
40 this.queryString = queryString;
41 }
42
43 /***
44 * get the query string
45 *
46 * @return the query string part of the filename
47 */
48 public String getQueryString()
49 {
50 return queryString;
51 }
52
53 /***
54 * get the path and query string e.g. /path/servlet?param1=true
55 *
56 * @return the path and its query string
57 */
58 public String getPathQuery()
59 {
60 StringBuffer sb = new StringBuffer(250);
61 sb.append(getPath());
62 sb.append("?");
63 sb.append(getQueryString());
64
65 return sb.toString();
66 }
67
68 /***
69 * get the path encoded suitable for url like filesystem e.g. (http, webdav)
70 *
71 * @param charset the charset used for the path encoding
72 */
73 public String getPathQueryEncoded(String charset) throws URIException, FileSystemException
74 {
75 if (getQueryString() == null)
76 {
77 if (charset != null)
78 {
79 return URIUtil.encodePath(getPathDecoded(), charset);
80 }
81 else
82 {
83 return URIUtil.encodePath(getPathDecoded());
84 }
85 }
86
87 StringBuffer sb = new StringBuffer(250);
88 if (charset != null)
89 {
90 sb.append(URIUtil.encodePath(getPathDecoded(), charset));
91 }
92 else
93 {
94 sb.append(URIUtil.encodePath(getPathDecoded()));
95 }
96 sb.append("?");
97 sb.append(getQueryString());
98 return sb.toString();
99 }
100
101 public FileName createName(final String absPath, FileType type)
102 {
103 return new URLFileName(getScheme(),
104 getHostName(),
105 getPort(),
106 getDefaultPort(),
107 getUserName(),
108 getPassword(),
109 absPath,
110 type,
111 getQueryString());
112 }
113
114 /***
115 * append query string to the uri
116 *
117 * @return the uri
118 */
119 protected String createURI()
120 {
121 if (getQueryString() != null)
122 {
123 StringBuffer sb = new StringBuffer(250);
124 sb.append(super.createURI());
125 sb.append("?");
126 sb.append(getQueryString());
127
128 return sb.toString();
129 }
130
131 return super.createURI();
132 }
133
134 public String getURIEncoded(String charset) throws FileSystemException, URIException
135 {
136 StringBuffer sb = new StringBuffer(80);
137 appendRootUri(sb, true);
138 sb.append(getPathQueryEncoded(charset));
139 return sb.toString();
140 }
141 }