2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.jms
;
19 import java
.io
.IOException
;
20 import java
.io
.InputStream
;
22 import javax
.jms
.BytesMessage
;
23 import javax
.jms
.Destination
;
24 import javax
.jms
.JMSException
;
25 import javax
.jms
.Message
;
26 import javax
.jms
.Session
;
28 import org
.apache
.commons
.io
.IOUtils
;
29 import org
.apache
.commons
.logging
.Log
;
30 import org
.apache
.commons
.logging
.LogFactory
;
31 import org
.argeo
.slc
.SlcException
;
32 import org
.argeo
.slc
.core
.attachment
.Attachment
;
33 import org
.argeo
.slc
.core
.attachment
.AttachmentUploader
;
34 import org
.springframework
.core
.io
.Resource
;
35 import org
.springframework
.jms
.JmsException
;
36 import org
.springframework
.jms
.core
.JmsTemplate
;
37 import org
.springframework
.jms
.core
.MessageCreator
;
39 public class JmsAttachmentUploader
implements AttachmentUploader
{
40 private final static Log log
= LogFactory
41 .getLog(JmsAttachmentUploader
.class);
43 public final static String ATTACHMENT_ID
= "slc_attachmentId";
44 public final static String ATTACHMENT_NAME
= "slc_attachmentName";
45 public final static String ATTACHMENT_CONTENT_TYPE
= "slc_attachmentContentType";
47 private JmsTemplate jmsTemplate
;
48 private Destination destination
;
50 public void upload(final Attachment attachment
, final Resource resource
) {
52 jmsTemplate
.send(destination
, new MessageCreator() {
54 public Message
createMessage(Session session
)
56 BytesMessage message
= session
.createBytesMessage();
57 message
.setStringProperty(ATTACHMENT_ID
, attachment
59 message
.setStringProperty(ATTACHMENT_NAME
, attachment
61 message
.setStringProperty(ATTACHMENT_CONTENT_TYPE
,
62 attachment
.getContentType());
64 InputStream in
= null;
66 in
= resource
.getInputStream();
67 byte[] buffer
= new byte[1024 * 1024];
69 while ((read
= in
.read(buffer
)) > 0) {
70 message
.writeBytes(buffer
, 0, read
);
72 } catch (IOException e
) {
73 throw new SlcException(
74 "Cannot write into byte message for attachment "
75 + attachment
+ " and resource "
78 IOUtils
.closeQuietly(in
);
83 } catch (JmsException e
) {
84 if (log
.isTraceEnabled())
85 log
.debug("Cannot upload", e
);
86 else if (log
.isDebugEnabled())
87 log
.debug("Cannot upload: " + e
.getMessage());
92 public void setJmsTemplate(JmsTemplate jmsTemplate
) {
93 this.jmsTemplate
= jmsTemplate
;
96 public void setDestination(Destination destination
) {
97 this.destination
= destination
;