Strabon

changeset 1543:5945172a87c0

Merged QueryBean.java file with the chnages of gstam and teo.
author George Stamoulis <gstam@di.uoa.gr>
date Tue Oct 24 10:27:26 2017 +0300 (12 months ago)
parents f9ad4e3a214a
children 3932604c130d
files endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java
line diff
     1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Wed Oct 11 14:28:49 2017 +0300
     1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Tue Oct 24 10:27:26 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,386 +52,381 @@
    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 -	
    1.33 -	/**
    1.34 -	 * Attributes carrying values to be rendered by the query.jsp file 
    1.35 -	 */
    1.36 -	private static final String ERROR		= "error";
    1.37 -	private static final String RESPONSE	= "response";
    1.38 -	
    1.39 -	/**
    1.40 -	 * Error returned by QueryBean
    1.41 -	 */
    1.42 -	private static final String PARAM_ERROR = "stSPARQL Query Results Format or SPARQL query are not set or are invalid.";
    1.43 -	
    1.44 -	/**
    1.45 -	 * The context of the servlet
    1.46 -	 */
    1.47 -	private ServletContext context;
    1.48 -	
    1.49 -	/**
    1.50 -	 * Wrapper over Strabon
    1.51 -	 */
    1.52 -	private StrabonBeanWrapper strabonWrapper;
    1.53 -	
    1.54 -	/**
    1.55 -	 * The name of the temporary directory to store KML/KMZ files
    1.56 -	 * for presentation in Google Maps 
    1.57 -	 */
    1.58 -	private String tempDirectory;
    1.59 -	
    1.60 -	/**
    1.61 -	 * The absolute path of the temporary directory
    1.62 -	 */
    1.63 -	private String basePath;
    1.64 -	
    1.65 -	/**
    1.66 -	 * The name of this web application
    1.67 -	 */
    1.68 -	private String appName;
    1.69 -	
    1.70 +    private static Logger logger = LoggerFactory.getLogger(eu.earthobservatory.org.StrabonEndpoint.QueryBean.class);
    1.71  
    1.72 -	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    1.73 -		doPost(request, response);
    1.74 -	}
    1.75 +    /**
    1.76 +     * Attributes carrying values to be rendered by the query.jsp file
    1.77 +     */
    1.78 +    private static final String ERROR = "error";
    1.79 +    private static final String RESPONSE = "response";
    1.80  
    1.81 -	public void init(ServletConfig servletConfig) throws ServletException {
    1.82 -		super.init(servletConfig);
    1.83 +    /**
    1.84 +     * Error returned by QueryBean
    1.85 +     */
    1.86 +    private static final String PARAM_ERROR = "stSPARQL Query Results Format or SPARQL query are not set or are invalid.";
    1.87  
    1.88 -		// get the context of the servlet
    1.89 -		context = getServletContext();
    1.90 -		
    1.91 -		// get the context of the application
    1.92 -		WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
    1.93 +    /**
    1.94 +     * The context of the servlet
    1.95 +     */
    1.96 +    private ServletContext context;
    1.97  
    1.98 -		// the the strabon wrapper
    1.99 -		strabonWrapper = (StrabonBeanWrapper) applicationContext.getBean("strabonBean");
   1.100 -	
   1.101 -		
   1.102 -		// get the name of this web application
   1.103 -		appName = context.getContextPath().replace("/", "");
   1.104 -		
   1.105 -	}
   1.106 +    /**
   1.107 +     * Wrapper over Strabon
   1.108 +     */
   1.109 +    private StrabonBeanWrapper strabonWrapper;
   1.110  
   1.111 -	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   1.112 -		request.setCharacterEncoding("UTF-8");
   1.113 -		
   1.114 -		// check connection details
   1.115 -		if (strabonWrapper.getStrabon() == null) {
   1.116 -			RequestDispatcher dispatcher = request.getRequestDispatcher("/connection.jsp");
   1.117 -			
   1.118 -			// pass the current details of the connection
   1.119 -			request.setAttribute("username", strabonWrapper.getUsername());
   1.120 -			request.setAttribute("password", strabonWrapper.getPassword());
   1.121 -			request.setAttribute("dbname", 	 strabonWrapper.getDatabaseName());
   1.122 -			request.setAttribute("hostname", strabonWrapper.getHostName());
   1.123 -			request.setAttribute("port", 	 strabonWrapper.getPort());
   1.124 -			request.setAttribute("dbengine", strabonWrapper.getDBEngine());
   1.125 -			
   1.126 -			// pass the other parameters as well
   1.127 -			request.setAttribute("query", request.getParameter("query"));
   1.128 -			if(request.getParameter("format").equalsIgnoreCase("PIECHART") || 
   1.129 -					request.getParameter("format").equalsIgnoreCase("AREACHART")|| 
   1.130 -					request.getParameter("format").equalsIgnoreCase("COLUMNCHART")){
   1.131 -				request.setAttribute("format", "CHART");
   1.132 -			} else{
   1.133 -				request.setAttribute("format", request.getParameter("format"));
   1.134 -			}
   1.135 -			request.setAttribute("handle", request.getParameter("handle"));
   1.136 -			
   1.137 -			
   1.138 -			// forward the request
   1.139 -			dispatcher.forward(request, response);
   1.140 -			
   1.141 -		} else {
   1.142 -		
   1.143 -			if (Common.VIEW_TYPE.equals(request.getParameter(Common.VIEW))) {
   1.144 -				// HTML visual interface
   1.145 -				processVIEWRequest(request, response);
   1.146 -				
   1.147 -	
   1.148 -			} else {// invoked as a service
   1.149 -				processRequest(request, response);
   1.150 -		    }
   1.151 -		}
   1.152 -	}
   1.153 +    /**
   1.154 +     * The name of the temporary directory to store KML/KMZ files for
   1.155 +     * presentation in Google Maps
   1.156 +     */
   1.157 +    private String tempDirectory;
   1.158  
   1.159 -	/**
   1.160 -     * Processes the request made by a client of the endpoint that uses it as a service. 
   1.161 -     * 
   1.162 +    /**
   1.163 +     * The absolute path of the temporary directory
   1.164 +     */
   1.165 +    private String basePath;
   1.166 +
   1.167 +    /**
   1.168 +     * The name of this web application
   1.169 +     */
   1.170 +    private String appName;
   1.171 +
   1.172 +    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   1.173 +        doPost(request, response);
   1.174 +    }
   1.175 +
   1.176 +    public void init(ServletConfig servletConfig) throws ServletException {
   1.177 +        super.init(servletConfig);
   1.178 +
   1.179 +        // get the context of the servlet
   1.180 +        context = getServletContext();
   1.181 +
   1.182 +        // get the context of the application
   1.183 +        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
   1.184 +
   1.185 +        // the the strabon wrapper
   1.186 +        strabonWrapper = (StrabonBeanWrapper) applicationContext.getBean("strabonBean");
   1.187 +
   1.188 +        // get the name of this web application
   1.189 +        appName = context.getContextPath().replace("/", "");
   1.190 +
   1.191 +    }
   1.192 +
   1.193 +    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   1.194 +        request.setCharacterEncoding("UTF-8");
   1.195 +
   1.196 +        // check connection details
   1.197 +        if (strabonWrapper.getStrabon() == null) {
   1.198 +            RequestDispatcher dispatcher = request.getRequestDispatcher("/connection.jsp");
   1.199 +
   1.200 +            // pass the current details of the connection
   1.201 +            request.setAttribute("username", strabonWrapper.getUsername());
   1.202 +            request.setAttribute("password", strabonWrapper.getPassword());
   1.203 +            request.setAttribute("dbname", strabonWrapper.getDatabaseName());
   1.204 +            request.setAttribute("hostname", strabonWrapper.getHostName());
   1.205 +            request.setAttribute("port", strabonWrapper.getPort());
   1.206 +            request.setAttribute("dbengine", strabonWrapper.getDBEngine());
   1.207 +            request.setAttribute("googlemapskey", strabonWrapper.getDBEngine());
   1.208 +
   1.209 +            // pass the other parameters as well
   1.210 +            request.setAttribute("query", request.getParameter("query"));
   1.211 +            if (request.getParameter("format").equalsIgnoreCase("PIECHART")
   1.212 +                    || request.getParameter("format").equalsIgnoreCase("AREACHART")
   1.213 +                    || request.getParameter("format").equalsIgnoreCase("COLUMNCHART")) {
   1.214 +                request.setAttribute("format", "CHART");
   1.215 +            } else {
   1.216 +                request.setAttribute("format", request.getParameter("format"));
   1.217 +            }
   1.218 +            request.setAttribute("handle", request.getParameter("handle"));
   1.219 +
   1.220 +            // forward the request
   1.221 +            dispatcher.forward(request, response);
   1.222 +
   1.223 +        } else {
   1.224 +            // add googlemapsAPIscriptsource attribute for Google API script source tag
   1.225 +            request.setAttribute("googlemapsAPIscriptsource", strabonWrapper.googleMapsAPIScriptSourceForJSP());
   1.226 +
   1.227 +            if (Common.VIEW_TYPE.equals(request.getParameter(Common.VIEW))) {
   1.228 +                // HTML visual interface
   1.229 +                processVIEWRequest(request, response);
   1.230 +
   1.231 +            } else {// invoked as a service
   1.232 +                processRequest(request, response);
   1.233 +            }
   1.234 +        }
   1.235 +    }
   1.236 +
   1.237 +    /**
   1.238 +     * Processes the request made by a client of the endpoint that uses it as a
   1.239 +     * 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<TupleQueryResultFormat> 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 -			TupleQueryResultFormat 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 -				System.out.println(format.getName());
   1.277 -				strabonWrapper.query(query, format.getName(), out);
   1.278 -				response.setStatus(HttpServletResponse.SC_OK);
   1.279 -				
   1.280 -			} catch (Exception e) {
   1.281 -				logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e);
   1.282 -				response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
   1.283 -				out.print(ResponseMessages.getXMLHeader());
   1.284 -				out.print(ResponseMessages.getXMLException(e.getMessage()));
   1.285 -				out.print(ResponseMessages.getXMLFooter());
   1.286 -			}
   1.287 -		}
   1.288 -	    	
   1.289 -		out.flush();
   1.290 -	}
   1.291 -	
   1.292 -	/**
   1.293 -     * Processes the request made from the HTML visual interface of Strabon 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 +                strabonWrapper.query(query, format.getName(), out);
   1.326 +                response.setStatus(HttpServletResponse.SC_OK);
   1.327 +
   1.328 +            } catch (Exception e) {
   1.329 +                logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e);
   1.330 +                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
   1.331 +                out.print(ResponseMessages.getXMLHeader());
   1.332 +                out.print(ResponseMessages.getXMLException(e.getMessage()));
   1.333 +                out.print(ResponseMessages.getXMLFooter());
   1.334 +            }
   1.335 +        }
   1.336 +
   1.337 +        out.flush();
   1.338 +    }
   1.339 +
   1.340 +    /**
   1.341 +     * Processes the request made from the HTML visual interface of Strabon
   1.342 +     * 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 -			
   1.364 -		} else {
   1.365 -			// do not decode the SPARQL query (see bugs #65 and #49)
   1.366 -			//String query = URLDecoder.decode(request.getParameter("query"), "UTF-8");
   1.367 -			
   1.368 -			String query = request.getParameter("query");
   1.369 -			String format = request.getParameter("format");
   1.370 -			System.out.println(format);
   1.371 -			String handle = request.getParameter("handle");
   1.372 -			String maxLimit = request.getParameter("maxLimit");
   1.373 -			
   1.374 -			// get stSPARQLQueryResultFormat from given format name
   1.375 -			TupleQueryResultFormat queryResultFormat = stSPARQLQueryResultFormat.valueOf(format);
   1.376 -			
   1.377 -			if (query == null || format == null || queryResultFormat == null) {
   1.378 -				dispatcher = request.getRequestDispatcher("query.jsp");
   1.379 -				request.setAttribute(ERROR, PARAM_ERROR);
   1.380 -				dispatcher.forward(request, response);
   1.381 -				
   1.382 -			} else {
   1.383 -				query = strabonWrapper.addLimit(query, maxLimit);
   1.384 -				if ("download".equals(handle)) { // download as attachment
   1.385 -					ServletOutputStream out = response.getOutputStream();
   1.386 -					
   1.387 -					response.setContentType(queryResultFormat.getDefaultMIMEType());
   1.388 -				    response.setHeader("Content-Disposition", 
   1.389 -				    				"attachment; filename=results." + 
   1.390 -				    				queryResultFormat.getDefaultFileExtension() + "; " + 
   1.391 -				    				queryResultFormat.getCharset());
   1.392 -				    
   1.393 -				    try {
   1.394 -						strabonWrapper.query(query, format, out);
   1.395 -						response.setStatus(HttpServletResponse.SC_OK);
   1.396 -						
   1.397 -				    } catch (Exception e) {
   1.398 -				    	response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
   1.399 -						out.print(ResponseMessages.getXMLHeader());
   1.400 -						out.print(ResponseMessages.getXMLException(e.getMessage()));
   1.401 -						out.print(ResponseMessages.getXMLFooter());
   1.402 -				    }
   1.403 -				    
   1.404 -				    out.flush();
   1.405 -				    
   1.406 -				} else if (("map".equals(handle) || "map_local".equals(handle) || "timemap".equals(handle)) && 
   1.407 -						(queryResultFormat == stSPARQLQueryResultFormat.KML || 
   1.408 -						 queryResultFormat == stSPARQLQueryResultFormat.KMZ) ) {
   1.409 -					// show map (only valid for KML/KMZ)
   1.410 -					
   1.411 -					// get dispatcher
   1.412 -					dispatcher = request.getRequestDispatcher("query.jsp");
   1.413 -					
   1.414 -					// re-assign handle
   1.415 -					request.setAttribute("handle", handle);
   1.416 -					
   1.417 -					SecureRandom random = new SecureRandom();
   1.418 -					String temp = new BigInteger(130, random).toString(32);
   1.419 -					
   1.420 -					// the temporary KML/KMZ file to create in the server
   1.421 -					String tempKMLFile = temp + "." + queryResultFormat.getDefaultFileExtension();;
   1.422 -					
   1.423 -					try{
   1.424 -						Date date = new Date();
   1.425 -						
   1.426 -						// get the absolute path of the temporary directory
   1.427 -						if(!request.getParameter("handle").toString().contains("timemap")){
   1.428 -							tempDirectory =  appName + "-temp";
   1.429 -							
   1.430 -							basePath = context.getRealPath("/") + "/../ROOT/" + tempDirectory + "/";
   1.431 -							// fix the temporary directory for this web application
   1.432 -							
   1.433 -							FileUtils.forceMkdir(new File(basePath));
   1.434 +        if (reqFuncionality.equals("Update")) {
   1.435 +            // get the dispatcher for forwarding the rendering of the response
   1.436 +            dispatcher = request.getRequestDispatcher("/Update");
   1.437 +            dispatcher.forward(request, response);
   1.438  
   1.439 -							@SuppressWarnings("unchecked")
   1.440 -							Iterator<File> it = FileUtils.iterateFiles(new File(basePath), null, false);
   1.441 -							while(it.hasNext()){
   1.442 -								File tbd = new File((it.next()).getAbsolutePath());
   1.443 -								if (FileUtils.isFileOlder(new File(tbd.getAbsolutePath()), date.getTime())){
   1.444 -									FileUtils.forceDelete(new File(tbd.getAbsolutePath()));
   1.445 -								}
   1.446 -							}
   1.447 -						} else{ //timemap case
   1.448 -							tempDirectory =  "js/timemap";
   1.449 -							basePath = context.getRealPath("/") + tempDirectory + "/";
   1.450 -							// fix the temporary directory for this web application
   1.451 -						}
   1.452 +        } else {
   1.453 +            // do not decode the SPARQL query (see bugs #65 and #49)
   1.454 +            //String query = URLDecoder.decode(request.getParameter("query"), "UTF-8");
   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 -						// fix the temporary directory for this web application
   1.462 -						
   1.463 -						// create temporary KML/KMZ file
   1.464 -						File file = new File(basePath + tempKMLFile);
   1.465 -						// if file does not exist, then create it
   1.466 -						if(!file.exists()){
   1.467 -							file.createNewFile();
   1.468 -						}
   1.469 -						
   1.470 -						try {
   1.471 -							// query and write the result in the temporary KML/KMZ file
   1.472 -							FileOutputStream fos = new FileOutputStream(basePath + tempKMLFile);
   1.473 -							strabonWrapper.query(query, format, fos);
   1.474 -							fos.close();
   1.475 -						
   1.476 -							if(request.getParameter("handle").toString().contains("timemap")){
   1.477 -								request.setAttribute("pathToKML", tempDirectory+"/"+ tempKMLFile);
   1.478 -							}else {
   1.479 -								request.setAttribute("pathToKML", 
   1.480 -										request.getScheme() + "://" +  
   1.481 -										request.getServerName() + ":" + request.getServerPort() + 
   1.482 -										"/" + tempDirectory + "/" + tempKMLFile);
   1.483 -							}
   1.484 -							
   1.485 -						} catch (MalformedQueryException e) {
   1.486 -							logger.error("[StrabonEndpoint.QueryBean] Error during querying. {}", e.getMessage());
   1.487 -							request.setAttribute(ERROR, e.getMessage());
   1.488 -							
   1.489 -						} catch (Exception e) {
   1.490 -							logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e);
   1.491 -							request.setAttribute(ERROR, e.getMessage());
   1.492 -						}
   1.493 -						
   1.494 -						dispatcher.forward(request, response);
   1.495 +            // get stSPARQLQueryResultFormat from given format name
   1.496 +            TupleQueryResultFormat queryResultFormat = stSPARQLQueryResultFormat.valueOf(format);
   1.497  
   1.498 -					} catch(IOException e) {
   1.499 -						logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e);
   1.500 -					}
   1.501 +            if (query == null || format == null || queryResultFormat == null) {
   1.502 +                dispatcher = request.getRequestDispatcher("query.jsp");
   1.503 +                request.setAttribute(ERROR, PARAM_ERROR);
   1.504 +                dispatcher.forward(request, response);
   1.505  
   1.506 -				} else { // "plain" is assumed as the default
   1.507 -					dispatcher = request.getRequestDispatcher("query.jsp");
   1.508 -					ByteArrayOutputStream bos = new ByteArrayOutputStream();
   1.509 -					
   1.510 -					try {
   1.511 -						strabonWrapper.query(query, format, bos);
   1.512 -						if (format.equals(Common.getHTMLFormat())) {
   1.513 -							request.setAttribute(RESPONSE, bos.toString());
   1.514 -						} 
   1.515 -						else if(format.equals(Format.PIECHART.toString())
   1.516 -								|| format.equals(Format.AREACHART.toString()) 
   1.517 -								|| format.equals(Format.COLUMNCHART.toString())){
   1.518 -							request.setAttribute("format","CHART");
   1.519 -							request.setAttribute(RESPONSE, strabonWrapper.getgChartString());
   1.520 -						}
   1.521 -	
   1.522 -						else {
   1.523 -							request.setAttribute(RESPONSE, StringEscapeUtils.escapeHtml(bos.toString()));
   1.524 -						}
   1.525 -						
   1.526 -					} catch (MalformedQueryException e) {
   1.527 -						logger.error("[StrabonEndpoint.QueryBean] Error during querying. {}", e.getMessage());
   1.528 -						request.setAttribute(ERROR, e.getMessage());
   1.529 -						
   1.530 -					} catch (Exception e) {
   1.531 -						logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e);
   1.532 -						request.setAttribute(ERROR, e.getMessage());
   1.533 -						
   1.534 -					} finally {
   1.535 -						dispatcher.forward(request, response);
   1.536 -					}
   1.537 -				}
   1.538 -			}
   1.539 -		}
   1.540 -	}
   1.541 -	
   1.542 -	/**
   1.543 -	 * Given an Accept header, it parses it and extracts the mime types for the accepted formats.
   1.544 -	 * The header might contain multiple accepted values and qvalues as well, however, qvalues
   1.545 -	 * are ignored. The extracted mime types are then transformed to stSPARQLQueryResultFormat
   1.546 -	 * and a list of such objects is returned. If a mimetype is not valid, then it is ignored.
   1.547 -	 * If all mimetypes are invalid, then the returned list has zero elements, but it is not
   1.548 -	 * null.
   1.549 -	 * 
   1.550 -	 * @param header
   1.551 -	 * @return
   1.552 -	 */
   1.553 -	private List<TupleQueryResultFormat> parseMultiValuedAcceptHeader(String header) {
   1.554 +            } else {
   1.555 +                query = strabonWrapper.addLimit(query, maxLimit);
   1.556 +                if ("download".equals(handle)) { // download as attachment
   1.557 +                    ServletOutputStream out = response.getOutputStream();
   1.558 +
   1.559 +                    response.setContentType(queryResultFormat.getDefaultMIMEType());
   1.560 +                    response.setHeader("Content-Disposition",
   1.561 +                            "attachment; filename=results."
   1.562 +                            + queryResultFormat.getDefaultFileExtension() + "; "
   1.563 +                            + queryResultFormat.getCharset());
   1.564 +
   1.565 +                    try {
   1.566 +                        strabonWrapper.query(query, format, out);
   1.567 +                        response.setStatus(HttpServletResponse.SC_OK);
   1.568 +
   1.569 +                    } catch (Exception e) {
   1.570 +                        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
   1.571 +                        out.print(ResponseMessages.getXMLHeader());
   1.572 +                        out.print(ResponseMessages.getXMLException(e.getMessage()));
   1.573 +                        out.print(ResponseMessages.getXMLFooter());
   1.574 +                    }
   1.575 +
   1.576 +                    out.flush();
   1.577 +
   1.578 +                } else if (("map".equals(handle) || "map_local".equals(handle) || "timemap".equals(handle))
   1.579 +                        && (queryResultFormat == stSPARQLQueryResultFormat.KML
   1.580 +                        || queryResultFormat == stSPARQLQueryResultFormat.KMZ)) {
   1.581 +                    // show map (only valid for KML/KMZ)
   1.582 +
   1.583 +                    // get dispatcher
   1.584 +                    dispatcher = request.getRequestDispatcher("query.jsp");
   1.585 +
   1.586 +                    // re-assign handle
   1.587 +                    request.setAttribute("handle", handle);
   1.588 +
   1.589 +                    SecureRandom random = new SecureRandom();
   1.590 +                    String temp = new BigInteger(130, random).toString(32);
   1.591 +
   1.592 +                    // the temporary KML/KMZ file to create in the server
   1.593 +                    String tempKMLFile = temp + "." + queryResultFormat.getDefaultFileExtension();;
   1.594 +
   1.595 +                    try {
   1.596 +                        Date date = new Date();
   1.597 +
   1.598 +                        // get the absolute path of the temporary directory
   1.599 +                        if (!request.getParameter("handle").toString().contains("timemap")) {
   1.600 +                            tempDirectory = appName + "-temp";
   1.601 +
   1.602 +                            basePath = context.getRealPath("/") + "/../ROOT/" + tempDirectory + "/";
   1.603 +                            // fix the temporary directory for this web application
   1.604 +
   1.605 +                            FileUtils.forceMkdir(new File(basePath));
   1.606 +
   1.607 +                            @SuppressWarnings("unchecked")
   1.608 +                            Iterator<File> it = FileUtils.iterateFiles(new File(basePath), null, false);
   1.609 +                            while (it.hasNext()) {
   1.610 +                                File tbd = new File((it.next()).getAbsolutePath());
   1.611 +                                if (FileUtils.isFileOlder(new File(tbd.getAbsolutePath()), date.getTime())) {
   1.612 +                                    FileUtils.forceDelete(new File(tbd.getAbsolutePath()));
   1.613 +                                }
   1.614 +                            }
   1.615 +                        } else { //timemap case
   1.616 +                            tempDirectory = "js/timemap";
   1.617 +                            basePath = context.getRealPath("/") + tempDirectory + "/";
   1.618 +                            // fix the temporary directory for this web application
   1.619 +                        }
   1.620 +
   1.621 +                        // fix the temporary directory for this web application
   1.622 +                        // create temporary KML/KMZ file
   1.623 +                        File file = new File(basePath + tempKMLFile);
   1.624 +                        // if file does not exist, then create it
   1.625 +                        if (!file.exists()) {
   1.626 +                            file.createNewFile();
   1.627 +                        }
   1.628 +
   1.629 +                        try {
   1.630 +                            // query and write the result in the temporary KML/KMZ file
   1.631 +                            FileOutputStream fos = new FileOutputStream(basePath + tempKMLFile);
   1.632 +                            strabonWrapper.query(query, format, fos);
   1.633 +                            fos.close();
   1.634 +
   1.635 +                            if (request.getParameter("handle").toString().contains("timemap")) {
   1.636 +                                request.setAttribute("pathToKML", tempDirectory + "/" + tempKMLFile);
   1.637 +                            } else {
   1.638 +                                request.setAttribute("pathToKML",
   1.639 +                                        request.getScheme() + "://"
   1.640 +                                        + request.getServerName() + ":" + request.getServerPort()
   1.641 +                                        + "/" + tempDirectory + "/" + tempKMLFile);
   1.642 +                            }
   1.643 +
   1.644 +                        } catch (MalformedQueryException e) {
   1.645 +                            logger.error("[StrabonEndpoint.QueryBean] Error during querying. {}", e.getMessage());
   1.646 +                            request.setAttribute(ERROR, e.getMessage());
   1.647 +
   1.648 +                        } catch (Exception e) {
   1.649 +                            logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e);
   1.650 +                            request.setAttribute(ERROR, e.getMessage());
   1.651 +                        }
   1.652 +
   1.653 +                        dispatcher.forward(request, response);
   1.654 +
   1.655 +                    } catch (IOException e) {
   1.656 +                        logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e);
   1.657 +                    }
   1.658 +
   1.659 +                } else { // "plain" is assumed as the default
   1.660 +                    dispatcher = request.getRequestDispatcher("query.jsp");
   1.661 +                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
   1.662 +
   1.663 +                    try {
   1.664 +                        strabonWrapper.query(query, format, bos);
   1.665 +                        if (format.equals(Common.getHTMLFormat())) {
   1.666 +                            request.setAttribute(RESPONSE, bos.toString());
   1.667 +                        } else if (format.equals(Format.PIECHART.toString())
   1.668 +                                || format.equals(Format.AREACHART.toString())
   1.669 +                                || format.equals(Format.COLUMNCHART.toString())) {
   1.670 +                            request.setAttribute("format", "CHART");
   1.671 +                            request.setAttribute(RESPONSE, strabonWrapper.getgChartString());
   1.672 +                        } else {
   1.673 +                            request.setAttribute(RESPONSE, StringEscapeUtils.escapeHtml(bos.toString()));
   1.674 +                        }
   1.675 +
   1.676 +                    } catch (MalformedQueryException e) {
   1.677 +                        logger.error("[StrabonEndpoint.QueryBean] Error during querying. {}", e.getMessage());
   1.678 +                        request.setAttribute(ERROR, e.getMessage());
   1.679 +
   1.680 +                    } catch (Exception e) {
   1.681 +                        logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e);
   1.682 +                        request.setAttribute(ERROR, e.getMessage());
   1.683 +
   1.684 +                    } finally {
   1.685 +                        dispatcher.forward(request, response);
   1.686 +                    }
   1.687 +                }
   1.688 +            }
   1.689 +        }
   1.690 +    }
   1.691 +
   1.692 +    /**
   1.693 +     * Given an Accept header, it parses it and extracts the mime types for the
   1.694 +     * accepted formats. The header might contain multiple accepted values and
   1.695 +     * qvalues as well, however, qvalues are ignored. The extracted mime types
   1.696 +     * are then transformed to stSPARQLQueryResultFormat and a list of such
   1.697 +     * objects is returned. If a mimetype is not valid, then it is ignored. If
   1.698 +     * all mimetypes are invalid, then the returned list has zero elements, but
   1.699 +     * it is not null.
   1.700 +     *
   1.701 +     * @param header
   1.702 +     * @return
   1.703 +     */
   1.704 +    private List<TupleQueryResultFormat> parseMultiValuedAcceptHeader(String header) {
   1.705  		List<TupleQueryResultFormat> formats = new ArrayList<TupleQueryResultFormat>();
   1.706 -		
   1.707 -		StringTokenizer token = new StringTokenizer(header, ", ");
   1.708 -		
   1.709 -		while (token.hasMoreTokens()) {
   1.710 -			String value = token.nextToken();
   1.711 -			
   1.712 -			// value might contain qvalues (e.g., "text/plain; q=0.2")
   1.713 -			// for the time being, we just discard them
   1.714 -			int idx_sep_cut = value.indexOf(';');
   1.715 -			if (idx_sep_cut > 0) {
   1.716 -				value = value.substring(0, idx_sep_cut);
   1.717 -			}
   1.718 -			
   1.719 -			// get the stSPARQL Query Result format 
   1.720 +
   1.721 +        StringTokenizer token = new StringTokenizer(header, ", ");
   1.722 +
   1.723 +        while (token.hasMoreTokens()) {
   1.724 +            String value = token.nextToken();
   1.725 +
   1.726 +            // value might contain qvalues (e.g., "text/plain; q=0.2")
   1.727 +            // for the time being, we just discard them
   1.728 +            int idx_sep_cut = value.indexOf(';');
   1.729 +            if (idx_sep_cut > 0) {
   1.730 +                value = value.substring(0, idx_sep_cut);
   1.731 +            }
   1.732 +
   1.733 +            // get the stSPARQL Query Result format 
   1.734  			String valueFromMime = getValueFromMime(value); 
   1.735 -			TupleQueryResultFormat format = stSPARQLQueryResultFormat.valueOf(valueFromMime);		
   1.736 -				        
   1.737 -	        // keep only the valid formats (non-null)
   1.738 -	        if (format != null) {
   1.739 -	        	formats.add(format);
   1.740 -	        }
   1.741 -		}
   1.742 +			TupleQueryResultFormat format = stSPARQLQueryResultFormat.valueOf(valueFromMime);
   1.743 +
   1.744 +            // keep only the valid formats (non-null)
   1.745 +            if (format != null) {
   1.746 +                formats.add(format);
   1.747 +            }
   1.748 +        }
   1.749  
   1.750          return formats;
   1.751 -	}
   1.752 -	
   1.753 -	private String getValueFromMime(String value) {
   1.754 +    }
   1.755 +    
   1.756 +    private String getValueFromMime(String value) {
   1.757  		if (value.equalsIgnoreCase("application/sparql-results+json")) {
   1.758  			return "SPARQL/JSON";
   1.759  		}
   1.760 @@ -453,4 +448,4 @@
   1.761  		
   1.762  	}
   1.763  
   1.764 -}
   1.765 +}
   1.766 \ No newline at end of file