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

(-)src/META-INF/services/java.net.URLStreamHandlerFactory (+2 lines)
Line 1 Link Here
1
org.netbeans.core.NbURLStreamHandlerFactory$Standard
1
org.netbeans.core.NbURLStreamHandlerFactory$Standard
2
org.netbeans.core.filesystems.NbinstURLStreamHandlerFactory
3
(-)src/META-INF/services/org.openide.filesystems.URLMapper (+2 lines)
Line 1 Link Here
1
org.netbeans.core.filesystems.ArchiveURLMapper
1
org.netbeans.core.filesystems.ArchiveURLMapper
2
org.netbeans.core.filesystems.NbinstURLMapper
3
(-)src/org/netbeans/core/filesystems/NbinstURLMapper.java (+95 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.netbeans.core.filesystems;
15
16
import java.io.File;
17
import java.io.UnsupportedEncodingException;
18
import java.net.*;
19
20
import org.openide.ErrorManager;
21
import org.openide.filesystems.FileObject;
22
import org.openide.filesystems.FileUtil;
23
import org.openide.filesystems.URLMapper;
24
import org.openide.modules.InstalledFileLocator;
25
26
27
/**
28
 * URLMapper for the nbinst URL protocol.
29
 * The mapper handles only the translation from URL into FileObjects.
30
 * The opposite conversion is not needed, it is handled by the default URLMapper.
31
 * The format of the nbinst URL is nbinst://host/path.
32
 * The host part is optional, if presents it specifies the name of the supplying module.
33
 * The path is mandatory and specifies the relative path from the ${netbeans.home}, ${netbeans.user}
34
 * or ${netbeans.dirs}.
35
 * @author  Tomas Zezula
36
 */
37
public class NbinstURLMapper extends URLMapper {
38
    
39
    public static final String PROTOCOL = "nbinst";     //NOI18N
40
    
41
    /** Creates a new instance of NbInstURLMapper */
42
    public NbinstURLMapper() {
43
    }
44
45
    /**
46
     * Returns FileObjects for given URL
47
     * @param url the URL for which the FileObjects should be find.
48
     * @return FileObject[], never returns null, may return empty array.
49
     */
50
    public FileObject[] getFileObjects(URL url) {
51
        return decodeURL (url);
52
    }
53
54
    /**
55
     * Returns null, the translation into URL is doen by default URLMapper
56
     * @param fo
57
     * @param type
58
     * @return
59
     */
60
    public URL getURL(FileObject fo, int type) {
61
        return null;
62
    }
63
64
    /**
65
     * Resolves the nbinst URL into the array of the FileObjects.
66
     * @param url to be resolved
67
     * @return FileObject[], never returns null, may return empty array.
68
     */
69
    static FileObject[] decodeURL (URL url) {
70
        assert url != null;
71
        try {
72
            URI uri = new URI (url.toExternalForm());
73
            String protocol = uri.getScheme();
74
            if (PROTOCOL.equals(protocol)) {
75
                String module = uri.getHost();
76
                String path = uri.getPath();
77
                if (path.length()>0) {
78
                    try {
79
                        File file = InstalledFileLocator.getDefault().locate(path.substring(1),module,false);
80
                        if (file != null) {
81
                            return URLMapper.findFileObjects(file.toURL());
82
                        }
83
                    }
84
                    catch (MalformedURLException mue) {
85
                        ErrorManager.getDefault().notify(mue);
86
                    }
87
                }
88
            }
89
        } catch (URISyntaxException use) {
90
            ErrorManager.getDefault().notify(use);
91
        }
92
        return new FileObject[0];
93
    }
94
95
}
(-)src/org/netbeans/core/filesystems/NbinstURLStreamHandlerFactory.java (+129 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
package org.netbeans.core.filesystems;
14
15
16
import java.io.IOException;
17
import java.io.FileNotFoundException;
18
import java.io.InputStream;
19
import java.io.OutputStream;
20
import java.net.URL;
21
import java.net.URLConnection;
22
import java.net.URLStreamHandler;
23
import java.net.URLStreamHandlerFactory;
24
import java.net.UnknownServiceException;
25
import org.openide.ErrorManager;
26
import org.openide.filesystems.FileObject;
27
import org.openide.filesystems.FileLock;
28
29
30
31
/**
32
 * StreamHandlerFactory for nbinst protocol
33
 */
34
public class NbinstURLStreamHandlerFactory implements URLStreamHandlerFactory {
35
36
    /**
37
     * Creates URLStreamHandler for nbinst protocol
38
     * @param protocol
39
     * @return NbinstURLStreamHandler if the protocol is nbinst otherwise null
40
     */
41
    public URLStreamHandler createURLStreamHandler(String protocol) {
42
        if (NbinstURLMapper.PROTOCOL.equals(protocol)) {
43
            return new NbinstURLStreamHandler ();
44
        }
45
        return null;
46
    }
47
48
    /**
49
     * URLStreamHandler for nbinst protocol
50
     */
51
    private static class NbinstURLStreamHandler extends URLStreamHandler {
52
53
        /**
54
         * Creates URLConnection for URL with nbinst protocol.
55
         * @param u URL for which the URLConnection should be created
56
         * @return URLConnection
57
         * @throws IOException
58
         */
59
        protected URLConnection openConnection(URL u) throws IOException {
60
            return new NbinstURLConnection (u);
61
        }
62
    }
63
64
    /** URLConnection for URL with nbinst protocol.
65
     *
66
     */
67
    private static class NbinstURLConnection extends URLConnection {
68
69
        private FileObject fo;
70
        private InputStream iStream;
71
        private OutputStream oStream;
72
73
        /**
74
         * Creates new URLConnection
75
         * @param url the parameter for which the connection should be
76
         * created
77
         */
78
        public NbinstURLConnection (URL url) {
79
            super (url);
80
        }
81
82
83
        public void connect() throws IOException {
84
            if (fo == null) {
85
                FileObject[] decoded = NbinstURLMapper.decodeURL(this.url);
86
                if (decoded.length>0) {
87
                    fo = decoded[0];
88
                }
89
                else {
90
                    throw new FileNotFoundException("Cannot find: " + url); // NOI18N
91
                }
92
            }
93
            if (fo.isFolder()) {
94
                throw new UnknownServiceException();
95
            }
96
        }
97
98
        public int getContentLength() {
99
            try {
100
                this.connect();
101
                return (int) this.fo.getSize();     //May cause overflow long->int
102
            } catch (IOException e) {
103
                return -1;
104
            }
105
        }
106
107
108
        public InputStream getInputStream() throws IOException {
109
            this.connect();
110
            if (iStream == null) {
111
                iStream = fo.getInputStream();
112
            }
113
            return iStream;
114
        }
115
116
117
        public String getHeaderField (String name) {
118
            if ("content-type".equals(name)) {                  //NOI18N
119
                try {
120
                    this.connect();
121
                    return fo.getMIMEType();
122
                } catch (IOException ioe) {
123
                    ErrorManager.getDefault().notify(ioe);
124
                }
125
            }
126
            return super.getHeaderField(name);
127
        }
128
    }
129
}
(-)test/unit/src/org/netbeans/core/filesystems/NbinstURLMapperTest.java (+181 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
package org.netbeans.core.filesystems;
14
15
16
import java.beans.PropertyVetoException;
17
import java.io.*;
18
import java.net.MalformedURLException;
19
import java.net.URL;
20
import java.net.URLConnection;
21
import java.util.StringTokenizer;
22
import org.openide.filesystems.*;
23
import org.openide.util.Lookup;
24
import org.openide.modules.InstalledFileLocator;
25
import org.netbeans.junit.NbTestCase;
26
27
28
public class NbinstURLMapperTest extends NbTestCase {
29
30
    private static final String FILE_NAME = "test.txt";     //NOI18N
31
    private static final String FOLDER_NAME = "modules";    //NOI18N
32
33
    private FileSystem fs;
34
    private int expectedLength;
35
36
    public NbinstURLMapperTest (String testName) throws IOException {
37
        super (testName);
38
    }
39
40
41
    protected void setUp() throws Exception {
42
        super.setUp();
43
        File f = this.getWorkDir();
44
        cleanUp (new File (f,FOLDER_NAME));
45
        Lookup.Result result = Lookup.getDefault().lookup (new Lookup.Template(InstalledFileLocator.class));
46
        boolean found = false;
47
        for (java.util.Iterator it = result.allInstances().iterator(); it.hasNext();) {
48
            Object locator = it.next();
49
            if (locator instanceof TestInstalledFileLocator) {
50
                ((TestInstalledFileLocator)locator).setRoot(f);
51
                found = true;
52
            }
53
        }
54
        assertTrue("The TestInstalledFileLocator can not be found in the default lookup.",found);
55
        f = new File (f,FOLDER_NAME);
56
        f.mkdir();
57
        f = new File (f,FILE_NAME);
58
        f.createNewFile();
59
        PrintWriter pw = null;
60
        try {
61
            pw = new PrintWriter(new FileWriter(f));
62
            pw.println(FILE_NAME);
63
        } finally {
64
            if (pw!=null) {
65
                pw.close ();
66
            }
67
        }
68
        this.expectedLength = (int) f.length();
69
        this.fs = this.mountFs ();
70
        assertNotNull ("Test was not able to mount filesystem.",this.fs);
71
    }
72
73
74
    protected void tearDown() throws Exception {
75
        this.umountFs (this.fs);
76
        super.tearDown();
77
    }
78
79
    public void testFindFileObject () throws MalformedURLException, IOException {
80
        URL url = new URL ("nbinst:///modules/test.txt");  //NOI18N
81
        FileObject[] fos = URLMapper.findFileObjects (url);
82
        assertTrue ("URLMapper returned null, violation of API contract.",fos!=null);
83
        assertTrue ("The nbinst URL was not resolved.",fos.length == 1);
84
        url = new URL ("nbinst://test-module/modules/test.txt");
85
        fos = URLMapper.findFileObjects (url);
86
        assertTrue ("URLMapper returned null, violation of API contract.",fos!=null);
87
        assertTrue ("The nbinst URL was not resolved.",fos.length == 1);
88
        url = new URL ("nbinst://foo-module/modules/test.txt");
89
        fos = URLMapper.findFileObjects (url);
90
        assertTrue ("URLMapper returned null, violation of API contract.",fos!=null);
91
        assertTrue ("The nbinst URL was resolved.",fos.length == 0);
92
    }
93
94
    public void testURLConenction () throws MalformedURLException, IOException {
95
        URL url = new URL ("nbinst:///modules/test.txt");                //NOI18N
96
        URLConnection connection = url.openConnection();
97
        assertEquals ("URLConnection returned wrong content length.",connection.getContentLength(),expectedLength);
98
        BufferedReader in = null;
99
        try {
100
            in = new BufferedReader  ( new InputStreamReader (connection.getInputStream()));
101
            String line = in.readLine();
102
            assertTrue("URLConnection returned invalid InputStream",line.equals(FILE_NAME));
103
        } finally {
104
            if (in != null) {
105
                in.close ();
106
            }
107
        }
108
    }
109
110
111
112
113
114
    private FileSystem mountFs () throws IOException {
115
        File f = FileUtil.normalizeFile(this.getWorkDir());
116
        String parentName;
117
        while ((parentName=f.getParent())!=null) {
118
            f = new File (parentName);
119
        }
120
        try {
121
            LocalFileSystem fs = new LocalFileSystem ();
122
            fs.setRootDirectory (f);
123
            Repository.getDefault().addFileSystem(fs);
124
            return fs;
125
        } catch (PropertyVetoException pve) {
126
            return null;
127
        }
128
    }
129
130
    private void umountFs (FileSystem fs) {
131
        assertNotNull ("umountFs called with null FileSystem.",fs);
132
        Repository.getDefault().removeFileSystem(fs);
133
    }
134
135
    private void cleanUp (File f) {
136
        if (!f.exists()) {
137
            return;
138
        }
139
        if (f.isDirectory()) {
140
            File[] files = f.listFiles();
141
            for (int i = 0; i < files.length; i++) {
142
                cleanUp(files[i]);
143
            }
144
        }
145
        f.delete();
146
    }
147
148
    public static class TestInstalledFileLocator extends InstalledFileLocator {
149
150
        private File root;
151
152
        public TestInstalledFileLocator () {
153
        }
154
155
156
        public void setRoot (File root) {
157
            this.root = root;
158
        }
159
160
        public File locate(String relativePath, String codeNameBase, boolean localized) {
161
            assert relativePath != null;
162
            if (root == null) {
163
                return null;
164
            }
165
            if (codeNameBase!= null && !"test-module".equals(codeNameBase)) {
166
                return null;
167
            }
168
            StringTokenizer tk = new StringTokenizer(relativePath,"/");
169
            File f = this.root;
170
            while (tk.hasMoreTokens()) {
171
                String part = tk.nextToken();
172
                f = new File (f,part);
173
                if (!f.exists()) {
174
                    return null;
175
                }
176
            }
177
            return f;
178
        }
179
    }
180
181
}

Return to bug 41266