public void appendJmsProperty( Message jmsMessage, Exchange exchange, org.apache.camel.Message in, String headerName, Object headerValue) throws JMSException { if (isStandardJMSHeader(headerName)) { if (headerName.equals("JMSCorrelationID")) { jmsMessage.setJMSCorrelationID( ExchangeHelper.convertToType(exchange, String.class, headerValue)); } else if (headerName.equals("JMSReplyTo") && headerValue != null) { if (headerValue instanceof String) { // if the value is a String we must normalize it first, and must include the prefix // as ActiveMQ requires that when converting the String to a javax.jms.Destination type headerValue = normalizeDestinationName((String) headerValue, true); } Destination replyTo = ExchangeHelper.convertToType(exchange, Destination.class, headerValue); JmsMessageHelper.setJMSReplyTo(jmsMessage, replyTo); } else if (headerName.equals("JMSType")) { jmsMessage.setJMSType(ExchangeHelper.convertToType(exchange, String.class, headerValue)); } else if (headerName.equals("JMSPriority")) { jmsMessage.setJMSPriority( ExchangeHelper.convertToType(exchange, Integer.class, headerValue)); } else if (headerName.equals("JMSDeliveryMode")) { JmsMessageHelper.setJMSDeliveryMode(exchange, jmsMessage, headerValue); } else if (headerName.equals("JMSExpiration")) { jmsMessage.setJMSExpiration( ExchangeHelper.convertToType(exchange, Long.class, headerValue)); } else { // The following properties are set by the MessageProducer: // JMSDestination // The following are set on the underlying JMS provider: // JMSMessageID, JMSTimestamp, JMSRedelivered // log at trace level to not spam log LOG.trace("Ignoring JMS header: {} with value: {}", headerName, headerValue); } } else if (shouldOutputHeader(in, headerName, headerValue, exchange)) { // only primitive headers and strings is allowed as properties // see message properties: http://java.sun.com/j2ee/1.4/docs/api/javax/jms/Message.html Object value = getValidJMSHeaderValue(headerName, headerValue); if (value != null) { // must encode to safe JMS header name before setting property on jmsMessage String key = jmsKeyFormatStrategy.encodeKey(headerName); // set the property JmsMessageHelper.setProperty(jmsMessage, key, value); } else if (LOG.isDebugEnabled()) { // okay the value is not a primitive or string so we cannot sent it over the wire LOG.debug( "Ignoring non primitive header: {} of class: {} with value: {}", new Object[] {headerName, headerValue.getClass().getName(), headerValue}); } } }
@SuppressWarnings("unchecked") public <T> T getProperty(String name, Object defaultValue, Class<T> type) { Object value = getProperty(name, defaultValue); if (value == null) { // lets avoid NullPointerException when converting to boolean for null values if (boolean.class.isAssignableFrom(type)) { return (T) Boolean.FALSE; } return null; } // eager same instance type test to avoid the overhead of invoking the type converter // if already same type if (type.isInstance(value)) { return type.cast(value); } return ExchangeHelper.convertToType(this, type, value); }
@Override public void marshal(Exchange exchange, Object graph, OutputStream stream) throws NoTypeConversionAvailableException, MessagingException, IOException { if (multipartWithoutAttachment || headersInline || exchange.getIn().hasAttachments()) { ContentType contentType = getContentType(exchange); // remove the Content-Type header. This will be wrong afterwards... exchange.getOut().removeHeader(Exchange.CONTENT_TYPE); byte[] bodyContent = ExchangeHelper.convertToMandatoryType(exchange, byte[].class, graph); Session session = Session.getInstance(System.getProperties()); MimeMessage mm = new MimeMessage(session); MimeMultipart mp = new MimeMultipart(multipartSubType); BodyPart part = new MimeBodyPart(); writeBodyPart(bodyContent, part, contentType); mp.addBodyPart(part); for (Map.Entry<String, Attachment> entry : exchange.getIn().getAttachmentObjects().entrySet()) { String attachmentFilename = entry.getKey(); Attachment attachment = entry.getValue(); part = new MimeBodyPart(); part.setDataHandler(attachment.getDataHandler()); part.setFileName(MimeUtility.encodeText(attachmentFilename, "UTF-8", null)); String ct = attachment.getDataHandler().getContentType(); contentType = new ContentType(ct); part.setHeader(CONTENT_TYPE, ct); if (!contentType.match("text/*") && binaryContent) { part.setHeader(CONTENT_TRANSFER_ENCODING, "binary"); } // Set headers to the attachment for (String headerName : attachment.getHeaderNames()) { List<String> values = attachment.getHeaderAsList(headerName); for (String value : values) { part.setHeader(headerName, value); } } mp.addBodyPart(part); exchange.getOut().removeAttachment(attachmentFilename); } mm.setContent(mp); // copy headers if required and if the content can be converted into // a String if (headersInline && includeHeaders != null) { for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) { if (includeHeaders.matcher(entry.getKey()).matches()) { String headerStr = ExchangeHelper.convertToType(exchange, String.class, entry.getValue()); if (headerStr != null) { mm.setHeader(entry.getKey(), headerStr); } } } } mm.saveChanges(); Enumeration<?> hl = mm.getAllHeaders(); List<String> headers = new ArrayList<String>(); if (!headersInline) { while (hl.hasMoreElements()) { Object ho = hl.nextElement(); if (ho instanceof Header) { Header h = (Header) ho; exchange.getOut().setHeader(h.getName(), h.getValue()); headers.add(h.getName()); } } mm.saveChanges(); } mm.writeTo(stream, headers.toArray(new String[0])); } else { // keep the original data InputStream is = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph); IOHelper.copyAndCloseInput(is, stream); } }