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.FileInputStream;
021    import java.io.FileOutputStream;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.net.MalformedURLException;
025    import java.net.URISyntaxException;
026    import java.net.URL;
027    
028    import javax.jms.JMSException;
029    
030    import org.apache.activemq.command.ActiveMQBlobMessage;
031    
032    /**
033     * {@link BlobUploadStrategy} and {@link BlobDownloadStrategy} implementation which use the local filesystem for storing
034     * the payload
035     *
036     */
037    public class FileSystemBlobStrategy implements BlobUploadStrategy, BlobDownloadStrategy{
038    
039       
040        private final BlobTransferPolicy policy;
041        private File rootFile;
042        
043        public FileSystemBlobStrategy(final BlobTransferPolicy policy) throws MalformedURLException, URISyntaxException  {
044            this.policy = policy;
045            
046            createRootFolder();
047        }
048        
049        /**
050         * Create the root folder if not exist 
051         * 
052         * @throws MalformedURLException
053         * @throws URISyntaxException
054         */
055        protected void createRootFolder() throws MalformedURLException, URISyntaxException {
056            rootFile = new File(new URL(policy.getUploadUrl()).toURI());
057            if (rootFile.exists() == false) {
058                rootFile.mkdirs();
059            } else if (rootFile.isDirectory() == false) {
060                throw new IllegalArgumentException("Given url is not a directory " + rootFile );
061            }
062        }
063        /*
064         * (non-Javadoc)
065         * @see org.apache.activemq.blob.BlobUploadStrategy#uploadFile(org.apache.activemq.command.ActiveMQBlobMessage, java.io.File)
066         */
067        public URL uploadFile(ActiveMQBlobMessage message, File file) throws JMSException, IOException {
068            return uploadStream(message, new FileInputStream(file));
069        }
070    
071        /*
072         * (non-Javadoc)
073         * @see org.apache.activemq.blob.BlobUploadStrategy#uploadStream(org.apache.activemq.command.ActiveMQBlobMessage, java.io.InputStream)
074         */
075        public URL uploadStream(ActiveMQBlobMessage message, InputStream in) throws JMSException, IOException {
076            File f = getFile(message);
077            FileOutputStream out = new FileOutputStream(f);
078            byte[] buffer = new byte[policy.getBufferSize()];
079            for (int c = in.read(buffer); c != -1; c = in.read(buffer)) {
080                out.write(buffer, 0, c);
081                out.flush();
082            }
083            out.flush();
084            out.close();
085            // File.toURL() is deprecated
086            return f.toURI().toURL();
087        }
088    
089        /*
090         * (non-Javadoc)
091         * @see org.apache.activemq.blob.BlobDownloadStrategy#deleteFile(org.apache.activemq.command.ActiveMQBlobMessage)
092         */
093        public void deleteFile(ActiveMQBlobMessage message) throws IOException, JMSException {
094            File f = getFile(message);
095            if (f.exists()) {
096                if (f.delete() == false) throw new IOException("Unable to delete file " + f);
097            }
098        }
099    
100        /**
101         * Returns a {@link FileInputStream} for the give {@link ActiveMQBlobMessage}
102         */
103        public InputStream getInputStream(ActiveMQBlobMessage message) throws IOException, JMSException {
104            return new FileInputStream(getFile(message));
105        }
106    
107        
108        /**
109         * Return the {@link File} for the {@link ActiveMQBlobMessage}. 
110         * 
111         * @param message
112         * @return file
113         * @throws JMSException
114         * @throws IOException 
115         */
116        protected File getFile(ActiveMQBlobMessage message) throws JMSException, IOException {
117            if (message.getURL() != null) {
118                    try {
119                                    return new File(message.getURL().toURI());
120                            } catch (URISyntaxException e) {
121                                    IOException ioe = new IOException("Unable to open file for message " + message);
122                                    ioe.initCause(e);
123                            }
124            }
125            //replace all : with _ to make windows more happy
126            String fileName = message.getJMSMessageID().replaceAll(":", "_"); 
127            return new File(rootFile, fileName);        
128            
129        }
130    }