Strabon
changeset 483:6132628057ef
completed DescribeBean and added "describe" command to endpoint script
author | Babis Nikolaou <charnik@di.uoa.gr> |
---|---|
date | Fri Jul 20 19:07:01 2012 +0300 (2012-07-20) |
parents | 2e35628b10d0 |
children | 119576ba1127 |
files | endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/DescribeBean.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StoreBean.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java scripts/endpoint |
line diff
1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/DescribeBean.java Fri Jul 20 15:23:38 2012 +0300 1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/DescribeBean.java Fri Jul 20 19:07:01 2012 +0300 1.3 @@ -1,14 +1,15 @@ 1.4 package eu.earthobservatory.org.StrabonEndpoint; 1.5 1.6 +import java.io.ByteArrayOutputStream; 1.7 import java.io.IOException; 1.8 import java.io.PrintWriter; 1.9 import java.io.UnsupportedEncodingException; 1.10 import java.net.URLDecoder; 1.11 import java.net.URLEncoder; 1.12 -import java.util.HashMap; 1.13 +import java.util.ArrayList; 1.14 +import java.util.Arrays; 1.15 import java.util.Iterator; 1.16 import java.util.List; 1.17 -import java.util.Map; 1.18 1.19 import javax.servlet.ServletConfig; 1.20 import javax.servlet.ServletContext; 1.21 @@ -17,8 +18,8 @@ 1.22 import javax.servlet.http.HttpServletRequest; 1.23 import javax.servlet.http.HttpServletResponse; 1.24 1.25 -import org.slf4j.Logger; 1.26 -import org.slf4j.LoggerFactory; 1.27 +import org.apache.commons.lang.StringEscapeUtils; 1.28 +import org.openrdf.rio.RDFFormat; 1.29 import org.springframework.web.context.WebApplicationContext; 1.30 import org.springframework.web.context.support.WebApplicationContextUtils; 1.31 1.32 @@ -26,8 +27,6 @@ 1.33 1.34 public class DescribeBean extends HttpServlet{ 1.35 1.36 - private static Logger logger = LoggerFactory.getLogger(eu.earthobservatory.org.StrabonEndpoint.DescribeBean.class); 1.37 - 1.38 private static final long serialVersionUID = -7541662133934957148L; 1.39 1.40 /** 1.41 @@ -44,123 +43,44 @@ 1.42 } 1.43 1.44 @Override 1.45 - public void doPost(HttpServletRequest request, HttpServletResponse response) 1.46 - throws ServletException, IOException 1.47 - { 1.48 - final class DataHive{ 1.49 - private String format; 1.50 - private String SPARQLQuery; 1.51 - private String errorMessage; 1.52 + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 1.53 + request.setCharacterEncoding("UTF-8"); 1.54 1.55 - DataHive(){ 1.56 - this.format = null; 1.57 - this.SPARQLQuery = null; 1.58 - this.errorMessage = null; 1.59 + String query = request.getParameter("SPARQLQuery"); 1.60 + if (query != null) { 1.61 + query = URLDecoder.decode(request.getParameter("SPARQLQuery"), "UTF-8"); 1.62 + } 1.63 + 1.64 + String reqFormat = request.getParameter("format"); 1.65 + //String reqAccept = request.getHeader("accept"); 1.66 + 1.67 + if (reqFormat == null || reqFormat.equals("HTML")) { 1.68 + response.setContentType("text/html; charset=UTF-8"); 1.69 + reqFormat = "HTML"; 1.70 + 1.71 + PrintWriter out = response.getWriter(); 1.72 + 1.73 + appendHTML1a(out, ""); 1.74 + appendHTMLQ(out, strabonWrapper); 1.75 + appendHTML1b(out); 1.76 + 1.77 + if (query != null) { 1.78 + out.write(query); 1.79 } 1.80 1.81 - public String getSPARQLQuery() { 1.82 - return SPARQLQuery; 1.83 - } 1.84 - 1.85 - public void setSPARQLQuery(String sPARQLQuery) { 1.86 - SPARQLQuery = sPARQLQuery; 1.87 - } 1.88 - 1.89 - public String getFormat() { 1.90 - return format; 1.91 - } 1.92 - 1.93 - public void setFormat(String fFormat) { 1.94 - format = fFormat; 1.95 - } 1.96 - 1.97 - public String getErrorMessage() { 1.98 - return errorMessage; 1.99 - } 1.100 - 1.101 - public void setErrorMessage(String error) { 1.102 - this.errorMessage = error; 1.103 - } 1.104 - 1.105 - public String toString() { 1.106 - return "Format: " + (this.format != null ? this.format : " NULL") + 1.107 - ", SPARQLQuery: " + (this.SPARQLQuery != null ? this.SPARQLQuery : " NULL") + 1.108 - ", errormessage: " + (this.errorMessage != null ? this.errorMessage : " NULL") + "."; 1.109 - } 1.110 - } 1.111 - 1.112 - request.setCharacterEncoding("UTF-8"); 1.113 - 1.114 - DataHive hive = new DataHive(); 1.115 - 1.116 - String query = request.getParameter("SPARQLQuery"); 1.117 - String q = (query == null) ? null : URLDecoder.decode(request.getParameter("SPARQLQuery"), "UTF-8"); 1.118 - 1.119 - if (query == null) { 1.120 - query = request.getParameter("describe"); 1.121 - q = (query == null) ? null : URLDecoder.decode(request.getParameter("describe"), "UTF-8"); 1.122 - } 1.123 - 1.124 - hive.setSPARQLQuery(q); 1.125 - 1.126 - String reqFormat = (request.getParameter("format") == null) ? "" : request.getParameter("format"); 1.127 - String reqAccept = (request.getHeader("accept") == null) ? "" : request.getHeader("accept"); 1.128 - String reqFuncionality = (request.getParameter("submit") == null) ? "" : request.getParameter("submit"); 1.129 - 1.130 - 1.131 - if ((reqFormat == "") && (reqAccept == "")) { 1.132 - hive.setFormat("HTML"); 1.133 - response.setContentType("text/html; charset=UTF-8"); 1.134 - 1.135 - } else if (reqAccept.contains("application/vnd.ms-excel")) { 1.136 - response.setContentType("application/vnd.ms-excel"); 1.137 - hive.setFormat("Spreadsheet"); 1.138 - } else if (reqAccept.contains("application/sparql-results+xml")) { 1.139 - response.setContentType("application/sparql-results+xml"); 1.140 - hive.setFormat("XML"); 1.141 - } else if (reqAccept.contains("application/sparql-results+json")) { 1.142 - response.setContentType("application/sparql-results+json"); 1.143 - hive.setFormat("JSON"); 1.144 - } else if (reqAccept.contains("application/javascript")) { 1.145 - response.setContentType("application/javascript"); 1.146 - hive.setFormat("Javascript"); 1.147 - } else if (reqAccept.contains("text/plain")) { 1.148 - response.setContentType("text/plain"); 1.149 - hive.setFormat("NTriples"); 1.150 - } else if (reqAccept.contains("application/rdf+xml")) { 1.151 - response.setContentType("application/rdf+xml"); 1.152 - hive.setFormat("RDF/XML"); 1.153 - } else if (reqAccept.contains("text/csv")) { 1.154 - response.setContentType("text/csv"); 1.155 - hive.setFormat("CSV"); 1.156 - } else if (reqAccept.contains("text/tab-separated-values")) { 1.157 - response.setContentType("text/tab-separated-values"); 1.158 - hive.setFormat("TSV"); 1.159 - } 1.160 - 1.161 - 1.162 - PrintWriter out = response.getWriter(); 1.163 - out.flush(); 1.164 - 1.165 - 1.166 - appendHTML1a(out, ""); 1.167 - 1.168 - appendHTMLQ(out, strabonWrapper); 1.169 - 1.170 - appendHTML1b(out); 1.171 - 1.172 - if (hive.getSPARQLQuery() != null) 1.173 - out.write(hive.getSPARQLQuery()); 1.174 - 1.175 - appendHTML2(out, hive.getFormat()); 1.176 + appendHTML2(out, reqFormat); 1.177 1.178 String answer = ""; 1.179 - if (hive.getSPARQLQuery() != null) { 1.180 - StringBuilder errorMessage = new StringBuilder (); 1.181 - answer = evaluateQuery(strabonWrapper, hive.getFormat(), reqFuncionality, hive.getSPARQLQuery(), errorMessage); 1.182 - hive.setErrorMessage(errorMessage.toString()); 1.183 - if (hive.getErrorMessage() != null) { 1.184 - appendHTML3(out, hive.getErrorMessage()); 1.185 + if (query != null) { 1.186 + ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1.187 + 1.188 + try { 1.189 + // we use the default N-Triples format in case of HTML output 1.190 + strabonWrapper.describe(query, "N-Triples", bos); 1.191 + answer = bos.toString(); 1.192 + 1.193 + } catch (Exception e) { 1.194 + appendHTML3(out, e.getMessage()); 1.195 } 1.196 } 1.197 1.198 @@ -169,18 +89,48 @@ 1.199 out.println("<style type=\"text/css\">"); 1.200 out.println("table.result {border:1px solid #777777;}"); 1.201 out.println("table.result tr {border:1px dashed grey;}"); 1.202 - out.println("table.result th {background-color:grey;color:black;}"); 1.203 + out.println("table.result th {background-color:grey; color:black;}"); 1.204 out.println("</style>"); 1.205 - out.println("<table class=\"result\">"); 1.206 - out.append(answer); 1.207 - out.append("</table>"); 1.208 + out.println("<PRE>"); 1.209 + out.append(StringEscapeUtils.escapeHtml(answer)); 1.210 + out.println("</PRE>"); 1.211 } 1.212 + 1.213 appendHTML4(out); 1.214 appendHTML5(out); 1.215 - } 1.216 + 1.217 + response.setStatus(HttpServletResponse.SC_OK); 1.218 + out.flush(); 1.219 + 1.220 + } else { 1.221 + RDFFormat rdfFormat = RDFFormat.valueOf(reqFormat); 1.222 + 1.223 + // assuming N-Triples 1.224 + if (rdfFormat == null) { 1.225 + rdfFormat = RDFFormat.valueOf("N-Triples"); 1.226 + } 1.227 + 1.228 + response.setContentType(rdfFormat.getDefaultMIMEType()); 1.229 + response.setHeader("Content-Disposition", 1.230 + "attachment; filename=describe." + rdfFormat.getDefaultFileExtension() + "; " + rdfFormat.getCharset()); 1.231 + 1.232 + if (query != null) { 1.233 + try { 1.234 + strabonWrapper.describe(query, rdfFormat.getName(), response.getOutputStream()); 1.235 + response.setStatus(HttpServletResponse.SC_OK); 1.236 + 1.237 + } catch (Exception e) { 1.238 + 1.239 + response.getOutputStream().print(ResponseMessages.getXMLHeader()); 1.240 + response.getOutputStream().print(ResponseMessages.getXMLException(e.getMessage())); 1.241 + response.getOutputStream().print(ResponseMessages.getXMLFooter()); 1.242 + } 1.243 + } 1.244 + 1.245 + response.getOutputStream().flush(); 1.246 + } 1.247 + } 1.248 1.249 - 1.250 - 1.251 @Override 1.252 public void init(ServletConfig servletConfig) throws ServletException { 1.253 super.init(servletConfig); 1.254 @@ -192,23 +142,6 @@ 1.255 strabonWrapper = (StrabonBeanWrapper) applicationContext.getBean("strabonBean"); 1.256 } 1.257 1.258 - public String evaluateQuery(StrabonBeanWrapper strabonWrapper, String resultFormat, String reqFunctionality, String SPARQLQuery, StringBuilder errorMessage) { 1.259 - String answer = ""; 1.260 - 1.261 - try { 1.262 - if (SPARQLQuery == null) { 1.263 - answer = ""; 1.264 - } else { 1.265 - answer = (String) strabonWrapper.describe(SPARQLQuery, resultFormat); 1.266 - } 1.267 - } catch (Exception e) { 1.268 - logger.error("[StrabonEndpoint.DescribeBean] Error during describing.", e); 1.269 - errorMessage.append(e.getMessage()); 1.270 - } 1.271 - 1.272 - return answer; 1.273 - } 1.274 - 1.275 protected static void appendHTML1a(PrintWriter out, String pathToKML) { 1.276 out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"); 1.277 out.println("<html>"); 1.278 @@ -280,7 +213,7 @@ 1.279 out.println("<td width=\"90\" valign=\"top\" bgcolor=\"#dfe8f0\"> "); 1.280 out.println("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"165\" id=\"navigation\"> "); 1.281 out.println("<tr><td width=\"90\" class=\"style4\"><a href=\"Query\" class=\"navText\">Query</a></td></tr> "); 1.282 - out.println("<tr><td width=\"90\" class=\"style4\"><a href=\"Describe\" class=\"navText\">Describe</a></td></tr> "); 1.283 + out.println("<tr><td width=\"90\" class=\"style4\"><a href=\"Describe\" class=\"navText\">Describe</a></td></tr> "); 1.284 } 1.285 1.286 protected static void appendHTML1b(PrintWriter out) { 1.287 @@ -303,27 +236,15 @@ 1.288 1.289 out.println("<td id=\"output\";\"><center>Output Format:<br/><select name=\"format\" title=\"select one of the following output format types\">"); 1.290 1.291 - Map<String, String> selections = new HashMap<String, String>(); 1.292 - selections.put("HTML", "HTML"); 1.293 - selections.put("Spreadsheet", "Spreadsheet"); 1.294 - selections.put("XML", "XML"); 1.295 - selections.put("JSON", "JSON"); 1.296 - selections.put("Javascript", "Javascript"); 1.297 - selections.put("NTriples", "NTriples"); 1.298 - selections.put("RDF/XML", "RDF/XML"); 1.299 - selections.put("CSV", "CSV"); 1.300 - selections.put("TSV", "TSV"); 1.301 + ArrayList<String> formats = new ArrayList<String>(Arrays.asList("HTML", "N-Triples", "RDF/XML", "N3", "TURTLE", "TRIG", "TRIX", "BinaryRDF")); 1.302 1.303 - Iterator <String> it = selections.keySet().iterator(); 1.304 - 1.305 - while (it.hasNext()) { 1.306 - String key = it.next(); 1.307 - String value = selections.get(key); 1.308 + for (String rdfFormat: formats) { 1.309 out.print("<option "); 1.310 - if (key.equalsIgnoreCase(format)) 1.311 + if (rdfFormat.equalsIgnoreCase(format)) { 1.312 out.print("selected"); 1.313 + } 1.314 1.315 - out.println(" value=\"" + key + "\">" + value + "</option>"); 1.316 + out.println(" value=\"" + rdfFormat + "\">" + rdfFormat + "</option>"); 1.317 } 1.318 1.319 out.println("</select></center></td>");
2.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java Fri Jul 20 15:23:38 2012 +0300 2.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java Fri Jul 20 19:07:01 2012 +0300 2.3 @@ -153,7 +153,7 @@ 2.4 2.5 } else if (reqFormat.equalsIgnoreCase("KML file")) { 2.6 response.setContentType("application/vnd.google-earth.kml+xml; charset=UTF-8"); 2.7 - response.setHeader("Content-Disposition","attachment;filename=pico.kml"); 2.8 + response.setHeader("Content-Disposition","attachment;filename=doc.kml"); 2.9 hive.setFormat(Strabon.FORMAT_KML); 2.10 2.11 } else if (reqFormat.equalsIgnoreCase("KML")) { 2.12 @@ -162,7 +162,7 @@ 2.13 2.14 } else if (reqFormat.equalsIgnoreCase("KMZ file")) { 2.15 response.setContentType("application/vnd.google-earth.kmz; charset=UTF-8"); 2.16 - response.setHeader("Content-Disposition","attachment;filename=pico.kmz"); 2.17 + response.setHeader("Content-Disposition","attachment;filename=doc.kmz"); 2.18 hive.setFormat(Strabon.FORMAT_KMZ); 2.19 2.20 } else if (reqFormat.equalsIgnoreCase("KMZ")) {
3.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StoreBean.java Fri Jul 20 15:23:38 2012 +0300 3.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StoreBean.java Fri Jul 20 19:07:01 2012 +0300 3.3 @@ -22,7 +22,8 @@ 3.4 import org.springframework.web.context.support.WebApplicationContextUtils; 3.5 3.6 /** 3.7 - * @author charnik 3.8 + * 3.9 + * @author Charalampos Nikolaou <charnik@di.uoa.gr> 3.10 * 3.11 */ 3.12 public class StoreBean extends HttpServlet {
4.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java Fri Jul 20 15:23:38 2012 +0300 4.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java Fri Jul 20 19:07:01 2012 +0300 4.3 @@ -2,6 +2,7 @@ 4.4 4.5 import java.io.ByteArrayOutputStream; 4.6 import java.io.IOException; 4.7 +import java.io.OutputStream; 4.8 import java.io.StringReader; 4.9 import java.net.URL; 4.10 import java.util.ArrayList; 4.11 @@ -215,18 +216,28 @@ 4.12 return answer.toString(); 4.13 } 4.14 4.15 - public String describe(String queryString, String answerFormatStrabon) 4.16 + /** 4.17 + * Wrapper around Strabon.describeOp which takes an OutputStream to use for writing 4.18 + * the answer to a DESCRIBE query. 4.19 + * 4.20 + * @param queryString 4.21 + * @param answerFormatStrabon 4.22 + * @param out 4.23 + * @throws MalformedQueryException 4.24 + * @throws RepositoryException 4.25 + * @throws QueryEvaluationException 4.26 + * @throws TupleQueryResultHandlerException 4.27 + * @throws IOException 4.28 + * @throws ClassNotFoundException 4.29 + */ 4.30 + public void describe(String queryString, String answerFormatStrabon, OutputStream out) 4.31 throws MalformedQueryException, RepositoryException, QueryEvaluationException, TupleQueryResultHandlerException, IOException, ClassNotFoundException { 4.32 logger.info("[StrabonEndpoint] Received DESCRIBE query."); 4.33 if ((this.strabon == null) && (!init())) { 4.34 throw new RepositoryException("Could not connect to Strabon."); 4.35 } 4.36 4.37 - ByteArrayOutputStream answer = new ByteArrayOutputStream(); 4.38 - 4.39 - strabon.describe(queryString, answerFormatStrabon, strabon.getSailRepoConnection(), answer); 4.40 - 4.41 - return answer.toString(); 4.42 + strabon.describe(queryString, answerFormatStrabon, strabon.getSailRepoConnection(), out); 4.43 } 4.44 4.45
5.1 --- a/scripts/endpoint Fri Jul 20 15:23:38 2012 +0300 5.2 +++ b/scripts/endpoint Fri Jul 20 19:07:01 2012 +0300 5.3 @@ -14,7 +14,7 @@ 5.4 echo 5.5 echo "Execute SPARQL and SPARQL Update queries as well as store RDF triples on a Strabon endpoint." 5.6 echo 5.7 - echo " COMMAND : one of query, queryfile, update, store, or help" 5.8 + echo " COMMAND : one of query, queryfile, update, store, describe, or help" 5.9 echo " ENDPOINT : the URL of the Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint)" 5.10 echo " ARGS : arguments according to selected command" 5.11 echo 5.12 @@ -60,6 +60,15 @@ 5.13 echo " TRIPLES_URL : the URL containing the RDF triples to store" 5.14 } 5.15 5.16 +function help_describe() { 5.17 + echo "Usage: ${CMD} describe ENDPOINT DESCRIBE_QUERY [RESULT_FORMAT]" 5.18 + echo 5.19 + echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)" 5.20 + echo " DESCRIBE_QUERY : the SPARQL DESCRIBE query to execute" 5.21 + echo " RESULT_FORMAT : the format of the result. Possible values are \`N-Triples' (default)" 5.22 + echo " \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'" 5.23 +} 5.24 + 5.25 CURL_OPTS="-w HTTP_CODE='%{http_code}\n' -H \"Content-Type:application/x-www-form-urlencoded\" -H \"Accept:text/xml\"" 5.26 5.27 # if set to 1, then only the command to be executed is printed 5.28 @@ -89,6 +98,9 @@ 5.29 store) 5.30 help_store 5.31 ;; 5.32 + describe) 5.33 + help_describe 5.34 + ;; 5.35 *) 5.36 help 5.37 ;; 5.38 @@ -215,6 +227,37 @@ 5.39 ;; 5.40 esac 5.41 ;; 5.42 + describe) 5.43 + shift 5.44 + if ! test $# -ge 2; then 5.45 + help_describe 5.46 + exit 1 5.47 + fi 5.48 + URL="${1}/Describe" 5.49 + QUERY="${2}" 5.50 + 5.51 + shift 5.52 + shift 5.53 + 5.54 + # set default format 5.55 + FORMAT="N-Triples" 5.56 + if test $# -eq 1; then 5.57 + FORMAT="${1}" 5.58 + fi 5.59 + 5.60 + case "${FORMAT}" in 5.61 + N-Triples|RDF/XML|N3|TURTLE|TRIG|TRIX|BinaryRDF) 5.62 + shift 5.63 + ;; 5.64 + *) 5.65 + echo "${CMD}: unknown format \"${FORMAT}\"." 5.66 + echo "${CMD}: possible values are \`N-Triples' (default), \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'" 5.67 + exit 2 5.68 + ;; 5.69 + esac 5.70 + 5.71 + EXEC="curl ${CURL_OPTS} -d format='${FORMAT}' --data-urlencode SPARQLQuery='${QUERY}' ${URL}" 5.72 + ;; 5.73 *) 5.74 help 5.75 echo