KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > extbrowser > URLUtil


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
20 package org.netbeans.modules.extbrowser;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27
28 import org.openide.ErrorManager;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.URLMapper;
31 import org.openide.util.Lookup;
32
33 /** Utility class for various useful URL-related tasks.
34  * <p>There is similar class in extbrowser/webclient module doing almost the same work.
35  * @author Petr Jiricka
36  */

37 public class URLUtil {
38
39     /** results with URLMapper instances*/
40     private static Lookup.Result result;
41     
42     static {
43         result = Lookup.getDefault().lookup(new Lookup.Template (URLMapper.class));
44     }
45     
46     /** Creates a URL that is suitable for using in a different process on the
47      * same node, similarly to URLMapper.EXTERNAL. May just return the original
48      * URL if that's good enough.
49      * @param url URL that needs to be displayed in browser
50      * @param allowJar is <CODE>jar:</CODE> acceptable protocol?
51      * @return browsable URL or null
52      */

53     public static URL JavaDoc createExternalURL(URL JavaDoc url, boolean allowJar) {
54         if (url == null)
55             return null;
56
57         // return if the protocol is fine
58
if (isAcceptableProtocol(url, allowJar))
59             return url;
60         
61         // remove the anchor
62
String JavaDoc anchor = url.getRef();
63         String JavaDoc urlString = url.toString ();
64         int ind = urlString.indexOf('#');
65         if (ind >= 0) {
66             urlString = urlString.substring(0, ind);
67         }
68         
69         // map to an external URL using the anchor-less URL
70
try {
71             FileObject fo = URLMapper.findFileObject(new URL JavaDoc(urlString));
72             if (fo != null) {
73                 URL JavaDoc newUrl = getURLOfAppropriateType(fo, allowJar);
74                 if (newUrl != null) {
75                     // re-add the anchor if exists
76
urlString = newUrl.toString();
77                     if (ind >=0) {
78                         urlString = urlString + "#" + anchor; // NOI18N
79
}
80                     return new URL JavaDoc(urlString);
81                 }
82             }
83         }
84         catch (MalformedURLException JavaDoc e) {
85             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
86         }
87         
88         return url;
89     }
90     
91     /** Returns a URL for the given file object that can be correctly interpreted
92      * by usual web browsers (including Netscape 4.71, IE and Mozilla).
93      * First attempts to get an EXTERNAL URL, if that is a suitable URL, it is used;
94      * otherwise a NETWORK URL is used.
95      */

96     private static URL JavaDoc getURLOfAppropriateType(FileObject fo, boolean allowJar) {
97         // PENDING - there is still the problem that the HTTP server will be started
98
// (because the HttpServerURLMapper.getURL(...) method starts it),
99
// even when it is not needed
100
URL JavaDoc retVal;
101         URL JavaDoc suitable = null;
102         
103         Iterator JavaDoc instances = result.allInstances ().iterator();
104         while (instances.hasNext()) {
105             URLMapper mapper = (URLMapper) instances.next();
106             retVal = mapper.getURL (fo, URLMapper.EXTERNAL);
107             if ((retVal != null) && isAcceptableProtocol(retVal, allowJar)) {
108                 // return if this is a 'file' or 'jar' URL
109
String JavaDoc p = retVal.getProtocol().toLowerCase();
110                 if ("file".equals(p) || "jar".equals(p)) { // NOI18N
111
return retVal;
112                 }
113                 suitable = retVal;
114             }
115         }
116         
117         // if we found a suitable URL, return it
118
if (suitable != null) {
119             return suitable;
120         }
121         
122         return makeURLLocal(URLMapper.findURL(fo, URLMapper.NETWORK));
123     }
124     
125     private static URL JavaDoc makeURLLocal(URL JavaDoc input) {
126         String JavaDoc host = input.getHost();
127         try {
128             if (host.equals(InetAddress.getLocalHost().getHostName())) {
129                 host = "127.0.0.1"; // NOI18N
130
return new URL JavaDoc(input.getProtocol(), host, input.getPort(), input.getFile());
131             }
132             else return input;
133         } catch (UnknownHostException JavaDoc e) {
134             return input;
135         } catch (MalformedURLException JavaDoc e) {
136             return input;
137         }
138     }
139         
140     /** Returns true if the protocol is acceptable for usual web browsers.
141      * Specifically, returns true for file, http and ftp protocols.
142      */

143     private static boolean isAcceptableProtocol(URL JavaDoc url, boolean allowJar) {
144         String JavaDoc protocol = url.getProtocol().toLowerCase();
145         if ("http".equals(protocol) // NOI18N
146
|| "ftp".equals(protocol) // NOI18N
147
|| "file".equals(protocol)) // NOI18N
148
return true;
149         if (allowJar && "jar".equals(protocol)) { // NOI18N
150
String JavaDoc urlString = url.toString();
151             if (!urlString.toLowerCase().startsWith("jar:nbinst:")) // NOI18N
152
return true;
153         }
154         
155         return false;
156     }
157     
158     /** Determines whether a given browser is capable of displaying URLs with the jar: protocol.
159      * Currently, Mozilla and Firefox can do this.
160      * @param browser browser id - one of the constants defined in ExtWebBrowser
161      * @return true if the browser handles jar URLs
162      */

163     public static boolean browserHandlesJarURLs(String JavaDoc browser) {
164         return (ExtWebBrowser.MOZILLA.equals(browser) ||
165                 ExtWebBrowser.FIREFOX.equals(browser));
166     }
167
168 }
169
Popular Tags