]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAttachmentUploader.java
Improve SystemCall
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.activemq / src / main / java / org / argeo / slc / jms / JmsAttachmentUploader.java
1 package org.argeo.slc.jms;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 import javax.jms.BytesMessage;
7 import javax.jms.Destination;
8 import javax.jms.JMSException;
9 import javax.jms.Message;
10 import javax.jms.Session;
11
12 import org.apache.commons.io.IOUtils;
13 import org.argeo.slc.SlcException;
14 import org.argeo.slc.core.attachment.Attachment;
15 import org.argeo.slc.core.attachment.AttachmentUploader;
16 import org.springframework.core.io.Resource;
17 import org.springframework.jms.core.JmsTemplate;
18 import org.springframework.jms.core.MessageCreator;
19
20 public class JmsAttachmentUploader implements AttachmentUploader {
21 public final static String ATTACHMENT_ID = "slc_attachmentId";
22 public final static String ATTACHMENT_NAME = "slc_attachmentName";
23 public final static String ATTACHMENT_CONTENT_TYPE = "slc_attachmentContentType";
24
25 private JmsTemplate jmsTemplate;
26 private Destination destination;
27
28 public void upload(final Attachment attachment, final Resource resource) {
29 jmsTemplate.send(destination, new MessageCreator() {
30
31 public Message createMessage(Session session) throws JMSException {
32 BytesMessage message = session.createBytesMessage();
33 message.setStringProperty(ATTACHMENT_ID, attachment.getUuid());
34 message
35 .setStringProperty(ATTACHMENT_NAME, attachment
36 .getName());
37 message.setStringProperty(ATTACHMENT_CONTENT_TYPE, attachment
38 .getContentType());
39
40 InputStream in = null;
41 try {
42 in = resource.getInputStream();
43 byte[] buffer = new byte[1024 * 1024];
44 int read = -1;
45 while ((read = in.read(buffer)) > 0) {
46 message.writeBytes(buffer, 0, read);
47 }
48 } catch (IOException e) {
49 throw new SlcException(
50 "Cannot write into byte message for attachment "
51 + attachment + " and resource " + resource,
52 e);
53 } finally {
54 IOUtils.closeQuietly(in);
55 }
56 return message;
57 }
58 });
59
60 }
61
62 public void setJmsTemplate(JmsTemplate jmsTemplate) {
63 this.jmsTemplate = jmsTemplate;
64 }
65
66 public void setDestination(Destination destination) {
67 this.destination = destination;
68 }
69
70 }