emailportlet.java
来自「jetspeed源代码」· Java 代码 · 共 1,760 行 · 第 1/5 页
JAVA
1,760 行
/*
* Copyright 2000-2001,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jetspeed.modules.actions.portlets.email;
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.services.JetspeedSecurity;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.util.PortletConfigState;
import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction;
import org.apache.turbine.util.RunData;
import org.apache.torque.util.Criteria;
import org.apache.turbine.util.upload.FileItem;
import org.apache.velocity.context.Context;
// Java stuff
import java.util.Vector;
import java.util.Hashtable;
import java.util.List;
import java.util.Enumeration;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
//JavaMail
import javax.mail.Folder;
import javax.mail.AuthenticationFailedException;
import javax.mail.NoSuchProviderException;
import javax.mail.MessagingException;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Header;
// upload download to db
import javax.servlet.http.HttpServletResponse;
// email stuff
import org.apache.jetspeed.om.apps.email.EmailInboxPeer;
import org.apache.jetspeed.om.apps.email.EmailInbox;
import org.apache.jetspeed.util.PortletSessionState;
/**
* Email Action
*
* @author <a href="mailto:jlim@gluecode.com">Jonas Lim </a>
* @version $Id: EmailPortlet.java,v 1.2 2004/03/22 22:26:58 taylor Exp $
*/
public class EmailPortlet extends VelocityPortletAction
{
private static final JetspeedLogger log = JetspeedLogFactoryService
.getLogger(EmailPortlet.class.getName());
private final int maxPerPage = 10;
/**
* Subclasses should override this method if they wish to build specific
* content when maximized. Default behavior is to do the same as normal
* content.
*/
protected void buildMaximizedContext(VelocityPortlet portlet,
Context context, RunData rundata) throws Exception
{
buildNormalContext(portlet, context, rundata);
}
protected static final String CUSTOMIZE_TEMPLATE = "customizeTemplate";
/**
* Subclasses should override this method if they wish to provide their own
* customization behavior. Default is to use Portal base customizer action
*/
protected void buildConfigureContext(VelocityPortlet portlet,
Context context, RunData rundata)
{
try
{
super.buildConfigureContext(portlet, context, rundata);
} catch (Exception ex)
{
log.error("Exception", ex);
}
String template = PortletConfigState.getParameter(portlet, rundata,
CUSTOMIZE_TEMPLATE, null);
setTemplate(rundata, template);
}
/**
* Subclasses must override this method to provide default behavior for the
* portlet action
*/
protected void buildNormalContext(VelocityPortlet portlet, Context context,
RunData rundata) throws Exception
{
Hashtable userInfo = this.getEmailUserInfo(rundata, context);
Email email = null;
log.info("BuildNormalContext in EmailPortlet");
String host = getPortletParameter(rundata, context, "hostname");
context.put("host", host);
String user = (String) userInfo.get("username");
String pass = (String) userInfo.get("password");
String emailAdd = (String) userInfo.get("email");
context.put("emailAdd", emailAdd);
String message_prompt = (String)getPortletSession(rundata, context,
"message_prompt");
context.put("message_prompt", message_prompt);
setPortletSession(rundata, context, "message_prompt", "");
try
{
email = new Email(user, pass,
getPortletParameters(rundata, context));
//email.authenticateUser(user, pass);
context.put("hasLoggedIn", "yes");
} catch (AuthenticationFailedException ae)
{
message_prompt = "Please Enter a Valid Username and Password.";
context.put("message_prompt", message_prompt);
context.put("hasLoggedIn", "no");
log.error(ae);
return;
} catch (NoSuchProviderException np)
{
message_prompt = "Please Check Email parameters... Protocol(imap/pop3) is case-sensitive. ";
context.put("message_prompt", message_prompt);
context.put("hasLoggedIn", "no");
log.error(np);
return;
} catch (Exception e)
{
message_prompt = e.getMessage();
context.put("message_prompt", message_prompt);
context.put("hasLoggedIn", "no");
log.error(e);
return;
}
String showNumNewmessages = (String)getPortletSession(rundata, context,
"showNumNewmessages");
if (showNumNewmessages == null || showNumNewmessages.equals(""))
{ // get the number of new messages
// get the username and password in case the user did signout and
// login again using another username and password..
String protocol = getPortletParameter(rundata, context, "protocol");
context.put("protocol", protocol);
int numNewmessages = email.num_Newmessages();
String showInbox = (String)getPortletSession(rundata, context,
"showInbox");
log.info("showInbox " + showInbox);
if (showInbox == null || showInbox.equals(""))// for initial load
// only
{
if (numNewmessages != 0)
{
context.put("numNewmessages", String
.valueOf(numNewmessages)); // displays
// (numNewmessages)the
// number of new
// messages
}
}
context.put("num_Newmessages", String.valueOf(numNewmessages)); // displays
// no.
// of
// new
// messages
// beside
// Inbox(num_Newmessages)
setPortletSession(rundata, context, "showNumNewmessages", "yes");
setPortletSession(rundata, context, "numNewmessages", String
.valueOf(numNewmessages));
} else if (showNumNewmessages.equals("yes"))
{
String num_Newmessages = (String)getPortletSession(rundata, context,
"numNewmessages");
context.put("num_Newmessages", num_Newmessages);
}
String compose = (String)getPortletSession(rundata, context, "compose");
context.put("compose", compose);
String showInbox = (String)getPortletSession(rundata, context, "showInbox");
context.put("showInbox", showInbox);
String showContent = (String)getPortletSession(rundata, context,
"showContent");
context.put("showContent", showContent);
String createfolder = (String)getPortletSession(rundata, context,
"createfolder");
context.put("createfolder", createfolder);
String showFolders = (String)getPortletSession(rundata, context,
"showFolders");
context.put("showFolders", showFolders);
//get folder name
String protocol = (String)getPortletParameter(rundata, context, "protocol");
if (protocol.equals("imap"))
{
String folder_name = (String)getPortletSession(rundata, context,
"folder_name");
if (folder_name == null || folder_name.equals(""))
{
context.put("folder_name", "Inbox");
} else
{
context.put("folder_name", folder_name);
}
//messages on each folder
String showmessagefolder = (String)getPortletSession(rundata, context,
"showmessagefolder");
context.put("showmessagefolder", showmessagefolder);
}
String inboxMessages = (String)getPortletSession(rundata, context,
"inboxMessages");
checkMessages(rundata, context,email);
String msgeContent = (String)getPortletSession(rundata, context,
"msgeContent");
log.info("msgecontent " + msgeContent);
if (msgeContent == null || msgeContent.equals(""))
{
log.info("null");
//need to get the value of inContent to be able to retrieve the
// fields in reply form...
Vector inContent = (Vector) getPortletSession(rundata, context, "inContent");
context.put("inContent", inContent);
} else if (msgeContent.equals("yes"))
{
doShowcontent(rundata, context,email);
setPortletSession(rundata, context, "msgeContent", null);
}
String reply = (String)getPortletSession(rundata, context, "reply");
context.put("reply", reply);
String forward = (String)getPortletSession(rundata, context, "forward");
context.put("forward", forward);
String rsubject = (String)getPortletSession(rundata, context, "rsubject");
context.put("rsubject", rsubject);
String msg = (String)getPortletSession(rundata, context, "msg");
context.put("msg", msg);
//multiple pages
String msgctr = (String)getPortletSession(rundata, context, "msgcount");
String msgcount = "";
if (msgctr != null && !msgctr.equals(""))
{
Integer imsgctr = new Integer(msgctr);
context.put("msgcount", imsgctr);
} else
{
context.put("msgcount", "");
}
String pages = (String)getPortletSession(rundata, context,
"total_no_of_pages");
String total_no_of_pages = "";
if (pages != null && !pages.equals(""))
{
Integer ipages = new Integer(pages);
context.put("total_no_of_pages", ipages);
} else
{
context.put("total_no_of_pages", "");
}
String cpage = (String)getPortletSession(rundata, context, "cur_page");
String cur_page = "";
if (cpage != null && !cpage.equals(""))
{
Integer icur_page = new Integer(cpage);
context.put("cur_page", icur_page);
} else
{
context.put("cur_page", "");
}
String in_index = (String)getPortletSession(rundata, context, "start_index");
String start_index = "";
if (in_index != null && !in_index.equals(""))
{
Integer iin_index = new Integer(in_index);
context.put("start_index", iin_index);
} else
{
context.put("start_index", "");
}
String cRange = (String)getPortletSession(rundata, context, "range_per_page");
String range_per_page = "";
if (cRange != null && !cRange.equals(""))
{
Integer irange_per_page = new Integer(cRange);
context.put("range_per_page", irange_per_page);
} else
{
context.put("range_per_page", "");
}
if (protocol.equals("imap"))
{
context.put("protocol", protocol);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?