|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.httpserver; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.Iterator; import java.util.Comparator; import java.util.HashMap; import java.util.Enumeration; import java.util.TreeSet; import java.text.DateFormat; import java.net.InetAddress; import javax.servlet.*; import javax.servlet.http.*; import org.openide.util.NbBundle; import org.openide.TopManager; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.filesystems.FileSystem; import org.openide.util.SharedClassObject; /** Base servlet for servlets which access NetBeans Open APIs * * @author Petr Jiricka * @version 0.11 May 5, 1999 */ public abstract class NbBaseServlet extends HttpServlet { /** Initializes the servlet. */ public void init() throws ServletException { } /** Processes the request for both HTTP GET and POST methods * @param request servlet request * @param response servlet response */ protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException; /** Performs the HTTP GET operation. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { processRequest(request, response); } /** Performs the HTTP POST operation. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { processRequest(request, response); } /** * Returns a short description of the servlet. */ public String getServletInfo() { return NbBundle.getBundle(NbBaseServlet.class).getString("MSG_BaseServletDescr"); } /** Checks whether access should be permitted according to HTTP Server module access settings * (localhost/anyhost, granted addesses) * @return true if access is granted */ protected boolean checkAccess(HttpServletRequest request) throws IOException { HttpServerSettings settings = (HttpServerSettings)SharedClassObject.findObject (HttpServerSettings.class); if (settings == null) return false; if (settings.getHostProperty ().getHost ().equals(HttpServerSettings.ANYHOST)) return true; HashSet hs = settings.getGrantedAddressesSet(); if (hs.contains(request.getRemoteAddr().trim())) return true; String pathI = request.getPathInfo(); if (pathI == null) pathI = ""; // NOI18N // ask user try { String address = request.getRemoteAddr().trim(); if (settings.allowAccess(InetAddress.getByName(address), pathI)) return true; } catch (Exception ex) { TopManager.getDefault().notifyException(ex); return false; } return false; } protected boolean handleClasspathRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { String pathI = request.getPathInfo(); if (pathI == null) { return false; } if (pathI.length() == 0) return false; if (pathI.charAt(0) == '/') pathI = pathI.substring(1); if (pathI.length() == 0) return false; InputStream is = TopManager.getDefault().systemClassLoader().getResourceAsStream(pathI); if (is == null) return false; try { String encoding = null; int ind = pathI.lastIndexOf("."); // NOI18N if (ind != -1) { String ext = pathI.substring(ind + 1); encoding = FileUtil.getMIMEType(ext); } // PENDING - URL com/ behaves incorrectly if (encoding == null) { // encoding = "thisisabug/inclassloader"; // NOI18N return false; } response.setContentType(encoding); // don't know content length ServletOutputStream os = response.getOutputStream(); try { copyStream(is, os); } finally { os.close(); } } finally { is.close(); } return true; } /** Handles a request to a repository item. Returns true if the request was handled, i.e. * if the URL corresponds to a repository FileObject. According to whether this file object is a * folder, either outputs the file with appropriate content type or outputs a directory and file * listing for this folder. */ protected boolean handleRepositoryRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { String pathI = request.getPathInfo(); if (pathI == null) { return false; } FileObject fo = TopManager.getDefault().getRepository().findResource(pathI); if (fo == null) { // try with the trailing / if ((pathI.length() > 0) && (pathI.charAt(pathI.length() - 1) != '/')) fo = TopManager.getDefault().getRepository().findResource(pathI + '/'); } if (fo == null) return false; // handle the request if (fo.isFolder()) sendDirectory(request, response, fo); else sendFile(request,response,fo); return true; } private void sendFile(HttpServletRequest request, HttpServletResponse response, FileObject file) throws IOException { String encoding = file.getMIMEType(); response.setContentType(encoding); int len = (int)file.getSize(); response.setContentLength(len); response.setDateHeader("Last-Modified", file.lastModified().getTime()); // NOI18N InputStream in = file.getInputStream(); try { ServletOutputStream os = response.getOutputStream(); try { copyStream(in, os); } finally { os.close(); } } finally { in.close(); } } private void sendDirectory(HttpServletRequest request, HttpServletResponse response, FileObject file) throws IOException { String base = file.getPackageName('/'); response.setContentType("text/html"); // NOI18N String title = base; if (title.length() == 0) title = NbBundle.getBundle(NbBaseServlet.class).getString("LAB_REPOSITORY_ROOT"); PrintWriter out=response.getWriter(); out.print(" |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.