take 2 on fixing the EntityController merge

This commit is contained in:
bjl23 2010-10-01 19:24:22 +00:00
parent 96f5c225f2
commit e2e145954f

View file

@ -41,10 +41,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao; import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyDao; import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyDao;
import edu.cornell.mannlib.vitro.webapp.filestorage.FileModelHelper; import edu.cornell.mannlib.vitro.webapp.filestorage.model.FileInfo;
import edu.cornell.mannlib.vitro.webapp.filestorage.FileServingHelper;
import edu.cornell.mannlib.vitro.webapp.filestorage.backend.FileStorage;
import edu.cornell.mannlib.vitro.webapp.filestorage.backend.FileStorageSetup;
import edu.cornell.mannlib.vitro.webapp.search.beans.VitroQuery; import edu.cornell.mannlib.vitro.webapp.search.beans.VitroQuery;
import edu.cornell.mannlib.vitro.webapp.search.beans.VitroQueryWrapper; import edu.cornell.mannlib.vitro.webapp.search.beans.VitroQueryWrapper;
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper; import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper;
@ -102,7 +99,7 @@ public class EntityController extends VitroHttpServlet {
} }
// If this is an uploaded file, redirect to its "alias URL". // If this is an uploaded file, redirect to its "alias URL".
String aliasUrl = getAliasUrlForBytestreamIndividual(indiv); String aliasUrl = getAliasUrlForBytestreamIndividual(req, indiv);
if (aliasUrl != null) { if (aliasUrl != null) {
res.sendRedirect(req.getContextPath() + aliasUrl); res.sendRedirect(req.getContextPath() + aliasUrl);
return; return;
@ -230,7 +227,7 @@ public class EntityController extends VitroHttpServlet {
if( indiv.getURI().startsWith( vreq.getWebappDaoFactory().getDefaultNamespace() )){ if( indiv.getURI().startsWith( vreq.getWebappDaoFactory().getDefaultNamespace() )){
vreq.setAttribute("entityLinkedDataURL", indiv.getURI() + "/" + indiv.getLocalName() + ".rdf"); vreq.setAttribute("entityLinkedDataURL", indiv.getURI() + "/" + indiv.getLocalName() + ".rdf");
} }
vreq.setAttribute("css",css); vreq.setAttribute("css",css);
vreq.setAttribute("scripts", "/templates/entity/entity_inject_head.jsp"); vreq.setAttribute("scripts", "/templates/entity/entity_inject_head.jsp");
@ -269,21 +266,14 @@ public class EntityController extends VitroHttpServlet {
newModel.write( res.getOutputStream(), format ); newModel.write( res.getOutputStream(), format );
} }
private void doRedirect(HttpServletRequest req, HttpServletResponse res, private void doRedirect(HttpServletRequest req, HttpServletResponse res,
String redirectURL) { String redirectURL) {
//It seems like there must be a more standard way to do a redirect in tomcat. // It seems like there must be a better way to do this
String hn = req.getHeader("Host"); String hn = req.getHeader("Host");
if (req.isSecure()) { res.setHeader("Location", res.encodeURL( "http://" + hn + req.getContextPath() + redirectURL ));
res.setHeader("Location", res.encodeURL("https://" + hn res.setStatus(res.SC_SEE_OTHER);
+ req.getContextPath() + redirectURL)); }
log.info("doRedirect by using HTTPS");
} else {
res.setHeader("Location", res.encodeURL("http://" + hn
+ req.getContextPath() + redirectURL));
log.info("doRedirect by using HTTP");
}
res.setStatus(res.SC_SEE_OTHER);
}
private static Pattern LINKED_DATA_URL = Pattern.compile("^/individual/([^/]*)$"); private static Pattern LINKED_DATA_URL = Pattern.compile("^/individual/([^/]*)$");
private static Pattern NS_PREFIX_URL = Pattern.compile("^/individual/([^/]*)/([^/]*)$"); private static Pattern NS_PREFIX_URL = Pattern.compile("^/individual/([^/]*)/([^/]*)$");
@ -491,39 +481,16 @@ public class EntityController extends VitroHttpServlet {
* If this entity represents a File Bytestream, get its alias URL so we can * If this entity represents a File Bytestream, get its alias URL so we can
* properly serve the file contents. * properly serve the file contents.
*/ */
private String getAliasUrlForBytestreamIndividual(Individual entity) private String getAliasUrlForBytestreamIndividual(HttpServletRequest req, Individual entity)
throws IOException { throws IOException {
if (!FileModelHelper.isFileBytestream(entity)) { FileInfo fileInfo = FileInfo.instanceFromBytestreamUri(new VitroRequest(
log.debug("Entity at '" + entity.getURI() req).getWebappDaoFactory(), entity.getURI());
+ "' is not recognized as a FileByteStream."); if (fileInfo == null) {
return null; log.trace("Entity '" + entity.getURI() + "' is not a bytestream.");
}
FileStorage fs = (FileStorage) getServletContext().getAttribute(
FileStorageSetup.ATTRIBUTE_NAME);
if (fs == null) {
log.error("Servlet context does not contain file storage at '"
+ FileStorageSetup.ATTRIBUTE_NAME + "'");
return null;
}
String filename = fs.getFilename(entity.getURI());
if (filename == null) {
log.error("Entity at '" + entity.getURI()
+ "' is recognized as a FileByteStream, "
+ "but the file system does not recognize it.");
return null;
}
String url = FileServingHelper.getBytestreamAliasUrl(entity.getURI(),
filename);
if (url.equals(entity.getURI())) {
log.error("Entity at '" + entity.getURI()
+ "' is recognized as a FileByteStream, "
+ "but can't be translated to an alias URL.");
return null; return null;
} }
String url = fileInfo.getBytestreamAliasUrl();
log.debug("Alias URL for '" + entity.getURI() + "' is '" + url + "'"); log.debug("Alias URL for '" + entity.getURI() + "' is '" + url + "'");
return url; return url;
} }