NIHVIVO-1261 Pass the ServletContext down the chain to FileServingHelper, to prepare for the change in ConfigurationProperties.

This commit is contained in:
jeb228 2011-02-24 17:32:40 +00:00
parent ec12d6c743
commit f16c1826be
9 changed files with 58 additions and 35 deletions

View file

@ -240,7 +240,7 @@ public class ImageUploadController extends FreemarkerHttpServlet {
*/ */
private ResponseValues doUploadImage(VitroRequest vreq, Individual entity) { private ResponseValues doUploadImage(VitroRequest vreq, Individual entity) {
ImageUploadHelper helper = new ImageUploadHelper(fileStorage, ImageUploadHelper helper = new ImageUploadHelper(fileStorage,
vreq.getFullWebappDaoFactory()); vreq.getFullWebappDaoFactory(), getServletContext());
try { try {
// Did they provide a file to upload? If not, show an error. // 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, private ResponseValues doCreateThumbnail(VitroRequest vreq,
Individual entity) { Individual entity) {
ImageUploadHelper helper = new ImageUploadHelper(fileStorage, ImageUploadHelper helper = new ImageUploadHelper(fileStorage,
vreq.getFullWebappDaoFactory()); vreq.getFullWebappDaoFactory(), getServletContext());
try { try {
CropRectangle crop = validateCropCoordinates(vreq); CropRectangle crop = validateCropCoordinates(vreq);
@ -305,7 +305,7 @@ public class ImageUploadController extends FreemarkerHttpServlet {
*/ */
private ResponseValues doDeleteImage(VitroRequest vreq, Individual entity) { private ResponseValues doDeleteImage(VitroRequest vreq, Individual entity) {
ImageUploadHelper helper = new ImageUploadHelper(fileStorage, ImageUploadHelper helper = new ImageUploadHelper(fileStorage,
vreq.getFullWebappDaoFactory()); vreq.getFullWebappDaoFactory(), getServletContext());
helper.removeExistingImage(entity); helper.removeExistingImage(entity);
@ -318,7 +318,7 @@ public class ImageUploadController extends FreemarkerHttpServlet {
*/ */
private ResponseValues doDeleteThenEdit(VitroRequest vreq, Individual entity) { private ResponseValues doDeleteThenEdit(VitroRequest vreq, Individual entity) {
ImageUploadHelper helper = new ImageUploadHelper(fileStorage, ImageUploadHelper helper = new ImageUploadHelper(fileStorage,
vreq.getFullWebappDaoFactory()); vreq.getFullWebappDaoFactory(), getServletContext());
helper.removeExistingImage(entity); 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 // 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 // knows which placeholder it's using. However, this requires a significantly more
// complex implementation, so keeping it simple for now. // complex implementation, so keeping it simple for now.
String dummyThumbnailUrl = entity.isVClass("http://xmlns.com/foaf/0.1/Person") ? boolean isPerson = (entity != null)
DUMMY_THUMBNAIL_PERSON_URL : DUMMY_THUMBNAIL_NON_PERSON_URL; && 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_THUMBNAIL_URL, UrlBuilder.getUrl(dummyThumbnailUrl));
rv.put(BODY_FORM_ACTION, formAction); rv.put(BODY_FORM_ACTION, formAction);
rv.put(BODY_CANCEL_URL, cancelUrl); rv.put(BODY_CANCEL_URL, cancelUrl);

View file

@ -20,6 +20,7 @@ import java.util.Map;
import javax.media.jai.JAI; import javax.media.jai.JAI;
import javax.media.jai.RenderedOp; import javax.media.jai.RenderedOp;
import javax.media.jai.util.ImagingListener; import javax.media.jai.util.ImagingListener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItem;
@ -100,10 +101,11 @@ public class ImageUploadHelper {
private final FileStorage fileStorage; private final FileStorage fileStorage;
private final UploadedFileHelper uploadedFileHelper; private final UploadedFileHelper uploadedFileHelper;
ImageUploadHelper(FileStorage fileStorage, WebappDaoFactory webAppDaoFactory) { ImageUploadHelper(FileStorage fileStorage,
WebappDaoFactory webAppDaoFactory, ServletContext ctx) {
this.fileStorage = fileStorage; this.fileStorage = fileStorage;
this.uploadedFileHelper = new UploadedFileHelper(fileStorage, this.uploadedFileHelper = new UploadedFileHelper(fileStorage,
webAppDaoFactory); webAppDaoFactory, ctx);
} }
/** /**

View file

@ -5,6 +5,8 @@ package edu.cornell.mannlib.vitro.webapp.filestorage;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -19,13 +21,13 @@ public class FileServingHelper {
private static final String DEFAULT_PATH = "/individual/"; private static final String DEFAULT_PATH = "/individual/";
private static final String FILE_PATH = "/file/"; 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, * Get the default namespace from the configuration properties, and trim off
* and trim off the suffix. * the suffix.
*/ */
private static String initializeDefaultNamespace() { private static String getDefaultNamespace(ServletContext ctx) {
String defaultNamespace = ConfigurationProperties String defaultNamespace = ConfigurationProperties
.getProperty(FileStorageSetup.PROPERTY_DEFAULT_NAMESPACE); .getProperty(FileStorageSetup.PROPERTY_DEFAULT_NAMESPACE);
if (defaultNamespace == null) { if (defaultNamespace == null) {
@ -35,8 +37,11 @@ public class FileServingHelper {
} }
if (!defaultNamespace.endsWith(DEFAULT_PATH)) { if (!defaultNamespace.endsWith(DEFAULT_PATH)) {
if (!warned) {
log.warn("Default namespace does not match the expected form: '" log.warn("Default namespace does not match the expected form: '"
+ defaultNamespace + "'"); + defaultNamespace + "'");
warned = true;
}
} }
return defaultNamespace; return defaultNamespace;
@ -60,16 +65,20 @@ public class FileServingHelper {
* <li>null, if the original URI or the filename was null.</li> * <li>null, if the original URI or the filename was null.</li>
* </ul> * </ul>
*/ */
public static String getBytestreamAliasUrl(String uri, String filename) { public static String getBytestreamAliasUrl(String uri, String filename,
ServletContext ctx) {
if ((uri == null) || (filename == null)) { if ((uri == null) || (filename == null)) {
return 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 log.warn("uri does not start with the default namespace: '" + uri
+ "'"); + "'");
return uri; return uri;
} }
String remainder = uri.substring(DEFAULT_NAMESPACE.length()); String remainder = uri.substring(defaultNamespace.length());
try { try {
filename = URLEncoder.encode(filename, "UTF-8"); filename = URLEncoder.encode(filename, "UTF-8");
@ -98,7 +107,7 @@ public class FileServingHelper {
* *
* @return the URI, or <code>null</code> if the URL couldn't be translated. * @return the URI, or <code>null</code> if the URL couldn't be translated.
*/ */
public static String getBytestreamUri(String path) { public static String getBytestreamUri(String path, ServletContext ctx) {
if (path == null) { if (path == null) {
return null; return null;
} }
@ -115,6 +124,6 @@ public class FileServingHelper {
} }
remainder = remainder.substring(0, slashHere); remainder = remainder.substring(0, slashHere);
return DEFAULT_NAMESPACE + remainder; return getDefaultNamespace(ctx) + remainder;
} }
} }

View file

@ -6,6 +6,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -37,13 +39,15 @@ public class UploadedFileHelper {
private final IndividualDao individualDao; private final IndividualDao individualDao;
private final DataPropertyStatementDao dataPropertyStatementDao; private final DataPropertyStatementDao dataPropertyStatementDao;
private final ObjectPropertyStatementDao objectPropertyStatementDao; 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.fileStorage = fileStorage;
this.wadf = wadf; this.wadf = wadf;
this.individualDao = wadf.getIndividualDao(); this.individualDao = wadf.getIndividualDao();
this.dataPropertyStatementDao = wadf.getDataPropertyStatementDao(); this.dataPropertyStatementDao = wadf.getDataPropertyStatementDao();
this.objectPropertyStatementDao = wadf.getObjectPropertyStatementDao(); this.objectPropertyStatementDao = wadf.getObjectPropertyStatementDao();
this.ctx = ctx;
} }
/** /**
@ -170,7 +174,7 @@ public class UploadedFileHelper {
dataPropertyStatementDao dataPropertyStatementDao
.insertNewDataPropertyStatement(new DataPropertyStatementImpl( .insertNewDataPropertyStatement(new DataPropertyStatementImpl(
uri, VitroVocabulary.FS_ALIAS_URL, FileServingHelper uri, VitroVocabulary.FS_ALIAS_URL, FileServingHelper
.getBytestreamAliasUrl(uri, filename))); .getBytestreamAliasUrl(uri, filename, ctx)));
return individualDao.getIndividualByURI(uri); return individualDao.getIndividualByURI(uri);
} }

View file

@ -4,6 +4,8 @@ package edu.cornell.mannlib.vitro.webapp.filestorage.model;
import java.util.List; import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -32,8 +34,8 @@ public class FileInfo {
* URI, find the surrogate, and get the info. Otherwise, return null. * URI, find the surrogate, and get the info. Otherwise, return null.
*/ */
public static FileInfo instanceFromAliasUrl( public static FileInfo instanceFromAliasUrl(
WebappDaoFactory webappDaoFactory, String path) { WebappDaoFactory webappDaoFactory, String path, ServletContext ctx) {
String bytestreamUri = FileServingHelper.getBytestreamUri(path); String bytestreamUri = FileServingHelper.getBytestreamUri(path, ctx);
if (bytestreamUri == null) { if (bytestreamUri == null) {
return null; return null;
} }

View file

@ -78,8 +78,8 @@ public class FileServingServlet extends VitroHttpServlet {
String path = request.getServletPath() + request.getPathInfo(); String path = request.getServletPath() + request.getPathInfo();
log.debug("Path is '" + path + "'"); log.debug("Path is '" + path + "'");
FileInfo fileInfo = FileInfo.instanceFromAliasUrl(request FileInfo fileInfo = FileInfo.instanceFromAliasUrl(
.getFullWebappDaoFactory(), path); request.getFullWebappDaoFactory(), path, getServletContext());
log.debug("File info is '" + fileInfo + "'"); log.debug("File info is '" + fileInfo + "'");
if (fileInfo == null) { if (fileInfo == null) {
String message = "The request path is not valid for the File servlet: '" 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. // Open the actual byte stream.
InputStream in; InputStream in;
try { try {
in = fileStorage.getInputStream(fileInfo.getBytestreamUri(), actualFilename); in = fileStorage.getInputStream(fileInfo.getBytestreamUri(),
actualFilename);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
log.error(e, e); log.error(e, e);
response.sendError(SC_INTERNAL_SERVER_ERROR, e.toString()); response.sendError(SC_INTERNAL_SERVER_ERROR, e.toString());

View file

@ -6,6 +6,8 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -112,10 +114,10 @@ public class FileStorageUpdater implements FSUController {
public FileStorageUpdater(WebappDaoFactory wadf, Model model, public FileStorageUpdater(WebappDaoFactory wadf, Model model,
FileStorage fileStorage, File uploadDirectory, FileStorage fileStorage, File uploadDirectory,
File webappImageDirectory) { File webappImageDirectory, ServletContext ctx) {
this.model = model; this.model = model;
this.fileStorage = fileStorage; this.fileStorage = fileStorage;
this.uploadedFileHelper = new UploadedFileHelper(fileStorage, wadf); this.uploadedFileHelper = new UploadedFileHelper(fileStorage, wadf, ctx);
this.upgradeDirectory = new File(uploadDirectory, "upgrade"); this.upgradeDirectory = new File(uploadDirectory, "upgrade");
this.imageDirectoryWithBackup = new ImageDirectoryWithBackup(new File( this.imageDirectoryWithBackup = new ImageDirectoryWithBackup(new File(

View file

@ -120,7 +120,7 @@ public class UpdateUploadedFiles implements ServletContextListener {
* Update from old-style storage to new-style storage. * Update from old-style storage to new-style storage.
*/ */
FileStorageUpdater fsu = new FileStorageUpdater(wadf, jenaOntModel, FileStorageUpdater fsu = new FileStorageUpdater(wadf, jenaOntModel,
fileStorage, uploadDirectory, webappImageDirectory); fileStorage, uploadDirectory, webappImageDirectory, ctx);
fsu.update(); fsu.update();
/* /*

View file

@ -113,7 +113,7 @@ public class FileServingHelperTest extends AbstractTestClass {
} }
private void assertCorrectUrl(String uri, String filename, String expected) { 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); assertEquals("url", expected, actual);
} }