2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.jms
;
18 import java
.io
.IOException
;
19 import java
.io
.InputStream
;
21 import javax
.jms
.BytesMessage
;
22 import javax
.jms
.Destination
;
23 import javax
.jms
.JMSException
;
24 import javax
.jms
.Message
;
25 import javax
.jms
.Session
;
27 import org
.apache
.commons
.io
.IOUtils
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.slc
.SlcException
;
31 import org
.argeo
.slc
.core
.attachment
.Attachment
;
32 import org
.argeo
.slc
.core
.attachment
.AttachmentUploader
;
33 import org
.springframework
.core
.io
.Resource
;
34 import org
.springframework
.jms
.JmsException
;
35 import org
.springframework
.jms
.core
.JmsTemplate
;
36 import org
.springframework
.jms
.core
.MessageCreator
;
38 public class JmsAttachmentUploader
implements AttachmentUploader
{
39 private final static Log log
= LogFactory
40 .getLog(JmsAttachmentUploader
.class);
42 public final static String ATTACHMENT_ID
= "slc_attachmentId";
43 public final static String ATTACHMENT_NAME
= "slc_attachmentName";
44 public final static String ATTACHMENT_CONTENT_TYPE
= "slc_attachmentContentType";
46 private JmsTemplate jmsTemplate
;
47 private Destination destination
;
49 public void upload(final Attachment attachment
, final Resource resource
) {
51 jmsTemplate
.send(destination
, new MessageCreator() {
53 public Message
createMessage(Session session
)
55 BytesMessage message
= session
.createBytesMessage();
56 message
.setStringProperty(ATTACHMENT_ID
, attachment
58 message
.setStringProperty(ATTACHMENT_NAME
, attachment
60 message
.setStringProperty(ATTACHMENT_CONTENT_TYPE
,
61 attachment
.getContentType());
63 InputStream in
= null;
65 in
= resource
.getInputStream();
66 byte[] buffer
= new byte[1024 * 1024];
68 while ((read
= in
.read(buffer
)) > 0) {
69 message
.writeBytes(buffer
, 0, read
);
71 } catch (IOException e
) {
72 throw new SlcException(
73 "Cannot write into byte message for attachment "
74 + attachment
+ " and resource "
77 IOUtils
.closeQuietly(in
);
82 } catch (JmsException e
) {
83 if (log
.isTraceEnabled())
84 log
.debug("Cannot upload", e
);
85 else if (log
.isDebugEnabled())
86 log
.debug("Cannot upload: " + e
.getMessage());
91 public void setJmsTemplate(JmsTemplate jmsTemplate
) {
92 this.jmsTemplate
= jmsTemplate
;
95 public void setDestination(Destination destination
) {
96 this.destination
= destination
;