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.BlobDownloader;
028    import org.apache.activemq.blob.BlobUploader;
029    import org.apache.activemq.util.JMSExceptionSupport;
030    
031    /**
032     * An implementation of {@link BlobMessage} for out of band BLOB transfer
033     * 
034     * @version $Revision: $
035     * @openwire:marshaller code="29"
036     */
037    public class ActiveMQBlobMessage extends ActiveMQMessage implements BlobMessage {
038        public static final byte DATA_STRUCTURE_TYPE = CommandTypes.ACTIVEMQ_BLOB_MESSAGE;
039    
040        public static final String BINARY_MIME_TYPE = "application/octet-stream";
041    
042        private String remoteBlobUrl;
043        private String mimeType;
044        private String name;
045        private boolean deletedByBroker;
046    
047        private transient BlobUploader blobUploader;
048        private transient BlobDownloader blobDownloader;
049        private transient URL url;
050    
051        public Message copy() {
052            ActiveMQBlobMessage copy = new ActiveMQBlobMessage();
053            copy(copy);
054            return copy;
055        }
056    
057        private void copy(ActiveMQBlobMessage copy) {
058            super.copy(copy);
059            copy.setRemoteBlobUrl(getRemoteBlobUrl());
060            copy.setMimeType(getMimeType());
061            copy.setDeletedByBroker(isDeletedByBroker());
062            copy.setBlobUploader(getBlobUploader());
063        }
064    
065        public byte getDataStructureType() {
066            return DATA_STRUCTURE_TYPE;
067        }
068    
069        /**
070         * @openwire:property version=3 cache=false
071         */
072        public String getRemoteBlobUrl() {
073            return remoteBlobUrl;
074        }
075    
076        public void setRemoteBlobUrl(String remoteBlobUrl) {
077            this.remoteBlobUrl = remoteBlobUrl;
078            url = null;
079        }
080    
081        /**
082         * The MIME type of the BLOB which can be used to apply different content
083         * types to messages.
084         * 
085         * @openwire:property version=3 cache=true
086         */
087        public String getMimeType() {
088            if (mimeType == null) {
089                return BINARY_MIME_TYPE;
090            }
091            return mimeType;
092        }
093    
094        public void setMimeType(String mimeType) {
095            this.mimeType = mimeType;
096        }
097    
098        public String getName() {
099            return name;
100        }
101    
102        /**
103         * The name of the attachment which can be useful information if
104         * transmitting files over ActiveMQ
105         * 
106         * @openwire:property version=3 cache=false
107         */
108        public void setName(String name) {
109            this.name = name;
110        }
111    
112        /**
113         * @openwire:property version=3 cache=false
114         */
115        public boolean isDeletedByBroker() {
116            return deletedByBroker;
117        }
118    
119        public void setDeletedByBroker(boolean deletedByBroker) {
120            this.deletedByBroker = deletedByBroker;
121        }
122    
123        public String getJMSXMimeType() {
124            return getMimeType();
125        }
126    
127        public InputStream getInputStream() throws IOException, JMSException {
128            if(blobDownloader == null) {
129                return null;
130            }
131            return blobDownloader.getInputStream(this);
132        }
133    
134        public URL getURL() throws JMSException {
135            if (url == null && remoteBlobUrl != null) {
136                try {
137                    url = new URL(remoteBlobUrl);
138                } catch (MalformedURLException e) {
139                    throw JMSExceptionSupport.create(e);
140                }
141            }
142            return url;
143        }
144    
145        public void setURL(URL url) {
146            this.url = url;
147            remoteBlobUrl = url != null ? url.toExternalForm() : null;
148        }
149    
150        public BlobUploader getBlobUploader() {
151            return blobUploader;
152        }
153    
154        public void setBlobUploader(BlobUploader blobUploader) {
155            this.blobUploader = blobUploader;
156        }
157    
158        public BlobDownloader getBlobDownloader() {
159            return blobDownloader;
160        }
161    
162        public void setBlobDownloader(BlobDownloader blobDownloader) {
163            this.blobDownloader = blobDownloader;
164        }
165    
166        public void onSend() throws JMSException {
167            super.onSend();
168    
169            // lets ensure we upload the BLOB first out of band before we send the
170            // message
171            if (blobUploader != null) {
172                try {
173                    URL value = blobUploader.upload(this);
174                    setURL(value);
175                } catch (IOException e) {
176                    throw JMSExceptionSupport.create(e);
177                }
178            }
179        }
180    }