Strabon

changeset 91:c92abb8ed523

made endpoint return appropriate XML answers and status codes when the format is XML
author Babis Nikolaou <charnik@di.uoa.gr>
date Wed Apr 04 23:07:46 2012 +0300 (2012-04-04)
parents 99c070b82188
children 1ab50d5a58c4
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 Apr 04 22:37:24 2012 +0300
     1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Wed Apr 04 23:07:46 2012 +0300
     1.3 @@ -119,7 +119,6 @@
     1.4  			response.setContentType("text/html; charset=UTF-8");
     1.5  			hive.setFormat("HTML");
     1.6  		}
     1.7 -		//System.out.println("\n\n\n\n\format='"+this.format+"'\n\n\n\n\n");
     1.8  		
     1.9  		PrintWriter out = response.getWriter();
    1.10  		
    1.11 @@ -190,16 +189,49 @@
    1.12  			   //out.append("<div id=\"map_canvas\"></div>");
    1.13  				out.append("");
    1.14  			appendHTML5(out);
    1.15 -		} 
    1.16 -		else if ((hive.getFormat().equalsIgnoreCase("XML"))) {
    1.17 +			
    1.18 +		} else if ((hive.getFormat().equalsIgnoreCase("XML"))) {
    1.19 +			int status_code = HttpServletResponse.SC_OK;
    1.20 +			String answer = "";
    1.21 +			
    1.22 +			try {
    1.23 +				// execute query
    1.24 +				answer = (String) strabonWrapper.query(hive.getSPARQLQuery(), hive.getFormat());
    1.25 +				
    1.26 +			} catch (MalformedQueryException e) {
    1.27 +				status_code = HttpServletResponse.SC_BAD_REQUEST;
    1.28 +				answer = e.getMessage();
    1.29 +				
    1.30 +			} catch (RepositoryException e) {
    1.31 +				status_code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    1.32 +				answer = e.getMessage();
    1.33 +				
    1.34 +			} catch (QueryEvaluationException e) {
    1.35 +				status_code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    1.36 +				answer = e.getMessage();
    1.37 +				
    1.38 +			} catch (TupleQueryResultHandlerException e) {
    1.39 +				status_code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    1.40 +				answer = e.getMessage();
    1.41 +				
    1.42 +			} catch (ClassNotFoundException e) {
    1.43 +				status_code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    1.44 +				answer = e.getMessage();
    1.45 +			}
    1.46 +			
    1.47 +			// write response to client
    1.48  			response.setContentType("text/xml; charset=UTF-8");
    1.49 -			StringBuilder errorMessage = new StringBuilder ();
    1.50 -			String answer = evaluateQuery(strabonWrapper, hive.getFormat(), reqFuncionality, hive.getSPARQLQuery(), errorMessage);
    1.51 -			hive.setErrorMessage(errorMessage.toString());
    1.52 -			out.println(answer);
    1.53 -
    1.54 -		}
    1.55 -		else {
    1.56 +			response.setStatus(status_code);
    1.57 +			if (status_code == HttpServletResponse.SC_OK) {
    1.58 +				response.getWriter().append(answer);
    1.59 +				
    1.60 +			} else {
    1.61 +				response.getWriter().append(UpdateBean.getUPDATEHeaderResponse());
    1.62 +				response.getWriter().append(UpdateBean.getExceptionError(answer));
    1.63 +				response.getWriter().append(UpdateBean.getUPDATEFooterResponse());
    1.64 +			}
    1.65 +			
    1.66 +		} else { // HTML
    1.67  			
    1.68  			appendHTML1a(out,"");
    1.69  			
    1.70 @@ -246,28 +278,17 @@
    1.71  		context = getServletContext();
    1.72  		WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
    1.73  		
    1.74 -		strabonWrapper = (StrabonBeanWrapper)applicationContext.getBean("strabonBean");
    1.75 +		strabonWrapper = (StrabonBeanWrapper) applicationContext.getBean("strabonBean");
    1.76  	}
    1.77  
    1.78  	public String evaluateQuery(StrabonBeanWrapper strabonWrapper, String resultFormat, String reqFunctionality, String SPARQLQuery, StringBuilder errorMessage) {		
    1.79  		String answer = "";
    1.80 -
    1.81 -//		System.out.println("evaluateQuery: will call wrapper. Query  = " + this.SPARQLQuery);
    1.82 -//		System.out.println("evaluateQuery: will call wrapper. result = " + resultFormat);
    1.83 -
    1.84 +		
    1.85  		try {
    1.86  			if (SPARQLQuery == null) {
    1.87  				answer = "";
    1.88  			} else {
    1.89 -				//System.out.println("evaluateQuery: Calling...");
    1.90 -				if (reqFunctionality.equals("Update")) {
    1.91 -				//if (((String)this.SPARQLQuery).toLowerCase().contains("insert") || ((String)this.SPARQLQuery).toLowerCase().contains("delete"))  { 
    1.92 -				   answer = (String)strabonWrapper.update(SPARQLQuery, resultFormat);
    1.93 -				   }
    1.94 -				else{
    1.95 -				   answer = (String)strabonWrapper.query(SPARQLQuery, resultFormat);
    1.96 -				}
    1.97 -				//System.out.println("evaluateQuery: Called...");
    1.98 +				   answer = (String) strabonWrapper.query(SPARQLQuery, resultFormat);
    1.99  			}
   1.100  		} catch (MalformedQueryException e) {
   1.101  			e.printStackTrace();
   1.102 @@ -292,10 +313,6 @@
   1.103  			errorMessage.append(e.getMessage());
   1.104  		}
   1.105  
   1.106 -		//System.out.println("evaluateQuery: called wrapper. answer  = " + answer);
   1.107 -		//System.out.println("evaluateQuery: called wrapper. error   = " + ((this.errorMessage == null) ? "" : this.errorMessage));
   1.108 -
   1.109 -		
   1.110  		return answer;		
   1.111  	}
   1.112