1 package org
.argeo
.slc
.jms
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.InputStream
;
6 import javax
.jms
.BytesMessage
;
7 import javax
.jms
.JMSException
;
8 import javax
.jms
.Message
;
9 import javax
.jms
.MessageListener
;
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
;
16 public class JmsAttachmentListener
implements MessageListener
{
17 private AttachmentsStorage attachmentsStorage
;
19 public void onMessage(Message msg
) {
20 BytesMessage message
= (BytesMessage
) msg
;
22 InputStream in
= null;
24 SimpleAttachment attachment
= new SimpleAttachment();
25 attachment
.setUuid(msg
26 .getStringProperty(JmsAttachmentUploader
.ATTACHMENT_ID
));
27 attachment
.setName(msg
28 .getStringProperty(JmsAttachmentUploader
.ATTACHMENT_NAME
));
31 .getStringProperty(JmsAttachmentUploader
.ATTACHMENT_CONTENT_TYPE
));
34 Long bodyLength
= message
.getBodyLength();
35 if (bodyLength
> Integer
.MAX_VALUE
)
36 throw new SlcException("Attachment cannot be bigger than "
38 + " bytes with this transport. Use another transport.");
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 "
48 IOUtils
.closeQuietly(in
);
52 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
53 this.attachmentsStorage
= attachmentsStorage
;