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 */ 017package org.apache.activemq.blob; 018 019import java.io.File; 020import java.io.FileInputStream; 021import java.io.FileOutputStream; 022import java.io.IOException; 023import java.io.InputStream; 024import java.net.MalformedURLException; 025import java.net.URISyntaxException; 026import java.net.URL; 027 028import javax.jms.JMSException; 029 030import 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 */ 037public 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 try(FileInputStream fis = new FileInputStream(file)) { 069 return uploadStream(message, fis); 070 } 071 } 072 073 /* 074 * (non-Javadoc) 075 * @see org.apache.activemq.blob.BlobUploadStrategy#uploadStream(org.apache.activemq.command.ActiveMQBlobMessage, java.io.InputStream) 076 */ 077 public URL uploadStream(ActiveMQBlobMessage message, InputStream in) throws JMSException, IOException { 078 File f = getFile(message); 079 try(FileOutputStream out = new FileOutputStream(f)) { 080 byte[] buffer = new byte[policy.getBufferSize()]; 081 for (int c = in.read(buffer); c != -1; c = in.read(buffer)) { 082 out.write(buffer, 0, c); 083 out.flush(); 084 } 085 } 086 // File.toURL() is deprecated 087 return f.toURI().toURL(); 088 } 089 090 /* 091 * (non-Javadoc) 092 * @see org.apache.activemq.blob.BlobDownloadStrategy#deleteFile(org.apache.activemq.command.ActiveMQBlobMessage) 093 */ 094 public void deleteFile(ActiveMQBlobMessage message) throws IOException, JMSException { 095 File f = getFile(message); 096 if (f.exists()) { 097 if (f.delete() == false) throw new IOException("Unable to delete file " + f); 098 } 099 } 100 101 /** 102 * Returns a {@link FileInputStream} for the give {@link ActiveMQBlobMessage} 103 */ 104 public InputStream getInputStream(ActiveMQBlobMessage message) throws IOException, JMSException { 105 return new FileInputStream(getFile(message)); 106 } 107 108 109 /** 110 * Return the {@link File} for the {@link ActiveMQBlobMessage}. 111 * 112 * @param message 113 * @return file 114 * @throws JMSException 115 * @throws IOException 116 */ 117 protected File getFile(ActiveMQBlobMessage message) throws JMSException, IOException { 118 if (message.getURL() != null) { 119 // Do some checks on the received URL protocol 120 String protocol = message.getURL().getProtocol(); 121 if (!"file".contentEquals(protocol)) { 122 throw new IOException("The message URL protocol is incorrect"); 123 } 124 125 try { 126 return new File(message.getURL().toURI()); 127 } catch (URISyntaxException e) { 128 IOException ioe = new IOException("Unable to open file for message " + message); 129 ioe.initCause(e); 130 } 131 } 132 //replace all : with _ to make windows more happy 133 String fileName = message.getJMSMessageID().replaceAll(":", "_"); 134 return new File(rootFile, fileName); 135 136 } 137}