Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/main/java/org/dvcama/lodview/conf/ConfigurationBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class ConfigurationBean implements ServletContextAware, Cloneable {
private String httpRedirectSuffix;
private String httpRedirectPrefix;
private String endPointUrl;
private String lodliveUrl;
private String IRInamespace;
private String contentEncoding;
private String staticResourceURL;
Expand All @@ -43,6 +44,10 @@ public class ConfigurationBean implements ServletContextAware, Cloneable {
private String authPassword = null;
private String defaultInverseBehaviour = "collapse";

public enum ColorStrategy {RANDOM, CLASS, PREFIX}
private ColorStrategy colorStrategy = ColorStrategy.RANDOM;
public ColorStrategy getColorStrategy() {return colorStrategy;}

private List<String> defaultQueries = null, defaultRawDataQueries = null, defaultInversesQueries = null, defaultInversesTest = null, defaultInversesCountQueries = null, typeProperties = null, audioProperties = null, imageProperties = null, videoProperties = null, linkingProperties = null, titleProperties = null, descriptionProperties = null, longitudeProperties = null, latitudeProperties = null;
private List<String> colorPair = null, skipDomains = null, mainOntologiesPrefixes = null;
private Map<String, String> colorPairMatcher = null;
Expand All @@ -65,6 +70,7 @@ public void populateBean() throws IOException, Exception {
confModel = RDFDataMgr.loadModel(configFile.getAbsolutePath());

endPointUrl = getSingleConfValue("endpoint");
lodliveUrl = getSingleConfValue("lodliveUrl");
endPointType = getSingleConfValue("endpointType", "");
authPassword = getSingleConfValue("authPassword");
authUsername = getSingleConfValue("authUsername");
Expand Down Expand Up @@ -120,10 +126,12 @@ public void populateBean() throws IOException, Exception {

private Map<String, String> populateColorPairMatcher() {
Map<String, String> result = new HashMap<String, String>();
ResIterator iter = confModel.listSubjectsWithProperty(confModel.createProperty(confModel.getNsPrefixURI("conf"), "hasColorPair"));
ResIterator iter = confModel
.listSubjectsWithProperty(confModel.createProperty(confModel.getNsPrefixURI("conf"), "hasColorPair"));
while (iter.hasNext()) {
Resource res = iter.next();
NodeIterator values = confModel.listObjectsOfProperty(res, confModel.createProperty(confModel.getNsPrefixURI("conf"), "hasColorPair"));
NodeIterator values = confModel.listObjectsOfProperty(res,
confModel.createProperty(confModel.getNsPrefixURI("conf"), "hasColorPair"));
while (values.hasNext()) {
RDFNode node = values.next();
result.put(res.toString(), node.toString());
Expand All @@ -138,7 +146,8 @@ private String getSingleConfValue(String prop) {
}

private String getSingleConfValue(String prop, String defaultValue) {
NodeIterator iter = confModel.listObjectsOfProperty(confModel.createProperty(confModel.getNsPrefixURI("conf"), prop));
NodeIterator iter = confModel
.listObjectsOfProperty(confModel.createProperty(confModel.getNsPrefixURI("conf"), prop));
while (iter.hasNext()) {
RDFNode node = iter.next();
return node.toString();
Expand All @@ -148,7 +157,8 @@ private String getSingleConfValue(String prop, String defaultValue) {

private List<String> getMultiConfValue(String prop) {
List<String> result = new ArrayList<String>();
NodeIterator iter = confModel.listObjectsOfProperty(confModel.createProperty(confModel.getNsPrefixURI("conf"), prop));
NodeIterator iter = confModel
.listObjectsOfProperty(confModel.createProperty(confModel.getNsPrefixURI("conf"), prop));
while (iter.hasNext()) {
RDFNode node = iter.next();
result.add(node.toString());
Expand Down Expand Up @@ -195,6 +205,10 @@ public String getEndPointUrl() {
return endPointUrl;
}

public String getLodliveUrl() {
return lodliveUrl;
}

public List<String> getDefaultQueries() {
return defaultQueries;
}
Expand Down Expand Up @@ -315,8 +329,19 @@ public Object clone() {

@Override
public String toString() {
return "ConfigurationBean [confModel=" + confModel + ", context=" + context + ", confFile=" + confFile + ", endPointUrl=" + endPointUrl + ", IRInamespace=" + IRInamespace + ", contentEncoding=" + contentEncoding + ", staticResourceURL=" + staticResourceURL + ", preferredLanguage=" + preferredLanguage + ", publicUrlPrefix=" + publicUrlPrefix + ", authUsername=" + authUsername + ", authPassword=" + authPassword + ", defaultInverseBehaviour=" + defaultInverseBehaviour + ", defaultQueries=" + defaultQueries + ", defaultRawDataQueries=" + defaultRawDataQueries + ", defaultInversesQueries=" + defaultInversesQueries + ", defaultInversesTest=" + defaultInversesTest + ", defaultInversesCountQueries=" + defaultInversesCountQueries + ", typeProperties=" + typeProperties
+ ", imageProperties=" + imageProperties + ", audioProperties=" + audioProperties + ", videoProperties=" + videoProperties + ", linkingProperties=" + linkingProperties + ", titleProperties=" + titleProperties + ", descriptionProperties=" + descriptionProperties + ", longitudeProperties=" + longitudeProperties + ", latitudeProperties=" + latitudeProperties + ", colorPair=" + colorPair + ", skipDomains=" + skipDomains + ", rand=" + rand + "]";
return "ConfigurationBean [confModel=" + confModel + ", context=" + context + ", confFile=" + confFile
+ ", endPointUrl=" + endPointUrl + ", IRInamespace=" + IRInamespace + ", contentEncoding="
+ contentEncoding + ", staticResourceURL=" + staticResourceURL + ", preferredLanguage="
+ preferredLanguage + ", publicUrlPrefix=" + publicUrlPrefix + ", authUsername=" + authUsername
+ ", authPassword=" + authPassword + ", defaultInverseBehaviour=" + defaultInverseBehaviour
+ ", defaultQueries=" + defaultQueries + ", defaultRawDataQueries=" + defaultRawDataQueries
+ ", defaultInversesQueries=" + defaultInversesQueries + ", defaultInversesTest=" + defaultInversesTest
+ ", defaultInversesCountQueries=" + defaultInversesCountQueries + ", typeProperties=" + typeProperties
+ ", imageProperties=" + imageProperties + ", audioProperties=" + audioProperties + ", videoProperties="
+ videoProperties + ", linkingProperties=" + linkingProperties + ", titleProperties=" + titleProperties
+ ", descriptionProperties=" + descriptionProperties + ", longitudeProperties=" + longitudeProperties
+ ", latitudeProperties=" + latitudeProperties + ", colorPair=" + colorPair + ", skipDomains="
+ skipDomains + ", rand=" + rand + "]";
}

public String getHomeUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

Expand All @@ -30,7 +29,6 @@
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.view.RedirectView;
Expand Down Expand Up @@ -71,7 +69,9 @@ public ResourceController(MessageSource messageSource, OntologyBean ontoBean) {
}

@RequestMapping(value = { "{path:(?!staticResources).*$}", "{path:(?!staticResources).*$}/**" })
public Object resourceController(ModelMap model, HttpServletRequest req, HttpServletResponse res, Locale locale, @RequestParam(value = "output", defaultValue = "") String output, @CookieValue(value = "colorPair", defaultValue = "") String colorPair) throws UnsupportedEncodingException {
public Object resourceController(ModelMap model, HttpServletRequest req, HttpServletResponse res, Locale locale,
@RequestParam(value = "output", defaultValue = "") String output,
@CookieValue(value = "colorPair", defaultValue = "") String colorPair) throws UnsupportedEncodingException {
if (colorPair.equals("")) {
colorPair = conf.getRandomColorPair();
Cookie c = new Cookie("colorPair", colorPair);
Expand All @@ -81,7 +81,8 @@ public Object resourceController(ModelMap model, HttpServletRequest req, HttpSer
return resource(conf, model, req, res, locale, output, "", colorPair);
}

public Object resource(ConfigurationBean conf, ModelMap model, HttpServletRequest req, HttpServletResponse res, Locale locale, String output, String forceIRI, String colorPair) throws UnsupportedEncodingException {
public Object resource(ConfigurationBean conf, ModelMap model, HttpServletRequest req, HttpServletResponse res,
Locale locale, String output, String forceIRI, String colorPair) throws UnsupportedEncodingException {

model.addAttribute("conf", conf);

Expand Down Expand Up @@ -167,7 +168,8 @@ public Object resource(ConfigurationBean conf, ModelMap model, HttpServletReques
*/
if (requestUrl.matches(".+\\.(ntriples|n3|json|rdf)")) {
String outputType = "";
String newUrl = requestUrl.replaceFirst("/data/", "/resource/").replaceAll("\\.(ntriples|n3|json|rdf)$", "");
String newUrl = requestUrl.replaceFirst("/data/", "/resource/").replaceAll("\\.(ntriples|n3|json|rdf)$",
"");
RedirectView r = new RedirectView();
r.setExposeModelAttributes(false);
if (requestUrl.endsWith(".ntriples")) {
Expand All @@ -179,7 +181,8 @@ public Object resource(ConfigurationBean conf, ModelMap model, HttpServletReques
} else if (requestUrl.endsWith(".rdf")) {
outputType = "application/rdf+xml";
}
r.setUrl(newUrl + "?" + (req.getQueryString() != null ? req.getQueryString() + "&" : "") + "output=" + outputType);
r.setUrl(newUrl + "?" + (req.getQueryString() != null ? req.getQueryString() + "&" : "") + "output="
+ outputType);
return r;
}
}
Expand Down Expand Up @@ -221,7 +224,7 @@ public Object resource(ConfigurationBean conf, ModelMap model, HttpServletReques
if (redirect && !redirected) {
return redirect(req, IRIsuffix);
} else {
return htmlResource(model, IRI, colorPair, locale, req, res);
return htmlResource(model, conf, IRI, colorPair, locale, req, res);
}
} else {
return new ErrorController(conf).error406(res, model, colorPair);
Expand All @@ -232,31 +235,36 @@ public Object resource(ConfigurationBean conf, ModelMap model, HttpServletReques
} catch (Exception e) {
e.printStackTrace();
if (e.getMessage() != null && e.getMessage().startsWith("404")) {
return new ErrorController(conf).error404(res, model, e.getMessage(), colorPair, IRI, conf.getEndPointUrl());
return new ErrorController(conf).error404(res, model, e.getMessage(), colorPair, IRI,
conf.getEndPointUrl());
} else {
return new ErrorController(conf).error500(res, model, e.getMessage(), colorPair, IRI, conf.getEndPointUrl());
return new ErrorController(conf).error500(res, model, e.getMessage(), colorPair, IRI,
conf.getEndPointUrl());
}
}

}

private String htmlResource(ModelMap model, String IRI, String colorPair, Locale locale, HttpServletRequest req, HttpServletResponse res) throws Exception {
private String htmlResource(ModelMap model, ConfigurationBean conf, String IRI, String colorPair, Locale locale,
HttpServletRequest req, HttpServletResponse res) throws Exception {
model.addAttribute("contextPath", new UrlPathHelper().getContextPath(req));
ResultBean r = new ResourceBuilder(messageSource).buildHtmlResource(IRI, locale, conf, ontoBean);
model.addAttribute("colorPair", Misc.guessColor(colorPair, r, conf));
model.addAttribute("results", Misc.guessClass(r, conf, ontoBean));
model.addAttribute("ontoBean", ontoBean);

addDataLinks(IRI, model, req, locale);
addLodliveLink(locale, model, IRI);
addLodliveLink(locale, model, IRI, conf.getLodliveUrl());
enrichResponse(model, r, req, res);
return "resource";
}

private void addDataLinks(String IRI, ModelMap model, HttpServletRequest req, Locale locale) throws UnsupportedEncodingException {
private void addDataLinks(String IRI, ModelMap model, HttpServletRequest req, Locale locale)
throws UnsupportedEncodingException {

Map<String, Map<String, String>> rawdatalinks = new LinkedHashMap<String, Map<String, String>>();
String queryString = (req.getQueryString() != null ? "&amp;" + req.getQueryString().replaceAll("&", "&amp;") : "");
String queryString = (req.getQueryString() != null ? "&amp;" + req.getQueryString().replaceAll("&", "&amp;")
: "");

if (conf.getRedirectionStrategy().equals("pubby")) {

Expand All @@ -280,20 +288,24 @@ private void addDataLinks(String IRI, ModelMap model, HttpServletRequest req, Lo
if (conf.getEndPointType().equals("virtuoso")) {
{
Map<String, String> list = new LinkedHashMap<String, String>();
list.put("atom", conf.getEndPointUrl() + "?output=application%2Fatom%2Bxml&amp;query=DESCRIBE+%3C" + IRI + "%3E");
list.put("json", conf.getEndPointUrl() + "?output=application%2Fodata%2Bjson&amp;query=DESCRIBE+%3C" + IRI + "%3E");
list.put("atom", conf.getEndPointUrl() + "?output=application%2Fatom%2Bxml&amp;query=DESCRIBE+%3C" + IRI
+ "%3E");
list.put("json", conf.getEndPointUrl() + "?output=application%2Fodata%2Bjson&amp;query=DESCRIBE+%3C"
+ IRI + "%3E");
rawdatalinks.put("odata:", list);
}
{
Map<String, String> list = new LinkedHashMap<String, String>();
list.put("html", conf.getEndPointUrl() + "?output=text%2Fhtml&amp;query=DESCRIBE+%3C" + IRI + "%3E");
list.put("json", conf.getEndPointUrl() + "?output=application%2Fmicrodata%2Bjson&amp;query=DESCRIBE+%3C" + IRI + "%3E");
list.put("json", conf.getEndPointUrl() + "?output=application%2Fmicrodata%2Bjson&amp;query=DESCRIBE+%3C"
+ IRI + "%3E");
rawdatalinks.put("microdata:", list);
}
{
Map<String, String> list = new LinkedHashMap<String, String>();
list.put("csv", conf.getEndPointUrl() + "?output=text%2Fcsv&amp;query=DESCRIBE+%3C" + IRI + "%3E");
list.put("cxml", conf.getEndPointUrl() + "?output=format=text%2Fcxml&amp;query=DESCRIBE+%3C" + IRI + "%3E");
list.put("cxml",
conf.getEndPointUrl() + "?output=format=text%2Fcxml&amp;query=DESCRIBE+%3C" + IRI + "%3E");
rawdatalinks.put("rawdata:", list);
}
}
Expand All @@ -312,9 +324,11 @@ private RedirectView redirect(HttpServletRequest req, String IRIsuffix) throws U
// prefix mode
String redirectUrl = conf.getHttpRedirectPrefix().replaceAll("^/", "");
if (conf.getRedirectionStrategy().equals("pubby")) {
r.setUrl(conf.getPublicUrlPrefix() + redirectUrl + IRIsuffix.replaceAll("^resource/", "") + (req.getQueryString() != null ? "?" + req.getQueryString() : ""));
r.setUrl(conf.getPublicUrlPrefix() + redirectUrl + IRIsuffix.replaceAll("^resource/", "")
+ (req.getQueryString() != null ? "?" + req.getQueryString() : ""));
} else {
r.setUrl(conf.getPublicUrlPrefix().replaceAll(IRIsuffix + "$", "") + redirectUrl + IRIsuffix + (req.getQueryString() != null ? "?" + req.getQueryString() : ""));
r.setUrl(conf.getPublicUrlPrefix().replaceAll(IRIsuffix + "$", "") + redirectUrl + IRIsuffix
+ (req.getQueryString() != null ? "?" + req.getQueryString() : ""));
}
} else {
// suffix mode
Expand All @@ -325,15 +339,18 @@ private RedirectView redirect(HttpServletRequest req, String IRIsuffix) throws U
// redirectUrl += URLEncoder.encode(string, "UTF-8") + "/";
// }
// redirectUrl = redirectUrl.replaceAll("/$", "");
r.setUrl(req.getRequestURL() + redirectUrl + (req.getQueryString() != null ? "?" + req.getQueryString() : ""));
r.setUrl(req.getRequestURL() + redirectUrl
+ (req.getQueryString() != null ? "?" + req.getQueryString() : ""));
}

return r;

}

private void addLodliveLink(Locale locale, ModelMap model, String IRI) {
if (locale.getLanguage().equals("it")) {
private void addLodliveLink(Locale locale, ModelMap model, String IRI, String lodlive) {
if (lodlive != null) {
model.addAttribute("lodliveUrl", lodlive + "?" + IRI.replaceAll("#", "%23"));
} else if (locale.getLanguage().equals("it")) {
model.addAttribute("lodliveUrl", "http://lodlive.it?" + IRI.replaceAll("#", "%23"));
} else if (locale.getLanguage().equals("fr")) {
model.addAttribute("lodliveUrl", "http://fr.lodlive.it?" + IRI.replaceAll("#", "%23"));
Expand All @@ -350,10 +367,13 @@ private void enrichResponse(ModelMap model, ResultBean r, HttpServletRequest req
res.addHeader("Link", "<" + publicUrl + ">; rel=\"about\"");

@SuppressWarnings("unchecked")
Map<String, Map<String, String>> rawdatalinks = (LinkedHashMap<String, Map<String, String>>) model.get("rawdatalinks");
Map<String, Map<String, String>> rawdatalinks = (LinkedHashMap<String, Map<String, String>>) model
.get("rawdatalinks");
for (String k : rawdatalinks.keySet()) {
for (String k1 : rawdatalinks.get(k).keySet()) {
res.addHeader("Link", "<" + rawdatalinks.get(k).get(k1) + ">; rel=\"alternate\"; type=\"application/rdf+xml\"; title=\"Structured Descriptor Document (" + k1 + ")\"");
res.addHeader("Link", "<" + rawdatalinks.get(k).get(k1)
+ ">; rel=\"alternate\"; type=\"application/rdf+xml\"; title=\"Structured Descriptor Document ("
+ k1 + ")\"");
}
}
try {
Expand All @@ -366,11 +386,15 @@ private void enrichResponse(ModelMap model, ResultBean r, HttpServletRequest req
}

@RequestMapping(value = "/rawdata")
public ResponseEntity<String> resourceRawController(ModelMap model, @RequestParam(value = "IRI") String IRI, @RequestParam(value = "sparql") String sparql, @RequestParam(value = "contentType", defaultValue = "application/rdf+xml") String contentType) {
public ResponseEntity<String> resourceRawController(ModelMap model, @RequestParam(value = "IRI") String IRI,
@RequestParam(value = "sparql") String sparql,
@RequestParam(value = "contentType", defaultValue = "application/rdf+xml") String contentType) {
return resourceRaw(conf, model, IRI, sparql, contentType);
}

public ResponseEntity<String> resourceRaw(ConfigurationBean conf, ModelMap model, @RequestParam(value = "IRI") String IRI, @RequestParam(value = "sparql") String sparql, @RequestParam(value = "contentType", defaultValue = "application/rdf+xml") String contentType) {
public ResponseEntity<String> resourceRaw(ConfigurationBean conf, ModelMap model,
@RequestParam(value = "IRI") String IRI, @RequestParam(value = "sparql") String sparql,
@RequestParam(value = "contentType", defaultValue = "application/rdf+xml") String contentType) {
// System.out.println("ResourceController.resourceRaw()");
contentType = contentType.replaceAll("([a-zA-Z]) ([a-zA-Z])", "$1+$2");
Lang lang = RDFLanguages.contentTypeToLang(contentType);
Expand All @@ -383,11 +407,16 @@ public ResponseEntity<String> resourceRaw(ConfigurationBean conf, ModelMap model
try {
m.read(IRI);
} catch (Exception e) {
throw new Exception(messageSource.getMessage("error.noContentNegotiation", null, "sorry but content negotiation is not supported by the IRI", Locale.ENGLISH));
throw new Exception(messageSource.getMessage("error.noContentNegotiation", null,
"sorry but content negotiation is not supported by the IRI", Locale.ENGLISH));
}
return new ResponseEntity<String>(new ResourceBuilder(messageSource).buildRDFResource(IRI, m, lang, conf), headers, HttpStatus.OK);
return new ResponseEntity<String>(
new ResourceBuilder(messageSource).buildRDFResource(IRI, m, lang, conf), headers,
HttpStatus.OK);
} else {
return new ResponseEntity<String>(new ResourceBuilder(messageSource).buildRDFResource(IRI, sparql, lang, conf), headers, HttpStatus.OK);
return new ResponseEntity<String>(
new ResourceBuilder(messageSource).buildRDFResource(IRI, sparql, lang, conf), headers,
HttpStatus.OK);
}

} catch (Exception e) {
Expand Down
Loading