From f16c1826bedf64641d736c0ad779c68cc44a09c5 Mon Sep 17 00:00:00 2001 From: jeb228 Date: Thu, 24 Feb 2011 17:32:40 +0000 Subject: [PATCH] NIHVIVO-1261 Pass the ServletContext down the chain to FileServingHelper, to prepare for the change in ConfigurationProperties. --- .../freemarker/ImageUploadController.java | 15 +++++---- .../freemarker/ImageUploadHelper.java | 6 ++-- .../webapp/filestorage/FileServingHelper.java | 31 ++++++++++++------- .../filestorage/UploadedFileHelper.java | 8 +++-- .../webapp/filestorage/model/FileInfo.java | 6 ++-- .../serving/FileServingServlet.java | 7 +++-- .../updater/FileStorageUpdater.java | 6 ++-- .../servlet/setup/UpdateUploadedFiles.java | 12 +++---- .../filestorage/FileServingHelperTest.java | 2 +- 9 files changed, 58 insertions(+), 35 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadController.java index 7890b2de1..edc1c42bc 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadController.java @@ -240,7 +240,7 @@ public class ImageUploadController extends FreemarkerHttpServlet { */ private ResponseValues doUploadImage(VitroRequest vreq, Individual entity) { ImageUploadHelper helper = new ImageUploadHelper(fileStorage, - vreq.getFullWebappDaoFactory()); + vreq.getFullWebappDaoFactory(), getServletContext()); try { // Did they provide a file to upload? If not, show an error. @@ -283,7 +283,7 @@ public class ImageUploadController extends FreemarkerHttpServlet { private ResponseValues doCreateThumbnail(VitroRequest vreq, Individual entity) { ImageUploadHelper helper = new ImageUploadHelper(fileStorage, - vreq.getFullWebappDaoFactory()); + vreq.getFullWebappDaoFactory(), getServletContext()); try { CropRectangle crop = validateCropCoordinates(vreq); @@ -305,7 +305,7 @@ public class ImageUploadController extends FreemarkerHttpServlet { */ private ResponseValues doDeleteImage(VitroRequest vreq, Individual entity) { ImageUploadHelper helper = new ImageUploadHelper(fileStorage, - vreq.getFullWebappDaoFactory()); + vreq.getFullWebappDaoFactory(), getServletContext()); helper.removeExistingImage(entity); @@ -318,7 +318,7 @@ public class ImageUploadController extends FreemarkerHttpServlet { */ private ResponseValues doDeleteThenEdit(VitroRequest vreq, Individual entity) { ImageUploadHelper helper = new ImageUploadHelper(fileStorage, - vreq.getFullWebappDaoFactory()); + vreq.getFullWebappDaoFactory(), getServletContext()); helper.removeExistingImage(entity); @@ -395,8 +395,11 @@ public class ImageUploadController extends FreemarkerHttpServlet { // the template would add the placeholder url to the edit link, since it already // knows which placeholder it's using. However, this requires a significantly more // complex implementation, so keeping it simple for now. - String dummyThumbnailUrl = entity.isVClass("http://xmlns.com/foaf/0.1/Person") ? - DUMMY_THUMBNAIL_PERSON_URL : DUMMY_THUMBNAIL_NON_PERSON_URL; + boolean isPerson = (entity != null) + && entity.isVClass("http://xmlns.com/foaf/0.1/Person"); + String dummyThumbnailUrl = isPerson ? DUMMY_THUMBNAIL_PERSON_URL + : DUMMY_THUMBNAIL_NON_PERSON_URL; + rv.put(BODY_THUMBNAIL_URL, UrlBuilder.getUrl(dummyThumbnailUrl)); rv.put(BODY_FORM_ACTION, formAction); rv.put(BODY_CANCEL_URL, cancelUrl); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java index c4f2114a2..656a0f0a7 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/ImageUploadHelper.java @@ -20,6 +20,7 @@ import java.util.Map; import javax.media.jai.JAI; import javax.media.jai.RenderedOp; import javax.media.jai.util.ImagingListener; +import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.FileItem; @@ -100,10 +101,11 @@ public class ImageUploadHelper { private final FileStorage fileStorage; private final UploadedFileHelper uploadedFileHelper; - ImageUploadHelper(FileStorage fileStorage, WebappDaoFactory webAppDaoFactory) { + ImageUploadHelper(FileStorage fileStorage, + WebappDaoFactory webAppDaoFactory, ServletContext ctx) { this.fileStorage = fileStorage; this.uploadedFileHelper = new UploadedFileHelper(fileStorage, - webAppDaoFactory); + webAppDaoFactory, ctx); } /** diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/FileServingHelper.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/FileServingHelper.java index 2bb1b2838..c684f7180 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/FileServingHelper.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/FileServingHelper.java @@ -5,6 +5,8 @@ package edu.cornell.mannlib.vitro.webapp.filestorage; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import javax.servlet.ServletContext; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -19,13 +21,13 @@ public class FileServingHelper { private static final String DEFAULT_PATH = "/individual/"; private static final String FILE_PATH = "/file/"; - private static final String DEFAULT_NAMESPACE = initializeDefaultNamespace(); + private static boolean warned; // Only issue the warning once. /** - * At startup, get the default namespace from the configuration properties, - * and trim off the suffix. + * Get the default namespace from the configuration properties, and trim off + * the suffix. */ - private static String initializeDefaultNamespace() { + private static String getDefaultNamespace(ServletContext ctx) { String defaultNamespace = ConfigurationProperties .getProperty(FileStorageSetup.PROPERTY_DEFAULT_NAMESPACE); if (defaultNamespace == null) { @@ -35,8 +37,11 @@ public class FileServingHelper { } if (!defaultNamespace.endsWith(DEFAULT_PATH)) { - log.warn("Default namespace does not match the expected form: '" - + defaultNamespace + "'"); + if (!warned) { + log.warn("Default namespace does not match the expected form: '" + + defaultNamespace + "'"); + warned = true; + } } return defaultNamespace; @@ -60,16 +65,20 @@ public class FileServingHelper { *
  • null, if the original URI or the filename was null.
  • * */ - public static String getBytestreamAliasUrl(String uri, String filename) { + public static String getBytestreamAliasUrl(String uri, String filename, + ServletContext ctx) { if ((uri == null) || (filename == null)) { return null; } - if (!uri.startsWith(DEFAULT_NAMESPACE)) { + + String defaultNamespace = getDefaultNamespace(ctx); + + if (!uri.startsWith(defaultNamespace)) { log.warn("uri does not start with the default namespace: '" + uri + "'"); return uri; } - String remainder = uri.substring(DEFAULT_NAMESPACE.length()); + String remainder = uri.substring(defaultNamespace.length()); try { filename = URLEncoder.encode(filename, "UTF-8"); @@ -98,7 +107,7 @@ public class FileServingHelper { * * @return the URI, or null if the URL couldn't be translated. */ - public static String getBytestreamUri(String path) { + public static String getBytestreamUri(String path, ServletContext ctx) { if (path == null) { return null; } @@ -115,6 +124,6 @@ public class FileServingHelper { } remainder = remainder.substring(0, slashHere); - return DEFAULT_NAMESPACE + remainder; + return getDefaultNamespace(ctx) + remainder; } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/UploadedFileHelper.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/UploadedFileHelper.java index f10e5a50e..30568bff0 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/UploadedFileHelper.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/UploadedFileHelper.java @@ -6,6 +6,8 @@ import java.io.IOException; import java.io.InputStream; import java.util.List; +import javax.servlet.ServletContext; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -37,13 +39,15 @@ public class UploadedFileHelper { private final IndividualDao individualDao; private final DataPropertyStatementDao dataPropertyStatementDao; private final ObjectPropertyStatementDao objectPropertyStatementDao; + private final ServletContext ctx; - public UploadedFileHelper(FileStorage fileStorage, WebappDaoFactory wadf) { + public UploadedFileHelper(FileStorage fileStorage, WebappDaoFactory wadf, ServletContext ctx) { this.fileStorage = fileStorage; this.wadf = wadf; this.individualDao = wadf.getIndividualDao(); this.dataPropertyStatementDao = wadf.getDataPropertyStatementDao(); this.objectPropertyStatementDao = wadf.getObjectPropertyStatementDao(); + this.ctx = ctx; } /** @@ -170,7 +174,7 @@ public class UploadedFileHelper { dataPropertyStatementDao .insertNewDataPropertyStatement(new DataPropertyStatementImpl( uri, VitroVocabulary.FS_ALIAS_URL, FileServingHelper - .getBytestreamAliasUrl(uri, filename))); + .getBytestreamAliasUrl(uri, filename, ctx))); return individualDao.getIndividualByURI(uri); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/model/FileInfo.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/model/FileInfo.java index 2504c1878..2da25037b 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/model/FileInfo.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/model/FileInfo.java @@ -4,6 +4,8 @@ package edu.cornell.mannlib.vitro.webapp.filestorage.model; import java.util.List; +import javax.servlet.ServletContext; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -32,8 +34,8 @@ public class FileInfo { * URI, find the surrogate, and get the info. Otherwise, return null. */ public static FileInfo instanceFromAliasUrl( - WebappDaoFactory webappDaoFactory, String path) { - String bytestreamUri = FileServingHelper.getBytestreamUri(path); + WebappDaoFactory webappDaoFactory, String path, ServletContext ctx) { + String bytestreamUri = FileServingHelper.getBytestreamUri(path, ctx); if (bytestreamUri == null) { return null; } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/serving/FileServingServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/serving/FileServingServlet.java index eca034c65..6feb5c1c6 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/serving/FileServingServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/serving/FileServingServlet.java @@ -78,8 +78,8 @@ public class FileServingServlet extends VitroHttpServlet { String path = request.getServletPath() + request.getPathInfo(); log.debug("Path is '" + path + "'"); - FileInfo fileInfo = FileInfo.instanceFromAliasUrl(request - .getFullWebappDaoFactory(), path); + FileInfo fileInfo = FileInfo.instanceFromAliasUrl( + request.getFullWebappDaoFactory(), path, getServletContext()); log.debug("File info is '" + fileInfo + "'"); if (fileInfo == null) { String message = "The request path is not valid for the File servlet: '" @@ -107,7 +107,8 @@ public class FileServingServlet extends VitroHttpServlet { // Open the actual byte stream. InputStream in; try { - in = fileStorage.getInputStream(fileInfo.getBytestreamUri(), actualFilename); + in = fileStorage.getInputStream(fileInfo.getBytestreamUri(), + actualFilename); } catch (FileNotFoundException e) { log.error(e, e); response.sendError(SC_INTERNAL_SERVER_ERROR, e.toString()); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/updater/FileStorageUpdater.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/updater/FileStorageUpdater.java index 9b8172fd5..b31ae17e4 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/updater/FileStorageUpdater.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/filestorage/updater/FileStorageUpdater.java @@ -6,6 +6,8 @@ import java.io.File; import java.io.IOException; import java.util.Collection; +import javax.servlet.ServletContext; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -112,10 +114,10 @@ public class FileStorageUpdater implements FSUController { public FileStorageUpdater(WebappDaoFactory wadf, Model model, FileStorage fileStorage, File uploadDirectory, - File webappImageDirectory) { + File webappImageDirectory, ServletContext ctx) { this.model = model; this.fileStorage = fileStorage; - this.uploadedFileHelper = new UploadedFileHelper(fileStorage, wadf); + this.uploadedFileHelper = new UploadedFileHelper(fileStorage, wadf, ctx); this.upgradeDirectory = new File(uploadDirectory, "upgrade"); this.imageDirectoryWithBackup = new ImageDirectoryWithBackup(new File( diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateUploadedFiles.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateUploadedFiles.java index 0237772c8..1a12745e7 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateUploadedFiles.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateUploadedFiles.java @@ -48,11 +48,11 @@ public class UpdateUploadedFiles implements ServletContextListener { */ @Override public void contextInitialized(ServletContextEvent sce) { - - if (AbortStartup.isStartupAborted(sce.getServletContext())) { - return; - } - + + if (AbortStartup.isStartupAborted(sce.getServletContext())) { + return; + } + try { ServletContext ctx = sce.getServletContext(); @@ -120,7 +120,7 @@ public class UpdateUploadedFiles implements ServletContextListener { * Update from old-style storage to new-style storage. */ FileStorageUpdater fsu = new FileStorageUpdater(wadf, jenaOntModel, - fileStorage, uploadDirectory, webappImageDirectory); + fileStorage, uploadDirectory, webappImageDirectory, ctx); fsu.update(); /* diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/filestorage/FileServingHelperTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/filestorage/FileServingHelperTest.java index 27ac87fee..7ba75361e 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/filestorage/FileServingHelperTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/filestorage/FileServingHelperTest.java @@ -113,7 +113,7 @@ public class FileServingHelperTest extends AbstractTestClass { } private void assertCorrectUrl(String uri, String filename, String expected) { - String actual = FileServingHelper.getBytestreamAliasUrl(uri, filename); + String actual = FileServingHelper.getBytestreamAliasUrl(uri, filename, null); assertEquals("url", expected, actual); }