1 22 23 package org.objectweb.petals.binding.mail.listeners; 24 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.util.Date ; 28 import java.util.HashSet ; 29 import java.util.Set ; 30 import java.util.logging.Logger ; 31 32 import javax.activation.DataHandler ; 33 import javax.jbi.JBIException; 34 import javax.jbi.component.ComponentContext; 35 import javax.jbi.messaging.DeliveryChannel; 36 import javax.jbi.messaging.InOnly; 37 import javax.jbi.messaging.MessageExchange; 38 import javax.jbi.messaging.NormalizedMessage; 39 import javax.mail.Message ; 40 import javax.mail.MessagingException ; 41 import javax.mail.Multipart ; 42 import javax.mail.Part ; 43 import javax.mail.Session ; 44 import javax.mail.internet.InternetAddress ; 45 import javax.mail.internet.MimeBodyPart ; 46 import javax.mail.internet.MimeMessage ; 47 import javax.mail.internet.MimeMultipart ; 48 import javax.xml.namespace.QName ; 49 import javax.xml.transform.Source ; 50 51 import org.objectweb.petals.component.common.PEtALSComponentSDKException; 52 import org.objectweb.petals.component.common.bc.AbstractBindingComponent; 53 import org.objectweb.petals.component.common.util.SourceHelper; 54 import org.objectweb.petals.tools.jbicommon.util.StringHelper; 55 56 62 public class MimeMessageManager { 63 64 protected AbstractBindingComponent component; 65 66 protected Logger log; 67 68 protected String MEP = "mep"; 69 70 protected String IN_ONLY_MEP = "in-only"; 71 72 protected String ROBUST_IN_ONLY_MEP = "robust-in-only"; 73 74 protected String IN_OUT_MEP = "in-out"; 75 76 protected String IN_OPTIONAL_OUT_MEP = "in-optional-out"; 77 78 protected String OPERATION = "operation"; 79 80 91 public MimeMessageManager(Logger log, AbstractBindingComponent component) { 92 super(); 93 this.log = log; 94 this.component = component; 95 } 96 97 111 public MimeMessage mapNormalizedMessageToMimeMessage(Session session, 112 InternetAddress internetAddress, NormalizedMessage in) 113 throws JBIException { 114 115 MimeMessage mimeMessage = new MimeMessage (session); 116 117 setMimeMessageAttributes(mimeMessage, internetAddress); 119 120 Multipart multipart = new MimeMultipart (); 121 122 setMimeMessageBody(multipart, in); 124 125 setMimeMessageAttachments(multipart, in); 127 128 try { 129 mimeMessage.setContent(multipart); 130 } catch (javax.mail.MessagingException e) { 131 String msg = "Error setting Mime message content " 132 + "with all information collected from NormalizedMessage"; 133 log.severe(msg); 134 throw new JBIException(msg, e); 135 } 136 137 return mimeMessage; 138 } 139 140 151 @SuppressWarnings ("unchecked") 152 protected void setMimeMessageAttachments(Multipart multipart, 153 NormalizedMessage in) { 154 Set <String > attachmentIds = in.getAttachmentNames(); 155 Set <DataHandler > attachments = new HashSet <DataHandler >(); 156 if (attachmentIds != null) { 157 for (String id : attachmentIds) { 158 attachments.add(in.getAttachment(id)); 159 } 160 } 161 if (attachments != null && !attachments.isEmpty()) { 162 MimeBodyPart attachementBodyPart = null; 163 for (DataHandler handler : attachments) { 164 attachementBodyPart = new MimeBodyPart (); 165 try { 166 attachementBodyPart.setDataHandler(handler); 167 attachementBodyPart.setFileName(handler.getName()); 168 multipart.addBodyPart(attachementBodyPart); 169 } catch (javax.mail.MessagingException e) { 170 String msg = "Error setting MimeMessage attachments"; 171 log.severe(msg); 172 173 } 174 } 175 } 176 } 177 178 188 protected void setMimeMessageAttributes(MimeMessage mimeMessage, 189 InternetAddress internetAddress) throws JBIException { 190 try { 191 mimeMessage.setRecipient(Message.RecipientType.TO, internetAddress); 192 193 mimeMessage.setSubject(component.getContext().getComponentName()); 194 195 mimeMessage.setSentDate(new Date ()); 196 } catch (javax.mail.MessagingException e) { 197 String msg = "Error setting MimeMessage attributes"; 198 log.severe(msg); 199 throw new JBIException(msg, e); 200 } 201 } 202 203 215 protected void setMimeMessageBody(Multipart multipart, NormalizedMessage in) 216 throws JBIException { 217 MimeBodyPart messageBodyPart = new MimeBodyPart (); 218 String content = null; 219 try { 220 if (in.getContent() != null) { 221 content = SourceHelper.createString(in.getContent()); 222 } 223 } catch (Exception e) { 224 throw new JBIException(e); 225 } 226 227 try { 228 messageBodyPart.setContent(content, "text/plain"); 229 multipart.addBodyPart(messageBodyPart); 230 } catch (javax.mail.MessagingException e) { 231 throw new JBIException(e); 232 } 233 } 234 235 254 public void process(Message message, String address) throws JBIException { 255 String subject; 258 try { 259 subject = message.getSubject(); 260 } catch (MessagingException e1) { 261 String msg = "Error retrieving subject of a mail"; 262 throw new JBIException(msg, e1); 263 } 264 String operation = StringHelper.extractValueForAttribute(subject, 265 OPERATION, ";"); 266 String mep = StringHelper.extractValueForAttribute(subject, MEP, ";"); 267 268 if (!IN_ONLY_MEP.equalsIgnoreCase(mep)) { 271 if (!StringHelper.isNullOrEmpty(mep)) { 272 String msg = "Message Exchange Pattern (MEP) not supported" 273 + mep; 274 throw new JBIException(msg); 275 } else { 276 mep = IN_ONLY_MEP; 277 } 278 } 279 280 StringBuffer jbiContent = new StringBuffer (); 283 Set <DataHandler > attachments = new HashSet <DataHandler >(); 284 try { 285 createJBIContentAndAttachments(message, jbiContent, attachments); 286 } catch (Exception e) { 287 String msg = "Error during mail message processing, " 290 + "cannot create jbi content and attachment"; 291 throw new JBIException(msg, e); 292 } 293 294 MessageExchange exchange = null; 296 try { 297 exchange = createJBIMessageExchange(operation, mep, jbiContent 298 .toString(), attachments); 299 } catch (javax.jbi.messaging.MessagingException e) { 300 String msg = "Error during creation of the JBI " 301 + "message exchange from mail information"; 302 throw new JBIException(msg, e); 303 } 304 305 try { 307 component.sendMessage(address, exchange); 308 } catch (javax.jbi.messaging.MessagingException e) { 309 throw new JBIException(e); 310 } 311 312 } 313 314 325 protected MessageExchange createJBIMessageExchange(String operation, 326 String mep, String jbiContent, Set <DataHandler > attachments) 327 throws javax.jbi.messaging.MessagingException { 328 MessageExchange message = null; 329 if (IN_ONLY_MEP.equalsIgnoreCase(mep)) { 330 message = createInOnlyMessageExchange(operation, jbiContent, 331 attachments); 332 } 333 return message; 334 } 335 336 351 protected InOnly createInOnlyMessageExchange(String operation, 352 String jbiContent, Set <DataHandler > attachments) 353 throws javax.jbi.messaging.MessagingException { 354 355 InOnly msg = component.getChannel().createExchangeFactory().createInOnlyExchange(); 357 358 NormalizedMessage nm = msg.createMessage(); 360 361 Source source; 363 try { 364 source = SourceHelper.createSource(jbiContent); 365 } catch (PEtALSComponentSDKException e) { 366 throw new javax.jbi.messaging.MessagingException( 367 "Can't create source from String content", e); 368 } 369 nm.setContent(source); 370 371 if (!attachments.isEmpty()) { 373 for (DataHandler handler : attachments) { 374 nm.addAttachment(handler.getName(), handler); 375 } 376 } 377 378 msg.setMessage(nm, "IN"); 380 381 if (!StringHelper.isNullOrEmpty(operation)) { 383 msg.setOperation(new QName (operation)); 384 } 385 386 return msg; 387 } 388 389 405 protected void createJBIContentAndAttachments(Part message, 406 StringBuffer jbiContent, Set <DataHandler > attachments) 407 throws IOException , MessagingException { 408 Object content = message.getContent(); 409 if (content instanceof String ) { 410 jbiContent.append(content); 411 } else if (content instanceof Multipart ) { 412 Multipart mp = (Multipart ) content; 413 int count = mp.getCount(); 414 for (int i = 0; i < count; i++) { 415 createJBIContentAndAttachments(mp.getBodyPart(i), jbiContent, 416 attachments); 417 } 418 } else if (content instanceof InputStream ) { 419 attachments.add(message.getDataHandler()); 420 } 421 } 422 } 423 | Popular Tags |