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
.ByteArrayInputStream
;
20 import java
.io
.InputStream
;
22 import javax
.jms
.BytesMessage
;
23 import javax
.jms
.JMSException
;
24 import javax
.jms
.Message
;
25 import javax
.jms
.MessageListener
;
27 import org
.apache
.commons
.io
.IOUtils
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.argeo
.slc
.core
.attachment
.AttachmentsStorage
;
30 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
32 public class JmsAttachmentListener
implements MessageListener
{
33 private AttachmentsStorage attachmentsStorage
;
35 public void onMessage(Message msg
) {
36 BytesMessage message
= (BytesMessage
) msg
;
38 InputStream in
= null;
40 SimpleAttachment attachment
= new SimpleAttachment();
41 attachment
.setUuid(msg
42 .getStringProperty(JmsAttachmentUploader
.ATTACHMENT_ID
));
43 attachment
.setName(msg
44 .getStringProperty(JmsAttachmentUploader
.ATTACHMENT_NAME
));
47 .getStringProperty(JmsAttachmentUploader
.ATTACHMENT_CONTENT_TYPE
));
50 Long bodyLength
= message
.getBodyLength();
51 if (bodyLength
> Integer
.MAX_VALUE
)
52 throw new SlcException("Attachment cannot be bigger than "
54 + " bytes with this transport. Use another transport.");
56 byte[] buffer
= new byte[bodyLength
.intValue()];
57 message
.readBytes(buffer
);
58 in
= new ByteArrayInputStream(buffer
);
59 attachmentsStorage
.storeAttachment(attachment
, in
);
60 } catch (JMSException e
) {
61 throw new SlcException("Could not process attachment message "
64 IOUtils
.closeQuietly(in
);
68 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
69 this.attachmentsStorage
= attachmentsStorage
;