This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 39613
Collapse All | Expand All

(-)a/openide.filesystems/src/org/openide/filesystems/NbfsUtil.java (-62 / +17 lines)
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
(-)a/openide.filesystems/src/org/openide/filesystems/XMLFileSystem.java (-1 / +1 lines)
Lines 330-336 Link Here
330
        Handler handler = new Handler(DTD_MAP, rootElem = new ResourceElem(true, urls, null), validate); // NOI18N        
330
        Handler handler = new Handler(DTD_MAP, rootElem = new ResourceElem(true, urls, null), validate); // NOI18N        
331
331
332
        try {
332
        try {
333
            _setSystemName("XML_" + urls[0].toExternalForm().replace('/','-')); // NOI18N
333
            _setSystemName(urls[0].toExternalForm());
334
        } catch (PropertyVetoException pvx) {
334
        } catch (PropertyVetoException pvx) {
335
            rootElem = null;
335
            rootElem = null;
336
            throw pvx;
336
            throw pvx;
(-)a/openide.filesystems/test/unit/src/org/openide/filesystems/FileObjectTestHid.java (+1 lines)
Lines 2727-2732 Link Here
2727
        assertNotNull("had a URL for " + f1, u1);
2727
        assertNotNull("had a URL for " + f1, u1);
2728
        URI uri1 = new URI(u1.toExternalForm());
2728
        URI uri1 = new URI(u1.toExternalForm());
2729
        String path1 = uri1.getPath();
2729
        String path1 = uri1.getPath();
2730
        System.err.println("XXX u1=" + u1 + " uri1=" + uri1 + " path1=" + path1);
2730
        if (path1 != null) {
2731
        if (path1 != null) {
2731
            assertTrue("path of " + uri1 + " ends with /", path1.endsWith("/"));
2732
            assertTrue("path of " + uri1 + " ends with /", path1.endsWith("/"));
2732
            String path2 = path1 + f2.getNameExt();
2733
            String path2 = path1 + f2.getNameExt();

Return to bug 39613