// Note: Content Type - might need treatment! public class AttachmentAttacher{ public void process(Exchange exchange){ Message in = exchange.getIn(); byte[] file = in.getBody(byte[].class); String fileId = in.getHeader("CamelFileName",String.class); in.addAttachment(fileId, new DataHandler(file,"plain/text")); } }
@Override public void process(Exchange exchange) throws Exception { String newFileName = "internal-resized-switchyard.jpeg"; Image input = exchange.getIn().getBody(Image.class); exchange.getOut().addAttachment(newFileName, new DataHandler(input, "image/jpeg")); exchange.getOut().setBody(newFileName); }
@Override public void process(Exchange exchange) throws Exception { String newFileName = "resized-switchyard.jpeg"; Image input = exchange.getIn().getBody(Image.class); if (input == null) { throw new RuntimeException("Image for resize not found!"); } Image img = ImageIO.read(Classes.getResourceAsStream("switchyard.jpeg")); exchange.getOut().addAttachment(newFileName, new DataHandler(img, "image/jpeg")); exchange.getOut().setBody(newFileName); }
@Override public void process(Exchange exchange) throws Exception { String newFileName = "internal-switchyard.jpeg"; Image input = exchange.getIn().getBody(Image.class); if (input == null) { throw new RuntimeException("Image for resize not found!"); } ContentType type = new ContentType("image/jpeg"); exchange.getOut().addAttachment(newFileName, new DataHandler(input, type.getBaseType())); exchange.getOut().setBody(newFileName); }
import javax.activation.DataHandler; import javax.mail.util.ByteArrayDataSource; import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.Processor; public class AttachmentAttacher implements Processor { private final String mimetype; public AttachmentAttacher(String mimetype) { this.mimetype = mimetype; } @Override public void process(Exchange exchange){ Message in = exchange.getIn(); byte[] file = in.getBody(byte[].class); String fileId = in.getHeader("CamelFileName",String.class); in.addAttachment(fileId, new DataHandler(new ByteArrayDataSource(file, mimetype))); } }
/** * Copies content, headers, security subject and attachments from the JBI NormalizedMessage to the Camel Message. * * @param from the source {@link NormalizedMessage} * @param to the target {@link Message} */ public void copyFromJbiToCamel(NormalizedMessage from, Message to) { to.setBody(from.getContent()); Subject securitySubject = from.getSecuritySubject(); if (securitySubject != null) { to.setHeader(SECURITY_SUBJECT, securitySubject); } Exchange exchange = to.getExchange(); for (Object object : from.getPropertyNames()) { String key = object.toString(); Object value = from.getProperty(key); if (!strategies.applyFilterToCamelHeaders(key, value, exchange)) { to.setHeader(key, value); } } for (Object id : from.getAttachmentNames()) { to.addAttachment(id.toString(), from.getAttachment(id.toString())); } }
/** * {@inheritDoc} */ @Override public CamelBindingData decompose(Exchange exchange, CamelBindingData target) throws Exception { Message sourceMessage = exchange.getMessage(); getContextMapper().mapTo(exchange.getContext(), target); org.apache.camel.Message targetMessage = target.getMessage(); if (!sourceMessage.getAttachmentMap().isEmpty()) { for (Entry<String, DataSource> entry : sourceMessage.getAttachmentMap().entrySet()) { targetMessage.addAttachment(entry.getKey(), new DataHandler(entry.getValue())); } } ServiceOperation operation = exchange.getContract().getProviderOperation(); target.getMessage().getExchange().setProperty(OPERATION_NAME, operation.getName()); target.getMessage().getExchange().setProperty(FAULT_TYPE, operation.getFaultType()); target.getMessage().getExchange().setProperty(SERVICE_NAME, exchange.getProvider().getName()); targetMessage.setBody(sourceMessage.getContent()); return target; } }
/** * {@inheritDoc} */ @Override public CamelBindingData decompose(Exchange exchange, CamelBindingData target) throws Exception { Message sourceMessage = exchange.getMessage(); getContextMapper().mapTo(exchange.getContext(), target); org.apache.camel.Message targetMessage = target.getMessage(); if (!sourceMessage.getAttachmentMap().isEmpty()) { for (Entry<String, DataSource> entry : sourceMessage.getAttachmentMap().entrySet()) { targetMessage.addAttachment(entry.getKey(), new DataHandler(entry.getValue())); } } ServiceOperation operation = exchange.getContract().getProviderOperation(); target.getMessage().getExchange().setProperty(OPERATION_NAME, operation.getName()); target.getMessage().getExchange().setProperty(FAULT_TYPE, operation.getFaultType()); target.getMessage().getExchange().setProperty(SERVICE_NAME, exchange.getProvider().getName()); targetMessage.setBody(sourceMessage.getContent()); return target; } }