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 270390
Collapse All | Expand All

(-)7a0974c63e43 (+63 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 */
40
package org.netbeans.modules.dlight.libs.common.invalid;
41
42
import java.io.FileNotFoundException;
43
import java.io.IOException;
44
import java.net.URL;
45
import java.net.URLConnection;
46
import java.net.URLStreamHandler;
47
import org.openide.util.URLStreamHandlerRegistration;
48
49
/**
50
 *
51
 * @author vkvashin
52
 */
53
@URLStreamHandlerRegistration(protocol="invalid")
54
public class InvalidFileURLStreamHandler extends URLStreamHandler {
55
56
    public static final String PROTOCOL = "invalid"; //NOI18N
57
    public static final String PROTOCOL_PREFIX = "invalid:/"; //NOI18N
58
59
    @Override
60
    protected URLConnection openConnection(URL u) throws IOException {
61
        throw new FileNotFoundException(u.toString());
62
    }    
63
}
(-)a/dlight.libs.common/src/org/netbeans/modules/dlight/libs/common/invalid/InvalidFileUrlMapper.java (-2 / +29 lines)
Lines 40-46 Link Here
40
package org.netbeans.modules.dlight.libs.common.invalid;
40
package org.netbeans.modules.dlight.libs.common.invalid;
41
41
42
import java.net.MalformedURLException;
42
import java.net.MalformedURLException;
43
import java.net.URISyntaxException;
43
import java.net.URL;
44
import java.net.URL;
45
import org.netbeans.modules.dlight.libs.common.InvalidFileObjectSupport;
44
import org.netbeans.modules.dlight.libs.common.PathUtilities;
46
import org.netbeans.modules.dlight.libs.common.PathUtilities;
45
import org.openide.filesystems.FileObject;
47
import org.openide.filesystems.FileObject;
46
import org.openide.filesystems.FileStateInvalidException;
48
import org.openide.filesystems.FileStateInvalidException;
Lines 54-62 Link Here
54
 */
56
 */
55
@org.openide.util.lookup.ServiceProvider(service = org.openide.filesystems.URLMapper.class)
57
@org.openide.util.lookup.ServiceProvider(service = org.openide.filesystems.URLMapper.class)
56
public class InvalidFileUrlMapper extends URLMapper {
58
public class InvalidFileUrlMapper extends URLMapper {
57
    
59
58
    @Override
60
    @Override
59
    public FileObject[] getFileObjects(URL url) {
61
    public FileObject[] getFileObjects(URL url) {
62
        if (url.getProtocol().equals(InvalidFileURLStreamHandler.PROTOCOL)) {
63
            String path = unescapePath(url);
64
            FileObject fo = InvalidFileObjectSupport.getInvalidFileObject(InvalidFileObjectSupport.getDummyFileSystem(), path);
65
            if (fo != null) {
66
                return new FileObject[] { fo };
67
            }
68
        }
60
        return null;
69
        return null;
61
    }
70
    }
62
71
Lines 69-75 Link Here
69
                // even if the file is already created.
78
                // even if the file is already created.
70
                // So we'll try creating a "real" URL
79
                // So we'll try creating a "real" URL
71
                FileSystem fs = fo.getFileSystem();
80
                FileSystem fs = fo.getFileSystem();
72
                String root = fs.getRoot().toURL().toExternalForm();
81
                String root;
82
                if (fs == InvalidFileObjectSupport.getDummyFileSystem()) {
83
                    root = InvalidFileURLStreamHandler.PROTOCOL_PREFIX;
84
                } else {                    
85
                    root = fs.getRoot().toURL().toExternalForm();
86
                }
73
                String path = PathUtilities.escapePathForUseInURL(fo.getPath());
87
                String path = PathUtilities.escapePathForUseInURL(fo.getPath());
74
                String res;
88
                String res;
75
                if (root.endsWith("/")) { // NOI18N
89
                if (root.endsWith("/")) { // NOI18N
Lines 84-87 Link Here
84
        }
98
        }
85
        return null;
99
        return null;
86
    }    
100
    }    
101
    
102
    private static String unescapePath(URL url) {
103
        String path = url.getFile();
104
        if (path.contains("%")) { //NOI18N
105
            try {
106
                return url.toURI().getPath();
107
            } catch (URISyntaxException ex) {
108
                Exceptions.printStackTrace(ex);
109
            }
110
        }
111
        return path;
112
    }
113
    
87
}
114
}
(-)a/dlight.libs.common/test/unit/src/org/netbeans/modules/dlight/InvalidFileObjectSupportTest.java (-1 / +13 lines)
Lines 62-74 Link Here
62
 */
62
 */
63
public class InvalidFileObjectSupportTest {
63
public class InvalidFileObjectSupportTest {
64
64
65
    static {        
65
    static {
66
        // otherwise we get java.net.MalformedURLException: unknown protocol
66
        // otherwise we get java.net.MalformedURLException: unknown protocol
67
        // even if we register via @URLStreamHandlerRegistration annotation
67
        // even if we register via @URLStreamHandlerRegistration annotation
68
        URL.setURLStreamHandlerFactory(Lookup.getDefault().lookup(URLStreamHandlerFactory.class));
68
        URL.setURLStreamHandlerFactory(Lookup.getDefault().lookup(URLStreamHandlerFactory.class));
69
    }    
69
    }    
70
70
71
    @Test
71
    @Test
72
    public void testInvalidFileObjectURL() throws Exception {
73
        // see #270390 - StackOverflowError at java.io.UnixFileSystem.getBooleanAttributes
74
        FileSystem dummyFS = InvalidFileObjectSupport.getDummyFileSystem();
75
        FileObject invalidFO = InvalidFileObjectSupport.getInvalidFileObject(dummyFS, "/inexistent");
76
        final URL url = invalidFO.getURL();
77
        FileObject foundFO = URLMapper.findFileObject(url);
78
        assertEquals("Invalid and found by URL ", invalidFO, foundFO);
79
    }
80
81
    @Test
72
    public void testInvalidFileObject() throws Exception {
82
    public void testInvalidFileObject() throws Exception {
73
        File file = File.createTempFile("qwe", "asd");
83
        File file = File.createTempFile("qwe", "asd");
74
        FileObject origFo = null;
84
        FileObject origFo = null;
Lines 84-91 Link Here
84
        }
94
        }
85
        FileObject invalidFo1 = InvalidFileObjectSupport.getInvalidFileObject(fs, path);
95
        FileObject invalidFo1 = InvalidFileObjectSupport.getInvalidFileObject(fs, path);
86
        URI uri1 = invalidFo1.toURI(); // just to check that there is no assertions
96
        URI uri1 = invalidFo1.toURI(); // just to check that there is no assertions
97
        URL url1 = invalidFo1.toURL(); // just to check that there is no assertions
87
        FileObject invalidFo2 = InvalidFileObjectSupport.getInvalidFileObject(fs, path);
98
        FileObject invalidFo2 = InvalidFileObjectSupport.getInvalidFileObject(fs, path);
88
        URI uri2 = invalidFo2.toURI(); // just to check that there is no assertions
99
        URI uri2 = invalidFo2.toURI(); // just to check that there is no assertions
100
        URL url2 = invalidFo2.toURL(); // just to check that there is no assertions
89
        assertTrue(invalidFo1 == invalidFo2);
101
        assertTrue(invalidFo1 == invalidFo2);
90
        assertFalse(invalidFo1.isValid());
102
        assertFalse(invalidFo1.isValid());
91
        assertEquals(origFo.getName(), invalidFo1.getName());
103
        assertEquals(origFo.getName(), invalidFo1.getName());

Return to bug 270390