⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 context.java

📁 一个简单的java邮件系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	
	public synchronized boolean isLoggedIn() {
		return loggedIn;
	}
	
	public synchronized boolean isAdminLogonNeeded(ServletRequest request) throws Exception {
		String action = request.getParameter("action");
		if (OlivaUtils.nvl(action).equalsIgnoreCase("logout")) {
			adminLoggedIn = false;
		}

		if (!adminLoggedIn && _admin_enabled && _storage_enabled) {
			String user = request.getParameter("user");
			String pass = request.getParameter("pass");

			if (!OlivaUtils.nvl(user).equals("") && !OlivaUtils.nvl(pass).equals("")) {
				storage.logon(user, pass);
				storage.setCredentials(user, pass);
				storage.close();
				adminLoggedIn = true;
			}
		}
		return !adminLoggedIn;
	}

	public synchronized boolean isAdminLoggedIn() {
		return adminLoggedIn;
	}
		
	public String getHtmlNoPics(String html) {
		return html.replaceAll(imgPattern, "<x-img ");
	}	
		
	public synchronized void setRichContent(boolean richCtnt) {
		mailBean.setRichContent(richCtnt);
	}	

	/**
	 * Deletes messages from current mailbox folder
	 * 
	 * @param sr
	 *            ServletRequest from which messages' numbers and UIDs are
	 *            readed
	 *            
	 * @throws Exception
	 */
	public synchronized void deleteMessages(ServletRequest sr) throws Exception {
		int size = 10;
		Vector numbers = new Vector(size);
		Vector uids = new Vector(size);
		Enumeration params = sr.getParameterNames();
		while (params.hasMoreElements()) {
			String paramName = (String) params.nextElement();
			if (!Pattern.matches("no\\d*uid.*", paramName))
				continue;
			int uidIdx = paramName.indexOf("uid");
			int msgNo = Integer.parseInt(paramName.substring(2, uidIdx));
			String msgUID = paramName.substring(uidIdx + 3);
			numbers.add(new Integer(msgNo));
			uids.add(msgUID);
		}
		if(numbers.size() > 0)
			mailBean.deleteMessages(numbers, uids);
	}

	/**
	 * Parses ServletRequest and sends mail
	 * 
	 * @param sr
	 *            ServletRequest containing message to send as HTML form data
	 * @param delayedMesage
	 *            specifies whether or not Delayed Mesage mode is used
	 * @return list of addresses to which the message has been sent
	 * @throws Exception
	 */
	public synchronized String sendMessageFormData(ServletRequest sr,
			boolean delayedMessage) throws Exception {
		String to = "";
		String cc = "";
		String bcc = "";
		String subject = "";
		String msg = "";
		String msgid = null;
		String fwdno = null;
		String fwduid = null;
		String fwdpartno = null;
		boolean fwdasatt = false;
		String codePage = null;
		boolean asUnicode = false;
		boolean saveCopy = false;
		boolean requestNotify = false;
		boolean important = false;
		Vector vBodyParts = null;

		oliva.mail.FormDataParser fdp = new oliva.mail.FormDataParser(sr,
				clntCp, mailBean.getSizeLimit());
		
		while (fdp.hasMore()) {
			if (fdp.getParam().equalsIgnoreCase("to")) {
				to = fdp.getValue();
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("cc")) {
				cc = fdp.getValue();
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("bcc")) {
				bcc = fdp.getValue();
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("subject")) {
				subject = fdp.getValue();
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("msg")) {
				msg = fdp.getValue();
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("msgid")) {
				msgid = fdp.getValue();
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("fwdno")) {
				fwdno = fdp.getValue();
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("msguid")) {
				fwduid = fdp.getValue();
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("partno")) {
				fwdpartno = fdp.getValue();
				continue;
			}			
			if (fdp.getParam().equalsIgnoreCase("forward_as")) {
				if (fdp.getValue().equalsIgnoreCase("att"))
					fwdasatt = true;
				continue;
			}					
			if (fdp.getParam().equalsIgnoreCase("cp")) {
				codePage = fdp.getValue();
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("as_unicode")) {
				if (fdp.getValue().equalsIgnoreCase("true"))
					asUnicode = true;
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("save_copy")) {
				if (fdp.getValue().equalsIgnoreCase("true"))
					saveCopy = true;
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("rnotify")) {
				if (fdp.getValue().equalsIgnoreCase("true"))
					requestNotify = true;
				continue;
			}
			if (fdp.getParam().equalsIgnoreCase("important")) {
				if (fdp.getValue().equalsIgnoreCase("true"))
					important = true;
				continue;
			}
									
			if (!delayedMessage && !fdp.getAttFileName().equals("")) {
				if (vBodyParts == null)
					vBodyParts = new Vector(5);
				MimeBodyPart mbp = new MimeBodyPart();
				mbp.setDataHandler(new DataHandler(
						new oliva.mail.ByteArrayDataSource(fdp.read(), fdp
								.getAttCtntType())));
				String fname = fdp.getAttFileName();
				mbp.setFileName(Coder.encodeBASE64ifNeeded(fname, mailBean.getSendCodePage(), asUnicode));								
				vBodyParts.add(mbp);
			}
		}

		NewMessage newMsg = NewMessage.create(mailBean.getMyAddress(), to, cc,
				bcc, subject, msg, vBodyParts, msgid, asUnicode, requestNotify);
		newMsg.setFwdMessage(fwdno, fwduid, fwdpartno, fwdasatt, codePage);
		newMsg.saveCopy = saveCopy;
		if(important)
			newMsg.importance = NewMessage.IMP_HIGH;		
		
		if (delayedMessage) {
/*			
			delayedMsg = new NewMessage();
			if (to != null)
				delayedMsg.To = to;
			if (cc != null)
				delayedMsg.Cc = cc;
			if (bcc != null)
				delayedMsg.Bcc = bcc;
			if (subject != null)
				delayedMsg.Subject = subject;
			if (msg != null)
				delayedMsg.text = msg;
			if (msgid != null)
				delayedMsg.refId = msgid;
			if (fwdno != null)
				delayedMsg.fwdNo = fwdno;
			if (fwduid != null)
				delayedMsg.fwdUID = fwduid;
			if (fwdpartno != null)
				delayedMsg.fwdPartNo = fwdpartno;			
			if (codePage != null)
				delayedMsg.cp = codePage;
*/			
			delayedMsg = newMsg;
			return null;
		}
				 		
		return mailBean.sendMessage(newMsg)[0].toString();
	}		
	
/*
	private String getFirstReceipient(NewMessage newMsg) {
		if(newMsg == null)
			return "";
		String result = "";
		if(OlivaUtils.nvl(newMsg.To).trim().equals("")) {
			if(OlivaUtils.nvl(newMsg.Cc).trim().equals("")) {
				if(!OlivaUtils.nvl(newMsg.Bcc).trim().equals(""))
					result = newMsg.Bcc;
			} else
				result = newMsg.Cc;
		} else
			result = newMsg.To;
		return result;
	}
*/	

	public PdaPane getPdaLogo() {
		if(pdaLogo != null)
			return pdaLogo;
		pdaLogo = new PdaPane();
		pdaLogo.setColor(PdaPane.COLOR_CCFF99);
		pdaLogo.setContent("<font size=\"4\" color=\"#999966\" face=\"arial, tahoma, verdana\"><strong><i>Webmail</i></strong></font>");
	    return pdaLogo;
	}

	public WebPane getWebLogo() {
		if(webLogo != null)
			return webLogo;
		webLogo = new WebPane();
		webLogo.setColor(PdaPane.COLOR_CCFF99);
		webLogo.setHeight("35");		
		webLogo.setContent("<font size=\"4\" color=\"#999966\" face=\"arial, tahoma, verdana\"><strong><i>Webmail</i></strong></font>");
	    return webLogo;
	}
		
	public synchronized String sendMessage(ServletRequest request,
			boolean delayedMesage) throws Exception {
		  String to = OlivaUtils.nvl(request.getParameter("to"));
		  String cc = OlivaUtils.nvl(request.getParameter("cc"));
		  String bcc = OlivaUtils.nvl(request.getParameter("bcc"));
		  String subject = OlivaUtils.nvl(request.getParameter("subject"));
		  String msg = OlivaUtils.nvl(request.getParameter("msg"));
		  String msgid = request.getParameter("msgid");
		  String fwdno = request.getParameter("fwdno");
		  String fwduid = request.getParameter("msguid");
		  String fwdpartno = request.getParameter("partno");
		  boolean fwdasatt = OlivaUtils.nvl(request.getParameter("forward_as")).equalsIgnoreCase("att");
		  String codePage = request.getParameter("cp");
		  boolean asUnicode = OlivaUtils.nvl(request.getParameter("as_unicode")).equalsIgnoreCase("true");
		  boolean saveCopy = OlivaUtils.nvl(request.getParameter("save_copy")).equalsIgnoreCase("true");
		  boolean requestNotify = OlivaUtils.nvl(request.getParameter("rnotify")).equalsIgnoreCase("true");
		  boolean important = OlivaUtils.nvl(request.getParameter("important")).equalsIgnoreCase("true");					  					  					  
		  NewMessage newMsg = oliva.mail.NewMessage.create(
				  this.mailBean.getMyAddress(), 
				  to, cc, bcc, 
				  subject, msg);
		  newMsg.refId = msgid;
		  newMsg.setFwdMessage(fwdno, fwduid, fwdpartno, fwdasatt, codePage);
		  newMsg.asUnicode = asUnicode;
		  newMsg.saveCopy = saveCopy;
		  newMsg.requestNotify = requestNotify;
		  if(important)
			  newMsg.importance = MessageHeader.IMP_HIGH; 		  					  
		
		if (delayedMesage) {
			this.delayedMsg = newMsg;
			return null;
		}
		return mailBean.sendMessage(newMsg)[0].toString();
	}	
	
	/**
	 * Returns delayed message
	 * 
	 * @return delayed message. Delayed message is a message that temporary
	 *         stores a composed message before loggin in
	 */
	public synchronized NewMessage getDelayedMessage() {
		return delayedMsg;
	}

	/**
	 * Removes delayed message from the Bean
	 */
	public synchronized void emptyDelayedMessage() {
		delayedMsg = null;
	}

	public String getErrorString(Throwable exception) {
		String result = null;
		boolean fixed = false;
		String excStrLc = exception.toString().toLowerCase();
		if (exception instanceof javax.mail.AuthenticationFailedException) {
			if(excStrLc.indexOf("eof on socket") != -1)
				result = getI18nString("_str_connection_failed");
			else
				result = getI18nString("_str_auth_failed");
			fixed = true;			
/*			
			fixed = true;
			if(excStrLc.indexOf("login failed") != -1 ||
					excStrLc.indexOf("invalid password") != -1)
				result = getI18nString("_str_auth_failed");
			else if(excStrLc.indexOf("eof on socket") != -1)
				result = getI18nString("_str_connection_failed");
			else
				fixed = false;
*/				
		} else if (exception instanceof javax.mail.MessagingException) {
			if(excStrLc.indexOf("connect failed") != -1) {
				result = getI18nString("_str_connection_failed");
				fixed = true;					
			}
		} else if (exception instanceof oliva.mail.MessageTooBig
					|| exception instanceof oliva.mail.ContentTooLong) {
				result = getI18nString("_str_msg_size_limit")
						+ " " + String.valueOf(this.mailBean.getSizeLimit());
				fixed = true;
		}
		
		if(!fixed) {
			String errMsg = exception.getMessage();
			if (!OlivaUtils.nvl(errMsg).trim().equals("")) {
				if (exception instanceof javax.mail.MessagingException) {
					if (errMsg.indexOf("java.net.ConnectException") != -1)
						result = getI18nString("_str_connection_failed");
					else if (errMsg.indexOf("No recipient addresses") != -1)				
						result = getI18nString("_str_no_recipient");
					else
						result = errMsg;
				} else
					result = errMsg;
			} else {
				result = exception.getClass().getName();
			}
		}		
		return result;
	}
	
	public String getStackTrace(Throwable exception) {
		java.io.CharArrayWriter cw = new java.io.CharArrayWriter();
		java.io.PrintWriter pw = new java.io.PrintWriter(cw, true);
		exception.printStackTrace(pw);
		return cw.toString();
	}
	
	public void setNoCache(HttpServletResponse response) {
		//response.setHeader("Cache-Control", "no-cache; no-store; must-revalidate");
		response.setHeader("Cache-Control", "no-store; must-revalidate");
	}
	
	protected static void forward(HttpServlet servlet, HttpServletRequest request,
			HttpServletResponse response, String url) throws ServletException,
			IOException {
		servlet.getServletConfig().getServletContext().getRequestDispatcher(url)
				.forward(request, response);
	}

	protected static void sendErrorRedirect(HttpServlet servlet, HttpServletRequest request,
			HttpServletResponse response, String errorPageURL, Throwable e)
			throws ServletException, IOException {
		request.setAttribute("javax.servlet.jsp.jspException", e);
		servlet.getServletConfig().getServletContext().getRequestDispatcher(
				errorPageURL).forward(request, response);
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -