Skip to content

Commit

Permalink
Merge pull request #50 from hitontology/feature-logging
Browse files Browse the repository at this point in the history
Use SLF4J Logging instead of System.out.println() and System.err.println()
  • Loading branch information
dvcama committed Dec 3, 2021
2 parents b06e126 + bde1d5d commit 7ce43c3
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 41 deletions.
20 changes: 12 additions & 8 deletions src/main/java/org/dvcama/lodview/bean/OntologyBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.FileManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OntologyBean implements ServletContextAware {

private String ontoDir;
private ServletContext context;
private Model model;
final Logger logger = LoggerFactory.getLogger(OntologyBean.class);

public void init() {
File ontoDirFile = new File(ontoDir);
Expand All @@ -30,27 +34,27 @@ public void init() {
}
model = ModelFactory.createDefaultModel();
if (ontoDirFile.exists()) {
System.out.println("ontologies dir founded!");
logger.debug("ontologies dir founded!");
File[] list = ontoDirFile.listFiles();
for (File file : list) {
if (!file.isDirectory()) {
try {
System.out.println("loading " + file.getCanonicalPath());
logger.debug("loading " + file.getCanonicalPath());
FileManager.get().readModel(model, file.getAbsolutePath());
System.out.println("read successfully!");
logger.debug("read successfully!");
} catch (Exception e) {
System.err.println("error loading " + e.getMessage());
logger.error("error loading " + e.getMessage());
// e.printStackTrace();
}
}
}
} else {
System.out.println("no ontologies founded " + ontoDirFile.getAbsolutePath());
logger.debug("no ontologies founded " + ontoDirFile.getAbsolutePath());
}

// System.out.println("------------------- " + getHashResult("en",
// logger.debug("------------------- " + getHashResult("en",
// "http://dati.camera.it/ocd/parentCountry"));
// System.out.println("------------------- " + getHashResult("it",
// logger.debug("------------------- " + getHashResult("it",
// "http://dati.camera.it/ocd/parentCountry"));

}
Expand Down Expand Up @@ -98,7 +102,7 @@ private String getSingleValue(String preferredLanguage, Resource IRI, String pro
while (iter.hasNext()) {
RDFNode node = iter.nextNode();
Literal l = node.asLiteral();
//System.out.println(IRI + " " + preferredLanguage + " --> " + l.getLanguage() + " --> " + l.getLexicalForm());
//logger.debug(IRI + " " + preferredLanguage + " --> " + l.getLanguage() + " --> " + l.getLexicalForm());
if (!betterTitleMatch && (result.equals(defaultValue) || l.getLanguage().equals("en") || l.getLanguage().equals(preferredLanguage))) {
if (preferredLanguage.equals(l.getLanguage())) {
betterTitleMatch = true;
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/dvcama/lodview/conf/ConfigurationBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfigurationBean implements ServletContextAware, Cloneable {

final Logger logger = LoggerFactory.getLogger(ConfigurationBean.class);

protected Model confModel = null;
protected ServletContext context;
protected String confFile;
Expand Down Expand Up @@ -58,7 +63,7 @@ public ConfigurationBean() throws IOException, Exception {
}

public void populateBean() throws IOException, Exception {
System.out.println("Initializing configuration " + confFile);
logger.debug("Initializing configuration " + confFile);
File configFile = new File(confFile);
if (!configFile.isAbsolute()) {
configFile = new File(context.getRealPath("/") + "/WEB-INF/" + confFile);
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/dvcama/lodview/controllers/ErrorController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
public class ErrorController {

final Logger logger = LoggerFactory.getLogger(ErrorController.class);

@Autowired
ConfigurationBean conf;

Expand All @@ -29,7 +35,7 @@ public ErrorController() {
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE, reason = "unhandled encoding")
@RequestMapping(value = "/406")
public String error406(HttpServletResponse res, ModelMap model, @CookieValue(value = "colorPair") String colorPair) {
System.out.println("not acceptable");
logger.error("not acceptable");
model.addAttribute("statusCode", "406");
model.addAttribute("conf", conf);
colors(colorPair, res, model);
Expand All @@ -39,7 +45,7 @@ public String error406(HttpServletResponse res, ModelMap model, @CookieValue(val

@RequestMapping(value = "/404")
public String error404(HttpServletResponse res, ModelMap model, @RequestParam(value = "error", defaultValue = "") String error, @CookieValue(value = "colorPair", defaultValue = "") String colorPair, @RequestParam(value = "IRI", defaultValue = "") String IRI, @RequestParam(value = "endpoint", defaultValue = "") String endpoint) {
System.out.println("not found " + error + " -- " + IRI + " -- " + endpoint);
logger.error("not found " + error + " -- " + IRI + " -- " + endpoint);
/* spring bug? */
model.addAttribute("IRI", IRI);
model.addAttribute("endpoint", endpoint);
Expand All @@ -53,7 +59,7 @@ public String error404(HttpServletResponse res, ModelMap model, @RequestParam(va

@RequestMapping(value = "/400")
public String error400(HttpServletResponse res, ModelMap model, @RequestParam(value = "IRI", defaultValue = "") String IRI, @CookieValue(value = "colorPair", defaultValue = "") String colorPair) {
System.out.println("error on " + IRI);
logger.error("error on " + IRI);
/* spring bug? */
model.addAttribute("IRI", IRI.replaceAll("(http://.+),http://.+", "$1"));
model.addAttribute("conf", conf);
Expand All @@ -65,7 +71,7 @@ public String error400(HttpServletResponse res, ModelMap model, @RequestParam(va

@RequestMapping(value = { "/500", "/error" })
public String error500(HttpServletResponse res, ModelMap model, @RequestParam(value = "error", defaultValue = "") String error, @CookieValue(value = "colorPair", defaultValue = "") String colorPair, @RequestParam(value = "IRI", defaultValue = "") String IRI, @RequestParam(value = "endpoint", defaultValue = "") String endpoint) {
System.out.println("error on " + error + " -- " + IRI + " -- " + endpoint);
logger.error("error on " + error + " -- " + IRI + " -- " + endpoint);
/* spring bug? */
model.addAttribute("IRI", IRI);
model.addAttribute("endpoint", endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
public class LinkedResourcesController implements MessageSourceAware {

final Logger logger = LoggerFactory.getLogger(LinkedResourcesController.class);

@Autowired
private MessageSource messageSource;

Expand All @@ -51,7 +56,7 @@ public String resourceTitles(ModelMap model, HttpServletRequest req, HttpServlet
}

public String resourceTitles(ModelMap model, ConfigurationBean conf, HttpServletRequest req, HttpServletResponse res, Locale locale, String IRI, String[] abouts) throws IOException, Exception {
// System.out.println("LinkedResourcesController.resourceTitles() locale: "
// logger.trace("LinkedResourcesController.resourceTitles() locale: "
// + locale.getLanguage());
StringBuilder result = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>");
try {
Expand Down Expand Up @@ -84,7 +89,7 @@ public String resourceInversesController(ModelMap model, HttpServletRequest req,

public String resourceInverses(ModelMap model, ConfigurationBean conf, OntologyBean ontoBean, HttpServletRequest req, HttpServletResponse res, Locale locale, String IRI, String property, int start) throws IOException, Exception {
StringBuilder result = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>");
// System.out.println("LinkedResourcesController.resourceInverses()");
// logger.trace("LinkedResourcesController.resourceInverses()");
if (property.equals("")) {
/* counting inverse relations */
try {
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/dvcama/lodview/controllers/LodController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
public class LodController {

final Logger logger = LoggerFactory.getLogger(LodController.class);

@Autowired
ConfigurationBean confLinked;

Expand All @@ -34,13 +39,13 @@ public class LodController {
public String resource(HttpServletRequest req, HttpServletResponse res, Locale locale, @RequestParam(value = "IRI") String IRI) throws IOException, Exception {

if (confLinked.getSkipDomains().contains(IRI.replaceAll("http[s]*://([^/]+)/.*", "$1"))) {
// System.out.println("LodController.resource() - skip - " + IRI);
// logger.info("LodController.resource() - skip - " + IRI);
return "<root error=\"true\" about=\"" + StringEscapeUtils.escapeXml11(IRI) + "\"><title>" + //
StringEscapeUtils.escapeXml11(messageSource.getMessage("error.skipedDomain", null, "skiping this URI", locale)) + //
"</title><msg><![CDATA[skiping this URI, probably offline]]></msg></root>";
}
try {
System.out.println(" LodController.resource() - load - " + IRI);
logger.info(" LodController.resource() - load - " + IRI);
/* TODO: change this in UNION queries for better performance */
ResultBean results = new ResourceBuilder(messageSource).buildHtmlResource(IRI, locale, confLinked, null, true);

Expand Down Expand Up @@ -102,7 +107,7 @@ public String resource(HttpServletRequest req, HttpServletResponse res, Locale l

} catch (Exception e) {
// e.printStackTrace();
System.out.println(IRI + " unable to retrieve data " + e.getMessage());
logger.error(IRI + " unable to retrieve data " + e.getMessage());
return "<root error=\"true\" about=\"" + StringEscapeUtils.escapeXml11(IRI) + "\"><title>" + //
messageSource.getMessage("error.linkedResourceUnavailable", null, "unable to retrieve data", locale) + //
"</title><msg><![CDATA[" + e.getMessage() + "]]></msg></root>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@

import com.hp.hpl.jena.rdf.model.ModelFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
@RequestMapping(value = "/")
public class ResourceController {

final Logger logger = LoggerFactory.getLogger(ResourceController.class);

@Autowired
private MessageSource messageSource;

Expand Down Expand Up @@ -88,8 +94,8 @@ public Object resource(ConfigurationBean conf, ModelMap model, HttpServletReques
String IRIsuffix = new UrlPathHelper().getLookupPathForRequest(req).replaceAll("/lodview/", "/");
String requestUrl = req.getRequestURI();

System.out.println("IRIsuffix " + IRIsuffix);
System.out.println("requestUrl " + requestUrl);
logger.info("IRIsuffix " + IRIsuffix);
logger.info("requestUrl " + requestUrl);

model.addAttribute("path", new UrlPathHelper().getContextPath(req).replaceAll("/lodview/", "/"));
model.addAttribute("locale", locale.getLanguage());
Expand Down Expand Up @@ -184,18 +190,18 @@ public Object resource(ConfigurationBean conf, ModelMap model, HttpServletReques
}
}

System.out.println("####################################################################");
System.out.println("################# looking for " + IRI + " ################# ");
logger.info("####################################################################");
logger.info("################# looking for " + IRI + " ################# ");

String[] acceptedContent = req.getHeader("Accept").split(",");
if (redirected) {
acceptedContent = "text/html".split(",");
}
// System.out.println("Accept " + req.getHeader("Accept"));
// log.trace("Accept " + req.getHeader("Accept"));

AcceptList a = AcceptList.create(acceptedContent);
// System.out.println("-- AcceptList: " + a);
// System.out.println("-- OffertList: " + offeringRDF);
// log.trace("-- AcceptList: " + a);
// log.trace("-- OffertList: " + offeringRDF);

MediaType matchItem = AcceptList.match(offeringRDF, a);
Lang lang = matchItem != null ? RDFLanguages.contentTypeToLang(matchItem.getContentType()) : null;
Expand All @@ -210,7 +216,7 @@ public Object resource(ConfigurationBean conf, ModelMap model, HttpServletReques
} catch (Exception e) {
return new ErrorController(conf).error406(res, model, colorPair);
}
System.out.println("override content type " + matchItem.getContentType());
logger.debug("override content type " + matchItem.getContentType());
}

try {
Expand Down Expand Up @@ -371,7 +377,7 @@ public ResponseEntity<String> resourceRawController(ModelMap model, @RequestPara
}

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()");
// logger.trace("ResourceController.resourceRaw()");
contentType = contentType.replaceAll("([a-zA-Z]) ([a-zA-Z])", "$1+$2");
Lang lang = RDFLanguages.contentTypeToLang(contentType);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.UrlPathHelper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
public class StaticController {

final Logger logger = LoggerFactory.getLogger(StaticController.class);

@Autowired
ConfigurationBean conf;

Expand All @@ -44,7 +50,7 @@ public String home(HttpServletRequest req, HttpServletResponse res, Model model,
model.addAttribute("conf", conf);
model.addAttribute("locale", locale.getLanguage());
model.addAttribute("path", new UrlPathHelper().getContextPath(req).replaceAll("/lodview/", "/"));
System.out.println("home controller");
logger.debug("home controller");
return "home";
}

Expand Down
Loading

0 comments on commit 7ce43c3

Please sign in to comment.