Lines 42-50
Link Here
|
42 |
package org.openide.filesystems; |
42 |
package org.openide.filesystems; |
43 |
|
43 |
|
44 |
import java.io.UnsupportedEncodingException; |
44 |
import java.io.UnsupportedEncodingException; |
|
|
45 |
import java.net.URI; |
45 |
import java.net.URL; |
46 |
import java.net.URL; |
46 |
import java.net.URLDecoder; |
47 |
import java.net.URLDecoder; |
47 |
import java.net.URLEncoder; |
|
|
48 |
import java.security.AccessController; |
48 |
import java.security.AccessController; |
49 |
import java.security.PrivilegedActionException; |
49 |
import java.security.PrivilegedActionException; |
50 |
import java.security.PrivilegedExceptionAction; |
50 |
import java.security.PrivilegedExceptionAction; |
Lines 57-62
Link Here
|
57 |
final class NbfsUtil { |
57 |
final class NbfsUtil { |
58 |
/** url separator */ |
58 |
/** url separator */ |
59 |
private static final char SEPARATOR = '/'; |
59 |
private static final char SEPARATOR = '/'; |
|
|
60 |
/** old host name field prior to #39613 */ |
61 |
private static final String HOST = "nbhost"; // NOI18N |
60 |
|
62 |
|
61 |
/** |
63 |
/** |
62 |
* Gets URL with nbfs protocol for passes fo |
64 |
* Gets URL with nbfs protocol for passes fo |
Lines 64-76
Link Here
|
64 |
* @return url with nbfs protocol |
66 |
* @return url with nbfs protocol |
65 |
* @throws FileStateInvalidException if FileObject somehow corrupted |
67 |
* @throws FileStateInvalidException if FileObject somehow corrupted |
66 |
*/ |
68 |
*/ |
67 |
static URL getURL(FileObject fo) throws FileStateInvalidException { |
69 |
static URL getURL(final FileObject fo) throws FileStateInvalidException { |
68 |
final String fsPart = encodeFsPart(fo); |
|
|
69 |
final String foPart = encodeFoPart(fo); |
70 |
|
71 |
final String host = "nbhost"; //NOI18N |
72 |
final String file = combine(fsPart, foPart); |
73 |
|
74 |
// #13038: the URL constructor accepting a handler is a security-sensitive |
70 |
// #13038: the URL constructor accepting a handler is a security-sensitive |
75 |
// operation. Sometimes a user class loaded internally (customized bean...), |
71 |
// operation. Sometimes a user class loaded internally (customized bean...), |
76 |
// which has no privileges, needs to make and use an nbfs: URL, since this |
72 |
// which has no privileges, needs to make and use an nbfs: URL, since this |
Lines 79-86
Link Here
|
79 |
return AccessController.doPrivileged( |
75 |
return AccessController.doPrivileged( |
80 |
new PrivilegedExceptionAction<URL>() { |
76 |
new PrivilegedExceptionAction<URL>() { |
81 |
public URL run() throws Exception { |
77 |
public URL run() throws Exception { |
82 |
// #30397: the fsPart name cannot be null |
78 |
return new URI(FileURL.PROTOCOL, fo.getFileSystem().getSystemName(), "/" + fo.getPath(), null).toURL(); |
83 |
return new URL(FileURL.PROTOCOL, host, -1, file, new FileURL.Handler()); |
|
|
84 |
} |
79 |
} |
85 |
} |
80 |
} |
86 |
); |
81 |
); |
Lines 92-105
Link Here
|
92 |
} |
87 |
} |
93 |
} |
88 |
} |
94 |
|
89 |
|
95 |
private static String combine(final String host, final String file) { |
|
|
96 |
StringBuffer sb = new StringBuffer(); |
97 |
sb.append(SEPARATOR).append(host); |
98 |
sb.append(file); |
99 |
|
100 |
return sb.toString(); |
101 |
} |
102 |
|
103 |
private static String[] split(URL url) { |
90 |
private static String[] split(URL url) { |
104 |
String file = url.getFile(); |
91 |
String file = url.getFile(); |
105 |
int idx = file.indexOf("/", 1); |
92 |
int idx = file.indexOf("/", 1); |
Lines 125-169
Link Here
|
125 |
return null; |
112 |
return null; |
126 |
} |
113 |
} |
127 |
|
114 |
|
128 |
if (isOldEncoding(url)) { |
115 |
String host = url.getHost(); |
|
|
116 |
|
117 |
if (host == null || host.length() == 0) { |
129 |
return oldDecode(url); |
118 |
return oldDecode(url); |
130 |
} |
119 |
} |
131 |
|
120 |
|
132 |
String[] urlParts = split(url); |
121 |
String fsName, foName; |
|
|
122 |
if (host.equals(HOST)) { |
123 |
String[] urlParts = split(url); |
133 |
|
124 |
|
134 |
String fsName = decodeFsPart(urlParts[0]); |
125 |
fsName = decodeFsPart(urlParts[0]); |
135 |
String foName = decodeFoPart(urlParts[1]); |
126 |
foName = decodeFoPart(urlParts[1]); |
|
|
127 |
} else { |
128 |
fsName = host; |
129 |
foName = url.getPath().substring(1); |
130 |
} |
136 |
|
131 |
|
137 |
FileSystem fsys = Repository.getDefault().findFileSystem(fsName); |
132 |
FileSystem fsys = Repository.getDefault().findFileSystem(fsName); |
138 |
|
133 |
|
139 |
return (fsys == null) ? null : fsys.findResource(foName); |
134 |
return (fsys == null) ? null : fsys.findResource(foName); |
140 |
} |
135 |
} |
141 |
|
136 |
|
142 |
private static String encodeFsPart(FileObject fo) throws FileStateInvalidException { |
|
|
143 |
FileSystem fs = fo.getFileSystem(); |
144 |
|
145 |
return encoder(fs.getSystemName()); |
146 |
} |
147 |
|
148 |
private static String encodeFoPart(FileObject fo) { |
149 |
StringTokenizer elemsEnum; |
150 |
StringBuffer sBuff = new StringBuffer(); |
151 |
elemsEnum = new StringTokenizer(fo.getPath(), String.valueOf(SEPARATOR)); |
152 |
|
153 |
while (elemsEnum.hasMoreElements()) { |
154 |
sBuff.append(SEPARATOR); |
155 |
sBuff.append(encoder((String) elemsEnum.nextElement())); |
156 |
} |
157 |
|
158 |
String retVal = sBuff.toString(); |
159 |
|
160 |
if ((retVal.length() == 0) || (fo.isFolder() && (retVal.charAt(retVal.length() - 1) != SEPARATOR))) { |
161 |
retVal += SEPARATOR; |
162 |
} |
163 |
|
164 |
return retVal; |
165 |
} |
166 |
|
167 |
private static String decodeFsPart(String encodedStr) { |
137 |
private static String decodeFsPart(String encodedStr) { |
168 |
return decoder(encodedStr); |
138 |
return decoder(encodedStr); |
169 |
} |
139 |
} |
Lines 185-198
Link Here
|
185 |
return sBuff.toString(); |
155 |
return sBuff.toString(); |
186 |
} |
156 |
} |
187 |
|
157 |
|
188 |
private static String encoder(String elem) { |
|
|
189 |
try { |
190 |
return URLEncoder.encode(elem, "UTF-8"); // NOI18N |
191 |
} catch (UnsupportedEncodingException e) { |
192 |
throw new AssertionError(e); |
193 |
} |
194 |
} |
195 |
|
196 |
private static String decoder(String elem) { |
158 |
private static String decoder(String elem) { |
197 |
try { |
159 |
try { |
198 |
return URLDecoder.decode(elem, "UTF-8"); // NOI18N |
160 |
return URLDecoder.decode(elem, "UTF-8"); // NOI18N |
Lines 201-213
Link Here
|
201 |
} |
163 |
} |
202 |
} |
164 |
} |
203 |
|
165 |
|
204 |
// backward compatibility |
|
|
205 |
private static boolean isOldEncoding(URL url) { |
206 |
String host = url.getHost(); |
207 |
|
208 |
return (host == null) || (host.length() == 0); |
209 |
} |
210 |
|
211 |
private static FileObject oldDecode(URL u) { |
166 |
private static FileObject oldDecode(URL u) { |
212 |
String resourceName = u.getFile(); |
167 |
String resourceName = u.getFile(); |
213 |
|
168 |
|