KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > HttpServer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.util;
20
21 import org.openide.filesystems.FileObject;
22 import org.openide.util.NbBundle;
23
24 import java.net.InetAddress JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.UnknownHostException JavaDoc;
28
29
30 /** Maps internal NetBeans resources such as repository objects to URLs.
31 * The mapping is delegated to an HTTP server module, which registers to do
32 * the mapping. It is also responsible for actually serving the individual data objects
33 * from the Repository and resources from the system classpath.
34 * @author Petr Jiricka
35 * @deprecated The <code>httpserver</code> module should provide a replacement for this API if necessary.
36 */

37 public abstract class HttpServer {
38     /** regular server to be used */
39     private static HttpServer.Impl registeredServer = null;
40
41     private HttpServer() {
42     }
43
44     /** Returns a server implementation which is currently registered with the system.
45     * Server implementation obtained from Lookup has highest priority.
46     * 'Normal' registered server has priority over a default registered server.
47     * If no server has been registered, internal error is
48     */

49     private static HttpServer.Impl getServer() throws UnknownHostException JavaDoc {
50         Object JavaDoc o = Lookup.getDefault().lookup(HttpServer.Impl.class);
51
52         if (o != null) {
53             return (HttpServer.Impl) o;
54         }
55
56         if (registeredServer != null) {
57             return registeredServer;
58         } else {
59             throw new UnknownHostException JavaDoc(NbBundle.getBundle(HttpServer.class).getString("MSG_NoServerRegistered"));
60         }
61     }
62
63     /** Register the system HTTP server.
64     * Typically this would be done in {@link org.openide.modules.ModuleInstall#installed}
65     * or {@link org.openide.modules.ModuleInstall#restored}.
66     * @param server the server to register
67     * @throws SecurityException if there was already one registered
68     * @deprecated As of 2.11 use Lookup instead of registering HTTP server
69     */

70     public static void registerServer(HttpServer.Impl server)
71     throws SecurityException JavaDoc {
72         if (registeredServer != null) {
73             throw new SecurityException JavaDoc(NbBundle.getBundle(HttpServer.class).getString("SERVER_REGISTERED"));
74         }
75
76         registeredServer = server;
77     }
78
79     /** Deregister the system HTTP server.
80     * Typically this would be done in {@link org.openide.modules.ModuleInstall#uninstalled}.
81     * @param server the server to deregister
82     * @throws SecurityException if the specified server was not the installed one
83     * @deprecated As of 2.11 use Lookup instead of registering and derigistering HTTP server
84     */

85     public static void deregisterServer(HttpServer.Impl server)
86     throws SecurityException JavaDoc {
87         if (registeredServer == null) {
88             return; // [PENDING] maybe remove this test and let it throw sec exc --jglick
89
}
90
91         if (registeredServer != server) {
92             throw new SecurityException JavaDoc(NbBundle.getBundle(HttpServer.class).getString("SERVER_CANNOT_UNREGISTER"));
93         } else {
94             registeredServer = null;
95         }
96     }
97
98     /** Map a file object to a URL.
99     * Should ensure that the file object is accessible via the given URL.
100     * @param fo the file object to represent
101     * @return a URL providing access to it
102     * @throws MalformedURLException for the usual reasons
103     * @throws UnknownHostException for the usual reasons, or if there is no registered server
104     * @deprecated Use {@link org.openide.filesystems.URLMapper} instead.
105     */

106     public static URL JavaDoc getRepositoryURL(FileObject fo) throws MalformedURLException JavaDoc, UnknownHostException JavaDoc {
107         return getServer().getRepositoryURL(fo);
108     }
109
110     /** Map the repository root to a URL.
111     * This URL should serve a page from which repository objects are accessible.
112     * This means that it should serve a package-oriented view of the Repository, corresponding
113     * to a merge of all files present in the root folders of visible file systems.
114     * @return a URL
115     * @throws MalformedURLException for the usual reasons
116     * @throws UnknownHostException for the usual reasons, or if there is no registered server
117     * @deprecated Assumes repository equals classpath.
118     */

119     public static URL JavaDoc getRepositoryRoot() throws MalformedURLException JavaDoc, UnknownHostException JavaDoc {
120         return getServer().getRepositoryRoot();
121     }
122
123     /** Map a resource path to a URL.
124     * Should ensure that the resource is accessible via the given URL.
125     * @param resourcePath path of the resource in classloader format (e.g. <code>/some/path/resources/icon32.gif</code>)
126     * @return a URL providing access to it
127     * @see ClassLoader#getResource(java.lang.String)
128     * @throws MalformedURLException for the usual reasons
129     * @throws UnknownHostException for the usual reasons, or if there is no registered server
130     * @deprecated Use {@link org.openide.filesystems.URLMapper} with a URL protocol <code>nbres</code>.
131     */

132     public static URL JavaDoc getResourceURL(String JavaDoc resourcePath)
133     throws MalformedURLException JavaDoc, UnknownHostException JavaDoc {
134         return getServer().getResourceURL(resourcePath);
135     }
136
137     /** Get URL root for a resource from system classpath.
138     * @return the URL
139     * @throws MalformedURLException for the usual reasons
140     * @throws UnknownHostException for the usual reasons
141     * @see HttpServer#getResourceURL
142     * @deprecated Use {@link org.openide.filesystems.URLMapper} with a URL protocol <code>nbres</code>.
143     */

144     public static URL JavaDoc getResourceRoot() throws MalformedURLException JavaDoc, UnknownHostException JavaDoc {
145         return getServer().getResourceRoot();
146     }
147
148     /** Requests the server to allow access to it from a given IP address.
149     * This can be useful if a module wishes another machine to be able to access
150     * the server, such as a machine running a deployment server.
151     * The server may or may not grant access to the IP address, for example
152     * if the user does not wish to grant access to the IP address.
153     * @param addr address for which access is requested
154     * @return <code>true</code> if access has been granted
155     * @deprecated Should be replaced by an API in the <code>httpserver</code> module if still required.
156     */

157     public static boolean allowAccess(InetAddress JavaDoc addr)
158     throws UnknownHostException JavaDoc {
159         return getServer().allowAccess(addr);
160     }
161
162     /** Implementation of the HTTP server.
163     * Must be implemented by classes which want to register as a server.
164     * Implementations are obtained using Lookup.
165     * <p>Such a server must be prepared to specially serve pages from
166     * within the IDE, i.e. the Repository and the system class
167     * loader. (It may also serve external pages, if desired.) It should
168     * have a system option specifying at least the port number (<em>by
169     * default, an unused port above 1000</em>), the host access
170     * restrictions (<em>by default, only <code>localhost</code></em>),
171     * and an toggle to disable it. It should provide URLs using the
172     * protocol <code>http</code> so as not to need to register a new protocol
173     * handler.
174     * @deprecated Useful only for {@link HttpServer} which is itself deprecated.
175     */

176     public interface Impl {
177         /** Get the URL for a file object.
178         * @param fo the file object
179         * @return the URL
180         * @throws MalformedURLException for the usual reasons
181         * @throws UnknownHostException for the usual reasons
182         * @see HttpServer#getRepositoryURL
183         */

184         public URL JavaDoc getRepositoryURL(FileObject fo) throws MalformedURLException JavaDoc, UnknownHostException JavaDoc;
185
186         /** Get the URL for the Repository. For this URL,
187         * the implementation should display a page containing a list of links to subdirectories (packages).
188         * @return the URL
189         * @throws MalformedURLException for the usual reasons
190         * @throws UnknownHostException for the usual reasons
191         * @see HttpServer#getRepositoryRoot
192         */

193         public URL JavaDoc getRepositoryRoot() throws MalformedURLException JavaDoc, UnknownHostException JavaDoc;
194
195         /** Get the URL for a resource from system classpath. The URL must comply to java naming conventions,
196         * i.e. the URL must end with a fully qualified resource name.
197         * @param resourcePath the resource path
198         * @return the URL
199         * @throws MalformedURLException for the usual reasons
200         * @throws UnknownHostException for the usual reasons
201         * @see HttpServer#getResourceURL
202         */

203         public URL JavaDoc getResourceURL(String JavaDoc resourcePath)
204         throws MalformedURLException JavaDoc, UnknownHostException JavaDoc;
205
206         /** Get URL root for a resource from system classpath.
207         * @return the URL
208         * @throws MalformedURLException for the usual reasons
209         * @throws UnknownHostException for the usual reasons
210         * @see HttpServer#getResourceURL
211         */

212         public URL JavaDoc getResourceRoot() throws MalformedURLException JavaDoc, UnknownHostException JavaDoc;
213
214         /** Requests the server to allow access to it from a given IP address.
215         * This can be useful if a module wishes another machine to be able to access
216         * the server, such as a machine running a deployment server.
217         * The server may or may not grant access to the IP address, for example
218         * if the user does not wish to grant access to the IP address.
219         * @param addr address for which access is requested
220         * @return <code>true</code> if access has been granted
221         */

222         public boolean allowAccess(InetAddress JavaDoc addr) throws UnknownHostException JavaDoc;
223     }
224 }
225
Popular Tags