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
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.slc
.SlcException
;
16 import org
.argeo
.slc
.core
.attachment
.Attachment
;
17 import org
.argeo
.slc
.core
.attachment
.AttachmentUploader
;
18 import org
.springframework
.core
.io
.Resource
;
19 import org
.springframework
.jms
.JmsException
;
20 import org
.springframework
.jms
.core
.JmsTemplate
;
21 import org
.springframework
.jms
.core
.MessageCreator
;
23 public class JmsAttachmentUploader
implements AttachmentUploader
{
24 private final static Log log
= LogFactory
25 .getLog(JmsAttachmentUploader
.class);
27 public final static String ATTACHMENT_ID
= "slc_attachmentId";
28 public final static String ATTACHMENT_NAME
= "slc_attachmentName";
29 public final static String ATTACHMENT_CONTENT_TYPE
= "slc_attachmentContentType";
31 private JmsTemplate jmsTemplate
;
32 private Destination destination
;
34 public void upload(final Attachment attachment
, final Resource resource
) {
36 jmsTemplate
.send(destination
, new MessageCreator() {
38 public Message
createMessage(Session session
)
40 BytesMessage message
= session
.createBytesMessage();
41 message
.setStringProperty(ATTACHMENT_ID
, attachment
43 message
.setStringProperty(ATTACHMENT_NAME
, attachment
45 message
.setStringProperty(ATTACHMENT_CONTENT_TYPE
,
46 attachment
.getContentType());
48 InputStream in
= null;
50 in
= resource
.getInputStream();
51 byte[] buffer
= new byte[1024 * 1024];
53 while ((read
= in
.read(buffer
)) > 0) {
54 message
.writeBytes(buffer
, 0, read
);
56 } catch (IOException e
) {
57 throw new SlcException(
58 "Cannot write into byte message for attachment "
59 + attachment
+ " and resource "
62 IOUtils
.closeQuietly(in
);
67 } catch (JmsException e
) {
68 if (log
.isTraceEnabled())
69 log
.debug("Cannot upload", e
);
70 else if (log
.isDebugEnabled())
71 log
.debug("Cannot upload: " + e
.getMessage());
76 public void setJmsTemplate(JmsTemplate jmsTemplate
) {
77 this.jmsTemplate
= jmsTemplate
;
80 public void setDestination(Destination destination
) {
81 this.destination
= destination
;