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.blob;
018    
019    import java.io.File;
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.net.URL;
023    
024    import javax.jms.JMSException;
025    
026    import org.apache.activemq.command.ActiveMQBlobMessage;
027    
028    /**
029     * A helper class to represent a required upload of a BLOB to some remote URL
030     * 
031     * 
032     */
033    public class BlobUploader {
034    
035        private final BlobTransferPolicy blobTransferPolicy;
036        private File file;
037        private InputStream in;
038    
039        public BlobUploader(BlobTransferPolicy blobTransferPolicy, InputStream in) {
040            // need to do a defensive copy
041            this.blobTransferPolicy = blobTransferPolicy.copy();
042            this.in = in;
043        }
044    
045        public BlobUploader(BlobTransferPolicy blobTransferPolicy, File file) {
046            // need to do a defensive copy
047            this.blobTransferPolicy = blobTransferPolicy.copy();
048            this.file = file;
049        }
050    
051        public URL upload(ActiveMQBlobMessage message) throws JMSException, IOException {
052            if (file != null) {
053                return getStrategy().uploadFile(message, file);
054            } else {
055                return getStrategy().uploadStream(message, in);
056            }
057        }
058    
059        public BlobTransferPolicy getBlobTransferPolicy() {
060            return blobTransferPolicy;
061        }
062    
063        public BlobUploadStrategy getStrategy() {
064            return getBlobTransferPolicy().getUploadStrategy();
065        }
066    }