%
/******************************************************************************
* Copyright (c) 2000-2005 e-Deal
*
* e-Deal S.A.
* 55 rue Brillat Savarin
* 75013 Paris
* France
*
* T: +33 (0)1 53 80 80 30
* F: +33 (0)1 73 01 69 77
* http://www.e-deal.com
*
* La diffusion de ce code source sous quelque forme que ce soit sans
* l'autorisation de E-DEAL est interdite.
*
* Vous êtes autorisés à modifier ce code source uniquement pour votre usage
* propre et sous réserve que les mentions de copyright demeurent intactes.
* Ce code est fourni en l'état. Aucune garantie d'aucune sorte, explicite ou
* implicite n'est donnée. En aucun cas E-DEAL ne pourra être tenu pour
* responsable des dommages pouvant résulter de l'utilisation de ce code
* source.
*
******************************************************************************/
%><%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="true" import="com.edeal.frontline.*,java.util.*,javax.mail.*,javax.mail.internet.*,java.io.*"%><%@ page import="com.edeal.frontline.navigation.FlRequest" %><%@ page import="com.edeal.frontline.FlContext" %><%@ page import="com.edeal.frontline.DataDictionary" %><%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %><%
final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(getClass());
FlRequest flRequest = new FlRequest(request);
FlContext context = flRequest.getContext();
DataDictionary dico = context.getDataDictionary();
MessagesBean msgBean = (MessagesBean)session.getAttribute("Messages");
session.removeAttribute("Messages");
String msgID;
if (msgBean != null){
try {
msgID = msgBean.getID();
} catch (FrontlineException e) {
msgID = flRequest.getRequestParameterOrAttribute("id");
}
}else{
msgID = flRequest.getRequestParameterOrAttribute("msgID");
msgBean = new MessagesBean(msgID, session);
}
//clé du message à afficher
String toDisplay = Utils.getRequestParameter(context, request, "toDisplay");
if (toDisplay == null) toDisplay = "1";
String dbVendor = context.getContextParameter("DatabaseVendor");
boolean isOracle = "oracle".equalsIgnoreCase(dbVendor);
Hashtable messageOutline = new Hashtable();
Hashtable levelFromCID = new Hashtable();
String displayType = "";
try {
MessagesBean.Mail mail = null;
try {
mail = msgBean.getMail(null, null);
} catch (Exception e) {
logger.error("exception : " + e + " " + e.getMessage());
logger.error("STACK TRACE : ",e);
}
if (mail!=null) {
MimeMessage m = mail.getMimeMessage();
//construire la hashtable de toute l'arborescence
String levelStub = "";
int level = 0;
dumpPart(session, messageOutline, levelStub, level, displayType, m, levelFromCID);
//on a maintenant dans messageOutline toute l'arborescence du message
//chaque clé de la hashtable est un numéro permettant de situer l'entrée dans l'arbre (1 ; 1.1 ; 1.2 ; 1.1.1 ...)
//la valeur correspondant à chaque clé est un vecteur : (type de noeud, contenu du noeud).
// - Si le type vaut "MESSAGE", le contenu est la chaîne de caractère de l'enveloppe.
// Et on trouvera un autre élément dans la hashtable de clé même_numéroBODY (par exemple "1.1.1BODY")
// et de valeur le body du message
// - Si c'est un attachment, le type est le type de l'attachment et le contenu est le nom du fichier à récupérer
//afficher le mail qui nous intéresse -> initialisation de levelStub et level là où on veut
if (toDisplay.lastIndexOf(".") != -1) {
levelStub = toDisplay.substring(0, toDisplay.lastIndexOf(".") + 1);
level = new Integer(toDisplay.substring(toDisplay.lastIndexOf(".") + 1)).intValue();
} else {
levelStub = "";
level = new Integer(toDisplay).intValue();
}
if (messageOutline.containsKey(toDisplay)) {
StringBuffer newBody = new StringBuffer();
String body = "";
Vector node = (Vector)messageOutline.get(toDisplay);
if (!"MESSAGE".equals(node.elementAt(0))) {
%>
Not a mail! : <%= node.elementAt(0) %>
<%
}
else
{
body = (String)messageOutline.get(toDisplay + "BODY");
// traitement des images inline
int currPos = 0;
String pattern = "\"cid:";
int pos = body.indexOf(pattern);
while (pos > -1)
{
newBody.append(body.substring(0, pos));
currPos = pos + pattern.length();
body = body.substring(currPos);
int pos1 = body.indexOf("\"");
String levelToDisplay = (String)levelFromCID.get("<" + body.substring(0, pos1) + ">");
newBody.append("\"");
newBody.append(request.getContextPath());
newBody.append("/netbusiness/messages/serve_attachment.jsp?msgID=");
newBody.append(msgID);
newBody.append("&toDisplay=");
newBody.append(levelToDisplay);
body = body.substring(pos1);
pos = body.indexOf(pattern);
}
newBody.append(body);
%>
<%= newBody.toString() %>
<%
} else {
logger.warn("key to display not present in hashtable");
}
} else {
logger.debug("netbusiness/messages/display_message.jsp : mail null!"); %>
<%
}
} catch (Exception e ) {
logger.error(e + " " + e.getMessage());
logger.error("STACK TRACE : ",e);
}
%>
<%!
//dumpPart builds (recursively) the Hashtable messageOutline as the tree that represents the message p
public int dumpPart(HttpSession session, Hashtable messageOutline, String levelStub, int level, String displayType, javax.mail.Part p, Hashtable levelFromCID) throws Exception {
//logger.debug("\nDUMPPART " + levelStub + level);
//when we arrive here, levelStub + level is the level of the former brother
String envelope = null;
if (p instanceof Message) {
envelope = dumpEnvelope(session, (Message)p).toString();
}
String ct = p.getContentType();
String filename = p.getFileName();
boolean isRfc822= p.isMimeType("message/rfc822");
/*
* Using isMimeType to determine the content type avoids
* fetching the actual content data until we need it.
*/
if (p.isMimeType("multipart/*") && "".equals(levelStub)) {
level++;
dumpPart(session, messageOutline, levelStub + level + ".", 0, displayType, p, levelFromCID);
return level;
}
if ((p.isMimeType("text/plain") || p.isMimeType("text/html")) && filename==null ) {
++level;
//if the message is text/plain, then we are interested in the envelope
if (envelope != null) {
while (messageOutline.containsKey(levelStub.substring(2) + level)) { level++; }
//logger.debug("1. Putting envelope " + levelStub.substring(2) + level + " in hashtable : " + envelope);
Vector node = new Vector(2);
node.add(0, "MESSAGE");
node.add(1, envelope);
messageOutline.put(levelStub.substring(2) + level, node);
}
//store message body in the hashtable
messageOutline.put(levelStub.substring(2) + level + "BODY", "" + (String)p.getContent() + "
");
String cid = null;
Enumeration headers = p.getAllHeaders();
while (headers.hasMoreElements()) {
Header header = (Header)headers.nextElement();
//logger.debug(header.getName() + " / " + header.getValue());
if ("Content-ID".equalsIgnoreCase(header.getName())) {
cid = header.getValue();
}
}
if (levelStub != null && cid != null) {
levelFromCID.put(cid, levelStub.substring(2) + level);
}
return level;
} else if (p.isMimeType("multipart/*")) {
/**/
Multipart mp = (Multipart)p.getContent();
int count = mp.getCount();
//if this is multipart alternative, get the html form, not the text form
if (p.isMimeType("multipart/alternative") && count==2) {
//logger.debug("multipart/alternative");
//get the envelope
if (envelope != null) {
//level ++;
//while (messageOutline.containsKey(levelStub.substring(2) + (level+1))) { level++; }
Vector node = new Vector(2);
node.add(0, "MESSAGE");
node.add(1, envelope);
messageOutline.put(levelStub.substring(2) + (level+1), node);
String cid = null;
Enumeration headers = p.getAllHeaders();
while (headers.hasMoreElements()) {
Header header = (Header)headers.nextElement();
//logger.debug(header.getName() + " / " + header.getValue());
if ("Content-ID".equalsIgnoreCase(header.getName())) {
cid = header.getValue();
}
}
if (levelStub != null && cid != null) {
levelFromCID.put(cid, levelStub.substring(2) + (level + 1));
}
}
//call dump part on the text/html part
return dumpPart(session, messageOutline, levelStub, level, displayType, mp.getBodyPart(1), levelFromCID);
} else { //if this part is "true" multipart
//get the envelope
if (envelope != null) {
while (messageOutline.containsKey(levelStub.substring(2) + (level+1))) { level++; }
Vector node = new Vector(2);
node.add(0, "MESSAGE");
node.add(1, envelope);
messageOutline.put(levelStub.substring(2) + (level+1), node);
String cid = null;
Enumeration headers = p.getAllHeaders();
while (headers.hasMoreElements()) {
Header header = (Header)headers.nextElement();
//logger.debug(header.getName() + " / " + header.getValue());
if ("Content-ID".equalsIgnoreCase(header.getName())) {
cid = header.getValue();
}
}
if (levelStub != null && cid != null) {
levelFromCID.put(cid, levelStub.substring(2) + (level + 1));
}
}
//loop on its parts, same level in the tree
for (int i = 0; i < count; i++) {
dumpPart(session, messageOutline, levelStub, level, displayType, mp.getBodyPart(i), levelFromCID);
}
}
} else if (p.isMimeType("message/rfc822")) {
level++;
Object ocontent=p.getContent();
System.out.println("ocontent:"+ocontent.getClass().getName());
dumpPart(session, messageOutline, levelStub + level + ".", 0, displayType, (javax.mail.Part)p.getContent(), levelFromCID);
return level;
} else {
/*
* If we actually want to see the data, and it's not a
* MIME type we know, fetch it and check its Java type.
*/
Object o = p.getContent();
if (o instanceof String) {
level++;
while (messageOutline.containsKey(levelStub.substring(2) + level + "BODY")) { level++; }
//logger.debug("Putting Html body " + levelStub.substring(2) + level + " in hashtable");
messageOutline.put(levelStub.substring(2) + level + "BODY", o);
String cid = null;
Enumeration headers = p.getAllHeaders();
while (headers.hasMoreElements()) {
Header header = (Header)headers.nextElement();
//logger.debug(header.getName() + " / " + header.getValue());
if ("Content-ID".equalsIgnoreCase(header.getName())) {
cid = header.getValue();
}
}
if (levelStub != null && cid != null) {
levelFromCID.put(cid, levelStub.substring(2) + level);
}
return level;
} else {
level++;
while (messageOutline.containsKey(levelStub.substring(2) + level)) { level++; }
//logger.debug("Putting attachment " + levelStub.substring(2) + level + " in hashtable : " + filename);
Vector node = new Vector(2);
int size = p.getSize();
String sSize = "";
if (size < 1024) {
sSize = String.valueOf(size) + " " + Utils.getMessage(session, "L6376", "octets");
} else if (size < (1024 * 1024)) {
sSize = String.valueOf(size / 1024) + " " + Utils.getMessage(session, "L6377", "Ko");
} else {
float moSize = (float)Math.round((float)(100 * size) / (float)(1024 * 1024)) / (float)100;
sSize = String.valueOf(moSize) + " " + Utils.getMessage(session, "L6378", "Mo");
}
node.add(0, sSize);
node.add(1, filename);
messageOutline.put(levelStub.substring(2) + level, node);
String cid = null;
Enumeration headers = p.getAllHeaders();
while (headers.hasMoreElements()) {
Header header = (Header)headers.nextElement();
//logger.debug(header.getName() + " / " + header.getValue());
if ("Content-ID".equalsIgnoreCase(header.getName())) {
cid = header.getValue();
}
}
if (levelStub != null && cid != null) {
levelFromCID.put(cid, levelStub.substring(2) + level);
}
return level;
}
}
return -1;
} //end of dumpMessage
public StringBuffer dumpEnvelope(HttpSession session, Message m) throws Exception {
StringBuffer buf = new StringBuffer();
//buf.append("");
buf.append("");
Address[] a;
// FROM
buf.append("" + Utils.getMessage(session, "L6367", "De :") + " | ");
if ((a = m.getFrom()) != null) {
buf.append("");
for (int j = 0; j < a.length; j++) {
String displayName = ((InternetAddress)a[j]).getPersonal() != null ? ((InternetAddress)a[j]).getPersonal() : ((InternetAddress)a[j]).getAddress();
if (j!=0) buf.append(", ");
buf.append("" + displayName + "");
}
buf.append(" | ");
}
buf.append("
");
// TO
if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
buf.append("" + Utils.getMessage(session, "L6368", "A :") + " | ");
buf.append("");
for (int j = 0; j < a.length; j++) {
if (j!=0) buf.append(", ");
String displayName = ((InternetAddress)a[j]).getPersonal() != null ? ((InternetAddress)a[j]).getPersonal() : ((InternetAddress)a[j]).getAddress();
buf.append("" + displayName + "");
}
buf.append(" |
");
}
// CC
if ((a = m.getRecipients(Message.RecipientType.CC)) != null) {
buf.append("" + Utils.getMessage(session, "L622E", "Cc:") + " | ");
buf.append("");
for (int j = 0; j < a.length; j++) {
if (j!=0) buf.append(", ");
String displayName = ((InternetAddress)a[j]).getPersonal() != null ? ((InternetAddress)a[j]).getPersonal() : ((InternetAddress)a[j]).getAddress();
buf.append("" + displayName + "");
}
buf.append(" |
");
}
// DATE
Date d = m.getSentDate();
buf.append("" + Utils.getMessage(session, "F2019", "Date") + " | " +
(d != null ? Utils.formatToWeb(d, false) : Utils.getMessage(session, "L6369", "Inconnue")) + " |
");
// SUBJECT
buf.append("" + Utils.getMessage(session, "F203B", "Sujet") + " | " + m.getSubject() + " |
");
buf.append("");
//buf.append("
");
//logger.debug("Envelope : " + buf);
return buf;
} // end dumpenvelope
%><%
/******************************************************************************
* CVS Log File - This is no longer maintained!
*
* Revision 1.1 2005/03/11 14:37:51 brian
* Initial revision. Copied from sales/interaction
*
*
*****************************************************************************/
%>