public static Map cleanAndAdd(UMOEventContext muleEventContext) { Map props = new HashMap(); UMOMessage currentMessage = muleEventContext.getMessage(); final String SOAP_METHODS = "soapMethods"; for (Iterator iterator = currentMessage.getPropertyNames().iterator(); iterator.hasNext(); ) { String name = (String) iterator.next(); if (!StringUtils.equals(name, SOAP_METHODS) && !StringUtils.equals(name, SoapConstants.SOAP_ACTION_PROPERTY) && !StringUtils.equals(name, MuleProperties.MULE_METHOD_PROPERTY) && (!name.startsWith(MuleProperties.PROPERTY_PREFIX) || StringUtils.equals(name, MuleProperties.MULE_USER_PROPERTY)) && !HttpConstants.ALL_HEADER_NAMES.containsValue(name) && !StringUtils.equals(name, HttpConnector.HTTP_STATUS_PROPERTY)) { props.put(name, currentMessage.getProperty(name)); } } return props; }
private void sendViaClient(final MessageContext context, final OutMessage message) throws Exception { OutputHandler handler = new OutputHandler() { public void write(UMOEvent event, OutputStream out) throws IOException { try { Attachments atts = message.getAttachments(); if (atts != null && atts.size() > 0) { atts.write(out); } else { XMLStreamWriter writer = STAXUtils.createXMLStreamWriter(out, message.getEncoding(), context); message.getSerializer().writeMessage(message, writer, context); try { writer.flush(); } catch (XMLStreamException e) { logger.error(e); throw new XFireException("Couldn't send message.", e); } } } catch (XFireException e) { logger.error("Couldn't send message.", e); throw new IOException(e.getMessage()); } } public Map getHeaders(UMOEvent event) { Map headers = new HashMap(); headers.put(HttpConstants.HEADER_CONTENT_TYPE, getSoapMimeType(message)); headers.put(SoapConstants.SOAP_ACTION, message.getProperty(SoapConstants.SOAP_ACTION)); UMOMessage msg = event.getMessage(); for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext(); ) { String headerName = (String) iterator.next(); Object headerValue = msg.getStringProperty(headerName, null); // let us filter only MULE properties except MULE_USER, // Content-Type and Content-Lenght; all other properties are // allowed through including custom headers if ((!headerName.startsWith(MuleProperties.PROPERTY_PREFIX) || (MuleProperties.MULE_USER_PROPERTY.compareTo(headerName) == 0)) && (!HttpConstants.HEADER_CONTENT_TYPE.equalsIgnoreCase(headerName)) && (!HttpConstants.HEADER_CONTENT_LENGTH.equalsIgnoreCase(headerName))) { headers.put(headerName, headerValue); } } return headers; } }; // We can create a generic StreamMessageAdapter here as the underlying // transport will create one specific to the transport UMOStreamMessageAdapter sp = new StreamMessageAdapter(handler); sp.setProperty(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_POST); // set all properties on the message adapter UMOMessage msg = RequestContext.getEvent().getMessage(); for (Iterator i = msg.getPropertyNames().iterator(); i.hasNext(); ) { String propertyName = (String) i.next(); sp.setProperty(propertyName, msg.getProperty(propertyName)); } UMOStreamMessageAdapter result = null; try { result = sendStream(getUri(), sp); if (result != null) { InMessage inMessage; String contentType = sp.getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/xml"); InputStream in = result.getInputStream(); if (contentType.toLowerCase().indexOf("multipart/related") != -1) { try { Attachments atts = new JavaMailAttachments(in, contentType); InputStream msgIs = atts.getSoapMessage().getDataHandler().getInputStream(); inMessage = new InMessage( STAXUtils.createXMLStreamReader(msgIs, message.getEncoding(), context), getUri()); inMessage.setAttachments(atts); } catch (MessagingException e) { throw new IOException(e.getMessage()); } } else { inMessage = new InMessage( STAXUtils.createXMLStreamReader(in, message.getEncoding(), context), getUri()); } getEndpoint().onReceive(context, inMessage); } } finally { sp.release(); if (result != null) { result.release(); } } }
/* * (non-Javadoc) * * @see org.mule.transformers.AbstractEventAwareTransformer#transform(java.lang.Object, * java.lang.String, org.mule.umo.UMOEventContext) */ public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException { String endpointAddress = endpoint.getEndpointURI().getAddress(); SmtpConnector connector = (SmtpConnector) endpoint.getConnector(); UMOMessage eventMsg = context.getMessage(); String to = eventMsg.getStringProperty(MailProperties.TO_ADDRESSES_PROPERTY, endpointAddress); String cc = eventMsg.getStringProperty( MailProperties.CC_ADDRESSES_PROPERTY, connector.getCcAddresses()); String bcc = eventMsg.getStringProperty( MailProperties.BCC_ADDRESSES_PROPERTY, connector.getBccAddresses()); String from = eventMsg.getStringProperty( MailProperties.FROM_ADDRESS_PROPERTY, connector.getFromAddress()); String replyTo = eventMsg.getStringProperty( MailProperties.REPLY_TO_ADDRESSES_PROPERTY, connector.getReplyToAddresses()); String subject = eventMsg.getStringProperty(MailProperties.SUBJECT_PROPERTY, connector.getSubject()); String contentType = eventMsg.getStringProperty( MailProperties.CONTENT_TYPE_PROPERTY, connector.getContentType()); Properties headers = new Properties(); Properties customHeaders = connector.getCustomHeaders(); if (customHeaders != null && !customHeaders.isEmpty()) { headers.putAll(customHeaders); } Properties otherHeaders = (Properties) eventMsg.getProperty(MailProperties.CUSTOM_HEADERS_MAP_PROPERTY); if (otherHeaders != null && !otherHeaders.isEmpty()) { Map props = new HashMap(MuleManager.getInstance().getProperties()); for (Iterator iterator = eventMsg.getPropertyNames().iterator(); iterator.hasNext(); ) { String propertyKey = (String) iterator.next(); props.put(propertyKey, eventMsg.getProperty(propertyKey)); } headers.putAll(templateParser.parse(props, otherHeaders)); } if (logger.isDebugEnabled()) { StringBuffer buf = new StringBuffer(256); buf.append("Constructing email using:\n"); buf.append("To: ").append(to); buf.append("From: ").append(from); buf.append("CC: ").append(cc); buf.append("BCC: ").append(bcc); buf.append("Subject: ").append(subject); buf.append("ReplyTo: ").append(replyTo); buf.append("Content type: ").append(contentType); buf.append("Payload type: ").append(src.getClass().getName()); buf.append("Custom Headers: ").append(PropertiesUtils.propertiesToString(headers, false)); logger.debug(buf.toString()); } try { Message email = new MimeMessage( (Session) endpoint.getConnector().getDispatcher(endpoint).getDelegateSession()); // set mail details email.setRecipients(Message.RecipientType.TO, MailUtils.stringToInternetAddresses(to)); email.setSentDate(Calendar.getInstance().getTime()); if (StringUtils.isNotBlank(from)) { email.setFrom(MailUtils.stringToInternetAddresses(from)[0]); } if (StringUtils.isNotBlank(cc)) { email.setRecipients(Message.RecipientType.CC, MailUtils.stringToInternetAddresses(cc)); } if (StringUtils.isNotBlank(bcc)) { email.setRecipients(Message.RecipientType.BCC, MailUtils.stringToInternetAddresses(bcc)); } if (StringUtils.isNotBlank(replyTo)) { email.setReplyTo(MailUtils.stringToInternetAddresses(replyTo)); } email.setSubject(subject); for (Iterator iterator = headers.entrySet().iterator(); iterator.hasNext(); ) { Map.Entry entry = (Map.Entry) iterator.next(); email.setHeader(entry.getKey().toString(), entry.getValue().toString()); } // Create Multipart to put BodyParts in Multipart multipart = new MimeMultipart(); // Create Text Message BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("My Text"); multipart.addBodyPart(messageBodyPart); // Create Attachment messageBodyPart = new MimeBodyPart(); DataHandler dataHandler = (DataHandler) src; messageBodyPart.setDataHandler(dataHandler); messageBodyPart.setFileName(dataHandler.getName()); multipart.addBodyPart(messageBodyPart); email.setContent(multipart); return email; } catch (Exception e) { throw new TransformerException(this, e); } }