1 19 20 package org.netbeans.modules.httpserver; 21 22 import java.io.UnsupportedEncodingException ; 23 import java.net.InetAddress ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.net.URLEncoder ; 27 import java.net.URLDecoder ; 28 import java.net.UnknownHostException ; 29 import java.util.StringTokenizer ; 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 40 public class HttpServerURLMapper extends URLMapper { 41 42 43 public HttpServerURLMapper() { 44 } 45 46 50 public FileObject[] getFileObjects(URL url) { 51 String path = url.getPath(); 52 53 String wrapper = httpserverSettings().getWrapperBaseURL (); 55 if (path == null || !path.startsWith(wrapper)) 56 return null; 57 path = path.substring(wrapper.length()); 58 59 if (path.startsWith ("/")) path = path.substring (1); if (path.length() == 0) { 62 return new FileObject[0]; 63 } 64 URL u = decodeURL(path); 66 if (u == null) { 67 return new FileObject[0]; 68 } 69 return URLMapper.findFileObjects(u); 70 } 71 72 private URL decodeURL(String path) { 73 StringTokenizer slashTok = new StringTokenizer (path, "/", true); StringBuffer newPath = new StringBuffer (); 75 while (slashTok.hasMoreTokens()) { 76 String tok = slashTok.nextToken(); 77 if (tok.startsWith("/")) { newPath.append(tok); 79 } else { 80 try { 81 newPath.append(URLDecoder.decode(tok, "UTF-8")); } catch (UnsupportedEncodingException e) { 83 assert false : e; 84 return null; 85 } 86 } 87 } 88 89 try { 90 return new URL (newPath.toString()); 91 } catch (MalformedURLException ex) { 92 Exceptions.attachMessage(ex, "using: " + newPath); 93 Exceptions.printStackTrace(ex); 94 return null; 95 } 96 } 97 98 104 public URL getURL(FileObject fileObject, int type) { 105 106 if (type != URLMapper.NETWORK) 108 return null; 109 110 if (fileObject == null) 112 return null; 113 114 URL u = URLMapper.findURL(fileObject, URLMapper.EXTERNAL); 117 if (u == null) { 118 u = URLMapper.findURL(fileObject, URLMapper.INTERNAL); 120 if (u == null) { 121 return null; 122 } 123 } 124 String path = encodeURL(u); 125 HttpServerSettings settings = httpserverSettings(); 126 settings.setRunning(true); 127 try { 128 URL newURL = new URL ("http", getLocalHost(), 130 settings.getPort(), 131 settings.getWrapperBaseURL() + path); return newURL; 133 } catch (MalformedURLException e) { 134 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 135 return null; 136 } 137 } 138 139 private String encodeURL(URL u) { 140 String orig = u.toExternalForm(); 141 StringTokenizer slashTok = new StringTokenizer (orig, "/", true); StringBuffer path = new StringBuffer (); 143 while (slashTok.hasMoreTokens()) { 144 String tok = slashTok.nextToken(); 145 if (tok.startsWith("/")) { path.append(tok); 147 } else { 148 try { 149 path.append(URLEncoder.encode(tok, "UTF-8")); } catch (UnsupportedEncodingException e) { 151 assert false : e; 152 return null; 153 } 154 } 155 } 156 return path.toString(); 157 } 158 159 160 private static String getLocalHost() { 161 try { 162 return InetAddress.getLocalHost().getHostName(); 163 } catch (UnknownHostException e) { 164 return "127.0.0.1"; } 166 } 167 168 171 static HttpServerSettings httpserverSettings () { 172 return HttpServerSettings.getDefault(); 173 } 174 175 } 176 | Popular Tags |