From 9c547af5bbdb448cea113649507a749b8457062b Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Thu, 31 Oct 2013 22:42:38 +0100 Subject: [PATCH] [Major] cleaned up the FileServer/Client implentation Also renamed it from RMI to FileServer/Client. There is no reason this has to be RMI bound, as a client must simply implement FileClient where the upload/download methods can be implemented to be done over any kind of remote connection --- .../FileClient.java} | 14 +- .../FileClientUtil.java} | 116 +++++++------ .../FileDeletion.java} | 7 +- .../FileHandler.java} | 162 +++++++++++------- .../FilePart.java} | 13 +- 5 files changed, 180 insertions(+), 132 deletions(-) rename src/main/java/ch/eitchnet/{rmi/RMIFileClient.java => fileserver/FileClient.java} (84%) rename src/main/java/ch/eitchnet/{rmi/RmiHelper.java => fileserver/FileClientUtil.java} (54%) rename src/main/java/ch/eitchnet/{rmi/RmiFileDeletion.java => fileserver/FileDeletion.java} (91%) rename src/main/java/ch/eitchnet/{rmi/RmiFileHandler.java => fileserver/FileHandler.java} (53%) rename src/main/java/ch/eitchnet/{rmi/RmiFilePart.java => fileserver/FilePart.java} (90%) diff --git a/src/main/java/ch/eitchnet/rmi/RMIFileClient.java b/src/main/java/ch/eitchnet/fileserver/FileClient.java similarity index 84% rename from src/main/java/ch/eitchnet/rmi/RMIFileClient.java rename to src/main/java/ch/eitchnet/fileserver/FileClient.java index 9ddbab772..85bc54760 100644 --- a/src/main/java/ch/eitchnet/rmi/RMIFileClient.java +++ b/src/main/java/ch/eitchnet/fileserver/FileClient.java @@ -17,7 +17,7 @@ * along with ch.eitchnet.java.utils. If not, see . * */ -package ch.eitchnet.rmi; +package ch.eitchnet.fileserver; import java.rmi.RemoteException; @@ -25,7 +25,7 @@ import java.rmi.RemoteException; * @author Robert von Burg * */ -public interface RMIFileClient { +public interface FileClient { /** * Remote method with which a client can push parts of files to the server. It is up to the client to send as many @@ -36,23 +36,23 @@ public interface RMIFileClient { * @throws RemoteException * if something goes wrong with the remote call */ - public void uploadFilePart(RmiFilePart filePart) throws RemoteException; + public void uploadFilePart(FilePart filePart) throws RemoteException; /** * Remote method with which a client can delete files from the server. It only deletes single files if they exist * * @param fileDeletion - * the {@link RmiFileDeletion} defining the deletion request + * the {@link FileDeletion} defining the deletion request * * @return true if the file was deleted, false if the file did not exist * * @throws RemoteException * if something goes wrong with the remote call */ - public boolean deleteFile(RmiFileDeletion fileDeletion) throws RemoteException; + public boolean deleteFile(FileDeletion fileDeletion) throws RemoteException; /** - * Remote method which a client can request part of a file. The server will fill the given {@link RmiFilePart} with + * Remote method which a client can request part of a file. The server will fill the given {@link FilePart} with * a byte array of the file, with bytes from the file, respecting the desired offset. It is up to the client to call * this method multiple times for the entire file. It is a decision of the concrete implementation how much data is * returned in each part, the client may pass a request, but this is not definitive @@ -65,5 +65,5 @@ public interface RMIFileClient { * @throws RemoteException * if something goes wrong with the remote call */ - public RmiFilePart requestFile(RmiFilePart filePart) throws RemoteException; + public FilePart requestFile(FilePart filePart) throws RemoteException; } diff --git a/src/main/java/ch/eitchnet/rmi/RmiHelper.java b/src/main/java/ch/eitchnet/fileserver/FileClientUtil.java similarity index 54% rename from src/main/java/ch/eitchnet/rmi/RmiHelper.java rename to src/main/java/ch/eitchnet/fileserver/FileClientUtil.java index 049e4dbd7..934d406bc 100644 --- a/src/main/java/ch/eitchnet/rmi/RmiHelper.java +++ b/src/main/java/ch/eitchnet/fileserver/FileClientUtil.java @@ -17,13 +17,14 @@ * along with ch.eitchnet.java.utils. If not, see . * */ -package ch.eitchnet.rmi; +package ch.eitchnet.fileserver; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.rmi.RemoteException; +import java.text.MessageFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,25 +36,27 @@ import ch.eitchnet.utils.helper.StringHelper; * @author Robert von Burg * */ -public class RmiHelper { +public class FileClientUtil { - private static final Logger logger = LoggerFactory.getLogger(RmiHelper.class); + private static final Logger logger = LoggerFactory.getLogger(FileClientUtil.class); /** * @param rmiFileClient * @param origFilePart * @param dstFile */ - public static void downloadFile(RMIFileClient rmiFileClient, RmiFilePart origFilePart, File dstFile) { + public static void downloadFile(FileClient rmiFileClient, FilePart origFilePart, File dstFile) { // here we don't overwrite, the caller must make sure the destination file does not exist - if (dstFile.exists()) - throw new RuntimeException("The destination file " + dstFile.getAbsolutePath() - + " already exists. Delete it first, if you want to overwrite it!"); + if (dstFile.exists()) { + String msg = "The destination file {0} already exists. Delete it first, if you want to overwrite it!"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, dstFile.getAbsolutePath()); + throw new RuntimeException(msg); + } try { - RmiFilePart tmpPart = origFilePart; + FilePart tmpPart = origFilePart; int loops = 0; int startLength = tmpPart.getPartLength(); @@ -64,14 +67,17 @@ public class RmiHelper { tmpPart = rmiFileClient.requestFile(tmpPart); // validate length of data - if (tmpPart.getPartLength() != tmpPart.getPartBytes().length) - throw new RuntimeException("Invalid tmpPart. Part length is not as long as the bytes passed " - + tmpPart.getPartLength() + " / " + tmpPart.getPartBytes().length); + if (tmpPart.getPartLength() != tmpPart.getPartBytes().length) { + String msg = "Invalid tmpPart. Part length is not as long as the bytes passed {0} / {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, tmpPart.getPartLength(), tmpPart.getPartBytes().length); + throw new RuntimeException(msg); + } // validate offset is size of file if (tmpPart.getPartOffset() != dstFile.length()) { - throw new RuntimeException("The part offset $offset is not at the end of the file " - + tmpPart.getPartOffset() + " / " + dstFile.length()); + String msg = "The part offset $offset is not at the end of the file {0} / {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, tmpPart.getPartOffset(), dstFile.length()); + throw new RuntimeException(msg); } // append the part @@ -84,28 +90,33 @@ public class RmiHelper { if (tmpPart.getPartOffset() >= tmpPart.getFileLength()) break; } - RmiHelper.logger.info(tmpPart.getFileType() + ": " + tmpPart.getFileName() + ": Requested " + loops - + " parts. StartSize: " + startLength + " EndSize: " + tmpPart.getPartLength()); + + String msg = "{0}: {1}: Requested {2} parts. StartSize: {3} EndSize: {4}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, tmpPart.getFileType(), tmpPart.getFileName(), loops, startLength, + tmpPart.getPartLength()); + logger.info(msg); // validate that the offset is at the end of the file if (tmpPart.getPartOffset() != origFilePart.getFileLength()) { - throw new RuntimeException("Offset " + tmpPart.getPartOffset() + " is not at file length " - + origFilePart.getFileLength() + " after reading all the file parts!"); + msg = "Offset {0} is not at file length {1} after reading all the file parts!"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, tmpPart.getPartOffset(), origFilePart.getFileLength()); + throw new RuntimeException(msg); } // now validate hashes String dstFileHash = StringHelper.getHexString(FileHelper.hashFileSha256(dstFile)); if (!dstFileHash.equals(origFilePart.getFileHash())) { - throw new RuntimeException("Downloading the file " + origFilePart.getFileName() - + " failed because the hashes don't match. Expected: " + origFilePart.getFileHash() - + " / Actual: " + dstFileHash); + msg = "Downloading the file {0} failed because the hashes don''t match. Expected: {1} / Actual: {2}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, origFilePart.getFileName(), origFilePart.getFileHash(), dstFileHash); + throw new RuntimeException(msg); } } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException) e; - throw new RuntimeException("Downloading the file " + origFilePart.getFileName() - + " failed because of an underlying exception " + e.getLocalizedMessage()); + String msg = "Downloading the file {0} failed because of an underlying exception {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, origFilePart.getFileName(), e.getLocalizedMessage()); + throw new RuntimeException(msg); } } @@ -114,28 +125,29 @@ public class RmiHelper { * @param srcFile * @param fileType */ - public static void uploadFile(RMIFileClient rmiFileClient, File srcFile, String fileType) { + public static void uploadFile(FileClient rmiFileClient, File srcFile, String fileType) { // make sure the source file exists - if (!srcFile.canRead()) - throw new RuntimeException("The source file does not exist at " + srcFile.getAbsolutePath()); + if (!srcFile.canRead()) { + String msg = MessageFormat.format("The source file does not exist at {0}", srcFile.getAbsolutePath()); //$NON-NLS-1$ + throw new RuntimeException(msg); + } - BufferedInputStream inputStream = null; - try { + try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(srcFile));) { // get the size of the file long fileLength = srcFile.length(); String fileHash = StringHelper.getHexString(FileHelper.hashFileSha256(srcFile)); // create the file part to send - RmiFilePart filePart = new RmiFilePart(srcFile.getName(), fileType); + FilePart filePart = new FilePart(srcFile.getName(), fileType); filePart.setFileLength(fileLength); filePart.setFileHash(fileHash); // define the normal size of the parts we're sending. The last part will naturally have a different size int partLength; - if (fileLength > RmiFileHandler.MAX_PART_SIZE) - partLength = RmiFileHandler.MAX_PART_SIZE; + if (fileLength > FileHandler.MAX_PART_SIZE) + partLength = FileHandler.MAX_PART_SIZE; else partLength = (int) fileLength; @@ -143,8 +155,6 @@ public class RmiHelper { byte[] bytes = new byte[partLength]; // open the stream to the file - inputStream = new BufferedInputStream(new FileInputStream(srcFile)); - int read = 0; int offset = 0; @@ -159,11 +169,11 @@ public class RmiHelper { // validate we read the expected number of bytes if (read == -1) - throw new IOException("Something went wrong while reading the bytes as -1 was returned!"); + throw new IOException("Something went wrong while reading the bytes as -1 was returned!"); //$NON-NLS-1$ if (read != bytes.length) { - throw new IOException( - "Something went wrong while reading the bytes as the wrong number of bytes were read. Expected " - + bytes.length + " Actual: " + read); + String msg = "Something went wrong while reading the bytes as the wrong number of bytes were read. Expected {0} Actual: {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, bytes.length, read); + throw new IOException(msg); } // set the fields on the FilePart @@ -187,9 +197,11 @@ public class RmiHelper { // the last part of the file if (nextOffset + bytes.length > fileLength) { long remaining = fileLength - nextOffset; - if (remaining > RmiFileHandler.MAX_PART_SIZE) - throw new RuntimeException("Something went wrong as the remaining part " + remaining - + " is larger than MAX_PART_SIZE " + RmiFileHandler.MAX_PART_SIZE + "!"); + if (remaining > FileHandler.MAX_PART_SIZE) { + String msg = "Something went wrong as the remaining part {0} is larger than MAX_PART_SIZE {1}!"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, remaining, FileHandler.MAX_PART_SIZE); + throw new RuntimeException(msg); + } partLength = (int) remaining; bytes = new byte[partLength]; } @@ -198,22 +210,17 @@ public class RmiHelper { offset = nextOffset; } - RmiHelper.logger.info(filePart.getFileType() + ": " + filePart.getFileName() + ": Sent " + loops - + " parts. StartSize: " + startLength + " EndSize: " + filePart.getPartLength()); + String msg = "{0}: {1}: Sent {2} parts. StartSize: {3} EndSize: {4}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, filePart.getFileType(), filePart.getFileName(), loops, startLength, + filePart.getPartLength()); + logger.info(msg); } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException) e; - throw new RuntimeException("Uploading the file " + srcFile.getAbsolutePath() - + " failed because of an underlying exception " + e.getLocalizedMessage()); - } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - RmiHelper.logger.error("Exception while closing FileInputStream " + e.getLocalizedMessage()); - } - } + String msg = "Uploading the file {0} failed because of an underlying exception {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, srcFile.getAbsolutePath(), e.getLocalizedMessage()); + throw new RuntimeException(msg); } } @@ -222,13 +229,14 @@ public class RmiHelper { * @param fileDeletion * @param dstFile */ - public static void deleteFile(RMIFileClient rmiFileClient, RmiFileDeletion fileDeletion, File dstFile) { + public static void deleteFile(FileClient rmiFileClient, FileDeletion fileDeletion, File dstFile) { try { rmiFileClient.deleteFile(fileDeletion); } catch (RemoteException e) { - throw new RuntimeException("Deleting the file " + fileDeletion.getFileName() - + " failed because of an underlying exception " + e.getLocalizedMessage()); + String msg = "Deleting the file {0} failed because of an underlying exception {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, fileDeletion.getFileName(), e.getLocalizedMessage()); + throw new RuntimeException(msg); } } } diff --git a/src/main/java/ch/eitchnet/rmi/RmiFileDeletion.java b/src/main/java/ch/eitchnet/fileserver/FileDeletion.java similarity index 91% rename from src/main/java/ch/eitchnet/rmi/RmiFileDeletion.java rename to src/main/java/ch/eitchnet/fileserver/FileDeletion.java index 3071092f0..eecbb7050 100644 --- a/src/main/java/ch/eitchnet/rmi/RmiFileDeletion.java +++ b/src/main/java/ch/eitchnet/fileserver/FileDeletion.java @@ -17,16 +17,15 @@ * along with ch.eitchnet.java.utils. If not, see . * */ -package ch.eitchnet.rmi; +package ch.eitchnet.fileserver; import java.io.Serializable; /** * @author Robert von Burg */ -public class RmiFileDeletion implements Serializable { +public class FileDeletion implements Serializable { - // private static final long serialVersionUID = 1L; private String fileName; @@ -38,7 +37,7 @@ public class RmiFileDeletion implements Serializable { * @param fileType * the type of file to delete. This defines in which path the file resides */ - public RmiFileDeletion(String fileName, String fileType) { + public FileDeletion(String fileName, String fileType) { this.fileName = fileName; this.fileType = fileType; } diff --git a/src/main/java/ch/eitchnet/rmi/RmiFileHandler.java b/src/main/java/ch/eitchnet/fileserver/FileHandler.java similarity index 53% rename from src/main/java/ch/eitchnet/rmi/RmiFileHandler.java rename to src/main/java/ch/eitchnet/fileserver/FileHandler.java index 6dcc256b4..7267c562f 100644 --- a/src/main/java/ch/eitchnet/rmi/RmiFileHandler.java +++ b/src/main/java/ch/eitchnet/fileserver/FileHandler.java @@ -17,12 +17,13 @@ * along with ch.eitchnet.java.utils. If not, see . * */ -package ch.eitchnet.rmi; +package ch.eitchnet.fileserver; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.text.MessageFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,13 +33,13 @@ import ch.eitchnet.utils.helper.StringHelper; /** * This class handles remote requests of clients to upload or download a file. Uploading a file is done by calling - * {@link #handleFilePart(RmiFilePart)} and the downloading a file is done by calling {@link #requestFile(RmiFilePart)} + * {@link #handleFilePart(FilePart)} and the downloading a file is done by calling {@link #requestFile(FilePart)} * * @author Robert von Burg */ -public class RmiFileHandler { +public class FileHandler { - private static final Logger logger = LoggerFactory.getLogger(RmiFileHandler.class); + private static final Logger logger = LoggerFactory.getLogger(FileHandler.class); /** * DEF_PART_SIZE = default part size which is set to 1048576 bytes (1 MiB) @@ -46,23 +47,29 @@ public class RmiFileHandler { public static final int MAX_PART_SIZE = 1048576; private String basePath; + private boolean verbose; /** * */ - public RmiFileHandler(String basePath) { + public FileHandler(String basePath, boolean verbose) { File basePathF = new File(basePath); - if (!basePathF.exists()) - throw new RuntimeException("Base Path does not exist " + basePathF.getAbsolutePath()); - if (!basePathF.canWrite()) - throw new RuntimeException("Can not write to base path " + basePathF.getAbsolutePath()); + if (!basePathF.exists()) { + String msg = MessageFormat.format("Base Path does not exist {0}", basePathF.getAbsolutePath()); //$NON-NLS-1$ + throw new RuntimeException(msg); + } + if (!basePathF.canWrite()) { + String msg = MessageFormat.format("Can not write to base path {0}", basePathF.getAbsolutePath()); //$NON-NLS-1$ + throw new RuntimeException(msg); + } + this.verbose = verbose; this.basePath = basePath; } /** - * Method which a client can request part of a file. The server will fill the given {@link RmiFilePart} with a byte + * Method which a client can request part of a file. The server will fill the given {@link FilePart} with a byte * array of the file, with bytes from the file, respecting the desired offset. It is up to the client to call this * method multiple times for the entire file. It is a decision of the concrete implementation how much data is * returned in each part, the client may pass a request, but this is not definitive @@ -70,7 +77,7 @@ public class RmiFileHandler { * @param filePart * the part of the file */ - public RmiFilePart requestFile(RmiFilePart filePart) { + public FilePart requestFile(FilePart filePart) { // validate file name is legal String fileName = filePart.getFileName(); @@ -81,12 +88,15 @@ public class RmiFileHandler { validateFileType(fileType); // evaluate the path where the file should reside - File file = new File(this.basePath + "/" + fileType, filePart.getFileName()); + String fileTypePath = this.basePath + "/" + fileType; //$NON-NLS-1$ + File file = new File(fileTypePath, filePart.getFileName()); // now evaluate the file exists + String fileNotFoundMsg = "The file {0} could not be found in the location for files of type {1}"; //$NON-NLS-1$ if (!file.canRead()) { - throw new RuntimeException("The file " + fileName - + " could not be found in the location for files of type " + fileType); + String msg = fileNotFoundMsg; + msg = MessageFormat.format(msg, fileName, fileType); + throw new RuntimeException(msg); } // if this is the start of the file, then prepare the file part @@ -103,18 +113,21 @@ public class RmiFileHandler { // variables defining the part of the file we're going to return long requestOffset = filePart.getPartOffset(); int requestSize = filePart.getPartLength(); - if (requestSize > RmiFileHandler.MAX_PART_SIZE) { - throw new RuntimeException("The requested part size " + requestSize + " is greater than the allowed " - + RmiFileHandler.MAX_PART_SIZE); + if (requestSize > FileHandler.MAX_PART_SIZE) { + String msg = "The requested part size {0} is greater than the allowed {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, requestSize, MAX_PART_SIZE); + throw new RuntimeException(msg); } // validate lengths and offsets if (filePart.getFileLength() != fileSize) { - throw new RuntimeException("The part request has a file size " + filePart.getFileLength() - + ", but the file is actually " + fileSize); + String msg = "The part request has a file size {0}, but the file is actually {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, filePart.getFileLength(), fileSize); + throw new RuntimeException(msg); } else if (requestOffset > fileSize) { - throw new RuntimeException("The requested file part offset " + requestOffset - + " is greater than the size of the file " + fileSize); + String msg = "The requested file part offset {0} is greater than the size of the file {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, requestOffset, fileSize); + throw new RuntimeException(msg); } // Otherwise make sure the offset + request length is not larger than the actual file size. // If it is then this is the end part @@ -126,8 +139,11 @@ public class RmiFileHandler { long l = Math.min(requestSize, remaining); // this is a fail safe - if (l > RmiFileHandler.MAX_PART_SIZE) - throw new RuntimeException("Something went wrong. Min of requestSize and remaining is > MAX_PART_SIZE!"); + if (l > MAX_PART_SIZE) { + String msg = "Something went wrong. Min of requestSize and remaining is > MAX_PART_SIZE of {0}!"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, MAX_PART_SIZE); + throw new RuntimeException(msg); + } // this is the size of the array we want to return requestSize = (int) l; @@ -136,40 +152,42 @@ public class RmiFileHandler { } // now read the part of the file and set it as bytes for the file part - FileInputStream fin = null; - try { + try (FileInputStream fin = new FileInputStream(file);) { // position the stream - fin = new FileInputStream(file); long skip = fin.skip(requestOffset); - if (skip != requestOffset) - throw new IOException("Asked to skip " + requestOffset + " but only skipped " + skip); + if (skip != requestOffset) { + String msg = MessageFormat.format("Asked to skip {0} but only skipped {1}", requestOffset, skip); //$NON-NLS-1$ + throw new IOException(msg); + } // read the data byte[] bytes = new byte[requestSize]; int read = fin.read(bytes); - if (read != requestSize) - throw new IOException("Asked to read " + requestSize + " but only read " + read); + if (read != requestSize) { + String msg = MessageFormat.format("Asked to read {0} but only read {1}", requestSize, read); //$NON-NLS-1$ + throw new IOException(msg); + } // set the return result filePart.setPartBytes(bytes); } catch (FileNotFoundException e) { - throw new RuntimeException("The file " + fileName - + " could not be found in the location for files of type " + fileType); + String msg = MessageFormat.format(fileNotFoundMsg, fileName, fileType); + throw new RuntimeException(msg); } catch (IOException e) { - throw new RuntimeException("There was an error while reading from the file " + fileName); - } finally { - if (fin != null) { - try { - fin.close(); - } catch (IOException e) { - RmiFileHandler.logger.error("Error while closing FileInputStream: " + e.getLocalizedMessage()); - } - } + String msg = "There was an error while reading from the file {0}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, fileName); + throw new RuntimeException(msg); } - // we are returning the same object as the user gave us, just edited + // we are returning the same object as the user gave us, just modified + if (this.verbose) { + String msg = "Read {0} for file {1}/{2}"; //$NON-NLS-1$ + String fileSizeS = FileHelper.humanizeFileSize(filePart.getPartBytes().length); + msg = MessageFormat.format(msg, fileSizeS, fileType, fileName); + logger.info(msg); + } return filePart; } @@ -180,7 +198,7 @@ public class RmiFileHandler { * @param filePart * the part of the file */ - public void handleFilePart(RmiFilePart filePart) { + public void handleFilePart(FilePart filePart) { // validate file name is legal String fileName = filePart.getFileName(); @@ -191,11 +209,14 @@ public class RmiFileHandler { validateFileType(fileType); // evaluate the path where the file should reside - File dstFile = new File(this.basePath + "/" + fileType, filePart.getFileName()); + String fileTypePath = this.basePath + "/" + fileType; //$NON-NLS-1$ + File dstFile = new File(fileTypePath, filePart.getFileName()); // if the file already exists, then this may not be a start part if (filePart.getPartOffset() == 0 && dstFile.exists()) { - throw new RuntimeException("The file " + fileName + " already exist for type " + fileType); + String msg = "The file {0} already exist for type {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, fileName, fileType); + throw new RuntimeException(msg); } // write the part @@ -205,23 +226,34 @@ public class RmiFileHandler { if (filePart.isLastPart()) { String dstFileHash = StringHelper.getHexString(FileHelper.hashFileSha256(dstFile)); if (!dstFileHash.equals(filePart.getFileHash())) { - throw new RuntimeException("Uploading the file " + filePart.getFileName() - + " failed because the hashes don't match. Expected: " + filePart.getFileHash() + " / Actual: " - + dstFileHash); + String msg = "Uploading the file {0} failed because the hashes don''t match. Expected: {1} / Actual: {2}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, filePart.getFileName(), filePart.getFileHash(), dstFileHash); + throw new RuntimeException(msg); } } + + if (this.verbose) { + String msg; + if (filePart.isLastPart()) + msg = "Wrote {0} for part of file {1}/{2}"; //$NON-NLS-1$ + else + msg = "Wrote {0} for last part of file {1}/{2}"; //$NON-NLS-1$ + String fileSizeS = FileHelper.humanizeFileSize(filePart.getPartBytes().length); + msg = MessageFormat.format(msg, fileSizeS, fileType, fileName); + logger.info(msg); + } } /** * Method with which a client can delete files from the server. It only deletes single files if they exist * * @param fileDeletion - * the {@link RmiFileDeletion} defining the deletion request + * the {@link FileDeletion} defining the deletion request * * @return true if the file was deleted, false if the file did not exist * */ - public boolean deleteFile(RmiFileDeletion fileDeletion) { + public boolean deleteFile(FileDeletion fileDeletion) { // validate file name is legal String fileName = fileDeletion.getFileName(); @@ -232,10 +264,20 @@ public class RmiFileHandler { validateFileType(fileType); // evaluate the path where the file should reside - File fileToDelete = new File(this.basePath + "/" + fileType, fileDeletion.getFileName()); + String fileTypePath = this.basePath + "/" + fileType; //$NON-NLS-1$ + File fileToDelete = new File(fileTypePath, fileDeletion.getFileName()); // delete the file - return FileHelper.deleteFiles(new File[] { fileToDelete }, true); + boolean deletedFile = FileHelper.deleteFiles(new File[] { fileToDelete }, true); + + String msg; + if (deletedFile) + msg = "Deleted file {1}/{2}"; //$NON-NLS-1$ + else + msg = "Failed to delete file {1}/{2}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, fileType, fileName); + logger.info(msg); + return deletedFile; } /** @@ -246,10 +288,10 @@ public class RmiFileHandler { private void validateFileName(String fileName) { if (fileName == null || fileName.isEmpty()) { - throw new RuntimeException("The file name was not given! Can not find a file without a name!"); - } else if (fileName.contains("/")) { - throw new RuntimeException( - "The given file name contains illegal characters. The file name may not contain slashes!"); + throw new RuntimeException("The file name was not given! Can not find a file without a name!"); //$NON-NLS-1$ + } else if (fileName.contains("/")) { //$NON-NLS-1$ + String msg = "The given file name contains illegal characters. The file name may not contain slashes!"; //$NON-NLS-1$ + throw new RuntimeException(msg); } } @@ -260,10 +302,10 @@ public class RmiFileHandler { */ private void validateFileType(String fileType) { if (fileType == null || fileType.isEmpty()) { - throw new RuntimeException("The file type was not given! Can not find a file without a type!"); - } else if (fileType.contains("/")) { - throw new RuntimeException( - "The given file type contains illegal characters. The file type may not contain slashes!"); + throw new RuntimeException("The file type was not given! Can not find a file without a type!"); //$NON-NLS-1$ + } else if (fileType.contains("/")) { //$NON-NLS-1$ + String msg = "The given file type contains illegal characters. The file type may not contain slashes!"; //$NON-NLS-1$ + throw new RuntimeException(msg); } } } diff --git a/src/main/java/ch/eitchnet/rmi/RmiFilePart.java b/src/main/java/ch/eitchnet/fileserver/FilePart.java similarity index 90% rename from src/main/java/ch/eitchnet/rmi/RmiFilePart.java rename to src/main/java/ch/eitchnet/fileserver/FilePart.java index 627fcc373..dfdc19615 100644 --- a/src/main/java/ch/eitchnet/rmi/RmiFilePart.java +++ b/src/main/java/ch/eitchnet/fileserver/FilePart.java @@ -17,16 +17,15 @@ * along with ch.eitchnet.java.utils. If not, see . * */ -package ch.eitchnet.rmi; +package ch.eitchnet.fileserver; import java.io.Serializable; /** * @author Robert von Burg */ -public class RmiFilePart implements Serializable { +public class FilePart implements Serializable { - // private static final long serialVersionUID = 1L; private String fileName; @@ -45,18 +44,18 @@ public class RmiFilePart implements Serializable { * @param fileType * defines the type of file being uploaded or retrieved. This defines in which path the file resides */ - public RmiFilePart(String fileName, String fileType) { + public FilePart(String fileName, String fileType) { if (fileName == null || fileName.isEmpty()) - throw new RuntimeException("fileName may not be empty!"); + throw new RuntimeException("fileName may not be empty!"); //$NON-NLS-1$ if (fileType == null || fileType.isEmpty()) - throw new RuntimeException("fileType may not be empty!"); + throw new RuntimeException("fileType may not be empty!"); //$NON-NLS-1$ this.fileName = fileName; this.fileType = fileType; this.partOffset = 0; - this.partLength = RmiFileHandler.MAX_PART_SIZE; + this.partLength = FileHandler.MAX_PART_SIZE; this.partBytes = null; }