]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAttachmentListener.java
Start working on serialized JMS
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.activemq / src / main / java / org / argeo / slc / jms / JmsAttachmentListener.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.jms;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.InputStream;
21
22 import javax.jms.BytesMessage;
23 import javax.jms.JMSException;
24 import javax.jms.Message;
25 import javax.jms.MessageListener;
26
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;
31
32 public class JmsAttachmentListener implements MessageListener {
33 private AttachmentsStorage attachmentsStorage;
34
35 public void onMessage(Message msg) {
36 BytesMessage message = (BytesMessage) msg;
37
38 InputStream in = null;
39 try {
40 SimpleAttachment attachment = new SimpleAttachment();
41 attachment.setUuid(msg
42 .getStringProperty(JmsAttachmentUploader.ATTACHMENT_ID));
43 attachment.setName(msg
44 .getStringProperty(JmsAttachmentUploader.ATTACHMENT_NAME));
45 attachment
46 .setContentType(msg
47 .getStringProperty(JmsAttachmentUploader.ATTACHMENT_CONTENT_TYPE));
48
49 // Check body length
50 Long bodyLength = message.getBodyLength();
51 if (bodyLength > Integer.MAX_VALUE)
52 throw new SlcException("Attachment cannot be bigger than "
53 + Integer.MAX_VALUE
54 + " bytes with this transport. Use another transport.");
55
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 "
62 + msg, e);
63 } finally {
64 IOUtils.closeQuietly(in);
65 }
66 }
67
68 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
69 this.attachmentsStorage = attachmentsStorage;
70 }
71
72 }