📄 webmailservlet.java
字号:
/* CVS ID: $Id: WebMailServlet.java,v 1.8 2001/01/02 16:19:27 wastl Exp $ */package net.wastl.webmail.server;import java.net.*;import java.io.*;import java.util.*;import java.lang.reflect.*;import javax.mail.Session;import javax.mail.Provider;import net.wastl.webmail.server.http.*;import net.wastl.webmail.ui.*;import net.wastl.webmail.ui.html.*;import net.wastl.webmail.exceptions.*;import javax.servlet.*;import javax.servlet.http.*;/* * WebMailServer.java * * Created: Oct 1999 * * Copyright (C) 1999-2000 Sebastian Schaffert * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//** * This is WebMails main server. From here most parts will be administered. * This is the servlet implementation of WebMail (introduced in 0.6.1) * * Created: Tue Feb 2 12:07:25 1999 * * @author Sebastian Schaffert * @version $Revision: 1.8 $ *//* devink 7/15/2000 - service() now handles a TwoPassAuthenticationException, * as does newSession() * devink 9/24/2000 - remove TwoPassAuthenticationException stuff */public class WebMailServlet extends WebMailServer implements Servlet { ServletConfig srvlt_config; /** Size of the chunks that are sent. Must not be greater than 65536 */ private static final int chunk_size=8192; protected String basepath; protected String imgbase; public WebMailServlet() { } public void init(ServletConfig config) throws ServletException { System.err.println("Init"); srvlt_config=config; this.config=new Properties(); Enumeration enum=config.getInitParameterNames(); while(enum.hasMoreElements()) { String s=(String)enum.nextElement(); this.config.put(s,config.getInitParameter(s)); System.err.println(s+": "+config.getInitParameter(s)); } /* * Issue a warning if webmail.basepath and/or webmail.imagebase are * not set. */ if(config.getInitParameter("webmail.basepath")==null) { config.getServletContext().log("Warning: webmail.basepath initArg should be set to the WebMail Servlet's base path"); basepath=""; } else { basepath = config.getInitParameter("webmail.basepath"); } if(config.getInitParameter("webmail.imagebase") == null) { config.getServletContext().log("Error: webmail.basepath initArg should be set to the WebMail Servlet's base path"); imgbase=""; } else { imgbase = config.getInitParameter("webmail.imagebase"); } /* * Try to get the pathnames from the URL's if no path was given * in the initargs. */ if(config.getInitParameter("webmail.data.path") == null) { this.config.put("webmail.data.path",config.getServletContext().getRealPath("")+"/data"); } if(config.getInitParameter("webmail.lib.path") == null) { this.config.put("webmail.lib.path",config.getServletContext().getRealPath("")+"/lib"); } if(config.getInitParameter("webmail.template.path") == null) { this.config.put("webmail.template.path",config.getServletContext().getRealPath("")+"/lib/templates"); } if(config.getInitParameter("webmail.xml.path") == null) { this.config.put("webmail.xml.path",config.getServletContext().getRealPath("")+"/lib/xml"); } /* * Call the WebMailServer's initialization method * and forward all Exceptions to the ServletServer */ try { doInit(); } catch(Exception e) { e.printStackTrace(); throw new ServletException("Could not intialize: "+e.getMessage(),e); } } public void debugOut(String msg, Exception ex) { if(getDebug()) { srvlt_config.getServletContext().log(ex,msg); } } public ServletConfig getServletConfig() { return srvlt_config; } public String getServletInfo() { return getVersion()+"\n(c)2000 by Sebastian Schaffert\nThis software is distributed under the GNU General Public License (GPL)"; } public void destroy() { shutdown(); } /** * Handle a request to the WebMail servlet. * * This is the central method of the WebMailServlet. It first gathers all of the necessary information * from the client, then either creates or gets a Session and executes the URL handler for the given * path. */ public void service(ServletRequest req1, ServletResponse res1) throws ServletException { HttpServletRequest req=(HttpServletRequest)req1; HttpServletResponse res=(HttpServletResponse)res1; HTTPRequestHeader http_header=new HTTPRequestHeader(); Enumeration en=req.getHeaderNames(); while(en.hasMoreElements()) { String s=(String)en.nextElement(); http_header.setHeader(s,req.getHeader(s)); } if(req.getPathInfo()!=null) { http_header.setPath(req.getPathInfo()); } else { http_header.setPath("/"); } InetAddress addr; try { addr=InetAddress.getByName(req.getRemoteHost()); } catch(UnknownHostException e) { try { addr=InetAddress.getByName(req.getRemoteAddr()); } catch(Exception ex) { throw new ServletException("Remote host must identify!"); } } HTMLDocument content=null; int err_code=400; HTTPSession sess=null; /* Here we try to parse the MIME content that the Client sent in his POST since the JServ doesn't do that for us:-( At least we can use the functionality provided by the standalone server where we need to parse the content ourself anyway. */ try { BufferedOutputStream out=new BufferedOutputStream(res.getOutputStream()); /* First we try to use the Servlet API's methods to parse the parameters. Unfortunately, it doesn't know how to handle MIME multipart POSTs, so we will have to handle that ourselves */ /* First get all the parameters and set their values into http_header */ Enumeration enum2=req.getParameterNames(); while(enum2.hasMoreElements()) { String s=(String)enum2.nextElement(); http_header.setContent(s,req.getParameter(s)); } /* Then we set all the headers in http_header */ enum2=req.getHeaderNames(); while(enum2.hasMoreElements()) { String s=(String)enum2.nextElement(); http_header.setHeader(s,req.getHeader(s)); } /* In Servlet API 2.2 we might want to fetch the attributes also, but this doesn't work in API 2.0, so we leave it commented out */// enum=req.getAttributeNames();// while(enum.hasMoreElements()) {// String s=(String)enum.nextElement();// System.err.println("ATTR: "+s+" = "+req.getAttribute(s));// } /* Now let's try to handle multipart/form-data posts */ if(req.getContentType() != null && req.getContentType().toUpperCase().startsWith("MULTIPART/FORM-DATA")) { BufferedReader in=req.getReader(); System.err.println("Content type was multipart/form-data; Try parsing."); StringBuffer str=new StringBuffer(); int total=0; try { char[] buf=new char[8192]; int read=1; int cl=req.getContentLength(); /* Unfortunately we cannot check for in.ready() as it always returns false in JSDK 2.2. Therefore we must rely on req.getContentType and req.getContentLength */// while(in.ready() && read > 0 && (cl==-1 ||total < cl)) { while(read > 0 && (cl==-1 ||total < cl)) { read=in.read(buf,0,8192); if(read > 0) { total+=read; str.append(buf,0,read);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -