001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.command;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    import java.net.MalformedURLException;
022    import java.net.URL;
023    
024    import javax.jms.JMSException;
025    
026    import org.apache.activemq.BlobMessage;
027    import org.apache.activemq.blob.BlobUploader;
028    import org.apache.activemq.util.JMSExceptionSupport;
029    
030    /**
031     * An implementation of {@link BlobMessage} for out of band BLOB transfer
032     * 
033     * @version $Revision: $
034     * @openwire:marshaller code="29"
035     */
036    public class ActiveMQBlobMessage extends ActiveMQMessage implements BlobMessage {
037        public static final byte DATA_STRUCTURE_TYPE = CommandTypes.ACTIVEMQ_BLOB_MESSAGE;
038    
039        public static final String BINARY_MIME_TYPE = "application/octet-stream";
040    
041        private String remoteBlobUrl;
042        private String mimeType;
043        private String name;
044        private boolean deletedByBroker;
045    
046        private transient BlobUploader blobUploader;
047        private transient URL url;
048    
049        public Message copy() {
050            ActiveMQBlobMessage copy = new ActiveMQBlobMessage();
051            copy(copy);
052            return copy;
053        }
054    
055        private void copy(ActiveMQBlobMessage copy) {
056            super.copy(copy);
057            copy.setRemoteBlobUrl(getRemoteBlobUrl());
058            copy.setMimeType(getMimeType());
059            copy.setDeletedByBroker(isDeletedByBroker());
060            copy.setBlobUploader(getBlobUploader());
061        }
062    
063        public byte getDataStructureType() {
064            return DATA_STRUCTURE_TYPE;
065        }
066    
067        /**
068         * @openwire:property version=3 cache=false
069         */
070        public String getRemoteBlobUrl() {
071            return remoteBlobUrl;
072        }
073    
074        public void setRemoteBlobUrl(String remoteBlobUrl) {
075            this.remoteBlobUrl = remoteBlobUrl;
076            url = null;
077        }
078    
079        /**
080         * The MIME type of the BLOB which can be used to apply different content
081         * types to messages.
082         * 
083         * @openwire:property version=3 cache=true
084         */
085        public String getMimeType() {
086            if (mimeType == null) {
087                return BINARY_MIME_TYPE;
088            }
089            return mimeType;
090        }
091    
092        public void setMimeType(String mimeType) {
093            this.mimeType = mimeType;
094        }
095    
096        public String getName() {
097            return name;
098        }
099    
100        /**
101         * The name of the attachment which can be useful information if
102         * transmitting files over ActiveMQ
103         * 
104         * @openwire:property version=3 cache=false
105         */
106        public void setName(String name) {
107            this.name = name;
108        }
109    
110        /**
111         * @openwire:property version=3 cache=false
112         */
113        public boolean isDeletedByBroker() {
114            return deletedByBroker;
115        }
116    
117        public void setDeletedByBroker(boolean deletedByBroker) {
118            this.deletedByBroker = deletedByBroker;
119        }
120    
121        public String getJMSXMimeType() {
122            return getMimeType();
123        }
124    
125        public InputStream getInputStream() throws IOException, JMSException {
126            URL value = getURL();
127            if (value == null) {
128                return null;
129            }
130            return value.openStream();
131        }
132    
133        public URL getURL() throws JMSException {
134            if (url == null && remoteBlobUrl != null) {
135                try {
136                    url = new URL(remoteBlobUrl);
137                } catch (MalformedURLException e) {
138                    throw JMSExceptionSupport.create(e);
139                }
140            }
141            return url;
142        }
143    
144        public void setURL(URL url) {
145            this.url = url;
146            remoteBlobUrl = url != null ? url.toExternalForm() : null;
147        }
148    
149        public BlobUploader getBlobUploader() {
150            return blobUploader;
151        }
152    
153        public void setBlobUploader(BlobUploader blobUploader) {
154            this.blobUploader = blobUploader;
155        }
156    
157        public void onSend() throws JMSException {
158            super.onSend();
159    
160            // lets ensure we upload the BLOB first out of band before we send the
161            // message
162            if (blobUploader != null) {
163                try {
164                    URL value = blobUploader.upload(this);
165                    setURL(value);
166                } catch (IOException e) {
167                    throw JMSExceptionSupport.create(e);
168                }
169            }
170        }
171    }