TestFileController.java: Add javadocs

This commit is contained in:
mbarbier 2011-04-28 18:24:04 +00:00
parent 5b5d4769c4
commit e6af4d58b5

View file

@ -8,6 +8,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -55,15 +56,25 @@ public class TestFileController extends FreemarkerHttpServlet {
return "VIVO Harvester Test";
}
private String getHarvesterPath()
/**
* Returns the root location of the VIVO Harvester on this machine.
* @return the root location of the VIVO Harvester on this machine
*/
public static String getHarvesterPath()
{
//todo: complete
return "";
String harvesterPath = "/usr/share/vivo/harvester/"; //todo: hack
return harvesterPath;
}
private String getUploadPathBase(HttpServletRequest request) throws Exception
/**
* Returns the base directory used for all File Harvest uploads.
* @param context the current servlet context
* @return the base directory for file harvest uploads
* @throws Exception if the Vitro home directory could not be found
*/
private String getUploadPathBase(ServletContext context) throws Exception
{
String vitroHomeDirectoryName = ConfigurationProperties.getBean(request.getSession().getServletContext()).getProperty(FileStorageSetup.PROPERTY_VITRO_HOME_DIR);
String vitroHomeDirectoryName = ConfigurationProperties.getBean(context).getProperty(FileStorageSetup.PROPERTY_VITRO_HOME_DIR);
if (vitroHomeDirectoryName == null) {
throw new Exception("Vitro home directory name could not be found.");
}
@ -72,6 +83,12 @@ public class TestFileController extends FreemarkerHttpServlet {
return pathBase;
}
/**
* Gets the FileHarvestJob implementation that is needed to handle the specified request. This
* will depend on the type of harvest being performed (CSV, RefWorks, etc.)
* @param request the request from the browser
* @return the FileHarvestJob that will provide harvest-type-specific services for this request
*/
private FileHarvestJob getJob(HttpServletRequest request)
{
//todo: complete
@ -86,7 +103,7 @@ public class TestFileController extends FreemarkerHttpServlet {
JSONObject json = new JSONObject();
try {
String path = getUploadPathBase(request) + getSessionId(request) + "/";
String path = getUploadPathBase(request.getSession().getServletContext()) + getSessionId(request) + "/";
File directory = new File(path);
@ -116,7 +133,7 @@ public class TestFileController extends FreemarkerHttpServlet {
if(fileStreams.get(PARAMETER_UPLOADED_FILE) != null && fileStreams.get(PARAMETER_UPLOADED_FILE).size() > 0) {
FileItem csvStream = fileStreams.get(PARAMETER_UPLOADED_FILE).get(0);
String name = csvStream.getName();
name = handleNameCollision(path, name, directory);
name = handleNameCollision(name, directory);
File file = new File(path + name);
try {
csvStream.write(file);
@ -175,7 +192,18 @@ public class TestFileController extends FreemarkerHttpServlet {
}
private String handleNameCollision(String path, String filename, File directory) {
/**
* Handles a name conflict in a directory by providing a new name that does not conflict with the
* name of a file already uploaded.
* @param filename the name of the file to be added to the directory
* @param directory the directory where the file should be added, in which to check for files of the
* same name
* @return a filename that does not conflict with any files in the directory. If the filename parameter
* works, then that is returned. Otherwise a number is appended in parentheses to the part of
* the file name prior to the final "." symbol (if one exists).
*/
private String handleNameCollision(String filename, File directory) {
String path = directory.getPath();
String base = filename;
String extension = "";
if(filename.contains(".")) {
@ -193,6 +221,11 @@ public class TestFileController extends FreemarkerHttpServlet {
}
/**
* Returns the ID of the current session between server and browser.
* @param request the request coming in from the browser
* @return the session ID
*/
private String getSessionId(HttpServletRequest request) {
return request.getSession().getId();
}
@ -228,7 +261,9 @@ public class TestFileController extends FreemarkerHttpServlet {
/**
* Provides a way of throwing an exception whose message it is OK to display unedited to the user.
*/
private class ExceptionVisibleToUser extends Exception {
private static final long serialVersionUID = 1L;
public ExceptionVisibleToUser(Throwable cause) {
@ -238,18 +273,35 @@ public class TestFileController extends FreemarkerHttpServlet {
}
/**
* An implementation of FileHarvestJob that can be used for any CSV file harvest.
*/
class CsvHarvestJob implements FileHarvestJob {
/**
* Logger.
*/
private static final Log log = LogFactory.getLog(CsvHarvestJob.class);
/**
* The template file against which uploaded CSV files will be validated.
*/
private File templateFile;
/**
* Constructor.
* @param templateFileName just the name of the template file. The directory is assumed to be standard.
*/
public CsvHarvestJob(String templateFileName) {
templateFile = new File(getTemplateFileDirectory() + templateFileName);
}
/**
* Gets the path to the directory containing the template files.
* @return the path to the directory containing the template files
*/
private String getTemplateFileDirectory() {
String harvesterPath = "/usr/share/vivo/harvester/"; //todo: hack
String harvesterPath = TestFileController.getHarvesterPath();
String pathToTemplateFiles = harvesterPath + "files/";
return pathToTemplateFiles;
}
@ -291,6 +343,14 @@ class CsvHarvestJob implements FileHarvestJob {
return null;
}
/**
* Makes sure that the first line of the CSV file is identical to the first line of the template file. This is
* assuming we are expecting all user CSV files to contain an initial header line. If this is not the case, then
* this method is unnecessary.
* @param templateFirstLine the parsed-out contents of the first line of the template file
* @param line the parsed-out contents of the first line of the input file
* @return an error message if the two lines don't match, or null if they do
*/
private String validateCsvFirstLine(String[] templateFirstLine, String[] line) {
String errorMessage = "File header does not match specification";
if(line.length != templateFirstLine.length)
@ -302,7 +362,6 @@ class CsvHarvestJob implements FileHarvestJob {
}
return null;
}
}