<% /****************************************************************************** * 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 session="true" import="com.edeal.frontline.*,java.util.*,javax.mail.*,javax.mail.internet.*,java.io.*"%><%@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()); String contextPath = (String) session.getAttribute("contextPath"); FlContext context = null; if(contextPath != null) { context = Frontline.getContext(contextPath); } else { context = Frontline.getContext(request.getContextPath()); } Hashtable messageOutline = new Hashtable(); String displayType = ""; //MessagesBean correspondant String msgID = Utils.getRequestParameter(context, request, "msgID"); //clé du message à afficher String toDisplay = Utils.getRequestParameter(context, request, "toDisplay"); if (toDisplay == null) toDisplay = "1"; try { // logger.debug("\n----- DISPLAY ATTACHMENT !!! " + msgID + " level " + toDisplay); //we want to get the MimeMessage object from the messagesBean MessagesBean msgBean = new MessagesBean(msgID, context); 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); //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 Vector attachment = (Vector)messageOutline.get(toDisplay); if (attachment.size() == 3) { response.setHeader("Content-Disposition","attachment; filename=\"" + (String) attachment.elementAt(1) + "\""); response.setContentType((String)attachment.elementAt(0)); InputStream is = (InputStream)attachment.elementAt(2); OutputStream os = response.getOutputStream(); int tmp = 0; while ((tmp = is.read()) > -1) { os.write(tmp); } } else { %><%=attachment%><% } } else { logger.warn("sales/interaction/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) 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(); if (p.isMimeType("multipart/*") && "".equals(levelStub)) { level++; dumpPart(session, messageOutline, levelStub + level + ".", 0, displayType, p); return level; } /* * Using isMimeType to determine the content type avoids * fetching the actual content data until we need it. */ if (p.isMimeType("text/plain")) { //logger.debug("text/plain"); 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); } //logger.debug("Putting plain text body " + levelStub.substring(2) + level + " in hashtable"); //store message body in the hashtable messageOutline.put(levelStub.substring(2) + level + "BODY", "
" + (String)p.getContent() + "
"); 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); } //call dump part on the text/html part return dumpPart(session, messageOutline, levelStub, level, displayType, mp.getBodyPart(1)); } else { //if this part is "true" multipart //logger.debug("multipart/*"); /**/ //level ++; //get the envelope if (envelope != null) { while (messageOutline.containsKey(levelStub.substring(2) + (level+1))) { level++; } //logger.debug("3. Putting envelope " + levelStub + level + ".1 in hashtable : " + envelope); //logger.debug("3. Putting envelope " + levelStub.substring(2) + (level+1) + " in hashtable : " + envelope); Vector node = new Vector(2); node.add(0, "MESSAGE"); node.add(1, envelope); //messageOutline.put(levelStub + level + ".1", node); messageOutline.put(levelStub.substring(2) + (level+1), node); } //loop on its parts, same level in the tree for (int i = 0; i < count; i++) { //logger.debug("\nlooping on part index : " + i + " with level " + level); //dumpPart(session, messageOutline, levelStub, level, displayType, mp.getBodyPart(i)); dumpPart(session, messageOutline, levelStub, level, displayType, mp.getBodyPart(i)); //int levelInserted = dumpPart(session, messageOutline, levelStub, level, displayType, mp.getBodyPart(i)); //if (levelInserted != -1) {level = levelInserted;} } } } else if (p.isMimeType("message/rfc822")) { //logger.debug("message/rfc822"); //run dumpPart on the content, going down one level in the tree if we're not at the beginning //if (level != 0) { level++; dumpPart(session, messageOutline, levelStub + level + ".", 0, displayType, (javax.mail.Part)p.getContent()); return level; //} else { // return dumpPart(session, messageOutline, levelStub, level, displayType, (Part)p.getContent()); //} } 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(); /* Enumeration enum = p.getAllHeaders(); while (enum.hasMoreElements()) { Header header = (Header)enum.nextElement(); //logger.debug("Header : " + header.getName() + " / " + header.getValue()); } */ 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); return level; } else { level++; while (messageOutline.containsKey(levelStub.substring(2) + level)) { level++; } //logger.debug("Putting attachment (InputStream) " + levelStub.substring(2) + level + " in hashtable : " + filename); Vector node = new Vector(3); node.add(0, ct); node.add(1, filename); if (o instanceof InputStream) { node.add(2, o); } else { node.add(2, p.getInputStream()); } messageOutline.put(levelStub.substring(2) + level, node); return level; } } /* * If we're saving attachments, write out anything that * looks like an attachment into an appropriately named * file. Don't overwrite existing files to prevent * mistakes. */ /*if (saveAttachments && level != 0 && !p.isMimeType("multipart/*")) { String disp = p.getDisposition(); // many mailers don't include a Content-Disposition if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) { if (filename == null) filename = "Attachment" + attnum++; buf.append("Saving attachment to file " + filename); buf.append("
"); try { File f = new File(filename); if (f.exists()) // XXX - could try a series of names throw new IOException("file exists"); OutputStream os = new BufferedOutputStream(new FileOutputStream(f)); InputStream is = p.getInputStream(); int c; while ((c = is.read()) != -1) os.write(c); os.close(); } catch (IOException ex) { buf.append("Failed to save attachment: " + ex); buf.append("
"); } buf.append("---------------------------"); buf.append("
"); } }*/ //buf.append("
" + indentStr.substring(0, level * 15) + "END PART
"); //return buf; return -1; } //end of dumpMessage public StringBuffer dumpEnvelope(HttpSession session, Message m) throws Exception { //logger.debug("dumpEnvelope"); StringBuffer buf = new StringBuffer(); //buf.append(""); buf.append(""); Address[] a; // FROM buf.append(""); if ((a = m.getFrom()) != null) { buf.append(""); } buf.append(""); // TO if ((a = m.getRecipients(Message.RecipientType.TO)) != null) { buf.append(""); buf.append(""); } // DATE Date d = m.getSentDate(); buf.append(""); // SUBJECT buf.append(""); buf.append(""); //buf.append("
" + Utils.getMessage(session, "L6367", "De :") + ""); 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("
" + Utils.getMessage(session, "L6368", "A :") + ""); 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("
" + Utils.getMessage(session, "F2019", "Date") + "" + (d != null ? Utils.formatToWeb(d, false) : Utils.getMessage(session, "L6369", "Inconnue")) + "
" + Utils.getMessage(session, "F203B", "Sujet") + "" + m.getSubject() + "
 "); //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 * * *****************************************************************************/ %>