KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > httpserver > HttpServerURLMapper


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.httpserver;
21
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.net.InetAddress JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLEncoder JavaDoc;
27 import java.net.URLDecoder JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import org.openide.ErrorManager;
31 import org.openide.filesystems.URLMapper;
32 import org.openide.filesystems.FileObject;
33 import org.openide.util.Exceptions;
34
35 /** Implementation of a URLMapper which creates http URLs for fileobjects in the IDE.
36  * Directs the requests for URLs to WrapperServlet.
37  *
38  * @author Petr Jiricka, David Konecny
39  */

40 public class HttpServerURLMapper extends URLMapper {
41     
42     /** Creates a new instance of HttpServerURLMapper */
43     public HttpServerURLMapper() {
44     }
45     
46     /** Get an array of FileObjects for this url
47      * @param url to wanted FileObjects
48      * @return a suitable array of FileObjects, or null
49      */

50     public FileObject[] getFileObjects(URL JavaDoc url) {
51         String JavaDoc path = url.getPath();
52
53         // remove the wrapper servlet URI
54
String JavaDoc wrapper = httpserverSettings().getWrapperBaseURL ();
55         if (path == null || !path.startsWith(wrapper))
56             return null;
57         path = path.substring(wrapper.length());
58         
59         // resource name
60
if (path.startsWith ("/")) path = path.substring (1); // NOI18N
61
if (path.length() == 0) {
62             return new FileObject[0];
63         }
64         // decode path to EXTERNAL/INTERNAL type of URL
65
URL JavaDoc u = decodeURL(path);
66         if (u == null) {
67             return new FileObject[0];
68         }
69         return URLMapper.findFileObjects(u);
70     }
71     
72     private URL JavaDoc decodeURL(String JavaDoc path) {
73         StringTokenizer JavaDoc slashTok = new StringTokenizer JavaDoc(path, "/", true); // NOI18N
74
StringBuffer JavaDoc newPath = new StringBuffer JavaDoc();
75         while (slashTok.hasMoreTokens()) {
76             String JavaDoc tok = slashTok.nextToken();
77             if (tok.startsWith("/")) { // NOI18N
78
newPath.append(tok);
79             } else {
80                 try {
81                     newPath.append(URLDecoder.decode(tok, "UTF-8")); // NOI18N
82
} catch (UnsupportedEncodingException JavaDoc e) {
83                     assert false : e;
84                     return null;
85                 }
86             }
87         }
88         
89         try {
90             return new URL JavaDoc(newPath.toString());
91         } catch (MalformedURLException JavaDoc ex) {
92             Exceptions.attachMessage(ex, "using: " + newPath);
93             Exceptions.printStackTrace(ex);
94             return null;
95         }
96     }
97     
98     /** Get a good URL for this file object which works according to type:
99      * -inside this VM
100      * - inside this machine
101      * - from networked machines
102      * @return a suitable URL, or null
103      */

104     public URL JavaDoc getURL(FileObject fileObject, int type) {
105         
106         // only do external and network URLs
107
if (type != URLMapper.NETWORK)
108             return null;
109         
110         // fileObject must not be null
111
if (fileObject == null)
112             return null;
113         
114         // It should be OK to call URLMapper here because we call
115
// it with different then NETWORK type.
116
URL JavaDoc u = URLMapper.findURL(fileObject, URLMapper.EXTERNAL);
117         if (u == null) {
118             // if EXTERNAL type is not available try the INTERNAL one
119
u = URLMapper.findURL(fileObject, URLMapper.INTERNAL);
120             if (u == null) {
121                 return null;
122             }
123         }
124         String JavaDoc path = encodeURL(u);
125         HttpServerSettings settings = httpserverSettings();
126         settings.setRunning(true);
127         try {
128             URL JavaDoc newURL = new URL JavaDoc("http", // NOI18N
129
getLocalHost(),
130                 settings.getPort(),
131                 settings.getWrapperBaseURL() + path); // NOI18N
132
return newURL;
133         } catch (MalformedURLException JavaDoc e) {
134             ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
135             return null;
136         }
137     }
138     
139     private String JavaDoc encodeURL(URL JavaDoc u) {
140         String JavaDoc orig = u.toExternalForm();
141         StringTokenizer JavaDoc slashTok = new StringTokenizer JavaDoc(orig, "/", true); // NOI18N
142
StringBuffer JavaDoc path = new StringBuffer JavaDoc();
143         while (slashTok.hasMoreTokens()) {
144             String JavaDoc tok = slashTok.nextToken();
145             if (tok.startsWith("/")) { // NOI18N
146
path.append(tok);
147             } else {
148                 try {
149                     path.append(URLEncoder.encode(tok, "UTF-8")); // NOI18N
150
} catch (UnsupportedEncodingException JavaDoc e) {
151                     assert false : e;
152                     return null;
153                 }
154             }
155         }
156         return path.toString();
157     }
158     
159     /** Returns string for localhost */
160     private static String JavaDoc getLocalHost() {
161         try {
162             return InetAddress.getLocalHost().getHostName();
163         } catch (UnknownHostException JavaDoc e) {
164             return "127.0.0.1"; // NOI18N
165
}
166     }
167
168     /**
169      * Obtains settings of this module
170      */

171     static HttpServerSettings httpserverSettings () {
172         return HttpServerSettings.getDefault();
173     }
174     
175 }
176
Popular Tags