Strabon

changeset 172:a69499da7b01

stick for proper responseType for KML
author Michael Sioutis
date Mon May 21 20:17:03 2012 +0300 (2012-05-21)
parents e9332065a3dd
children 072932f4751e
files endpoint/WebContent/WEB-INF/beans.xml endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java
line diff
     1.1 --- a/endpoint/WebContent/WEB-INF/beans.xml	Mon May 21 18:08:55 2012 +0300
     1.2 +++ b/endpoint/WebContent/WEB-INF/beans.xml	Mon May 21 20:17:03 2012 +0300
     1.3 @@ -19,17 +19,17 @@
     1.4  
     1.5          <constructor-arg type="java.lang.String" >
     1.6              <!-- Database name -->
     1.7 -            <value>endpoint</value>
     1.8 +            <value>DLRstrabonv4</value>
     1.9          </constructor-arg>
    1.10  
    1.11          <constructor-arg type="java.lang.String" >
    1.12              <!-- Username -->
    1.13 -            <value>kkyzir</value>
    1.14 +            <value>postgres</value>
    1.15          </constructor-arg>
    1.16  
    1.17          <constructor-arg type="java.lang.String" >
    1.18              <!-- Password -->
    1.19 -            <value>postgresql</value>
    1.20 +            <value>brun1s86</value>
    1.21          </constructor-arg>
    1.22  
    1.23          <constructor-arg type="int" >
    1.24 @@ -345,4 +345,4 @@
    1.25              </list>
    1.26           </constructor-arg>
    1.27      </bean>
    1.28 -</beans>
    1.29 \ No newline at end of file
    1.30 +</beans>
     2.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Mon May 21 18:08:55 2012 +0300
     2.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Mon May 21 20:17:03 2012 +0300
     2.3 @@ -122,10 +122,10 @@
     2.4  			hive.setFormat("HTML");
     2.5  			response.setContentType("text/html; charset=UTF-8");			
     2.6  		} else if (reqAccept.contains("application/vnd.google-earth.kml+xml")) {
     2.7 -			response.setContentType("application/vnd.google-earth.kml+xml; charset=UTF-8");
     2.8 +			response.setContentType("application/vnd.google-earth.kml+xml");
     2.9  			hive.setFormat("KML");
    2.10  		} else if (reqAccept.contains("application/vnd.google-earth.kmz")) {
    2.11 -			response.setContentType("application/vnd.google-earth.kmz; charset=UTF-8");
    2.12 +			response.setContentType("application/vnd.google-earth.kmz");
    2.13  			hive.setFormat("KMZ");
    2.14  		} else if (reqAccept.contains("application/sparql-results+xml")) {			
    2.15  			response.setContentType("application/sparql-results+xml; charset=UTF-8");
    2.16 @@ -134,10 +134,10 @@
    2.17  			response.setContentType("text/xml; charset=UTF-8");
    2.18  			hive.setFormat("XML");
    2.19  		} else if (reqFormat.equalsIgnoreCase("KML")) {
    2.20 -			response.setContentType("application/vnd.google-earth.kml+xml; charset=UTF-8");
    2.21 +			response.setContentType("application/vnd.google-earth.kml+xml");
    2.22  			hive.setFormat("KML");
    2.23  		} else if (reqFormat.equalsIgnoreCase("KMZ")) {
    2.24 -			response.setContentType("application/vnd.google-earth.kmz; charset=UTF-8");
    2.25 +			response.setContentType("application/vnd.google-earth.kmz");
    2.26  			hive.setFormat("KMZ");
    2.27  		} else if (reqFormat.equalsIgnoreCase("SPARQLRESULTS"))  {
    2.28  			response.setContentType("application/sparql-results+xml; charset=UTF-8");
    2.29 @@ -163,89 +163,50 @@
    2.30  
    2.31  		if ((hive.getFormat().equalsIgnoreCase("KML")) || (hive.getFormat().equalsIgnoreCase("KMZ"))) {
    2.32  			StringBuilder errorMessage = new StringBuilder ();
    2.33 -			String answer = evaluateQuery(strabonWrapper, hive.getFormat(), reqFuncionality, hive.getSPARQLQuery(), errorMessage);
    2.34 -			hive.setErrorMessage(errorMessage.toString());
    2.35 -			SecureRandom random = new SecureRandom();
    2.36 -			String extension = (hive.format.equalsIgnoreCase("KMLMAP") ? "kml" : "kmz");
    2.37 -			String temp = new BigInteger(130, random).toString(32);   
    2.38 +			//String answer = evaluateQuery(strabonWrapper, hive.getFormat(), reqFuncionality, hive.getSPARQLQuery(), errorMessage);
    2.39 +			//hive.setErrorMessage(errorMessage.toString());
    2.40 +			//SecureRandom random = new SecureRandom();
    2.41 +			//String extension = (hive.format.equalsIgnoreCase("KMLMAP") ? "kml" : "kmz");
    2.42 +			//String temp = new BigInteger(130, random).toString(32);   
    2.43  			
    2.44 -			String basePath = context.getRealPath("/") + "/../ROOT/tmp/";
    2.45 +			int status_code = HttpServletResponse.SC_OK;
    2.46 +			String answer = "";
    2.47  
    2.48 -			try{
    2.49 -				Date date = new Date();
    2.50 +			try {
    2.51 +				// execute query
    2.52 +				answer = (String) strabonWrapper.query(hive.getSPARQLQuery(), hive.getFormat());
    2.53  
    2.54 -				FileUtils.forceMkdir(new File(basePath));
    2.55 +			} catch (MalformedQueryException e) {
    2.56 +				status_code = HttpServletResponse.SC_BAD_REQUEST;
    2.57 +				answer = e.getMessage();
    2.58  
    2.59 -				Iterator it = FileUtils.iterateFiles(new File(basePath), null, false);
    2.60 -				while(it.hasNext()){
    2.61 -					File tbd = new File(((File) it.next()).getAbsolutePath());
    2.62 -					if (FileUtils.isFileOlder( new File(tbd.getAbsolutePath()), date.getTime())){
    2.63 -						FileUtils.forceDelete(new File(tbd.getAbsolutePath()));
    2.64 -					}
    2.65 -				}
    2.66 +			} catch (RepositoryException e) {
    2.67 +				status_code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    2.68 +				answer = e.getMessage();
    2.69  
    2.70 -				File file =new File(basePath + temp + "." + extension);
    2.71 +			} catch (QueryEvaluationException e) {
    2.72 +				status_code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    2.73 +				answer = e.getMessage();
    2.74  
    2.75 -				//if file doesnt exists, then create it
    2.76 -				if(!file.exists()){
    2.77 -					file.createNewFile();
    2.78 -				}
    2.79 +			} catch (TupleQueryResultHandlerException e) {
    2.80 +				status_code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    2.81 +				answer = e.getMessage();
    2.82  
    2.83 -				FileWriter fw = null;
    2.84 -				if(hive.getFormat().equalsIgnoreCase("KML"))
    2.85 -				{
    2.86 -					fw = new FileWriter(basePath + temp + ".kml");
    2.87 -				}
    2.88 -				else //KMZ
    2.89 -				{
    2.90 -					fw = new FileWriter(basePath + temp + ".kmz");
    2.91 -				}
    2.92 -				BufferedWriter bw = new BufferedWriter(fw);
    2.93 -				bw.write(answer);
    2.94 -				bw.close();
    2.95 -				//FileUtils.forceDeleteOnExit(new File((String) context.getRealPath("/") + "/../ROOT/tmp/" + temp + ".kml"));
    2.96 -
    2.97 -				//System.out.println("Done");
    2.98 -
    2.99 -				
   2.100 -			} catch(IOException e) {
   2.101 -				e.printStackTrace();
   2.102 +			} catch (ClassNotFoundException e) {
   2.103 +				status_code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
   2.104 +				answer = e.getMessage();
   2.105  			}
   2.106  
   2.107 -			response.setDateHeader("Expires", 0);			
   2.108 -			response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
   2.109 +			//response.setContentType("text/plain; charset=UTF-8");
   2.110 +                        response.setStatus(status_code);
   2.111 +			if (status_code == HttpServletResponse.SC_OK) {
   2.112 +				response.getWriter().append(answer);
   2.113  
   2.114 -			String pathToKML = "";
   2.115 -			if (hive.getFormat().equalsIgnoreCase("KML")) {
   2.116 -				response.setContentType("application/vnd.google-earth.kml+xml; charset=UTF-8");
   2.117 -				response.setHeader("Location", request.getScheme() + "://" +  request.getServerName() +":" + request.getServerPort() +"/tmp/" + temp + ".kml");
   2.118 -				if (answer!="")
   2.119 -					pathToKML = request.getScheme() + "://" +  request.getServerName() +":" + request.getServerPort() +"/tmp/" + temp + ".kml";
   2.120  			} else {
   2.121 -				response.setContentType("application/vnd.google-earth.kmz; charset=UTF-8");
   2.122 -				response.setHeader("Location", request.getScheme() + "://" +  request.getServerName() +":" + request.getServerPort() +"/tmp/" + temp + ".kmz");
   2.123 -				if (answer!="")
   2.124 -					pathToKML = request.getScheme() + "://" +  request.getServerName() +":" + request.getServerPort() +"/tmp/" + temp + ".kmz";
   2.125 +				response.getWriter().append(ResponseMessages.getXMLHeader());
   2.126 +				response.getWriter().append(ResponseMessages.getXMLException(answer));
   2.127 +				response.getWriter().append(ResponseMessages.getXMLFooter());
   2.128  			}
   2.129 -			
   2.130 -			appendHTML1a(out,pathToKML);
   2.131 -
   2.132 -			appendHTMLQ(out, strabonWrapper);
   2.133 -
   2.134 -			appendHTML1b(out);
   2.135 -
   2.136 -			if (hive.getSPARQLQuery() != null)
   2.137 -				out.write(hive.getSPARQLQuery());
   2.138 -
   2.139 -			appendHTML2(out, hive.getFormat());
   2.140 -
   2.141 -			out.append("</table></td></tr></table>");
   2.142 -
   2.143 -			appendHTML4(out);
   2.144 -			if (answer!="")
   2.145 -				//out.append("<div id=\"map_canvas\"></div>");
   2.146 -				out.append("");
   2.147 -			appendHTML5(out);
   2.148  
   2.149  		} else if ((hive.getFormat().equalsIgnoreCase("KMLMAP")) || (hive.getFormat().equalsIgnoreCase("KMZMAP"))) {
   2.150