Strabon
changeset 1541:8876b5feea9d
Added support for getting results in JSON, GeoJSON, TSV, HTML, KML when using http request to a strabon endpoint.
author | George Stamoulis <gstam@di.uoa.gr> |
---|---|
date | Wed Oct 11 14:27:12 2017 +0300 (2017-10-11) |
parents | 5bf0afdae932 |
children | f9ad4e3a214a |
files | endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultFormat.java |
line diff
1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java Tue Oct 10 17:41:06 2017 +0300 1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java Wed Oct 11 14:27:12 2017 +0300 1.3 @@ -2,9 +2,9 @@ 1.4 * This Source Code Form is subject to the terms of the Mozilla Public 1.5 * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.7 - * 1.8 + * 1.9 * Copyright (C) 2010, 2011, 2012, 2013, 2014 Pyravlos Team 1.10 - * 1.11 + * 1.12 * http://www.strabon.di.uoa.gr/ 1.13 */ 1.14 package eu.earthobservatory.org.StrabonEndpoint; 1.15 @@ -43,7 +43,7 @@ 1.16 import eu.earthobservatory.utils.Format; 1.17 1.18 /** 1.19 - * 1.20 + * 1.21 * @author Kostis Kyzirakos <kkyzir@di.uoa.gr> 1.22 * @author Manos Karpathiotakis <mk@di.uoa.gr> 1.23 * @author Charalampos Nikolaou <charnik@di.uoa.gr> 1.24 @@ -52,377 +52,405 @@ 1.25 */ 1.26 public class QueryBean extends HttpServlet { 1.27 1.28 - private static final long serialVersionUID = -378175118289907707L; 1.29 + private static final long serialVersionUID = -378175118289907707L; 1.30 1.31 - private static Logger logger = LoggerFactory.getLogger(eu.earthobservatory.org.StrabonEndpoint.QueryBean.class); 1.32 + private static Logger logger = LoggerFactory.getLogger(eu.earthobservatory.org.StrabonEndpoint.QueryBean.class); 1.33 + 1.34 + /** 1.35 + * Attributes carrying values to be rendered by the query.jsp file 1.36 + */ 1.37 + private static final String ERROR = "error"; 1.38 + private static final String RESPONSE = "response"; 1.39 + 1.40 + /** 1.41 + * Error returned by QueryBean 1.42 + */ 1.43 + private static final String PARAM_ERROR = "stSPARQL Query Results Format or SPARQL query are not set or are invalid."; 1.44 + 1.45 + /** 1.46 + * The context of the servlet 1.47 + */ 1.48 + private ServletContext context; 1.49 + 1.50 + /** 1.51 + * Wrapper over Strabon 1.52 + */ 1.53 + private StrabonBeanWrapper strabonWrapper; 1.54 + 1.55 + /** 1.56 + * The name of the temporary directory to store KML/KMZ files 1.57 + * for presentation in Google Maps 1.58 + */ 1.59 + private String tempDirectory; 1.60 + 1.61 + /** 1.62 + * The absolute path of the temporary directory 1.63 + */ 1.64 + private String basePath; 1.65 + 1.66 + /** 1.67 + * The name of this web application 1.68 + */ 1.69 + private String appName; 1.70 + 1.71 1.72 - /** 1.73 - * Attributes carrying values to be rendered by the query.jsp file 1.74 - */ 1.75 - private static final String ERROR = "error"; 1.76 - private static final String RESPONSE = "response"; 1.77 + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 1.78 + doPost(request, response); 1.79 + } 1.80 1.81 - /** 1.82 - * Error returned by QueryBean 1.83 - */ 1.84 - private static final String PARAM_ERROR = "stSPARQL Query Results Format or SPARQL query are not set or are invalid."; 1.85 + public void init(ServletConfig servletConfig) throws ServletException { 1.86 + super.init(servletConfig); 1.87 1.88 - /** 1.89 - * The context of the servlet 1.90 - */ 1.91 - private ServletContext context; 1.92 + // get the context of the servlet 1.93 + context = getServletContext(); 1.94 + 1.95 + // get the context of the application 1.96 + WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context); 1.97 1.98 - /** 1.99 - * Wrapper over Strabon 1.100 - */ 1.101 - private StrabonBeanWrapper strabonWrapper; 1.102 + // the the strabon wrapper 1.103 + strabonWrapper = (StrabonBeanWrapper) applicationContext.getBean("strabonBean"); 1.104 + 1.105 + 1.106 + // get the name of this web application 1.107 + appName = context.getContextPath().replace("/", ""); 1.108 + 1.109 + } 1.110 1.111 - /** 1.112 - * The name of the temporary directory to store KML/KMZ files for 1.113 - * presentation in Google Maps 1.114 - */ 1.115 - private String tempDirectory; 1.116 + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 1.117 + request.setCharacterEncoding("UTF-8"); 1.118 + 1.119 + // check connection details 1.120 + if (strabonWrapper.getStrabon() == null) { 1.121 + RequestDispatcher dispatcher = request.getRequestDispatcher("/connection.jsp"); 1.122 + 1.123 + // pass the current details of the connection 1.124 + request.setAttribute("username", strabonWrapper.getUsername()); 1.125 + request.setAttribute("password", strabonWrapper.getPassword()); 1.126 + request.setAttribute("dbname", strabonWrapper.getDatabaseName()); 1.127 + request.setAttribute("hostname", strabonWrapper.getHostName()); 1.128 + request.setAttribute("port", strabonWrapper.getPort()); 1.129 + request.setAttribute("dbengine", strabonWrapper.getDBEngine()); 1.130 + 1.131 + // pass the other parameters as well 1.132 + request.setAttribute("query", request.getParameter("query")); 1.133 + if(request.getParameter("format").equalsIgnoreCase("PIECHART") || 1.134 + request.getParameter("format").equalsIgnoreCase("AREACHART")|| 1.135 + request.getParameter("format").equalsIgnoreCase("COLUMNCHART")){ 1.136 + request.setAttribute("format", "CHART"); 1.137 + } else{ 1.138 + request.setAttribute("format", request.getParameter("format")); 1.139 + } 1.140 + request.setAttribute("handle", request.getParameter("handle")); 1.141 + 1.142 + 1.143 + // forward the request 1.144 + dispatcher.forward(request, response); 1.145 + 1.146 + } else { 1.147 + 1.148 + if (Common.VIEW_TYPE.equals(request.getParameter(Common.VIEW))) { 1.149 + // HTML visual interface 1.150 + processVIEWRequest(request, response); 1.151 + 1.152 + 1.153 + } else {// invoked as a service 1.154 + processRequest(request, response); 1.155 + } 1.156 + } 1.157 + } 1.158 1.159 - /** 1.160 - * The absolute path of the temporary directory 1.161 - */ 1.162 - private String basePath; 1.163 - 1.164 - /** 1.165 - * The name of this web application 1.166 - */ 1.167 - private String appName; 1.168 - 1.169 - public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 1.170 - doPost(request, response); 1.171 - } 1.172 - 1.173 - public void init(ServletConfig servletConfig) throws ServletException { 1.174 - super.init(servletConfig); 1.175 - 1.176 - // get the context of the servlet 1.177 - context = getServletContext(); 1.178 - 1.179 - // get the context of the application 1.180 - WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context); 1.181 - 1.182 - // the the strabon wrapper 1.183 - strabonWrapper = (StrabonBeanWrapper) applicationContext.getBean("strabonBean"); 1.184 - 1.185 - // get the name of this web application 1.186 - appName = context.getContextPath().replace("/", ""); 1.187 - 1.188 - } 1.189 - 1.190 - public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 1.191 - request.setCharacterEncoding("UTF-8"); 1.192 - 1.193 - // check connection details 1.194 - if (strabonWrapper.getStrabon() == null) { 1.195 - RequestDispatcher dispatcher = request.getRequestDispatcher("/connection.jsp"); 1.196 - 1.197 - // pass the current details of the connection 1.198 - request.setAttribute("username", strabonWrapper.getUsername()); 1.199 - request.setAttribute("password", strabonWrapper.getPassword()); 1.200 - request.setAttribute("dbname", strabonWrapper.getDatabaseName()); 1.201 - request.setAttribute("hostname", strabonWrapper.getHostName()); 1.202 - request.setAttribute("port", strabonWrapper.getPort()); 1.203 - request.setAttribute("dbengine", strabonWrapper.getDBEngine()); 1.204 - request.setAttribute("googlemapskey", strabonWrapper.getDBEngine()); 1.205 - 1.206 - // pass the other parameters as well 1.207 - request.setAttribute("query", request.getParameter("query")); 1.208 - if (request.getParameter("format").equalsIgnoreCase("PIECHART") 1.209 - || request.getParameter("format").equalsIgnoreCase("AREACHART") 1.210 - || request.getParameter("format").equalsIgnoreCase("COLUMNCHART")) { 1.211 - request.setAttribute("format", "CHART"); 1.212 - } else { 1.213 - request.setAttribute("format", request.getParameter("format")); 1.214 - } 1.215 - request.setAttribute("handle", request.getParameter("handle")); 1.216 - 1.217 - // forward the request 1.218 - dispatcher.forward(request, response); 1.219 - 1.220 - } else { 1.221 - // add googlemapsAPIscriptsource attribute for Google API script source tag 1.222 - request.setAttribute("googlemapsAPIscriptsource", strabonWrapper.googleMapsAPIScriptSourceForJSP()); 1.223 - 1.224 - if (Common.VIEW_TYPE.equals(request.getParameter(Common.VIEW))) { 1.225 - // HTML visual interface 1.226 - processVIEWRequest(request, response); 1.227 - 1.228 - } else {// invoked as a service 1.229 - processRequest(request, response); 1.230 - } 1.231 - } 1.232 - } 1.233 - 1.234 - /** 1.235 - * Processes the request made by a client of the endpoint that uses it as a 1.236 - * service. 1.237 - * 1.238 + /** 1.239 + * Processes the request made by a client of the endpoint that uses it as a service. 1.240 + * 1.241 * @param request 1.242 * @param response 1.243 - * @throws IOException 1.244 + * @throws IOException 1.245 */ 1.246 - private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { 1.247 - ServletOutputStream out = response.getOutputStream(); 1.248 - 1.249 - // get desired formats (we check only the Accept header) 1.250 - List<stSPARQLQueryResultFormat> formats = parseMultiValuedAcceptHeader(request.getHeader("accept")); 1.251 - 1.252 - // get the query and the limit 1.253 - String query = request.getParameter("query"); 1.254 - String maxLimit = request.getParameter("maxLimit"); 1.255 - 1.256 - // check for required parameters 1.257 - if (formats.size() == 0 || query == null) { 1.258 - logger.error("[StrabonEndpoint.QueryBean] {}", PARAM_ERROR); 1.259 - response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 1.260 - out.print(ResponseMessages.getXMLHeader()); 1.261 - out.print(ResponseMessages.getXMLException(PARAM_ERROR)); 1.262 - out.print(ResponseMessages.getXMLFooter()); 1.263 - 1.264 - } else { 1.265 - // just use the first specified format 1.266 - stSPARQLQueryResultFormat format = formats.get(0); 1.267 - 1.268 - // do not decode the SPARQL query (see bugs #65 and #49) 1.269 - //query = URLDecoder.decode(request.getParameter("query"), "UTF-8"); 1.270 - query = request.getParameter("query"); 1.271 - 1.272 - response.setContentType(format.getDefaultMIMEType()); 1.273 - 1.274 - try { 1.275 - query = strabonWrapper.addLimit(query, maxLimit); 1.276 - strabonWrapper.query(query, format.getName(), out); 1.277 - response.setStatus(HttpServletResponse.SC_OK); 1.278 - 1.279 - } catch (Exception e) { 1.280 - logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e); 1.281 - response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 1.282 - out.print(ResponseMessages.getXMLHeader()); 1.283 - out.print(ResponseMessages.getXMLException(e.getMessage())); 1.284 - out.print(ResponseMessages.getXMLFooter()); 1.285 - } 1.286 - } 1.287 - 1.288 - out.flush(); 1.289 - } 1.290 - 1.291 - /** 1.292 - * Processes the request made from the HTML visual interface of Strabon 1.293 - * Endpoint. 1.294 - * 1.295 + private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { 1.296 + ServletOutputStream out = response.getOutputStream(); 1.297 + 1.298 + // get desired formats (we check only the Accept header) 1.299 + List<TupleQueryResultFormat> formats = parseMultiValuedAcceptHeader(request.getHeader("accept")); 1.300 + 1.301 + // get the query and the limit 1.302 + String query = request.getParameter("query"); 1.303 + String maxLimit = request.getParameter("maxLimit"); 1.304 + 1.305 + // check for required parameters 1.306 + if (formats.size() == 0 || query == null) { 1.307 + logger.error("[StrabonEndpoint.QueryBean] {}", PARAM_ERROR); 1.308 + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 1.309 + out.print(ResponseMessages.getXMLHeader()); 1.310 + out.print(ResponseMessages.getXMLException(PARAM_ERROR)); 1.311 + out.print(ResponseMessages.getXMLFooter()); 1.312 + 1.313 + } else { 1.314 + // just use the first specified format 1.315 + TupleQueryResultFormat format = formats.get(0); 1.316 + 1.317 + // do not decode the SPARQL query (see bugs #65 and #49) 1.318 + //query = URLDecoder.decode(request.getParameter("query"), "UTF-8"); 1.319 + query = request.getParameter("query"); 1.320 + 1.321 + response.setContentType(format.getDefaultMIMEType()); 1.322 + 1.323 + try { 1.324 + query = strabonWrapper.addLimit(query, maxLimit); 1.325 + System.out.println(format.getName()); 1.326 + strabonWrapper.query(query, format.getName(), out); 1.327 + response.setStatus(HttpServletResponse.SC_OK); 1.328 + 1.329 + } catch (Exception e) { 1.330 + logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e); 1.331 + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 1.332 + out.print(ResponseMessages.getXMLHeader()); 1.333 + out.print(ResponseMessages.getXMLException(e.getMessage())); 1.334 + out.print(ResponseMessages.getXMLFooter()); 1.335 + } 1.336 + } 1.337 + 1.338 + out.flush(); 1.339 + } 1.340 + 1.341 + /** 1.342 + * Processes the request made from the HTML visual interface of Strabon Endpoint. 1.343 + * 1.344 * @param request 1.345 * @param response 1.346 * @throws ServletException 1.347 * @throws IOException 1.348 */ 1.349 - private void processVIEWRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 1.350 - RequestDispatcher dispatcher; 1.351 + private void processVIEWRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 1.352 + RequestDispatcher dispatcher; 1.353 1.354 - // check whether Update submit button was fired 1.355 - String reqFuncionality = (request.getParameter("submit") == null) ? "" : request.getParameter("submit"); 1.356 + // check whether Update submit button was fired 1.357 + String reqFuncionality = (request.getParameter("submit") == null) ? "" : request.getParameter("submit"); 1.358 1.359 - if (reqFuncionality.equals("Update")) { 1.360 - // get the dispatcher for forwarding the rendering of the response 1.361 - dispatcher = request.getRequestDispatcher("/Update"); 1.362 - dispatcher.forward(request, response); 1.363 + if (reqFuncionality.equals("Update")) { 1.364 + // get the dispatcher for forwarding the rendering of the response 1.365 + dispatcher = request.getRequestDispatcher("/Update"); 1.366 + dispatcher.forward(request, response); 1.367 + 1.368 + } else { 1.369 + // do not decode the SPARQL query (see bugs #65 and #49) 1.370 + //String query = URLDecoder.decode(request.getParameter("query"), "UTF-8"); 1.371 + 1.372 + String query = request.getParameter("query"); 1.373 + String format = request.getParameter("format"); 1.374 + System.out.println(format); 1.375 + String handle = request.getParameter("handle"); 1.376 + String maxLimit = request.getParameter("maxLimit"); 1.377 + 1.378 + // get stSPARQLQueryResultFormat from given format name 1.379 + TupleQueryResultFormat queryResultFormat = stSPARQLQueryResultFormat.valueOf(format); 1.380 + 1.381 + if (query == null || format == null || queryResultFormat == null) { 1.382 + dispatcher = request.getRequestDispatcher("query.jsp"); 1.383 + request.setAttribute(ERROR, PARAM_ERROR); 1.384 + dispatcher.forward(request, response); 1.385 + 1.386 + } else { 1.387 + query = strabonWrapper.addLimit(query, maxLimit); 1.388 + if ("download".equals(handle)) { // download as attachment 1.389 + ServletOutputStream out = response.getOutputStream(); 1.390 + 1.391 + response.setContentType(queryResultFormat.getDefaultMIMEType()); 1.392 + response.setHeader("Content-Disposition", 1.393 + "attachment; filename=results." + 1.394 + queryResultFormat.getDefaultFileExtension() + "; " + 1.395 + queryResultFormat.getCharset()); 1.396 + 1.397 + try { 1.398 + strabonWrapper.query(query, format, out); 1.399 + response.setStatus(HttpServletResponse.SC_OK); 1.400 + 1.401 + } catch (Exception e) { 1.402 + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 1.403 + out.print(ResponseMessages.getXMLHeader()); 1.404 + out.print(ResponseMessages.getXMLException(e.getMessage())); 1.405 + out.print(ResponseMessages.getXMLFooter()); 1.406 + } 1.407 + 1.408 + out.flush(); 1.409 + 1.410 + } else if (("map".equals(handle) || "map_local".equals(handle) || "timemap".equals(handle)) && 1.411 + (queryResultFormat == stSPARQLQueryResultFormat.KML || 1.412 + queryResultFormat == stSPARQLQueryResultFormat.KMZ) ) { 1.413 + // show map (only valid for KML/KMZ) 1.414 + 1.415 + // get dispatcher 1.416 + dispatcher = request.getRequestDispatcher("query.jsp"); 1.417 + 1.418 + // re-assign handle 1.419 + request.setAttribute("handle", handle); 1.420 + 1.421 + SecureRandom random = new SecureRandom(); 1.422 + String temp = new BigInteger(130, random).toString(32); 1.423 + 1.424 + // the temporary KML/KMZ file to create in the server 1.425 + String tempKMLFile = temp + "." + queryResultFormat.getDefaultFileExtension();; 1.426 + 1.427 + try{ 1.428 + Date date = new Date(); 1.429 + 1.430 + // get the absolute path of the temporary directory 1.431 + if(!request.getParameter("handle").toString().contains("timemap")){ 1.432 + tempDirectory = appName + "-temp"; 1.433 + 1.434 + basePath = context.getRealPath("/") + "/../ROOT/" + tempDirectory + "/"; 1.435 + // fix the temporary directory for this web application 1.436 + 1.437 + FileUtils.forceMkdir(new File(basePath)); 1.438 1.439 - } else { 1.440 - // do not decode the SPARQL query (see bugs #65 and #49) 1.441 - //String query = URLDecoder.decode(request.getParameter("query"), "UTF-8"); 1.442 + @SuppressWarnings("unchecked") 1.443 + Iterator<File> it = FileUtils.iterateFiles(new File(basePath), null, false); 1.444 + while(it.hasNext()){ 1.445 + File tbd = new File((it.next()).getAbsolutePath()); 1.446 + if (FileUtils.isFileOlder(new File(tbd.getAbsolutePath()), date.getTime())){ 1.447 + FileUtils.forceDelete(new File(tbd.getAbsolutePath())); 1.448 + } 1.449 + } 1.450 + } else{ //timemap case 1.451 + tempDirectory = "js/timemap"; 1.452 + basePath = context.getRealPath("/") + tempDirectory + "/"; 1.453 + // fix the temporary directory for this web application 1.454 + } 1.455 1.456 - String query = request.getParameter("query"); 1.457 - String format = request.getParameter("format"); 1.458 - String handle = request.getParameter("handle"); 1.459 - String maxLimit = request.getParameter("maxLimit"); 1.460 1.461 - // get stSPARQLQueryResultFormat from given format name 1.462 - TupleQueryResultFormat queryResultFormat = stSPARQLQueryResultFormat.valueOf(format); 1.463 + // fix the temporary directory for this web application 1.464 + 1.465 + // create temporary KML/KMZ file 1.466 + File file = new File(basePath + tempKMLFile); 1.467 + // if file does not exist, then create it 1.468 + if(!file.exists()){ 1.469 + file.createNewFile(); 1.470 + } 1.471 + 1.472 + try { 1.473 + // query and write the result in the temporary KML/KMZ file 1.474 + FileOutputStream fos = new FileOutputStream(basePath + tempKMLFile); 1.475 + strabonWrapper.query(query, format, fos); 1.476 + fos.close(); 1.477 + 1.478 + if(request.getParameter("handle").toString().contains("timemap")){ 1.479 + request.setAttribute("pathToKML", tempDirectory+"/"+ tempKMLFile); 1.480 + }else { 1.481 + request.setAttribute("pathToKML", 1.482 + request.getScheme() + "://" + 1.483 + request.getServerName() + ":" + request.getServerPort() + 1.484 + "/" + tempDirectory + "/" + tempKMLFile); 1.485 + } 1.486 + 1.487 + } catch (MalformedQueryException e) { 1.488 + logger.error("[StrabonEndpoint.QueryBean] Error during querying. {}", e.getMessage()); 1.489 + request.setAttribute(ERROR, e.getMessage()); 1.490 + 1.491 + } catch (Exception e) { 1.492 + logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e); 1.493 + request.setAttribute(ERROR, e.getMessage()); 1.494 + } 1.495 + 1.496 + dispatcher.forward(request, response); 1.497 1.498 - if (query == null || format == null || queryResultFormat == null) { 1.499 - dispatcher = request.getRequestDispatcher("query.jsp"); 1.500 - request.setAttribute(ERROR, PARAM_ERROR); 1.501 - dispatcher.forward(request, response); 1.502 + } catch(IOException e) { 1.503 + logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e); 1.504 + } 1.505 1.506 - } else { 1.507 - query = strabonWrapper.addLimit(query, maxLimit); 1.508 - if ("download".equals(handle)) { // download as attachment 1.509 - ServletOutputStream out = response.getOutputStream(); 1.510 - 1.511 - response.setContentType(queryResultFormat.getDefaultMIMEType()); 1.512 - response.setHeader("Content-Disposition", 1.513 - "attachment; filename=results." 1.514 - + queryResultFormat.getDefaultFileExtension() + "; " 1.515 - + queryResultFormat.getCharset()); 1.516 - 1.517 - try { 1.518 - strabonWrapper.query(query, format, out); 1.519 - response.setStatus(HttpServletResponse.SC_OK); 1.520 - 1.521 - } catch (Exception e) { 1.522 - response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 1.523 - out.print(ResponseMessages.getXMLHeader()); 1.524 - out.print(ResponseMessages.getXMLException(e.getMessage())); 1.525 - out.print(ResponseMessages.getXMLFooter()); 1.526 - } 1.527 - 1.528 - out.flush(); 1.529 - 1.530 - } else if (("map".equals(handle) || "map_local".equals(handle) || "timemap".equals(handle)) 1.531 - && (queryResultFormat == stSPARQLQueryResultFormat.KML 1.532 - || queryResultFormat == stSPARQLQueryResultFormat.KMZ)) { 1.533 - // show map (only valid for KML/KMZ) 1.534 - 1.535 - // get dispatcher 1.536 - dispatcher = request.getRequestDispatcher("query.jsp"); 1.537 - 1.538 - // re-assign handle 1.539 - request.setAttribute("handle", handle); 1.540 - 1.541 - SecureRandom random = new SecureRandom(); 1.542 - String temp = new BigInteger(130, random).toString(32); 1.543 - 1.544 - // the temporary KML/KMZ file to create in the server 1.545 - String tempKMLFile = temp + "." + queryResultFormat.getDefaultFileExtension();; 1.546 - 1.547 - try { 1.548 - Date date = new Date(); 1.549 - 1.550 - // get the absolute path of the temporary directory 1.551 - if (!request.getParameter("handle").toString().contains("timemap")) { 1.552 - tempDirectory = appName + "-temp"; 1.553 - 1.554 - basePath = context.getRealPath("/") + "/../ROOT/" + tempDirectory + "/"; 1.555 - // fix the temporary directory for this web application 1.556 - 1.557 - FileUtils.forceMkdir(new File(basePath)); 1.558 - 1.559 - @SuppressWarnings("unchecked") 1.560 - Iterator<File> it = FileUtils.iterateFiles(new File(basePath), null, false); 1.561 - while (it.hasNext()) { 1.562 - File tbd = new File((it.next()).getAbsolutePath()); 1.563 - if (FileUtils.isFileOlder(new File(tbd.getAbsolutePath()), date.getTime())) { 1.564 - FileUtils.forceDelete(new File(tbd.getAbsolutePath())); 1.565 - } 1.566 - } 1.567 - } else { //timemap case 1.568 - tempDirectory = "js/timemap"; 1.569 - basePath = context.getRealPath("/") + tempDirectory + "/"; 1.570 - // fix the temporary directory for this web application 1.571 - } 1.572 - 1.573 - // fix the temporary directory for this web application 1.574 - // create temporary KML/KMZ file 1.575 - File file = new File(basePath + tempKMLFile); 1.576 - // if file does not exist, then create it 1.577 - if (!file.exists()) { 1.578 - file.createNewFile(); 1.579 - } 1.580 - 1.581 - try { 1.582 - // query and write the result in the temporary KML/KMZ file 1.583 - FileOutputStream fos = new FileOutputStream(basePath + tempKMLFile); 1.584 - strabonWrapper.query(query, format, fos); 1.585 - fos.close(); 1.586 - 1.587 - if (request.getParameter("handle").toString().contains("timemap")) { 1.588 - request.setAttribute("pathToKML", tempDirectory + "/" + tempKMLFile); 1.589 - } else { 1.590 - request.setAttribute("pathToKML", 1.591 - request.getScheme() + "://" 1.592 - + request.getServerName() + ":" + request.getServerPort() 1.593 - + "/" + tempDirectory + "/" + tempKMLFile); 1.594 - } 1.595 - 1.596 - } catch (MalformedQueryException e) { 1.597 - logger.error("[StrabonEndpoint.QueryBean] Error during querying. {}", e.getMessage()); 1.598 - request.setAttribute(ERROR, e.getMessage()); 1.599 - 1.600 - } catch (Exception e) { 1.601 - logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e); 1.602 - request.setAttribute(ERROR, e.getMessage()); 1.603 - } 1.604 - 1.605 - dispatcher.forward(request, response); 1.606 - 1.607 - } catch (IOException e) { 1.608 - logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e); 1.609 - } 1.610 - 1.611 - } else { // "plain" is assumed as the default 1.612 - dispatcher = request.getRequestDispatcher("query.jsp"); 1.613 - ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1.614 - 1.615 - try { 1.616 - strabonWrapper.query(query, format, bos); 1.617 - if (format.equals(Common.getHTMLFormat())) { 1.618 - request.setAttribute(RESPONSE, bos.toString()); 1.619 - } else if (format.equals(Format.PIECHART.toString()) 1.620 - || format.equals(Format.AREACHART.toString()) 1.621 - || format.equals(Format.COLUMNCHART.toString())) { 1.622 - request.setAttribute("format", "CHART"); 1.623 - request.setAttribute(RESPONSE, strabonWrapper.getgChartString()); 1.624 - } else { 1.625 - request.setAttribute(RESPONSE, StringEscapeUtils.escapeHtml(bos.toString())); 1.626 - } 1.627 - 1.628 - } catch (MalformedQueryException e) { 1.629 - logger.error("[StrabonEndpoint.QueryBean] Error during querying. {}", e.getMessage()); 1.630 - request.setAttribute(ERROR, e.getMessage()); 1.631 - 1.632 - } catch (Exception e) { 1.633 - logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e); 1.634 - request.setAttribute(ERROR, e.getMessage()); 1.635 - 1.636 - } finally { 1.637 - dispatcher.forward(request, response); 1.638 - } 1.639 - } 1.640 - } 1.641 - } 1.642 - } 1.643 - 1.644 - /** 1.645 - * Given an Accept header, it parses it and extracts the mime types for the 1.646 - * accepted formats. The header might contain multiple accepted values and 1.647 - * qvalues as well, however, qvalues are ignored. The extracted mime types 1.648 - * are then transformed to stSPARQLQueryResultFormat and a list of such 1.649 - * objects is returned. If a mimetype is not valid, then it is ignored. If 1.650 - * all mimetypes are invalid, then the returned list has zero elements, but 1.651 - * it is not null. 1.652 - * 1.653 - * @param header 1.654 - * @return 1.655 - */ 1.656 - private List<stSPARQLQueryResultFormat> parseMultiValuedAcceptHeader(String header) { 1.657 - List<stSPARQLQueryResultFormat> formats = new ArrayList<stSPARQLQueryResultFormat>(); 1.658 - 1.659 - StringTokenizer token = new StringTokenizer(header, ", "); 1.660 - 1.661 - while (token.hasMoreTokens()) { 1.662 - String value = token.nextToken(); 1.663 - 1.664 - // value might contain qvalues (e.g., "text/plain; q=0.2") 1.665 - // for the time being, we just discard them 1.666 - int idx_sep_cut = value.indexOf(';'); 1.667 - if (idx_sep_cut > 0) { 1.668 - value = value.substring(0, idx_sep_cut); 1.669 - } 1.670 - 1.671 - // get the stSPARQL Query Result format 1.672 - stSPARQLQueryResultFormat format = stSPARQLQueryResultFormat.forMIMEType(value); 1.673 - 1.674 - // keep only the valid formats (non-null) 1.675 - if (format != null) { 1.676 - formats.add(format); 1.677 - } 1.678 - } 1.679 + } else { // "plain" is assumed as the default 1.680 + dispatcher = request.getRequestDispatcher("query.jsp"); 1.681 + ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1.682 + 1.683 + try { 1.684 + strabonWrapper.query(query, format, bos); 1.685 + if (format.equals(Common.getHTMLFormat())) { 1.686 + request.setAttribute(RESPONSE, bos.toString()); 1.687 + } 1.688 + else if(format.equals(Format.PIECHART.toString()) 1.689 + || format.equals(Format.AREACHART.toString()) 1.690 + || format.equals(Format.COLUMNCHART.toString())){ 1.691 + request.setAttribute("format","CHART"); 1.692 + request.setAttribute(RESPONSE, strabonWrapper.getgChartString()); 1.693 + } 1.694 + 1.695 + else { 1.696 + request.setAttribute(RESPONSE, StringEscapeUtils.escapeHtml(bos.toString())); 1.697 + } 1.698 + 1.699 + } catch (MalformedQueryException e) { 1.700 + logger.error("[StrabonEndpoint.QueryBean] Error during querying. {}", e.getMessage()); 1.701 + request.setAttribute(ERROR, e.getMessage()); 1.702 + 1.703 + } catch (Exception e) { 1.704 + logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e); 1.705 + request.setAttribute(ERROR, e.getMessage()); 1.706 + 1.707 + } finally { 1.708 + dispatcher.forward(request, response); 1.709 + } 1.710 + } 1.711 + } 1.712 + } 1.713 + } 1.714 + 1.715 + /** 1.716 + * Given an Accept header, it parses it and extracts the mime types for the accepted formats. 1.717 + * The header might contain multiple accepted values and qvalues as well, however, qvalues 1.718 + * are ignored. The extracted mime types are then transformed to stSPARQLQueryResultFormat 1.719 + * and a list of such objects is returned. If a mimetype is not valid, then it is ignored. 1.720 + * If all mimetypes are invalid, then the returned list has zero elements, but it is not 1.721 + * null. 1.722 + * 1.723 + * @param header 1.724 + * @return 1.725 + */ 1.726 + private List<TupleQueryResultFormat> parseMultiValuedAcceptHeader(String header) { 1.727 + List<TupleQueryResultFormat> formats = new ArrayList<TupleQueryResultFormat>(); 1.728 + 1.729 + StringTokenizer token = new StringTokenizer(header, ", "); 1.730 + 1.731 + while (token.hasMoreTokens()) { 1.732 + String value = token.nextToken(); 1.733 + 1.734 + // value might contain qvalues (e.g., "text/plain; q=0.2") 1.735 + // for the time being, we just discard them 1.736 + int idx_sep_cut = value.indexOf(';'); 1.737 + if (idx_sep_cut > 0) { 1.738 + value = value.substring(0, idx_sep_cut); 1.739 + } 1.740 + 1.741 + // get the stSPARQL Query Result format 1.742 + String valueFromMime = getValueFromMime(value); 1.743 + TupleQueryResultFormat format = stSPARQLQueryResultFormat.valueOf(valueFromMime); 1.744 + 1.745 + // keep only the valid formats (non-null) 1.746 + if (format != null) { 1.747 + formats.add(format); 1.748 + } 1.749 + } 1.750 1.751 return formats; 1.752 - } 1.753 + } 1.754 + 1.755 + private String getValueFromMime(String value) { 1.756 + if (value.equalsIgnoreCase("application/sparql-results+json")) { 1.757 + return "SPARQL/JSON"; 1.758 + } 1.759 + else if (value.equalsIgnoreCase("application/json") || value.equalsIgnoreCase("application/geojson")) { 1.760 + return "GeoJSON"; 1.761 + } 1.762 + else if (value.equalsIgnoreCase("application/vnd.google-earth.kml+xml") || value.equalsIgnoreCase("application/kml")) { 1.763 + return "KML"; 1.764 + } 1.765 + else if (value.equalsIgnoreCase("text/tab-separated-values")) { 1.766 + return "TSV"; 1.767 + } 1.768 + else if (value.equalsIgnoreCase("text/html")) { 1.769 + return "HTML"; 1.770 + } 1.771 + else { 1.772 + return "XML"; 1.773 + } 1.774 + 1.775 + } 1.776 1.777 }
2.1 --- a/resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultFormat.java Tue Oct 10 17:41:06 2017 +0300 2.2 +++ b/resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultFormat.java Wed Oct 11 14:27:12 2017 +0300 2.3 @@ -72,7 +72,6 @@ 2.4 public static final stSPARQLQueryResultFormat HTML = new stSPARQLQueryResultFormat("HTML", 2.5 Arrays.asList("text/html"), Charset.forName("UTF-8"), Arrays.asList("html", "htm")); 2.6 2.7 - 2.8 /** 2.9 * CHART format (so that results can be displayed using google charts) 2.10 */