Strabon

changeset 328:5a4a3f40bead

changed endpoint according to new query prototype (naive). This completes fix for Bug #04
author Babis Nikolaou <charnik@di.uoa.gr>
date Fri Jun 22 19:36:31 2012 +0300 (2012-06-22)
parents c7dd5add6c9d
children a449fcf2e56c
files endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java
line diff
     1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Fri Jun 22 18:46:45 2012 +0300
     1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Fri Jun 22 19:36:31 2012 +0300
     1.3 @@ -38,6 +38,7 @@
     1.4  
     1.5  import eu.earthobservatory.org.StrabonEndpoint.StrabonBeanWrapper.Entry;
     1.6  import eu.earthobservatory.org.StrabonEndpoint.ResponseMessages;
     1.7 +import eu.earthobservatory.runtime.generaldb.Strabon;
     1.8  
     1.9  
    1.10  public class QueryBean extends HttpServlet {
    1.11 @@ -113,77 +114,85 @@
    1.12  		String reqAccept = (request.getHeader("accept") == null) ? "" : request.getHeader("accept");
    1.13  		String reqFuncionality = (request.getParameter("submit") == null) ? "" : request.getParameter("submit");
    1.14  		
    1.15 -		//System.out.println("request format: " + reqFormat);
    1.16 -		//System.out.println("request accept: " + reqAccept);
    1.17 -		//System.out.println("request functionality: " + reqFuncionality);
    1.18 -
    1.19  		// check whether Update submit button was fired
    1.20  		if (reqFuncionality.equals("Update")) { // only for executions from web browsers
    1.21 -			//System.out.println("Running update");
    1.22 -			//System.out.println("Datahive: " + hive.toString());
    1.23  			response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
    1.24 -			response.sendRedirect("Update?SPARQLQuery=" +URLEncoder.encode(hive.getSPARQLQuery(), "UTF-8"));
    1.25 +			response.sendRedirect("Update?SPARQLQuery=" + URLEncoder.encode(hive.getSPARQLQuery(), "UTF-8"));
    1.26  
    1.27  			return;
    1.28  		}
    1.29  
    1.30  		if ((reqFormat == "") && (reqAccept == "")) {
    1.31 -			hive.setFormat("HTML");
    1.32 -			response.setContentType("text/html; charset=UTF-8");			
    1.33 +			hive.setFormat(Strabon.FORMAT_HTML);
    1.34 +			response.setContentType("text/html; charset=UTF-8");
    1.35 +			
    1.36  		} else if (reqAccept.contains("application/vnd.google-earth.kml+xml")) {
    1.37  			response.setContentType("application/vnd.google-earth.kml+xml");
    1.38 -			hive.setFormat("KML");
    1.39 +			hive.setFormat(Strabon.FORMAT_KML);
    1.40 +			
    1.41  		} else if (reqAccept.contains("application/vnd.google-earth.kmz")) {
    1.42  			response.setContentType("application/vnd.google-earth.kmz");
    1.43 -			hive.setFormat("KMZ");
    1.44 +			hive.setFormat(Strabon.FORMAT_KMZ);
    1.45 +			
    1.46  		} else if (reqAccept.contains("application/sparql-results+xml")) {			
    1.47  			response.setContentType("application/sparql-results+xml; charset=UTF-8");
    1.48 -			hive.setFormat("XML");
    1.49 +			hive.setFormat(Strabon.FORMAT_XML);
    1.50 +			
    1.51  		} else if (reqAccept.contains("text/xml")) {
    1.52  			response.setContentType("text/xml; charset=UTF-8");
    1.53 -			hive.setFormat("XML");
    1.54 +			hive.setFormat(Strabon.FORMAT_XML);
    1.55 +			
    1.56  		} else if (reqAccept.contains("application/json"))  {
    1.57  			response.setContentType("application/json; charset=UTF-8");
    1.58 -			hive.setFormat("GEOJSON");
    1.59 +			hive.setFormat(Strabon.FORMAT_GEOJSON);
    1.60  
    1.61  		} else if (reqFormat.equalsIgnoreCase("KML file")) {
    1.62  			response.setContentType("application/vnd.google-earth.kml+xml; charset=UTF-8");
    1.63 -		        response.setHeader("Content-Disposition","attachment;filename=pico.kml");
    1.64 -			hive.setFormat("KML");
    1.65 +		    response.setHeader("Content-Disposition","attachment;filename=pico.kml");
    1.66 +			hive.setFormat(Strabon.FORMAT_KML);
    1.67 +			
    1.68  		} else if (reqFormat.equalsIgnoreCase("KML")) {
    1.69  			response.setContentType("text/plain; charset=UTF-8");
    1.70 -			hive.setFormat("KML");
    1.71 +			hive.setFormat(Strabon.FORMAT_KML);
    1.72 +			
    1.73  		} else if (reqFormat.equalsIgnoreCase("KMZ file")) {
    1.74  			response.setContentType("application/vnd.google-earth.kmz; charset=UTF-8");
    1.75  			response.setHeader("Content-Disposition","attachment;filename=pico.kmz");
    1.76 -			hive.setFormat("KMZ");
    1.77 +			hive.setFormat(Strabon.FORMAT_KMZ);
    1.78 +			
    1.79  		} else if (reqFormat.equalsIgnoreCase("KMZ")) {
    1.80  			response.setContentType("text/plain; charset=UTF-8");
    1.81 -			hive.setFormat("KMZ");
    1.82 +			hive.setFormat(Strabon.FORMAT_KMZ);
    1.83 +			
    1.84  		} else if (reqFormat.equalsIgnoreCase("SPARQLRESULTS"))  {
    1.85  			response.setContentType("application/sparql-results+xml; charset=UTF-8");
    1.86 -			hive.setFormat("XML");
    1.87 -		} else if (reqFormat.equalsIgnoreCase("XML"))  {
    1.88 +			hive.setFormat(Strabon.FORMAT_XML);
    1.89 +			
    1.90 +		} else if (reqFormat.equalsIgnoreCase(Strabon.FORMAT_XML))  {
    1.91  			response.setContentType("text/xml; charset=UTF-8");
    1.92 -			hive.setFormat("XML");
    1.93 +			hive.setFormat(Strabon.FORMAT_XML);
    1.94 +			
    1.95  		} else if (reqFormat.equalsIgnoreCase("KMLMAP"))  {
    1.96  			response.setContentType("text/html; charset=UTF-8");
    1.97  			hive.setFormat("KMLMAP");
    1.98 +			
    1.99  		} else if (reqFormat.equalsIgnoreCase("KMZMAP"))  {
   1.100  			response.setContentType("text/html; charset=UTF-8");
   1.101  			hive.setFormat("KMZMAP");
   1.102 -		} else if (reqFormat.equalsIgnoreCase("GEOJSON"))  {
   1.103 +			
   1.104 +		} else if (reqFormat.equalsIgnoreCase(Strabon.FORMAT_GEOJSON))  {
   1.105  			response.setContentType("application/json; charset=UTF-8");
   1.106 -			hive.setFormat("GEOJSON");
   1.107 +			hive.setFormat(Strabon.FORMAT_GEOJSON);
   1.108 +			
   1.109  		} else {
   1.110  			response.setContentType("text/html; charset=UTF-8");
   1.111 -			hive.setFormat("HTML");
   1.112 +			hive.setFormat(Strabon.FORMAT_HTML);
   1.113  		}
   1.114  
   1.115  		PrintWriter out = response.getWriter();
   1.116          out.flush();
   1.117  
   1.118 -		if ((hive.getFormat().equalsIgnoreCase("KML")) || (hive.getFormat().equalsIgnoreCase("KMZ"))) {
   1.119 +		if ((hive.getFormat().equalsIgnoreCase(Strabon.FORMAT_KML)) || (hive.getFormat().equalsIgnoreCase(Strabon.FORMAT_KMZ))) {
   1.120  			int status_code = HttpServletResponse.SC_OK;
   1.121  			String answer = "";
   1.122  
   1.123 @@ -215,21 +224,23 @@
   1.124              response.setStatus(status_code);
   1.125  			if (status_code == HttpServletResponse.SC_OK) {
   1.126                 out.append(answer.toString());
   1.127 +               
   1.128  			} else {
   1.129  				response.getWriter().append(ResponseMessages.getXMLHeader());
   1.130  				response.getWriter().append(ResponseMessages.getXMLException(answer));
   1.131  				response.getWriter().append(ResponseMessages.getXMLFooter());
   1.132 +				
   1.133  			}
   1.134  
   1.135  		} else if ((hive.getFormat().equalsIgnoreCase("KMLMAP")) || (hive.getFormat().equalsIgnoreCase("KMZMAP"))) {
   1.136  
   1.137  			StringBuilder errorMessage = new StringBuilder ();
   1.138 -			String answer = evaluateQuery(strabonWrapper, hive.getFormat().equalsIgnoreCase("KMLMAP") ? "KML" : "KML", reqFuncionality, hive.getSPARQLQuery(), errorMessage);
   1.139 +			String answer = evaluateQuery(strabonWrapper, Strabon.FORMAT_KML, reqFuncionality, hive.getSPARQLQuery(), errorMessage);
   1.140  			hive.setErrorMessage(errorMessage.toString());
   1.141  			SecureRandom random = new SecureRandom();
   1.142  			String temp = new BigInteger(130, random).toString(32);  			
   1.143  			String basePath = context.getRealPath("/") + "/../ROOT/tmp/"; 
   1.144 -			String extension = (hive.format.equalsIgnoreCase("KMLMAP") ? "kml" : "kmz");
   1.145 +			String extension = (hive.format.equalsIgnoreCase("KMLMAP") ? Strabon.FORMAT_KML.toLowerCase() : Strabon.FORMAT_KMZ.toLowerCase());
   1.146  
   1.147  			try{
   1.148  				Date date = new Date();
   1.149 @@ -267,7 +278,7 @@
   1.150  				   kmzout.write(answer.getBytes());
   1.151  			       kmzout.closeEntry();
   1.152  				   kmzout.close();
   1.153 -				   }
   1.154 +				}
   1.155  				bw.close();
   1.156  				//FileUtils.forceDeleteOnExit(new File((String) context.getRealPath("/") + "/../ROOT/tmp/" + temp + ".kml"));
   1.157  
   1.158 @@ -331,7 +342,7 @@
   1.159  			}
   1.160  
   1.161  			// write response to client
   1.162 -			if(hive.getFormat().equalsIgnoreCase("XML"))
   1.163 +			if(hive.getFormat().equalsIgnoreCase(Strabon.FORMAT_XML))
   1.164  			{
   1.165  				response.setContentType("text/xml; charset=UTF-8");
   1.166  			}
   1.167 @@ -352,7 +363,7 @@
   1.168  		} 
   1.169  		else { // HTML
   1.170  
   1.171 -			appendHTML1a(out,"");
   1.172 +			appendHTML1a(out, "");
   1.173  
   1.174  			appendHTMLQ(out, strabonWrapper);
   1.175  
     2.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Fri Jun 22 18:46:45 2012 +0300
     2.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Fri Jun 22 19:36:31 2012 +0300
     2.3 @@ -1,5 +1,6 @@
     2.4  package eu.earthobservatory.org.StrabonEndpoint;
     2.5  
     2.6 +import java.io.ByteArrayOutputStream;
     2.7  import java.io.IOException;
     2.8  import java.io.StringReader;
     2.9  import java.net.URL;
    2.10 @@ -14,7 +15,6 @@
    2.11  import org.openrdf.repository.RepositoryException;
    2.12  import org.openrdf.repository.sail.SailRepositoryConnection;
    2.13  import org.openrdf.rio.RDFFormat;
    2.14 -
    2.15  import org.slf4j.Logger;
    2.16  import org.slf4j.LoggerFactory;
    2.17  
    2.18 @@ -180,15 +180,18 @@
    2.19  		}
    2.20  	}
    2.21  
    2.22 -	public Object query(String queryString, String answerFormatStrabon)
    2.23 -	throws MalformedQueryException, RepositoryException, QueryEvaluationException, 
    2.24 -	TupleQueryResultHandlerException, IOException, ClassNotFoundException {
    2.25 +	public String query(String queryString, String answerFormatStrabon)
    2.26 +	throws MalformedQueryException, RepositoryException, QueryEvaluationException, TupleQueryResultHandlerException, IOException, ClassNotFoundException {
    2.27  		logger.info("[StrabonEndpoint] Received SELECT query.");
    2.28  		if ((this.strabon == null) && (!init())) {
    2.29  			throw new RepositoryException("Could not connect to Strabon.");
    2.30  		} 
    2.31  
    2.32 -		return strabon.query(queryString, answerFormatStrabon, strabon.getSailRepoConnection());
    2.33 +		ByteArrayOutputStream answer = new ByteArrayOutputStream();
    2.34 +		
    2.35 +		strabon.query(queryString, answerFormatStrabon, strabon.getSailRepoConnection(), answer);
    2.36 +		
    2.37 +		return answer.toString();
    2.38  	}
    2.39  
    2.40  	public Object update(String updateString, String answerFormatStrabon)