1 19 20 package org.netbeans.modules.httpserver; 21 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.net.InetAddress ; 25 import java.net.UnknownHostException ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.net.URLConnection ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.http.*; 31 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.URLMapper; 34 import org.openide.util.NbBundle; 35 import org.openide.util.SharedClassObject; 36 37 import javax.servlet.ServletOutputStream ; 38 39 43 public class WrapperServlet extends NbBaseServlet { 44 45 private static final long serialVersionUID = 8009602136746998361L; 46 47 48 public WrapperServlet () { 49 } 50 51 55 protected void processRequest (HttpServletRequest request, HttpServletResponse response) 56 throws ServletException , java.io.IOException { 57 if (!checkAccess(request)) { 58 response.sendError(HttpServletResponse.SC_FORBIDDEN, 59 NbBundle.getMessage(WrapperServlet.class, "MSG_HTTP_FORBIDDEN")); 60 return; 61 } 62 ServletOutputStream out = response.getOutputStream (); 65 try { 66 String requestURL = getRequestURL(request); 67 URLMapper serverMapper = new HttpServerURLMapper(); 69 FileObject files[] = serverMapper.getFileObjects(new URL (requestURL)); 70 if ((files == null) || (files.length != 1)) { 71 throw new IOException (); 72 } 73 URL internal = URLMapper.findURL(files[0], URLMapper.INTERNAL); 74 URLConnection conn = internal.openConnection(); 75 76 response.setContentType(conn.getContentType ()); 79 InputStream in = conn.getInputStream (); 80 byte [] buff = new byte [256]; 81 int len; 82 83 while ((len = in.read (buff)) != -1) { 84 out.write (buff, 0, len); 85 out.flush(); 86 } 87 in.close (); 88 89 } 90 catch (MalformedURLException ex) { 91 try { 92 response.sendError (HttpServletResponse.SC_NOT_FOUND, 93 NbBundle.getMessage(WrapperServlet.class, "MSG_HTTP_NOT_FOUND")); 94 } 95 catch (IOException ex2) {} 96 } 97 catch (IOException ex) { 98 try { 99 response.sendError (HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 100 } 101 catch (IOException ex2) {} 102 } 103 finally { 104 try { out.close(); } catch (Exception ex) {} 105 } 106 } 107 108 private String getRequestURL(HttpServletRequest request) throws UnknownHostException , MalformedURLException { 109 HttpServerSettings settings = HttpServerSettings.getDefault(); 110 111 String pi = request.getPathInfo(); 112 if (pi.startsWith("/")) { pi = pi.substring(1); 114 } 115 URL reconstructedURL = new URL ("http", InetAddress.getLocalHost ().getHostName (), 117 settings.getPort (), 118 settings.getWrapperBaseURL () + pi.toString()); 119 return reconstructedURL.toExternalForm(); 120 } 121 122 125 public String getServletInfo() { 126 return NbBundle.getMessage(WrapperServlet.class, "MSG_WrapperServletDescr"); 127 } 128 129 } 130 | Popular Tags |