|
From: Umberto C. <umb...@us...> - 2004-04-14 14:38:09
|
Update of /cvsroot/dcm4che/dcm4jboss-mail/src/java/org/dcm4chex/dcmmail In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15995/src/java/org/dcm4chex/dcmmail Modified Files: FetchMailService.java Log Message: Index: FetchMailService.java =================================================================== RCS file: /cvsroot/dcm4che/dcm4jboss-mail/src/java/org/dcm4chex/dcmmail/FetchMailService.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** FetchMailService.java 13 Apr 2004 08:22:07 -0000 1.1 --- FetchMailService.java 14 Apr 2004 14:37:57 -0000 1.2 *************** *** 20,87 **** package org.dcm4chex.dcmmail; import java.io.File; import javax.mail.Message; import org.jboss.system.ServiceMBeanSupport; /** * @author gun...@ti... * @version $Revision$ $Date$ * @since 10.04.2004 - * */ ! public class FetchMailService extends ServiceMBeanSupport { ! private String host = "imap"; ! private String user = "dicommail"; ! private String password = "dicommail"; ! public final String getHost() { ! return host; ! } ! public final void setHost(String host) { ! this.host = host; ! } ! public final String getUser() { ! return user; ! } ! public final void setUser(String user) { ! this.user = user; ! } ! public final void setPassword(String password) { ! this.password = password; ! } ! protected void startService() throws Exception { ! } ! protected void stopService() throws Exception { ! } ! public void getMail() { ! Message[] mails = getMessages(); ! for (int i = 0; i < mails.length; i++) { ! try { ! File dcmdir = SpoolDirectory.createTempDir(SpoolDirectory.INCOMING); ! // TODO store DICOM files attached to mails[i] into dcmdir ! DcmMail dcmmail = new DcmMail(); ! dcmmail.setDicomdir(dcmdir); ! JMSDelegate.getInstance().queueIncoming(dcmmail); ! } catch (Exception e) { ! log.error("Failed to process " + mails[i], e); ! } ! } } ! private Message[] getMessages() { ! // TODO Auto-generated method stub ! return new Message[0]; ! } } --- 20,307 ---- package org.dcm4chex.dcmmail; + import java.io.BufferedOutputStream; import java.io.File; + import java.io.FileOutputStream; + import java.io.IOException; + import java.io.InputStream; + import java.io.OutputStream; + import java.text.SimpleDateFormat; + import java.util.Properties; + import javax.mail.BodyPart; + import javax.mail.Flags; + import javax.mail.Folder; import javax.mail.Message; + import javax.mail.MessagingException; + import javax.mail.Multipart; + import javax.mail.NoSuchProviderException; + import javax.mail.Part; + import javax.mail.Session; + import javax.mail.Store; + import javax.mail.internet.MimeBodyPart; + import javax.mail.internet.MimeMessage; import org.jboss.system.ServiceMBeanSupport; + import org.xml.sax.helpers.AttributesImpl; /** + * A Service to fetch "dicom" mails from a properly configured IMAP folder. + * Each attached dicom file is stored in a temporary directory and a new JMS + * message is sent to the dicommail consumers. + * Non-dicom mail are ignored, albeit an Exception is produced. + * N.B.: Non-dicom mails are not removed from the IMAP folder. Is up to the + * mail administrator to remove it through a client. + * * @author gun...@ti... + * @author umb...@ti... * * @version $Revision$ $Date$ * @since 10.04.2004 */ ! public class FetchMailService extends ServiceMBeanSupport ! { ! private String host = "imap"; ! private String user = "dicommail"; ! private String password = "dicommail"; ! private Folder inbox = null; ! public final String getHost() ! { ! return host; ! } ! public final void setHost(String host) ! { ! this.host = host; ! } ! public final String getUser() ! { ! return user; ! } ! public final void setUser(String user) ! { ! this.user = user; ! } ! public final void setPassword(String password) ! { ! this.password = password; ! } ! protected void startService() throws Exception ! { ! } ! protected void stopService() throws Exception ! { } ! /** ! * public service. ! * Checks for mail in the configured imap "inbox" folder. ! * If messages containind Application/dicom attachments are present, the ! * correspondig dicom files are stored in a newly created temporary directory. ! * Furthermore, a DcmMail object containing all the necessary information ! * to retrieve the dicom images is created and sent to the JMS queue. ! * Non-dicom mails are ignored. ! * Once terminated the scan of every mail in the folder, the folder is closed. ! * ! * @throws MessagingException ! */ ! public void getMail() throws MessagingException ! { ! ! Message[] mails = getMessages(); ! ! for (int i = 0; i < mails.length; i++) ! { ! try ! { ! ! File dcmdir = null; ! DcmMail dcmmail = null; ! MimeMessage msg = null; ! ! msg = testMessage(mails[i]); ! ! Multipart content = (Multipart) msg.getContent(); ! ! log.info( ! "Storing to disk files of message: " ! + mails[i].getSubject()); ! dcmdir = SpoolDirectory.createTempDir(SpoolDirectory.INCOMING); ! log.info("dcmdir: " + dcmdir.getAbsolutePath()); ! ! dcmmail = new DcmMail(); ! dcmmail.setDicomdir(dcmdir); ! StringBuffer bodyText = new StringBuffer(); ! ! for (int j = 0; j < content.getCount(); j++) ! { ! BodyPart body = content.getBodyPart(j); ! if (body instanceof MimeBodyPart) ! { ! MimeBodyPart bodymime = (MimeBodyPart) body; ! if (bodymime.isMimeType("text/plain")) ! { ! log.info("collecting text/plain Body part"); ! bodyText.append(bodymime.getContent().toString()); ! bodyText.append("\n"); ! } else if (bodymime.isMimeType("Application/dicom")) ! { ! log.info("collecting Application/dicom Body part"); ! String disp = bodymime.getDisposition(); ! if (disp == null ! || disp.equalsIgnoreCase(Part.ATTACHMENT)) ! { ! String filename = bodymime.getFileName(); ! File dcmfile = new File(dcmdir, filename); ! log.info( ! "saving file: " ! + dcmfile.getAbsolutePath()); ! OutputStream os = ! new BufferedOutputStream( ! new FileOutputStream(dcmfile)); ! InputStream is = bodymime.getInputStream(); ! int c; ! while ((c = is.read()) != -1) ! os.write(c); ! os.close(); ! } ! } ! } else ! { ! log.warn( ! "Ignoring message part:" ! + body.getDescription() ! + "of mail " ! + mails[i]); ! } ! } //body parts cycle ! ! dcmmail.setBody(bodyText.toString()); ! ! //takes first FROM address ! dcmmail.setFromAddress(mails[i].getFrom()[0].toString()); ! ! //takes first TO address ! dcmmail.setToAddress( ! mails[i] ! .getRecipients(Message.RecipientType.TO)[0] ! .toString()); ! ! dcmmail.setSubject(mails[i].getSubject()); ! ! mails[i].setFlag(Flags.Flag.DELETED, true); ! ! JMSDelegate.getInstance().queueIncoming(dcmmail); ! ! } catch (Exception e) ! { ! log.error( ! "Failed to process mail message. -Subject:" ! + mails[i].getSubject() ! + " -SentDate:" ! + new SimpleDateFormat("dd/MM/yyyy:HH:mm").format( ! mails[i].getSentDate()), ! e); ! } ! } // message cycle ! ! inbox.close(true); //should delete each file marked as "DELETED" ! log.info("inbox folder closed."); ! } ! ! /** ! * @return Message[] the list of messages contained in the inbox folder. ! * @throws MessagingException in case of probems during imap connection and ! * message retrival ! */ ! private Message[] getMessages() throws MessagingException ! { ! try ! { ! Session sess = Session.getDefaultInstance(new Properties(), null); ! Store st = sess.getStore("imap"); ! ! AttributesImpl attr = new AttributesImpl(); ! ! log.info("Connecting to IMAP server @ " + this.host); ! st.connect(this.host, this.user, this.password); ! ! log.info("Attempting to open default folder (inbox)"); ! inbox = st.getFolder("inbox"); ! ! inbox.open(Folder.READ_WRITE); ! ! log.info("Downloading message list from folder"); ! Message[] messages = inbox.getMessages(); ! ! return messages; ! ! } catch (NoSuchProviderException e) ! { ! throw new MessagingException("No Such Provider", e); ! } catch (MessagingException e) ! { ! throw e; ! } ! } ! ! /** ! * Controls if a Message is a MimeMessage and if contains Multipart body ! * parts. ! * ! * @param Message msg the message to be controlled. ! * @return MimeMessage the casted-to MimeMessage message. ! * @throws MessagingException if Message isn't a MimeMessage or if doesn't ! * contain Multipart body parts. ! */ ! private MimeMessage testMessage(Message msg) throws MessagingException ! { ! if (!(msg instanceof MimeMessage)) ! { ! throw new MessagingException( ! new StringBuffer() ! .append("message nr.:") ! .append(msg.getMessageNumber()) ! .append(" with subject ") ! .append(msg.getSubject()) ! .append(" is not a MIME message. Ignored.") ! .toString()); ! } ! ! MimeMessage returnmsg = (MimeMessage) msg; ! Object contentObject; ! ! try ! { ! contentObject = returnmsg.getContent(); ! } catch (IOException e) ! { ! throw new MessagingException("IOError during content retrieve", e); ! } catch (MessagingException e) ! { ! throw e; ! } ! ! if (!(contentObject instanceof Multipart)) ! { ! throw new MessagingException( ! new StringBuffer() ! .append("message nr.:") ! .append(msg.getMessageNumber()) ! .append(" with subject ") ! .append(msg.getSubject()) ! .append(" contains an NON Multipart content. Ignored.") ! .toString()); ! } ! ! return returnmsg; ! } } |