webtier@glassfish.java.net

Re: JavaMail 1.4.4/demo/msgshow.java

From: <forums_at_java.net>
Date: Sat, 17 Sep 2011 02:26:08 -0500 (CDT)

Hi,

I have resolved all my problems. This is the method as it leaves,

private void dumpPart(
            Part p,
            int attnum,
            HttpServletRequest request,
            HttpServletResponse response)
             throws Exception {
       
        try {
           
             HTMLMessage htmlMessage = new HTMLMessage();
            List<DataFile> dfList = new ArrayList<DataFile>();
           
            if (p instanceof Message)
                htmlMessage = dumpEnvelope((Message)p);
   
            /** Dump input stream ..
   
            InputStream is = p.getInputStream();
            // If "is" is not already buffered, wrap a
BufferedInputStream
            // around it.
            if (!(is instanceof BufferedInputStream))
                is = new BufferedInputStream(is);
            int c;
            while ((c = is.read()) != -1)
                System.out.write(c);
   
            **/
   
            /*
             * Using isMimeType to determine the content type
avoids
             * fetching the actual content data until we need it.
             */
            if (p.isMimeType("text/plain")) {
                String content = (String)p.getContent();
                htmlMessage.setMessage(content.getBytes());
            } else if (p.isMimeType("multipart/*")) {
                Multipart mp = (Multipart)p.getContent();
                int count = mp.getCount();
                for (int i = 0; i < count; i++){
                    dumpPart(mp.getBodyPart(i), ++attnum,
request, response);
                }
            } else if (p.isMimeType("message/rfc822")) {
                dumpPart((Part)p.getContent(), ++attnum, request,
response);
            } 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) {
                   
htmlMessage.setMessage(((String)o).getBytes());
                } else if (o instanceof InputStream) {
                    InputStream is = (InputStream)o;
                    int c;
                    StringBuffer sb = new StringBuffer();
                    while ((c = is.read()) != -1)
                        sb.append(c);
                    byte[] content =
(sb.toString()).getBytes();
                    htmlMessage.setMessage(content);
                } else {
                    ByteArrayOutputStream byteOut = new
ByteArrayOutputStream();
                    ObjectOutputStream objOut = new
ObjectOutputStream(byteOut);
                    objOut.writeObject(o);
                    byte[] b = byteOut.toByteArray();
                    htmlMessage.setMessage(b);
                }
            }
           
            /*
             * 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.
             */
           
            String strCt = p.getContentType();
            ContentType ct = new ContentType(strCt);
            String bt = ct.getBaseType();
           
            String filename = p.getFileName();
           
            if (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;
                   
                    DataFile df = new DataFile();
               
                    File f = new File(filename);
                    if (f.exists())
                    // XXX - could try a series of names
                        throw new IOException("file
exists");
                   
                    df.setContentType(bt);
                    df.setFileName(filename);
                    df.setFile(f);
                    //((MimeBodyPart)p).saveFile(f);
                   
                    dfList.add(df);
                }
            }
           
            HashMap<HTMLMessage, List<DataFile>> hashMap = new
HashMap<HTMLMessage, List<DataFile>>();
            hashMap.put(htmlMessage, dfList);
           
            save(hashMap);
       
        } catch (Exception e) {
            request.setAttribute("exception", e);
           
request.getRequestDispatcher("/error.jsp").forward(request, response);
        }
       
}
public void save(HashMap<HTMLMessage, List<DataFile>> hashMap){
        messagesAttachedFileManager.placeMessagesAttachedFile(hashMap);
}
 

being 'messagesAttachedFileManager' an ejb facade.

Everything works fine. Now I have another problem, I do not know the way to
get the extension of the files,

            String filename = p.getFileName();

            if (disp == null ||
disp.equalsIgnoreCase(Part.ATTACHMENT)) {
                if (filename == null)
                    filename = "Attachment" + attnum;

Well, I put a name to the files but without extension.

How could I know witch are those extensions?

Kind regards,
Jose


--
[Message sent by forum member 'josealvarezdelara']
View Post: http://forums.java.net/node/843703