From cd0f9f1f6de921de77ff0140e7aee91a29a77e9d Mon Sep 17 00:00:00 2001 From: ryounes Date: Fri, 29 Apr 2011 19:53:45 +0000 Subject: [PATCH] Add utility method to determine whether an individual's uri is in the default namespace. --- .../controller/freemarker/UrlBuilder.java | 17 +++++++++++++++++ .../individual/BaseIndividualTemplateModel.java | 15 +++------------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java index 3aaa8bd3f..2fbeabfb5 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java @@ -18,6 +18,7 @@ import org.openrdf.model.impl.URIImpl; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl; import edu.cornell.mannlib.vitro.webapp.beans.Portal; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.filters.PortalPickerFilter; @@ -319,6 +320,22 @@ public class UrlBuilder { return profileUrl; } + public static boolean isUriInDefaultNamespace(String individualUri, VitroRequest vreq) { + return isUriInDefaultNamespace(individualUri, vreq.getWebappDaoFactory()); + } + + public static boolean isUriInDefaultNamespace(String individualUri, WebappDaoFactory wadf) { + try { + URI uri = new URIImpl(individualUri); // throws exception if individualUri is invalid + String namespace = uri.getNamespace(); + String defaultNamespace = wadf.getDefaultNamespace(); + return defaultNamespace.equals(namespace); + } catch (Exception e) { + log.warn(e); + return false; + } + } + public static String urlEncode(String str) { String encoding = "ISO-8859-1"; String encodedUrl = null; diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/BaseIndividualTemplateModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/BaseIndividualTemplateModel.java index 01959331b..1d355e2e9 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/BaseIndividualTemplateModel.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/BaseIndividualTemplateModel.java @@ -94,18 +94,9 @@ public abstract class BaseIndividualTemplateModel extends BaseTemplateModel { String individualUri = getUri(); String profileUrl = getProfileUrl(); - - URI uri = new URIImpl(individualUri); - String namespace = uri.getNamespace(); - - // Individuals in the default namespace - // e.g., http://vivo.cornell.edu/individual/n2345/n2345.rdf - // where default namespace = http://vivo.cornell.edu/individual/ - // Other individuals: http://some.other.namespace/n2345?format=rdfxml - String defaultNamespace = vreq.getWebappDaoFactory().getDefaultNamespace(); - return (defaultNamespace.equals(namespace)) ? profileUrl + "/" + getLocalName() + ".rdf" - : UrlBuilder.addParams(profileUrl, "format", "rdfxml"); - + boolean isUriInDefaultNamespace = UrlBuilder.isUriInDefaultNamespace(individualUri, vreq); + return isUriInDefaultNamespace ? profileUrl + "/" + getLocalName() + ".rdf" + : UrlBuilder.addParams(profileUrl, "format", "rdfxml"); } public String getEditUrl() {