001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.webclient;
043:
044: import java.net.URL;
045: import java.net.MalformedURLException;
046: import java.util.Iterator;
047:
048: import org.openide.ErrorManager;
049: import org.openide.filesystems.FileObject;
050: import org.openide.filesystems.URLMapper;
051: import org.openide.util.Lookup;
052:
053: /** Utility class for various useful URL-related tasks.
054: * !!! COPIED from extbrowser.
055: *
056: * @author Petr Jiricka
057: */
058: public class URLUtil {
059:
060: /** results with URLMapper instances*/
061: private static Lookup.Result result;
062:
063: static {
064: result = Lookup.getDefault().lookup(
065: new Lookup.Template(URLMapper.class));
066: }
067:
068: /** Creates a URL that is suitable for using in a different process on the
069: * same node, similarly to URLMapper.EXTERNAL. May just return the original
070: * URL if that's good enough.
071: */
072: public static URL createExternalURL(URL url) {
073: if (url == null)
074: return null;
075:
076: // return if the protocol is fine
077: if (isAcceptableProtocol(url.getProtocol().toLowerCase()))
078: return url;
079:
080: // remove the anchor
081: String anchor = url.getRef();
082: String urlString = url.toString();
083: int ind = urlString.indexOf('#');
084: if (ind >= 0) {
085: urlString = urlString.substring(0, ind);
086: }
087:
088: // map to an external URL using the anchor-less URL
089: try {
090: FileObject fos[] = URLMapper.findFileObjects(new URL(
091: urlString));
092: if ((fos != null) && (fos.length > 0)) {
093: URL newUrl = getURLOfAppropriateType(fos[0]);
094: if (newUrl != null) {
095: // re-add the anchor if exists
096: urlString = newUrl.toString();
097: if (ind >= 0) {
098: urlString = urlString + "#" + anchor; // NOI18N
099: }
100: return new URL(urlString);
101: }
102: }
103: } catch (MalformedURLException e) {
104: ErrorManager.getDefault().notify(
105: ErrorManager.INFORMATIONAL, e);
106: }
107:
108: return url;
109: }
110:
111: /** Returns a URL for the given file object that can be correctly interpreted
112: * by usual web browsers (including Netscape 4.71, IE and Mozilla).
113: * First attempts to get an EXTERNAL URL, if that is a suitable URL, it is used;
114: * otherwise a NETWORK URL is used.
115: */
116: private static URL getURLOfAppropriateType(FileObject fo) {
117: // PENDING - there is still the problem that the HTTP server will be started
118: // (because the HttpServerURLMapper.getURL(...) method starts it),
119: // even when it is not needed
120: URL retVal;
121: URL suitable = null;
122:
123: Iterator instances = result.allInstances().iterator();
124: while (instances.hasNext()) {
125: URLMapper mapper = (URLMapper) instances.next();
126: retVal = mapper.getURL(fo, URLMapper.EXTERNAL);
127: if ((retVal != null)
128: && isAcceptableProtocol(retVal.getProtocol()
129: .toLowerCase())) {
130: // return if this is a 'file' URL
131: if ("file".equals(retVal.getProtocol().toLowerCase())) { // NOI18N
132: return retVal;
133: }
134: suitable = retVal;
135: }
136: }
137:
138: // if we found a suitable URL, return it
139: if (suitable != null) {
140: return suitable;
141: }
142:
143: return URLMapper.findURL(fo, URLMapper.NETWORK);
144: }
145:
146: /** Returns true if the protocol is acceptable for usual web browsers.
147: * Specifically, returns true for file, http and ftp protocols.
148: */
149: private static boolean isAcceptableProtocol(String protocol) {
150: if ("http".equals(protocol) // NOI18N
151: || "ftp".equals(protocol) // NOI18N
152: || "file".equals(protocol)) // NOI18N
153: return true;
154:
155: return false;
156: }
157:
158: }
|