]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAttachmentListener.java
8b91e7697093dfdd6c4bd0e4a1be824cb3ff6f91
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.activemq / src / main / java / org / argeo / slc / jms / JmsAttachmentListener.java
1 package org.argeo.slc.jms;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5
6 import javax.jms.BytesMessage;
7 import javax.jms.JMSException;
8 import javax.jms.Message;
9 import javax.jms.MessageListener;
10
11 import org.apache.commons.io.IOUtils;
12 import org.argeo.slc.SlcException;
13 import org.argeo.slc.core.attachment.AttachmentsStorage;
14 import org.argeo.slc.core.attachment.SimpleAttachment;
15
16 public class JmsAttachmentListener implements MessageListener {
17 private AttachmentsStorage attachmentsStorage;
18
19 public void onMessage(Message msg) {
20 BytesMessage message = (BytesMessage) msg;
21
22 InputStream in = null;
23 try {
24 SimpleAttachment attachment = new SimpleAttachment();
25 attachment.setUuid(msg
26 .getStringProperty(JmsAttachmentUploader.ATTACHMENT_ID));
27 attachment.setName(msg
28 .getStringProperty(JmsAttachmentUploader.ATTACHMENT_NAME));
29 attachment
30 .setContentType(msg
31 .getStringProperty(JmsAttachmentUploader.ATTACHMENT_CONTENT_TYPE));
32
33 // Check body length
34 Long bodyLength = message.getBodyLength();
35 if (bodyLength > Integer.MAX_VALUE)
36 throw new SlcException("Attachment cannot be bigger than "
37 + Integer.MAX_VALUE
38 + " bytes with this transport. Use another transport.");
39
40 byte[] buffer = new byte[bodyLength.intValue()];
41 message.readBytes(buffer);
42 in = new ByteArrayInputStream(buffer);
43 attachmentsStorage.storeAttachment(attachment, in);
44 } catch (JMSException e) {
45 throw new SlcException("Could not process attachment message "
46 + msg, e);
47 } finally {
48 IOUtils.closeQuietly(in);
49 }
50 }
51
52 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
53 this.attachmentsStorage = attachmentsStorage;
54 }
55
56 }