1 package org
.argeo
.slc
.jms
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
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
;
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
;
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";
25 private JmsTemplate jmsTemplate
;
26 private Destination destination
;
28 public void upload(final Attachment attachment
, final Resource resource
) {
29 jmsTemplate
.send(destination
, new MessageCreator() {
31 public Message
createMessage(Session session
) throws JMSException
{
32 BytesMessage message
= session
.createBytesMessage();
33 message
.setStringProperty(ATTACHMENT_ID
, attachment
.getUuid());
35 .setStringProperty(ATTACHMENT_NAME
, attachment
37 message
.setStringProperty(ATTACHMENT_CONTENT_TYPE
, attachment
40 InputStream in
= null;
42 in
= resource
.getInputStream();
43 byte[] buffer
= new byte[1024 * 1024];
45 while ((read
= in
.read(buffer
)) > 0) {
46 message
.writeBytes(buffer
, 0, read
);
48 } catch (IOException e
) {
49 throw new SlcException(
50 "Cannot write into byte message for attachment "
51 + attachment
+ " and resource " + resource
,
54 IOUtils
.closeQuietly(in
);
62 public void setJmsTemplate(JmsTemplate jmsTemplate
) {
63 this.jmsTemplate
= jmsTemplate
;
66 public void setDestination(Destination destination
) {
67 this.destination
= destination
;