Adding linked data. NIHVIVO-49
This commit is contained in:
parent
619f0dc646
commit
5da0eeff46
3 changed files with 398 additions and 243 deletions
|
@ -4,8 +4,11 @@ package edu.cornell.mannlib.vitro.webapp.controller;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.io.Writer;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.servlet.RequestDispatcher;
|
import javax.servlet.RequestDispatcher;
|
||||||
|
@ -13,6 +16,7 @@ import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletOutputStream;
|
import javax.servlet.ServletOutputStream;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
@ -20,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
|
||||||
import com.hp.hpl.jena.ontology.OntModel;
|
import com.hp.hpl.jena.ontology.OntModel;
|
||||||
import com.hp.hpl.jena.rdf.model.Literal;
|
import com.hp.hpl.jena.rdf.model.Literal;
|
||||||
import com.hp.hpl.jena.rdf.model.Model;
|
import com.hp.hpl.jena.rdf.model.Model;
|
||||||
|
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||||
import com.hp.hpl.jena.rdf.model.ModelMaker;
|
import com.hp.hpl.jena.rdf.model.ModelMaker;
|
||||||
import com.hp.hpl.jena.rdf.model.Property;
|
import com.hp.hpl.jena.rdf.model.Property;
|
||||||
import com.hp.hpl.jena.rdf.model.Resource;
|
import com.hp.hpl.jena.rdf.model.Resource;
|
||||||
|
@ -35,7 +40,9 @@ 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.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.web.EntityWebUtils;
|
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapperFactory;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.web.ContentType;
|
||||||
import edu.cornell.mannlib.vitro.webapp.web.jsptags.StringProcessorTag;
|
import edu.cornell.mannlib.vitro.webapp.web.jsptags.StringProcessorTag;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -52,38 +59,55 @@ public class EntityController extends VitroHttpServlet {
|
||||||
private String default_body_jsp = Controllers.ENTITY_JSP;
|
private String default_body_jsp = Controllers.ENTITY_JSP;
|
||||||
private ApplicationBean appBean;
|
private ApplicationBean appBean;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author bdc34
|
|
||||||
*/
|
|
||||||
public void doGet( HttpServletRequest req, HttpServletResponse res )
|
public void doGet( HttpServletRequest req, HttpServletResponse res )
|
||||||
throws IOException, ServletException {
|
throws IOException, ServletException {
|
||||||
try {
|
try {
|
||||||
super.doGet(req, res);
|
super.doGet(req, res);
|
||||||
|
|
||||||
log.debug("In doGet");
|
|
||||||
|
|
||||||
VitroRequest vreq = new VitroRequest(req);
|
VitroRequest vreq = new VitroRequest(req);
|
||||||
IndividualDao iwDao = vreq.getWebappDaoFactory().getIndividualDao();
|
//get URL without hostname or servlet context
|
||||||
ObjectPropertyDao opDao = vreq.getWebappDaoFactory().getObjectPropertyDao();
|
String url = req.getRequestURI().substring(req.getContextPath().length());
|
||||||
|
|
||||||
Individual entity = null;
|
//Check to see if the request is for a non-information resource, redirect if it is.
|
||||||
|
String redirectURL = checkForRedirect ( url, req.getHeader("accept") );
|
||||||
|
if( redirectURL != null ){
|
||||||
|
doRedirect( req, res, redirectURL );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ContentType rdfFormat = checkForLinkedDataRequest(url,req.getHeader("accept"));
|
||||||
|
if( rdfFormat != null ){
|
||||||
|
doRdf( vreq, res, rdfFormat );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Individual indiv = null;
|
||||||
try{
|
try{
|
||||||
entity = EntityWebUtils.getEntityFromRequest(vreq);
|
indiv = getEntityFromRequest( vreq);
|
||||||
}catch(Throwable th){
|
}catch(Throwable th){
|
||||||
doHelp(res);
|
doHelp(res);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if( entity == null ){
|
|
||||||
doNotFound(req, res );
|
if( indiv == null || checkForHidden(vreq, indiv) || checkForSunset(vreq, indiv)){
|
||||||
|
doNotFound(vreq, res);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( checkForHidden( vreq, entity ) ||
|
doHtml( vreq, res , indiv);
|
||||||
checkForSunset( vreq, entity ) ){
|
|
||||||
doNotFound( req, res);
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(e);
|
||||||
|
req.setAttribute("javax.servlet.jsp.jspException",e);
|
||||||
|
RequestDispatcher rd = req.getRequestDispatcher("/error.jsp");
|
||||||
|
rd.forward(req, res);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doHtml(VitroRequest vreq, HttpServletResponse res, Individual indiv) throws ServletException, IOException {
|
||||||
|
IndividualDao iwDao = vreq.getWebappDaoFactory().getIndividualDao();
|
||||||
|
ObjectPropertyDao opDao = vreq.getWebappDaoFactory().getObjectPropertyDao();
|
||||||
|
|
||||||
//Check if a "relatedSubjectUri" parameter has been supplied, and,
|
//Check if a "relatedSubjectUri" parameter has been supplied, and,
|
||||||
//if so, retrieve the related individual.t
|
//if so, retrieve the related individual.t
|
||||||
|
@ -104,15 +128,15 @@ public class EntityController extends VitroHttpServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
entity.setKeywords(iwDao.getKeywordsForIndividualByMode(entity.getURI(),"visible"));
|
indiv.setKeywords(iwDao.getKeywordsForIndividualByMode(indiv.getURI(),"visible"));
|
||||||
entity.sortForDisplay();
|
indiv.sortForDisplay();
|
||||||
|
|
||||||
String vclassName = "unknown";
|
String vclassName = "unknown";
|
||||||
String customView = null;
|
String customView = null;
|
||||||
String customCss = null;
|
String customCss = null;
|
||||||
if( entity.getVClass() != null ){
|
if( indiv.getVClass() != null ){
|
||||||
vclassName = entity.getVClass().getName();
|
vclassName = indiv.getVClass().getName();
|
||||||
List<VClass> clasList = entity.getVClasses(true);
|
List<VClass> clasList = indiv.getVClasses(true);
|
||||||
for (VClass clas : clasList) {
|
for (VClass clas : clasList) {
|
||||||
customView = clas.getCustomDisplayView();
|
customView = clas.getCustomDisplayView();
|
||||||
if (customView != null) {
|
if (customView != null) {
|
||||||
|
@ -126,7 +150,7 @@ public class EntityController extends VitroHttpServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (customView == null) { //still
|
if (customView == null) { //still
|
||||||
clasList = entity.getVClasses(false);
|
clasList = indiv.getVClasses(false);
|
||||||
for (VClass clas : clasList) {
|
for (VClass clas : clasList) {
|
||||||
customView = clas.getCustomDisplayView();
|
customView = clas.getCustomDisplayView();
|
||||||
if (customView != null) {
|
if (customView != null) {
|
||||||
|
@ -141,28 +165,28 @@ public class EntityController extends VitroHttpServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.error("Entity " + entity.getURI() + " with vclass URI " +
|
log.error("Entity " + indiv.getURI() + " with vclass URI " +
|
||||||
entity.getVClassURI() + ", no vclass with that URI exists");
|
indiv.getVClassURI() + ", no vclass with that URI exists");
|
||||||
}
|
}
|
||||||
if (customView!=null) {
|
if (customView!=null) {
|
||||||
// insert test for whether a css files of the same name exists, and populate the customCss string for use when construction the header
|
// insert test for whether a css files of the same name exists, and populate the customCss string for use when construction the header
|
||||||
}
|
}
|
||||||
String netid = iwDao.getNetId(entity.getURI());
|
String netid = iwDao.getNetId(indiv.getURI());
|
||||||
|
|
||||||
vreq.setAttribute("netid", netid);
|
vreq.setAttribute("netid", netid);
|
||||||
vreq.setAttribute("vclassName", vclassName);
|
vreq.setAttribute("vclassName", vclassName);
|
||||||
vreq.setAttribute("entity",entity);
|
vreq.setAttribute("entity",indiv);
|
||||||
Portal portal = vreq.getPortal();
|
Portal portal = vreq.getPortal();
|
||||||
vreq.setAttribute("portal",String.valueOf(portal));
|
vreq.setAttribute("portal",String.valueOf(portal));
|
||||||
String view= getViewFromRequest(req);
|
String view= getViewFromRequest(vreq);
|
||||||
if( view == null){
|
if( view == null){
|
||||||
if (customView == null) {
|
if (customView == null) {
|
||||||
view = default_jsp;
|
view = default_jsp;
|
||||||
vreq.setAttribute("bodyJsp","/"+Controllers.ENTITY_JSP);
|
vreq.setAttribute("bodyJsp","/"+Controllers.ENTITY_JSP);
|
||||||
log.debug("no custom view and no view parameter in request for rendering "+entity.getName());
|
log.debug("no custom view and no view parameter in request for rendering "+indiv.getName());
|
||||||
} else {
|
} else {
|
||||||
view = default_jsp;
|
view = default_jsp;
|
||||||
log.debug("setting custom view templates/entity/"+ customView + " for rendering "+entity.getName());
|
log.debug("setting custom view templates/entity/"+ customView + " for rendering "+indiv.getName());
|
||||||
vreq.setAttribute("bodyJsp", "/templates/entity/"+customView);
|
vreq.setAttribute("bodyJsp", "/templates/entity/"+customView);
|
||||||
}
|
}
|
||||||
vreq.setAttribute("entityPropsListJsp",Controllers.ENTITY_PROP_LIST_JSP);
|
vreq.setAttribute("entityPropsListJsp",Controllers.ENTITY_PROP_LIST_JSP);
|
||||||
|
@ -170,20 +194,20 @@ public class EntityController extends VitroHttpServlet {
|
||||||
vreq.setAttribute("entityMergedPropsListJsp",Controllers.ENTITY_MERGED_PROP_LIST_GROUPED_JSP);
|
vreq.setAttribute("entityMergedPropsListJsp",Controllers.ENTITY_MERGED_PROP_LIST_GROUPED_JSP);
|
||||||
vreq.setAttribute("entityKeywordsListJsp",Controllers.ENTITY_KEYWORDS_LIST_JSP);
|
vreq.setAttribute("entityKeywordsListJsp",Controllers.ENTITY_KEYWORDS_LIST_JSP);
|
||||||
} else if (view.equals("rdf.rdf")) {
|
} else if (view.equals("rdf.rdf")) {
|
||||||
writeRDF(entity, req, res);
|
writeRDF(indiv, vreq, res);
|
||||||
// BJL23 temporarily disabling this until we add filtering of hidden properties (get RDF through Vitro API with filtering DAOs)
|
// BJL23 temporarily disabling this until we add filtering of hidden properties (get RDF through Vitro API with filtering DAOs)
|
||||||
} else {
|
} else {
|
||||||
log.debug("Found view parameter "+view+" in request for rendering "+entity.getName());
|
log.debug("Found view parameter "+view+" in request for rendering "+indiv.getName());
|
||||||
}
|
}
|
||||||
//set title before we do the highlighting so we don't get markup in it.
|
//set title before we do the highlighting so we don't get markup in it.
|
||||||
vreq.setAttribute("title",entity.getName());
|
vreq.setAttribute("title",indiv.getName());
|
||||||
//setup highlighter for search terms
|
//setup highlighter for search terms
|
||||||
checkForSearch(req, entity);
|
checkForSearch(vreq, indiv);
|
||||||
|
|
||||||
// set CSS and script elements
|
// set CSS and script elements
|
||||||
String contextPath = "";
|
String contextPath = "";
|
||||||
if (req.getContextPath().length()>1) {
|
if (vreq.getContextPath().length()>1) {
|
||||||
contextPath = req.getContextPath();
|
contextPath = vreq.getContextPath();
|
||||||
}
|
}
|
||||||
String css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\""
|
String css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\""
|
||||||
+ contextPath + "/" + portal.getThemeDir() + "css/entity.css\"/>\n"
|
+ contextPath + "/" + portal.getThemeDir() + "css/entity.css\"/>\n"
|
||||||
|
@ -203,12 +227,240 @@ public class EntityController extends VitroHttpServlet {
|
||||||
|
|
||||||
RequestDispatcher rd = vreq.getRequestDispatcher( view );
|
RequestDispatcher rd = vreq.getRequestDispatcher( view );
|
||||||
rd.forward(vreq,res);
|
rd.forward(vreq,res);
|
||||||
} catch (Throwable e) {
|
|
||||||
log.error(e);
|
|
||||||
req.setAttribute("javax.servlet.jsp.jspException",e);
|
|
||||||
RequestDispatcher rd = req.getRequestDispatcher("/error.jsp");
|
|
||||||
rd.forward(req, res);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void doRdf(VitroRequest vreq, HttpServletResponse res,
|
||||||
|
ContentType rdfFormat) throws IOException, ServletException {
|
||||||
|
|
||||||
|
Individual indiv = getEntityFromRequest(vreq);
|
||||||
|
if( indiv == null ){
|
||||||
|
doNotFound(vreq, res);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Model ontModel = null;
|
||||||
|
HttpSession session = vreq.getSession(false);
|
||||||
|
if( session != null )
|
||||||
|
ontModel =(Model)session.getAttribute("jenaOntModel");
|
||||||
|
if( ontModel == null)
|
||||||
|
ontModel = (Model)getServletContext().getAttribute("jenaOntModel");
|
||||||
|
|
||||||
|
Model newModel = getRDF(indiv, ontModel, ModelFactory.createDefaultModel(), true);
|
||||||
|
|
||||||
|
res.setContentType(rdfFormat.getMediaType());
|
||||||
|
String format = "";
|
||||||
|
if ( RDFXML_MIMETYPE.equals(rdfFormat.getMediaType()))
|
||||||
|
format = "RDF/XML";
|
||||||
|
else if( N3_MIMETYPE.equals(rdfFormat.getMediaType()))
|
||||||
|
format = "N3";
|
||||||
|
else if ( TTL_MIMETYPE.equals(rdfFormat.getMediaType()))
|
||||||
|
format ="TTL";
|
||||||
|
|
||||||
|
newModel.write( res.getOutputStream(), format );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doRedirect(HttpServletRequest req, HttpServletResponse res,
|
||||||
|
String redirectURL) {
|
||||||
|
// It seems like there must be a better way to do this
|
||||||
|
String hn = req.getHeader("Host");
|
||||||
|
res.setHeader("Location", res.encodeURL( "http://" + hn + req.getContextPath() + redirectURL ));
|
||||||
|
res.setStatus(res.SC_SEE_OTHER);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static Pattern LINKED_DATA_URL = Pattern.compile("^/individual/([^/]*)$");
|
||||||
|
private static Pattern NS_PREFIX_URL = Pattern.compile("^/individual/([^/]*)/([^/]*)$");
|
||||||
|
|
||||||
|
/**
|
||||||
|
Gets the entity id from the request.
|
||||||
|
Works for the following styles of URLs:
|
||||||
|
|
||||||
|
/individual?id=individualLocalName
|
||||||
|
/individual?entityId=individualLocalName
|
||||||
|
/individual?uri=urlencodedURI
|
||||||
|
/individual?nedit=bdc34
|
||||||
|
/individual?nedIt=bdc34
|
||||||
|
/individual/nsprefix/localname
|
||||||
|
/individual/localname
|
||||||
|
/individual/localname/localname.rdf
|
||||||
|
/individual/localname/localname.n3
|
||||||
|
/individual/localname/localname.ttl
|
||||||
|
/individual/localname/localname.html
|
||||||
|
|
||||||
|
@return null on failure.
|
||||||
|
*/
|
||||||
|
public static Individual getEntityFromRequest(VitroRequest vreq) {
|
||||||
|
String netIdStr = null;
|
||||||
|
Individual entity = null;
|
||||||
|
IndividualDao iwDao = vreq.getWebappDaoFactory().getIndividualDao();
|
||||||
|
|
||||||
|
String entityIdStr = vreq.getParameter("id");
|
||||||
|
if (entityIdStr == null || entityIdStr.equals(""))
|
||||||
|
entityIdStr = vreq.getParameter("entityId");
|
||||||
|
|
||||||
|
if( entityIdStr != null){
|
||||||
|
try {
|
||||||
|
String entityURI = vreq.getWebappDaoFactory().getDefaultNamespace()+"individual"+entityIdStr;
|
||||||
|
entity = iwDao.getIndividualByURI(entityURI);
|
||||||
|
} catch ( Exception e ) {
|
||||||
|
log.warn("Could not parse entity id: " + entityIdStr);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
String entityURIStr = vreq.getParameter("uri");
|
||||||
|
if (entityURIStr != null) {
|
||||||
|
try {
|
||||||
|
entity = iwDao.getIndividualByURI(entityURIStr);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Could not retrieve entity "+entityURIStr);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
//get URL without hostname or servlet context
|
||||||
|
String url = vreq.getRequestURI().substring(vreq.getContextPath().length());
|
||||||
|
|
||||||
|
/* check for parts of URL that indicate request for RDF
|
||||||
|
http://vivo.cornell.edu/individual/n23/n23.rdf
|
||||||
|
http://vivo.cornell.edu/individual/n23/n23.n3
|
||||||
|
http://vivo.cornell.edu/individual/n23/n23.ttl */
|
||||||
|
String uri = null;
|
||||||
|
Matcher m = RDF_REQUEST.matcher(url);
|
||||||
|
if( m.matches() && m.groupCount() == 1)
|
||||||
|
uri = m.group(1);
|
||||||
|
m = N3_REQUEST.matcher(url);
|
||||||
|
if( m.matches() && m.groupCount() == 1)
|
||||||
|
uri = m.group(1);
|
||||||
|
m = TTL_REQUEST.matcher(url);
|
||||||
|
if( m.matches() && m.groupCount() == 1)
|
||||||
|
uri= m.group(1);
|
||||||
|
m = HTML_REQUEST.matcher(url);
|
||||||
|
if( m.matches() && m.groupCount() == 1)
|
||||||
|
uri= m.group(1);
|
||||||
|
if( uri != null )
|
||||||
|
return iwDao.getIndividualByURI(vreq.getWebappDaoFactory().getDefaultNamespace() + uri);
|
||||||
|
|
||||||
|
// see if we can get the URI from a name space prefix and a local name
|
||||||
|
Matcher prefix_match = NS_PREFIX_URL.matcher(url);
|
||||||
|
if( prefix_match.matches() && prefix_match.groupCount() == 2){
|
||||||
|
String prefix = prefix_match.group(1);
|
||||||
|
String localName = prefix_match.group(2);
|
||||||
|
|
||||||
|
//String[] requestParts = requestURI.split("/individual/");
|
||||||
|
//String[] URIParts = requestParts[1].split("/");
|
||||||
|
//String localName = URIParts[1];
|
||||||
|
|
||||||
|
String namespace = "";
|
||||||
|
NamespaceMapper namespaceMapper = NamespaceMapperFactory.getNamespaceMapper(vreq.getSession().getServletContext());
|
||||||
|
String t;
|
||||||
|
namespace = ( (t = namespaceMapper.getNamespaceForPrefix(prefix)) != null) ? t : "";
|
||||||
|
|
||||||
|
return iwDao.getIndividualByURI(namespace+localName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// see if we can get a local name
|
||||||
|
Matcher linkedDataMatch = LINKED_DATA_URL.matcher(url);
|
||||||
|
if( linkedDataMatch.matches() && linkedDataMatch.groupCount() == 1){
|
||||||
|
String localName = linkedDataMatch.group(1);
|
||||||
|
String ns = vreq.getWebappDaoFactory().getDefaultNamespace();
|
||||||
|
return iwDao.getIndividualByURI( ns + localName );
|
||||||
|
}
|
||||||
|
|
||||||
|
//so we try to get the netid
|
||||||
|
netIdStr = vreq.getParameter("netId");
|
||||||
|
if (netIdStr==null || netIdStr.equals(""))
|
||||||
|
netIdStr = vreq.getParameter("netid");
|
||||||
|
if ( netIdStr != null ){
|
||||||
|
uri = iwDao.getIndividualURIFromNetId(netIdStr);
|
||||||
|
return iwDao.getIndividualByURI(uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static Pattern URI_PATTERN = Pattern.compile("^/individual/([^/]*)$");
|
||||||
|
//Redirect if the request is for http://hostname/individual/localname
|
||||||
|
// if accept is nothing or text/html redirect to ???
|
||||||
|
// if accept is some RDF thing redirect to the URL for RDF
|
||||||
|
private String checkForRedirect(String url, String acceptHeader) {
|
||||||
|
Matcher m = URI_PATTERN.matcher(url);
|
||||||
|
if( m.matches() && m.groupCount() == 1 ){
|
||||||
|
ContentType c = checkForLinkedDataRequest(url, acceptHeader);
|
||||||
|
if( c != null ){
|
||||||
|
String redirectUrl = "/individual/" + m.group(1) + "/" + m.group(1) ;
|
||||||
|
if( RDFXML_MIMETYPE.equals( c.getMediaType()) ){
|
||||||
|
return redirectUrl + ".rdf";
|
||||||
|
}else if( N3_MIMETYPE.equals( c.getMediaType() )){
|
||||||
|
return redirectUrl + ".n3";
|
||||||
|
}else if( TTL_MIMETYPE.equals( c.getMediaType() )){
|
||||||
|
return redirectUrl + ".ttl";
|
||||||
|
}else{ //this case shouldn't happen
|
||||||
|
return redirectUrl + ".rdf";
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
//redirect to HTML representation
|
||||||
|
return "/individual/" + m.group(1) + "/" + m.group(1) + ".html";
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Pattern RDF_REQUEST = Pattern.compile("^/individual/([^/]*)/\\1.rdf$");
|
||||||
|
private static Pattern N3_REQUEST = Pattern.compile("^/individual/([^/]*)/\\1.n3$");
|
||||||
|
private static Pattern TTL_REQUEST = Pattern.compile("^/individual/([^/]*)/\\1.ttl$");
|
||||||
|
private static Pattern HTML_REQUEST = Pattern.compile("^/individual/([^/]*)/\\1.html$");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return null if this is not a linked data request, returns content type if it is a
|
||||||
|
* linked data request.
|
||||||
|
*/
|
||||||
|
private ContentType checkForLinkedDataRequest(String url, String acceptHeader) {
|
||||||
|
try {
|
||||||
|
//check the accept header
|
||||||
|
if (acceptHeader != null) {
|
||||||
|
List<ContentType> actualContentTypes = new ArrayList<ContentType>();
|
||||||
|
actualContentTypes.add(new ContentType( XHTML_MIMETYPE ));
|
||||||
|
actualContentTypes.add(new ContentType( HTML_MIMETYPE ));
|
||||||
|
|
||||||
|
actualContentTypes.add(new ContentType( RDFXML_MIMETYPE ));
|
||||||
|
actualContentTypes.add(new ContentType( N3_MIMETYPE ));
|
||||||
|
actualContentTypes.add(new ContentType( TTL_MIMETYPE ));
|
||||||
|
|
||||||
|
|
||||||
|
ContentType best = ContentType.getBestContentType(acceptHeader,actualContentTypes);
|
||||||
|
if (best!=null && (
|
||||||
|
RDFXML_MIMETYPE.equals(best.getMediaType()) ||
|
||||||
|
N3_MIMETYPE.equals(best.getMediaType()) ||
|
||||||
|
TTL_MIMETYPE.equals(best.getMediaType()) ))
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* check for parts of URL that indicate request for RDF
|
||||||
|
http://vivo.cornell.edu/individual/n23/n23.rdf
|
||||||
|
http://vivo.cornell.edu/individual/n23/n23.n3
|
||||||
|
http://vivo.cornell.edu/individual/n23/n23.ttl
|
||||||
|
*/
|
||||||
|
|
||||||
|
Matcher m = RDF_REQUEST.matcher(url);
|
||||||
|
if( m.matches() )
|
||||||
|
return new ContentType(RDFXML_MIMETYPE);
|
||||||
|
m = N3_REQUEST.matcher(url);
|
||||||
|
if( m.matches() )
|
||||||
|
return new ContentType(N3_MIMETYPE);
|
||||||
|
m = TTL_REQUEST.matcher(url);
|
||||||
|
if( m.matches() )
|
||||||
|
return new ContentType(TTL_MIMETYPE);
|
||||||
|
|
||||||
|
} catch (Throwable th) {
|
||||||
|
log.error("problem while checking accept header " , th);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkForSunset(VitroRequest vreq, Individual entity) {
|
private boolean checkForSunset(VitroRequest vreq, Individual entity) {
|
||||||
|
@ -266,6 +518,7 @@ public class EntityController extends VitroHttpServlet {
|
||||||
Property dp = ontModel.getProperty(ds.getDatapropURI());
|
Property dp = ontModel.getProperty(ds.getDatapropURI());
|
||||||
//if(!(dp instanceof DatatypeProperty)) System.out.println("not datatype prop "+dp.getURI());
|
//if(!(dp instanceof DatatypeProperty)) System.out.println("not datatype prop "+dp.getURI());
|
||||||
|
|
||||||
|
//TODO: improve to handle languages and XSD data types
|
||||||
Literal lit = newModel.createLiteral(ds.getData());
|
Literal lit = newModel.createLiteral(ds.getData());
|
||||||
newModel.add(newModel.createStatement(subj, dp, lit));
|
newModel.add(newModel.createStatement(subj, dp, lit));
|
||||||
}
|
}
|
||||||
|
@ -283,8 +536,6 @@ public class EntityController extends VitroHttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkForSearch(HttpServletRequest req, Individual ent) {
|
private void checkForSearch(HttpServletRequest req, Individual ent) {
|
||||||
|
|
||||||
|
|
||||||
if (req.getSession().getAttribute("LastQuery") != null) {
|
if (req.getSession().getAttribute("LastQuery") != null) {
|
||||||
VitroQueryWrapper qWrap = (VitroQueryWrapper) req.getSession()
|
VitroQueryWrapper qWrap = (VitroQueryWrapper) req.getSession()
|
||||||
.getAttribute("LastQuery");
|
.getAttribute("LastQuery");
|
||||||
|
|
|
@ -28,8 +28,12 @@ public class VitroHttpServlet extends HttpServlet
|
||||||
private WebappDaoFactory myAssertionsWebappDaoFactory = null;
|
private WebappDaoFactory myAssertionsWebappDaoFactory = null;
|
||||||
private WebappDaoFactory myDeductionsWebappDaoFactory = null;
|
private WebappDaoFactory myDeductionsWebappDaoFactory = null;
|
||||||
|
|
||||||
|
public final static String XHTML_MIMETYPE ="application/xhtml+xml";
|
||||||
|
public final static String HTML_MIMETYPE ="text/html";
|
||||||
|
|
||||||
public final static String RDFXML_MIMETYPE ="application/rdf+xml";
|
public final static String RDFXML_MIMETYPE ="application/rdf+xml";
|
||||||
public final static String N3_MIMETYPE ="text/rdf+n3"; //unofficial and unregistered
|
public final static String N3_MIMETYPE ="text/n3"; //unofficial and unregistered
|
||||||
|
public final static String TTL_MIMETYPE = "text/turtle"; //unofficial and unregistered
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup the auth flag, portal flag and portal bean objects.
|
* Setup the auth flag, portal flag and portal bean objects.
|
||||||
|
|
|
@ -1,100 +0,0 @@
|
||||||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.web;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper;
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapperFactory;
|
|
||||||
|
|
||||||
public class EntityWebUtils {
|
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(EntityWebUtils.class.getName());
|
|
||||||
|
|
||||||
/**
|
|
||||||
Gets the entity id from the request.
|
|
||||||
Works for entities.id and NetId's
|
|
||||||
@return -1 on faliure.
|
|
||||||
|
|
||||||
This is copied from EntityServlet.java.
|
|
||||||
Maybe this should be in package like vivo.web.EntityWebUtil?
|
|
||||||
*/
|
|
||||||
public static Individual getEntityFromRequest(VitroRequest vreq) {
|
|
||||||
String netIdStr = null;
|
|
||||||
Individual entity = null;
|
|
||||||
IndividualDao iwDao = vreq.getWebappDaoFactory().getIndividualDao();
|
|
||||||
|
|
||||||
String entityIdStr = vreq.getParameter("id");
|
|
||||||
if (entityIdStr == null || entityIdStr.equals(""))
|
|
||||||
entityIdStr = vreq.getParameter("entityId");
|
|
||||||
|
|
||||||
if( entityIdStr != null){
|
|
||||||
try {
|
|
||||||
String entityURI = vreq.getWebappDaoFactory().getDefaultNamespace()+"individual"+entityIdStr;
|
|
||||||
entity = iwDao.getIndividualByURI(entityURI);
|
|
||||||
} catch ( Exception e ) {
|
|
||||||
log.error("Could not parse entity id: " + entityIdStr);
|
|
||||||
return null; //some error message to logs here
|
|
||||||
}
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
String entityURIStr = vreq.getParameter("uri");
|
|
||||||
if (entityURIStr != null) {
|
|
||||||
try {
|
|
||||||
entity = iwDao.getIndividualByURI(entityURIStr);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
log.error("Could not retrieve entity "+entityURIStr);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
netIdStr = vreq.getParameter("netId"); //so we try to get the netid
|
|
||||||
if (netIdStr==null || netIdStr.equals(""))
|
|
||||||
netIdStr = vreq.getParameter("netid");
|
|
||||||
if ( netIdStr != null ){
|
|
||||||
String uri = iwDao.getIndividualURIFromNetId(netIdStr);
|
|
||||||
return iwDao.getIndividualByURI(uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
// see if we can get the URI from a namespace prefix and a local name
|
|
||||||
String requestURI = vreq.getRequestURI();
|
|
||||||
|
|
||||||
String[] requestParts = requestURI.split("/individual/");
|
|
||||||
String[] URIParts = requestParts[1].split("/");
|
|
||||||
|
|
||||||
String namespace = "";
|
|
||||||
NamespaceMapper namespaceMapper = NamespaceMapperFactory.getNamespaceMapper(vreq.getSession().getServletContext());
|
|
||||||
String t;
|
|
||||||
namespace = ( (t = namespaceMapper.getNamespaceForPrefix(URIParts[0])) != null) ? t : "";
|
|
||||||
String localName = URIParts[1];
|
|
||||||
|
|
||||||
return vreq.getWebappDaoFactory().getIndividualDao().getIndividualByURI(namespace+localName);
|
|
||||||
|
|
||||||
// entityIdStr = vreq.getParameter("adw");
|
|
||||||
// if (entityIdStr != null && !entityIdStr.equals("")){
|
|
||||||
// return vreq.getWebappDaoFactory.getIndividualDao().getIndividualByExternalId(ExternalIdDaoDb.ADW_DEPT_ID,entityIdStr);
|
|
||||||
// }
|
|
||||||
// entityIdStr = vreq.getParameter("ohr");
|
|
||||||
// if (entityIdStr != null && !entityIdStr.equals("")){
|
|
||||||
// return vreq.getWebappDaoFactory.getIndividualDao().getIndividualByExternalId(ExternalIdDaoDb.OHR_DEPT_ID,entityIdStr);
|
|
||||||
// }
|
|
||||||
// entityIdStr = vreq.getParameter("osp");
|
|
||||||
// if (entityIdStr!=null && !entityIdStr.equals("")){
|
|
||||||
// return vreq.getWebappDaoFactory.getIndividualDao().getIndividualByExternalId(ExternalIdDaoDb.OSP_DEPT_ID,entityIdStr);
|
|
||||||
// }
|
|
||||||
//return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String DEFAULT_THUMB_IMG_WIDTH = "100";
|
|
||||||
private static String IMG_DIR = "images/";
|
|
||||||
private static String NO_IMG_ALT = "no picture available";
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue