NIHVIVO-1261 Pass the ServletContext down the chain to FileServingHelper, to prepare for the change in ConfigurationProperties.
This commit is contained in:
parent
ec12d6c743
commit
f16c1826be
9 changed files with 58 additions and 35 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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)) {
|
||||
if (!warned) {
|
||||
log.warn("Default namespace does not match the expected form: '"
|
||||
+ defaultNamespace + "'");
|
||||
warned = true;
|
||||
}
|
||||
}
|
||||
|
||||
return defaultNamespace;
|
||||
|
@ -60,16 +65,20 @@ public class FileServingHelper {
|
|||
* <li>null, if the original URI or the filename was null.</li>
|
||||
* </ul>
|
||||
*/
|
||||
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 <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) {
|
||||
return null;
|
||||
}
|
||||
|
@ -115,6 +124,6 @@ public class FileServingHelper {
|
|||
}
|
||||
remainder = remainder.substring(0, slashHere);
|
||||
|
||||
return DEFAULT_NAMESPACE + remainder;
|
||||
return getDefaultNamespace(ctx) + remainder;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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();
|
||||
|
||||
/*
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue