From d36bb46ac6d9ba7ee1403ccc5e8042eace9faef1 Mon Sep 17 00:00:00 2001 From: dvcama Date: Fri, 24 Jun 2016 12:31:08 +0200 Subject: [PATCH] rebase --- .gitignore | 3 + LICENSE | 7 + WebContent/META-INF/MANIFEST.MF | 3 + pom.xml | 151 ++++++ .../java/org/dvcama/lodview/SimpleGraph.java | 453 ++++++++++++++++++ .../lodview/proxy/controller/Common.java | 44 ++ .../proxy/controller/ResourceProxy.java | 77 +++ .../proxy/controller/SimpleEndpoint.java | 127 +++++ .../lodview/proxy/controller/SparqlProxy.java | 76 +++ src/main/java/org/dvcama/lodview/utils.java | 287 +++++++++++ src/main/resources/logback.xml | 26 + .../WEB-INF/spring/appServlet/controllers.xml | 15 + .../spring/appServlet/servlet-context.xml | 52 ++ .../webapp/WEB-INF/spring/root-context.xml | 8 + src/main/webapp/WEB-INF/views/error.jsp | 11 + src/main/webapp/WEB-INF/views/json.jsp | 1 + src/main/webapp/WEB-INF/web.xml | 55 +++ 17 files changed, 1396 insertions(+) create mode 100644 LICENSE create mode 100644 WebContent/META-INF/MANIFEST.MF create mode 100644 pom.xml create mode 100644 src/main/java/org/dvcama/lodview/SimpleGraph.java create mode 100644 src/main/java/org/dvcama/lodview/proxy/controller/Common.java create mode 100644 src/main/java/org/dvcama/lodview/proxy/controller/ResourceProxy.java create mode 100644 src/main/java/org/dvcama/lodview/proxy/controller/SimpleEndpoint.java create mode 100644 src/main/java/org/dvcama/lodview/proxy/controller/SparqlProxy.java create mode 100644 src/main/java/org/dvcama/lodview/utils.java create mode 100644 src/main/resources/logback.xml create mode 100644 src/main/webapp/WEB-INF/spring/appServlet/controllers.xml create mode 100644 src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml create mode 100644 src/main/webapp/WEB-INF/spring/root-context.xml create mode 100644 src/main/webapp/WEB-INF/views/error.jsp create mode 100644 src/main/webapp/WEB-INF/views/json.jsp create mode 100644 src/main/webapp/WEB-INF/web.xml diff --git a/.gitignore b/.gitignore index 23f6f63..6887286 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +<<<<<<< 0076a25b1f04963ac385c86d296f09a02052c433 *.class # Mobile Tools for Java (J2ME) @@ -10,6 +11,8 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +======= +>>>>>>> first working commit ################# ## Eclipse ################# diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..20a92c6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2014 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/WebContent/META-INF/MANIFEST.MF b/WebContent/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5e94951 --- /dev/null +++ b/WebContent/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5c22c66 --- /dev/null +++ b/pom.xml @@ -0,0 +1,151 @@ + + 4.0.0 + lodview + resource-to-sparqlresult + 0.0.1-SNAPSHOT + war + + + + UTF-8 + 1.7 + 4.0.6.RELEASE + 1.8.1 + 1.6.1 + + + + + + + + + org.springframework + spring-webmvc + ${org.springframework-version} + + + + + org.slf4j + slf4j-api + 1.7.1 + + + org.slf4j + jcl-over-slf4j + 1.7.1 + + + ch.qos.logback + logback-classic + 1.0.7 + + + + + org.apache.tomcat + tomcat-servlet-api + 7.0.30 + provided + + + javax.servlet.jsp + jsp-api + 2.1 + provided + + + javax.servlet.jsp.jstl + jstl-api + 1.2 + + + javax.servlet + servlet-api + + + + + org.glassfish.web + jstl-impl + 1.2 + + + javax.servlet + servlet-api + + + + + + + + org.apache.commons + commons-lang3 + 3.3.1 + + + + org.openrdf.sesame + sesame-runtime + 4.1.2 + + + + + + ${project.artifactId} + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + ${java-version} + ${java-version} + + + + org.apache.maven.plugins + maven-dependency-plugin + + + install + install + + sources + + + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.0 + + + org.eclipse.jetty + jetty-maven-plugin + 9.0.6.v20130930 + + + /${project.artifactId} + + + + + + + + io.spring.repo.maven.milestone + http://repo.spring.io/milestone/ + + false + + + + + diff --git a/src/main/java/org/dvcama/lodview/SimpleGraph.java b/src/main/java/org/dvcama/lodview/SimpleGraph.java new file mode 100644 index 0000000..01f3d65 --- /dev/null +++ b/src/main/java/org/dvcama/lodview/SimpleGraph.java @@ -0,0 +1,453 @@ +package org.dvcama.lodview; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.StringReader; +import java.io.StringWriter; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Set; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import org.openrdf.model.BNode; +import org.openrdf.model.Resource; +import org.openrdf.model.Statement; +import org.openrdf.model.URI; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.model.vocabulary.RDF; +import org.openrdf.query.BindingSet; +import org.openrdf.query.GraphQuery; +import org.openrdf.query.TupleQuery; +import org.openrdf.query.TupleQueryResult; +import org.openrdf.query.resultio.sparqljson.SPARQLResultsJSONWriter; +import org.openrdf.repository.Repository; +import org.openrdf.repository.RepositoryConnection; +import org.openrdf.repository.RepositoryException; +import org.openrdf.repository.RepositoryResult; +import org.openrdf.repository.sail.SailRepository; +import org.openrdf.rio.RDFFormat; +import org.openrdf.rio.RDFWriter; +import org.openrdf.rio.Rio; +import org.openrdf.sail.inferencer.fc.ForwardChainingRDFSInferencer; +import org.openrdf.sail.memory.MemoryStore; + +public class SimpleGraph { + + Repository therepository = null; + + // useful -local- constants + static RDFFormat NTRIPLES = RDFFormat.NTRIPLES; + static RDFFormat N3 = RDFFormat.N3; + static RDFFormat RDFXML = RDFFormat.RDFXML; + static String RDFTYPE = RDF.TYPE.toString(); + + /** + * In memory Sesame repository without type inferencing + */ + public SimpleGraph() { + this(false); + } + + /** + * In memory Sesame Repository with optional inferencing + * + * @param inferencing + */ + public SimpleGraph(boolean inferencing) { + try { + if (inferencing) { + therepository = new SailRepository(new ForwardChainingRDFSInferencer(new MemoryStore())); + + } else { + therepository = new SailRepository(new MemoryStore()); + } + therepository.initialize(); + } catch (RepositoryException e) { + e.printStackTrace(); + } + } + + /** + * Literal factory + * + * @param s + * the literal value + * @param typeuri + * uri representing the type (generally xsd) + * @return + */ + public org.openrdf.model.Literal Literal(String s, URI typeuri) { + try { + RepositoryConnection con = therepository.getConnection(); + try { + ValueFactory vf = con.getValueFactory(); + if (typeuri == null) { + return vf.createLiteral(s); + } else { + return vf.createLiteral(s, typeuri); + } + } finally { + con.close(); + } + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * Untyped Literal factory + * + * @param s + * the literal + * @return + */ + public org.openrdf.model.Literal Literal(String s) { + return Literal(s, null); + } + + /** + * URIref factory + * + * @param uri + * @return + */ + public URI URIref(String uri) { + try { + RepositoryConnection con = therepository.getConnection(); + try { + ValueFactory vf = con.getValueFactory(); + return vf.createURI(uri); + } finally { + con.close(); + } + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * BNode factory + * + * @return + */ + public BNode bnode() { + try { + RepositoryConnection con = therepository.getConnection(); + try { + ValueFactory vf = con.getValueFactory(); + return vf.createBNode(); + } finally { + con.close(); + } + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * dump RDF graph + * + * @param out + * output stream for the serialization + * @param outform + * the RDF serialization format for the dump + * @return + */ + public void dumpRDF(OutputStream out, RDFFormat outform) { + try { + RepositoryConnection con = therepository.getConnection(); + try { + RDFWriter w = Rio.createWriter(outform, out); + con.export(w, new Resource[0]); + } finally { + con.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Convenience URI import for RDF/XML sources + * + * @param urlstring + * absolute URI of the data source + */ + public void addURI(String urlstring) { + addURI(urlstring, RDFFormat.RDFXML); + } + + /** + * Import data from URI source Request is made with proper HTTP ACCEPT + * header and will follow redirects for proper LOD source negotiation + * + * @param urlstring + * absolute URI of the data source + * @param format + * RDF format to request/parse from data source + */ + public void addURI(String urlstring, RDFFormat format) { + try { + RepositoryConnection con = therepository.getConnection(); + try { + con.add(new URL(urlstring), null, format); + } finally { + con.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Import RDF data from a string + * + * @param rdfstring + * string with RDF data + * @param format + * RDF format of the string (used to select parser) + * @throws Exception + */ + public void addString(String rdfstring, RDFFormat format) throws Exception { + try { + RepositoryConnection con = therepository.getConnection(); + try { + StringReader sr = new StringReader(rdfstring); + con.add(sr, "", format, new Resource[0]); + } finally { + con.close(); + } + } catch (Exception e) { + throw e; + } + } + + /** + * Import RDF data from a file + * + * @param location + * of file (/path/file) with RDF data + * @param format + * RDF format of the string (used to select parser) + */ + public void addFile(String filepath, RDFFormat format) { + try { + RepositoryConnection con = therepository.getConnection(); + try { + con.add(new File(filepath), "", format, new Resource[0]); + } finally { + con.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Insert Triple/Statement into graph + * + * @param s + * subject uriref + * @param p + * predicate uriref + * @param o + * value object (URIref or Literal) + */ + public void add(URI s, URI p, Value o) { + try { + RepositoryConnection con = therepository.getConnection(); + try { + ValueFactory myFactory = con.getValueFactory(); + Statement st = myFactory.createStatement((Resource) s, p, (Value) o); + con.add(st, new Resource[0]); + } finally { + con.close(); + } + } catch (Exception e) { + // handle exception + } + } + + /** + * Tuple pattern query - find all statements with the pattern, where null is + * a wild card + * + * @param s + * subject (null for wildcard) + * @param p + * predicate (null for wildcard) + * @param o + * object (null for wildcard) + * @return serialized graph of results + */ + public List tuplePattern(URI s, URI p, Value o) { + try { + RepositoryConnection con = therepository.getConnection(); + try { + RepositoryResult repres = con.getStatements(s, p, o, true, new Resource[0]); + ArrayList reslist = new ArrayList(); + while (repres.hasNext()) { + reslist.add(repres.next()); + } + return reslist; + } finally { + con.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * Execute a CONSTRUCT/DESCRIBE SPARQL query against the graph + * + * @param qs + * CONSTRUCT or DESCRIBE SPARQL query + * @param format + * the serialization format for the returned graph + * @return serialized graph of results + */ + public String runSPARQL(String qs, RDFFormat format) { + try { + RepositoryConnection con = therepository.getConnection(); + try { + GraphQuery query = con.prepareGraphQuery(org.openrdf.query.QueryLanguage.SPARQL, qs); + StringWriter stringout = new StringWriter(); + RDFWriter w = Rio.createWriter(format, stringout); + query.evaluate(w); + return stringout.toString(); + } finally { + con.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public void runJsonSPARQL(String qs, OutputStream servletOutputStream) { + try { + /* + * fix for Exception in thread "main" + * javax.net.ssl.SSLHandshakeException: + * sun.security.validator.ValidatorException: PKIX path building + * failed: + * sun.security.provider.certpath.SunCertPathBuilderException: + * unable to find valid certification path to requested target + */ + TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + + } }; + + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + + // Create all-trusting host name verifier + HostnameVerifier allHostsValid = new HostnameVerifier() { + public boolean verify(String hostname, SSLSession session) { + return true; + } + }; + // Install the all-trusting host verifier + HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); + /* + * end of the fix + */ + + RepositoryConnection con = therepository.getConnection(); + try { + TupleQuery query = con.prepareTupleQuery(org.openrdf.query.QueryLanguage.SPARQL, qs); + TupleQueryResult qres = query.evaluate(); + + org.openrdf.query.resultio.sparqljson.SPARQLResultsJSONWriter jsonWriter = new SPARQLResultsJSONWriter( + servletOutputStream); + jsonWriter.startQueryResult(qres.getBindingNames()); + while (qres.hasNext()) { + jsonWriter.handleSolution(qres.next()); + } + jsonWriter.endQueryResult(); + + } finally { + con.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Execute a SELECT SPARQL query against the graph + * + * @param qs + * SELECT SPARQL query + * @return list of solutions, each containing a hashmap of bindings + */ + public List runSPARQL(String qs) { + try { + RepositoryConnection con = therepository.getConnection(); + try { + TupleQuery query = con.prepareTupleQuery(org.openrdf.query.QueryLanguage.SPARQL, qs); + TupleQueryResult qres = query.evaluate(); + ArrayList reslist = new ArrayList(); + while (qres.hasNext()) { + BindingSet b = qres.next(); + Set names = b.getBindingNames(); + HashMap hm = new HashMap(); + for (Object n : names) { + hm.put((String) n, b.getValue((String) n)); + } + reslist.add(hm); + } + return reslist; + } finally { + con.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +// public static void main(String[] args) throws MalformedURLException, IOException { +// +// // create a graph with type inferencing +// SimpleGraph g = new SimpleGraph(false); +// +// // load the film schema and the example data +// // g.addFile("film-ontology.owl", SimpleGraph.RDFXML); +// System.out.println("adding"); +// g.addURI("http://dbpedia.org/resource/Roma", SimpleGraph.RDFXML); +// // g.addString(s, RDFFormat.RDFXML); +// System.out.println("added"); +// g.runJsonSPARQL("SELECT DISTINCT * WHERE {?s ?property ?object} ORDER BY ?property", System.out); +// +// } +} diff --git a/src/main/java/org/dvcama/lodview/proxy/controller/Common.java b/src/main/java/org/dvcama/lodview/proxy/controller/Common.java new file mode 100644 index 0000000..8257bf6 --- /dev/null +++ b/src/main/java/org/dvcama/lodview/proxy/controller/Common.java @@ -0,0 +1,44 @@ +package org.dvcama.lodview.proxy.controller; + +import java.io.UnsupportedEncodingException; + +import javax.servlet.ServletContext; +import javax.xml.transform.TransformerException; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.context.ServletContextAware; + +@Controller +@RequestMapping(value = { "" }) +public class Common implements ApplicationContextAware, ServletContextAware { + String errorMessage = ""; + + ApplicationContext applicationContext; + ServletContext servletContext; + + + @RequestMapping(value = "error.html") + public String rdf(ModelMap model) throws UnsupportedEncodingException, TransformerException { + String errorMsg = "sorry, an error occured"; + model.addAttribute("errorMsg", errorMsg); + return "error"; + } + + + + public void setServletContext(ServletContext arg0) { + // TODO Auto-generated method stub + + } + + public void setApplicationContext(ApplicationContext arg0) throws BeansException { + // TODO Auto-generated method stub + + } + +} diff --git a/src/main/java/org/dvcama/lodview/proxy/controller/ResourceProxy.java b/src/main/java/org/dvcama/lodview/proxy/controller/ResourceProxy.java new file mode 100644 index 0000000..144a1bc --- /dev/null +++ b/src/main/java/org/dvcama/lodview/proxy/controller/ResourceProxy.java @@ -0,0 +1,77 @@ +package org.dvcama.lodview.proxy.controller; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.xml.transform.TransformerException; + +import org.dvcama.lodview.SimpleGraph; +import org.openrdf.rio.RDFFormat; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.context.ServletContextAware; + +@Controller +@RequestMapping(value = { "resource" }) +public class ResourceProxy implements ApplicationContextAware, ServletContextAware { + String errorMessage = ""; + + ApplicationContext applicationContext; + ServletContext servletContext; + + @RequestMapping(value = "") + public String proxy(ModelMap model, HttpServletRequest req, HttpServletResponse res) + throws UnsupportedEncodingException, TransformerException { + String uri = req.getParameter("uri"); + String query = req.getParameter("query"); + String callback = req.getParameter("callback"); + + try { + + SimpleGraph g = new SimpleGraph(true); + try { + g.addURI(uri); + } catch (Exception e) { + g.addURI(uri, RDFFormat.NTRIPLES); + } + + OutputStream stream = new ByteArrayOutputStream(); + g.runJsonSPARQL(java.net.URLDecoder.decode(query, "UTF-8"), stream); + String result = stream.toString(); + result = result.replaceAll("\\\\/", "/"); + // System.out.println(result); + model.addAttribute("result", callback + "(" + result + ")"); + + res.addHeader("Via", "corsResourceProxy"); + res.setHeader("Content-Type", "application/json"); + res.addHeader("Access-Control-Allow-Origin", "*"); + // model.addAttribute("result", resultMap.get("result")); + } catch (Exception e) { + e.printStackTrace(); + errorMessage = "risorsa non disponibile"; + res.setStatus(400); + model.addAttribute("errorMsg", e.getMessage()); + return "error"; + } + return "json"; + } + + public void setServletContext(ServletContext arg0) { + // TODO Auto-generated method stub + + } + + public void setApplicationContext(ApplicationContext arg0) throws BeansException { + // TODO Auto-generated method stub + + } + +} diff --git a/src/main/java/org/dvcama/lodview/proxy/controller/SimpleEndpoint.java b/src/main/java/org/dvcama/lodview/proxy/controller/SimpleEndpoint.java new file mode 100644 index 0000000..976d026 --- /dev/null +++ b/src/main/java/org/dvcama/lodview/proxy/controller/SimpleEndpoint.java @@ -0,0 +1,127 @@ +package org.dvcama.lodview.proxy.controller; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.xml.transform.TransformerException; + +import org.apache.commons.lang3.StringUtils; +import org.dvcama.lodview.SimpleGraph; +import org.openrdf.rio.RDFFormat; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.context.ServletContextAware; + +@Controller +@RequestMapping(value = { "/endpoint" }) +public class SimpleEndpoint implements ApplicationContextAware, ServletContextAware { + String errorMessage = ""; + + ApplicationContext applicationContext; + ServletContext servletContext; + + @RequestMapping(value = "/sparql") + public String query(ModelMap model, HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException, TransformerException { + SimpleGraph g = null; + String query = req.getParameter("query"); + String callback = req.getParameter("callback"); + String id = StringUtils.substringBetween(query, "lodlive.it_", "?"); + //System.out.println("id---Z> " + id); + HttpSession session = req.getSession().getSessionContext().getSession(id); + + if (session.getAttribute("currentEndpoint") != null) { + g = (SimpleGraph) session.getAttribute("currentEndpoint"); + } else { + errorMessage = "risorsa non disponibile"; + res.setStatus(400); + model.addAttribute("errorMsg", errorMessage); + return "error"; + } + + System.out.println("query " + java.net.URLDecoder.decode(query, "UTF-8")); + OutputStream stream = new ByteArrayOutputStream(); + g.runJsonSPARQL(java.net.URLDecoder.decode(query, "UTF-8"), stream); + String result = stream.toString(); + result = result.replaceAll("\\\\/", "/"); + //System.out.println(result); + + model.addAttribute("result", callback + "(" + result + ")"); + + res.addHeader("Via", "corsResourceProxy"); + res.setHeader("Content-Type", "application/json"); + res.addHeader("Access-Control-Allow-Origin", "*"); + + return "json"; + } + + @RequestMapping(value = "") + public String rdf(ModelMap model, HttpServletRequest req) throws UnsupportedEncodingException, TransformerException { + // System.out.println(req.getSession().getId()); + String data = req.getParameter("data"); + String prefixes = req.getParameter("prefixes"); + String resource = req.getParameter("resource"); + if (data == null || prefixes == null || data.equals("") || prefixes.equals("")) { + errorMessage = "you have to provide some data and one or more prefixes of your resources
use \"data\" and \"prefixes\" params"; + model.addAttribute("errorMessage", errorMessage); + return "error"; + } + + String id = req.getSession().getId(); + if (req.getParameter("clear") != null && req.getParameter("clear").equals("true")) { + req.getSession().removeAttribute("currentEndpoint"); + } + + String[] prfixesList = prefixes.split(";"); + for (int i = 0; i < prfixesList.length; i++) { + data = data.replaceAll("^<(" + prfixesList[i] + "[^>]*)>", ""); + data = data.replaceAll("<(" + prfixesList[i] + "[^>]*)$", "", "resource=\"http://lodlive.it_" + id + "?$1"); + data = data.replaceAll("about=\"(" + prfixesList[i] + "[^\"]*\")>", "about=\"http://lodlive.it_" + id + "?$1"); + data = data.replaceAll("base=\"(" + prfixesList[i] + "[^\"]*\")>", "base=\"http://lodlive.it_" + id + "?$1"); + } + //System.out.println(data); + SimpleGraph g = null; + if (req.getSession().getAttribute("currentEndpoint") != null) { + g = (SimpleGraph) req.getSession().getAttribute("currentEndpoint"); + } else { + g = new SimpleGraph(true); + } + try { + g.addString(data, RDFFormat.RDFXML); + } catch (Exception e) { + System.out.println("NOT VALID RDF/XML"); + + } + try { + g.addString(data, RDFFormat.NTRIPLES); + } catch (Exception e) { + System.out.println("NOT VALID NTRIPLES"); + } + + req.getSession().setAttribute("currentEndpoint", g); + + model.addAttribute("resource", resource); + g.dumpRDF(System.out, RDFFormat.NTRIPLES); + return "redirect"; + } + + public void setServletContext(ServletContext arg0) { + // TODO Auto-generated method stub + + } + + public void setApplicationContext(ApplicationContext arg0) throws BeansException { + // TODO Auto-generated method stub + + } + +} diff --git a/src/main/java/org/dvcama/lodview/proxy/controller/SparqlProxy.java b/src/main/java/org/dvcama/lodview/proxy/controller/SparqlProxy.java new file mode 100644 index 0000000..3f9d626 --- /dev/null +++ b/src/main/java/org/dvcama/lodview/proxy/controller/SparqlProxy.java @@ -0,0 +1,76 @@ +package org.dvcama.lodview.proxy.controller; + +import java.io.UnsupportedEncodingException; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.xml.transform.TransformerException; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.context.ServletContextAware; + +@Controller +@RequestMapping(value = { "sparqlProxy" }) +public class SparqlProxy implements ApplicationContextAware, ServletContextAware { + String errorMessage = ""; + + ApplicationContext applicationContext; + ServletContext servletContext; + + + @RequestMapping(value = "error.html") + public String rdf(ModelMap model) throws UnsupportedEncodingException, TransformerException { + model.addAttribute("errorMsg", errorMessage); + return "error"; + } + + @RequestMapping(value = "") + public String proxy(ModelMap model, HttpServletRequest req, HttpServletResponse res, @RequestParam String query, @RequestParam String endpoint, @RequestParam String callback) throws UnsupportedEncodingException, TransformerException { + try { + Map resultMap = org.dvcama.lodview.utils.PostForRDF(endpoint+"?query="+java.net.URLEncoder.encode(query,"UTF-8"),"application/sparql-results+json"); + @SuppressWarnings("unchecked") + Map> hf = (Map>) resultMap.get("headerFields"); + + for (String field : hf.keySet()) { + if (field != null) { + try { + for (int i = 0; i < hf.get(field).size(); i++) { + // do we want the original headers? + // res.addHeader(field, hf.get(field).get(i)); + } + } catch (Exception e) { + // TODO: handle exception + } + } + } + res.addHeader("Via", "rdfSparqlProxy"); + model.addAttribute("result", callback + "(" + resultMap.get("result") + ")"); + } catch (Exception e) { + e.printStackTrace(); + res.setStatus(400); + model.addAttribute("errorMsg", e.getMessage()); + return "error"; + } + return "json"; + } + + public void setServletContext(ServletContext arg0) { + // TODO Auto-generated method stub + + } + + public void setApplicationContext(ApplicationContext arg0) throws BeansException { + // TODO Auto-generated method stub + + } + +} diff --git a/src/main/java/org/dvcama/lodview/utils.java b/src/main/java/org/dvcama/lodview/utils.java new file mode 100644 index 0000000..b9befe7 --- /dev/null +++ b/src/main/java/org/dvcama/lodview/utils.java @@ -0,0 +1,287 @@ +package org.dvcama.lodview; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import org.apache.http.Header; +import org.apache.http.HeaderElement; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpVersion; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.params.BasicHttpParams; +import org.apache.http.params.HttpParams; +import org.apache.http.params.HttpProtocolParams; +import org.apache.http.protocol.HTTP; +import org.apache.http.util.EntityUtils; +import org.openrdf.rio.RDFFormat; + +public class utils { + public static void main(String[] args) throws MalformedURLException, IOException, NoSuchAlgorithmException, KeyManagementException { + System.out.println(PostForJson("select * where {?f ?H ?J} LIMIT 10", "http://worldbank.270a.info/sparql").get("result")); + // System.out.println(RDFFormat.RDFXML.getDefaultMIMEType()); + // System.out.println(PostForRDF("http://rdf.freebase.com/ns/en.parliament_of_italy")); + + } + + public static Map PostForJson(String query, String endpoint) throws MalformedURLException, IOException, NoSuchAlgorithmException, KeyManagementException { + + /* + * fix for Exception in thread "main" + * javax.net.ssl.SSLHandshakeException: + * sun.security.validator.ValidatorException: PKIX path building failed: + * sun.security.provider.certpath.SunCertPathBuilderException: unable to + * find valid certification path to requested target + */ + TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + + } }; + + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + + // Create all-trusting host name verifier + HostnameVerifier allHostsValid = new HostnameVerifier() { + public boolean verify(String hostname, SSLSession session) { + return true; + } + }; + // Install the all-trusting host verifier + HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); + /* + * end of the fix + */ + + HttpGet httppost = new HttpGet(endpoint + "?query=" + URLEncoder.encode(URLDecoder.decode(query, "UTF-8"), "UTF-8")); + HttpClient httpclient = new DefaultHttpClient(); + List nameValuePairs = new ArrayList(2); + nameValuePairs.add(new BasicNameValuePair("query", URLDecoder.decode(query, "UTF-8"))); + nameValuePairs.add(new BasicNameValuePair("output", "json")); + httppost.setHeader("Accept-Charset", "utf-8"); + httppost.setHeader("Accept", "application/sparql-results+json"); + // Execute HTTP Post Request URLDecoder.decode(query, "UTF-8") + // System.out.println(httppost.getRequestLine()); + HttpResponse response = httpclient.execute(httppost); + String httpResult = EntityUtils.toString(response.getEntity()); + // System.out.println(httpResult); + + Map resultMap = new HashMap(); + resultMap.put("result", httpResult); + Map> hf = new HashMap>(); + + for (Header nameValuePair : response.getAllHeaders()) { + String name = nameValuePair.getName(); + List aH = new ArrayList(); + for (HeaderElement a : nameValuePair.getElements()) { + aH.add(a.getValue()); + } + hf.put(name, aH); + } + + resultMap.put("headerFields", hf); + return resultMap; + } + + public static Map PostForJsonOld(String query, String endpoint) throws MalformedURLException, IOException { + String data = ""; + if (query != null) { + query = URLDecoder.decode(query, "UTF-8"); + data = "query=" + URLEncoder.encode(query, "UTF-8"); + } + Map resultMap = new HashMap(); + String result = ""; + URLConnection conn = new URL(endpoint).openConnection(); + conn.setRequestProperty("Accept-Charset", "utf-8"); + System.out.println("conn " + conn.getURL()); + + conn.setRequestProperty("Accept", "application/sparql-results+json"); + conn.setDoOutput(true); + + OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); + wr.write(data); + wr.flush(); + // Get the response + System.out.println("reading output "); + BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); + + Map> hf = conn.getHeaderFields(); + String line; + while ((line = rd.readLine()) != null) { + result += line + "\n"; + } + System.err.println(result); + System.out.println("ended "); + wr.close(); + rd.close(); + + resultMap.put("result", result); + resultMap.put("headerFields", hf); + return resultMap; + } + + public static Map getRDF(String endpoint) throws MalformedURLException, IOException, NoSuchAlgorithmException, KeyManagementException { + + /* + * fix for Exception in thread "main" + * javax.net.ssl.SSLHandshakeException: + * sun.security.validator.ValidatorException: PKIX path building failed: + * sun.security.provider.certpath.SunCertPathBuilderException: unable to + * find valid certification path to requested target + */ + TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + + } }; + + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + + // Create all-trusting host name verifier + HostnameVerifier allHostsValid = new HostnameVerifier() { + public boolean verify(String hostname, SSLSession session) { + return true; + } + }; + // Install the all-trusting host verifier + HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); + /* + * end of the fix + */ + + Map resultMap = new HashMap(); + HttpParams params = new BasicHttpParams(); + HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); + HttpProtocolParams.setContentCharset(params, "utf-8"); + HttpProtocolParams.setHttpElementCharset(params, "utf-8"); + params.setBooleanParameter("http.protocol.expect-continue", false); + HttpClient httpclient = new DefaultHttpClient(params); + + HttpGet httpGet = new HttpGet(endpoint); + httpGet.addHeader("Accept", RDFFormat.RDFXML.getDefaultMIMEType()); + httpGet.addHeader("Accept-Charset", "utf-8"); + + System.out.println("connected to " + endpoint); + HttpResponse httpResponse = httpclient.execute(httpGet); + HttpEntity httpEntity = httpResponse.getEntity(); + // System.out.println(httpResponse.getLocale()); + // System.out.println(EntityUtils.getContentCharSet(httpEntity)); + String httpResult = EntityUtils.toString(httpEntity, HTTP.UTF_8); + // System.out.println("ended "); + // System.out.println(httpResult); + + resultMap.put("result", httpResult); + resultMap.put("headerFields", null); + return resultMap; + } + + public static Map PostForRDF(String endpoint, String format) throws MalformedURLException, IOException, NoSuchAlgorithmException, KeyManagementException { + + /* + * fix for Exception in thread "main" + * javax.net.ssl.SSLHandshakeException: + * sun.security.validator.ValidatorException: PKIX path building failed: + * sun.security.provider.certpath.SunCertPathBuilderException: unable to + * find valid certification path to requested target + */ + TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + + } }; + + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + + // Create all-trusting host name verifier + HostnameVerifier allHostsValid = new HostnameVerifier() { + public boolean verify(String hostname, SSLSession session) { + return true; + } + }; + // Install the all-trusting host verifier + HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); + /* + * end of the fix + */ + + Map resultMap = new HashMap(); + String result = ""; + URLConnection conn = new URL(endpoint).openConnection(); + + conn.setRequestProperty("Accept", format); + conn.setRequestProperty("Accept-Charset", "utf-8"); + + System.out.println("connected to " + endpoint); + conn.setDoOutput(true); + + // Get the response + // System.out.println("reading output "); + BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); + Map> hf = conn.getHeaderFields(); + String line = new String(new byte[0], Charset.forName("UTF-8")); + while ((line = rd.readLine()) != null) { + result += line + "\n"; + } + System.out.println("ended "); + // System.out.println(result); + + rd.close(); + resultMap.put("result", result); + resultMap.put("headerFields", hf); + return resultMap; + } + +} diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..80e6c5c --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,26 @@ + + + + + + true + + + + + + + + %-5level %logger{0} - %msg%n + + + + + + + + + + diff --git a/src/main/webapp/WEB-INF/spring/appServlet/controllers.xml b/src/main/webapp/WEB-INF/spring/appServlet/controllers.xml new file mode 100644 index 0000000..0ca00b6 --- /dev/null +++ b/src/main/webapp/WEB-INF/spring/appServlet/controllers.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml b/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml new file mode 100644 index 0000000..1ce6081 --- /dev/null +++ b/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/spring/root-context.xml b/src/main/webapp/WEB-INF/spring/root-context.xml new file mode 100644 index 0000000..6435207 --- /dev/null +++ b/src/main/webapp/WEB-INF/spring/root-context.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/main/webapp/WEB-INF/views/error.jsp b/src/main/webapp/WEB-INF/views/error.jsp new file mode 100644 index 0000000..c3d39dd --- /dev/null +++ b/src/main/webapp/WEB-INF/views/error.jsp @@ -0,0 +1,11 @@ + + + + +rdf sparql proxy error + + + oops - error! +
${errorMsg} ${errorMessage} + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/views/json.jsp b/src/main/webapp/WEB-INF/views/json.jsp new file mode 100644 index 0000000..967faeb --- /dev/null +++ b/src/main/webapp/WEB-INF/views/json.jsp @@ -0,0 +1 @@ +${result} \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..56730b2 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,55 @@ + + + + contextConfigLocation + /WEB-INF/spring/root-context.xml + + + org.springframework.web.context.ContextLoaderListener + + + appServlet + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + /WEB-INF/spring/appServlet/servlet-context.xml + + 1 + true + + + appServlet + / + + + encodingFilter + org.springframework.web.filter.CharacterEncodingFilter + + encoding + UTF-8 + + + forceEncoding + true + + + + encodingFilter + /* + + + 404 + /error + + + 500 + /error + + + 406 + /error + + + + + \ No newline at end of file