Strabon

changeset 526:61e60bba1cb0

completed QueryBean and updated endpoint respectively
author Babis Nikolaou <charnik@di.uoa.gr>
date Sat Jul 28 15:53:28 2012 +0300 (2012-07-28)
parents 9fcc1b2a52e3
children 99946ae17f9a
files endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java scripts/endpoint
line diff
     1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Sat Jul 28 15:52:58 2012 +0300
     1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Sat Jul 28 15:53:28 2012 +0300
     1.3 @@ -22,6 +22,7 @@
     1.4  import org.apache.commons.io.FileUtils;
     1.5  import org.apache.commons.lang.StringEscapeUtils;
     1.6  import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
     1.7 +import org.openrdf.rio.RDFFormat;
     1.8  import org.slf4j.Logger;
     1.9  import org.slf4j.LoggerFactory;
    1.10  import org.springframework.web.context.WebApplicationContext;
    1.11 @@ -117,8 +118,40 @@
    1.12       * @param response
    1.13       * @throws IOException 
    1.14       */
    1.15 -	private void processRequest(HttpServletRequest request, HttpServletResponse response) {
    1.16 +	private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
    1.17 +		ServletOutputStream out = response.getOutputStream();
    1.18  		
    1.19 +		// get the stSPARQL Query Result format (we check only the Accept header)
    1.20 +        stSPARQLQueryResultFormat format = stSPARQLQueryResultFormat.forMIMEType(request.getHeader("accept"));
    1.21 +        
    1.22 +        // get the query
    1.23 +		String query = request.getParameter("query");
    1.24 +    	
    1.25 +    	// check for required parameters
    1.26 +    	if (format == null || query == null) {
    1.27 +    		response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    1.28 +			out.print(ResponseMessages.getXMLHeader());
    1.29 +			out.print(ResponseMessages.getXMLException(PARAM_ERROR));
    1.30 +			out.print(ResponseMessages.getXMLFooter());
    1.31 +    		
    1.32 +    	} else {
    1.33 +    		// decode the query
    1.34 +    		query = URLDecoder.decode(request.getParameter("query"), "UTF-8");
    1.35 +    		
    1.36 +	    	response.setContentType(format.getDefaultMIMEType());
    1.37 +	    	try {
    1.38 +				strabonWrapper.query(query, format.getName(), out);
    1.39 +				response.setStatus(HttpServletResponse.SC_OK);
    1.40 +				
    1.41 +			} catch (Exception e) {
    1.42 +				response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    1.43 +				out.print(ResponseMessages.getXMLHeader());
    1.44 +				out.print(ResponseMessages.getXMLException(e.getMessage()));
    1.45 +				out.print(ResponseMessages.getXMLFooter());
    1.46 +			}
    1.47 +    	}
    1.48 +    	
    1.49 +    	out.flush();
    1.50  	}
    1.51  
    1.52  	/**
     2.1 --- a/scripts/endpoint	Sat Jul 28 15:52:58 2012 +0300
     2.2 +++ b/scripts/endpoint	Sat Jul 28 15:53:28 2012 +0300
     2.3 @@ -37,8 +37,7 @@
     2.4  	echo "	SPARQL_QUERY	: the SPARQL query to execute or the alias name corresponding to a"
     2.5  	echo "			  predefined query such as:"
     2.6  	echo "				\`size': SELECT (count(*) as ?c) WHERE {?s ?p ?o}"
     2.7 -	echo "	RESULT_FORMAT	: the format of the result. Possible values are \`KMLMAP\, \`GEOJSON', "
     2.8 -	echo "			  \`HTML', \`KMZMAP', \`XML' (default), or \`KML'."
     2.9 +	echo "	RESULT_FORMAT	: the format of the result. Possible values are \`XML' (default), \`KML', \`KMZ', \`GeoJSON', \`HTML', or \`TSV'"
    2.10  }
    2.11  
    2.12  function help_queryfile() {
    2.13 @@ -148,17 +147,32 @@
    2.14  		fi
    2.15  
    2.16  		case "${FORMAT}" in
    2.17 -			[Kk][Mm][Ll][Mm][Aa][Pp]|[Gg][Ee][Oo][Jj][Ss][Oo][Nn]|[Hh][Tt][Mm][Ll]|[Kk][mM][Zz][Mm][Aa][Pp]|[Xx][Mm][Ll]|[Kk][Mm][Ll])
    2.18 -				shift
    2.19 +			[Xx][Mm][Ll])
    2.20 +				MIME_TYPE="application/sparql-results+xml"
    2.21 +				;;
    2.22 +			[Tt][sS][vV])
    2.23 +				MIME_TYPE="text/tab-separated-values"
    2.24 +				;;
    2.25 +			[Kk][Mm][Ll])
    2.26 +				MIME_TYPE="application/vnd.google-earth.kml+xml"
    2.27 +				;;
    2.28 +			[Kk][mM][Zz])
    2.29 +				MIME_TYPE="application/vnd.google-earth.kmz"
    2.30 +				;;
    2.31 +			[Hh][Tt][Mm][Ll])
    2.32 +				MIME_TYPE="text/html"
    2.33 +				;;
    2.34 +			[Gg][Ee][Oo][Jj][Ss][Oo][Nn])
    2.35 +				MIME_TYPE="application/json"
    2.36  				;;
    2.37  			*)
    2.38  				echo "${CMD}: unknown format \"${FORMAT}\"."
    2.39 -				echo "${CMD}: possible values are \`KMLMAP\, \`GEOJSON' \`HTML', \`KMZMAP', \`XML' (default), or \`KML'"
    2.40 +				echo "${CMD}: possible values are \`KML', \`KMZ', \`GeoJSON', \`XML' (default), \`HTML', or \`TSV'"
    2.41  				exit 2
    2.42  				;;
    2.43  		esac
    2.44  
    2.45 -		EXEC="curl ${CURL_OPTS} -d format='${FORMAT}' --data-urlencode SPARQLQuery='${QUERY}' ${URL}"
    2.46 +		EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${QUERY}' ${URL}"
    2.47  		;;
    2.48  	queryfile)
    2.49  		shift
    2.50 @@ -185,17 +199,32 @@
    2.51  		fi
    2.52  
    2.53  		case "${FORMAT}" in
    2.54 -			[Kk][Mm][Ll][Mm][Aa][Pp]|[Gg][Ee][Oo][Jj][Ss][Oo][Nn]|[Hh][Tt][Mm][Ll]|[Kk][mM][Zz][Mm][Aa][Pp]|[Xx][Mm][Ll]|[Kk][Mm][Ll])
    2.55 -				shift
    2.56 +			[Xx][Mm][Ll])
    2.57 +				MIME_TYPE="application/sparql-results+xml"
    2.58 +				;;
    2.59 +			[Tt][sS][vV])
    2.60 +				MIME_TYPE="text/tab-separated-values"
    2.61 +				;;
    2.62 +			[Kk][Mm][Ll])
    2.63 +				MIME_TYPE="application/vnd.google-earth.kml+xml"
    2.64 +				;;
    2.65 +			[Kk][mM][Zz])
    2.66 +				MIME_TYPE="application/vnd.google-earth.kmz"
    2.67 +				;;
    2.68 +			[Hh][Tt][Mm][Ll])
    2.69 +				MIME_TYPE="text/html"
    2.70 +				;;
    2.71 +			[Gg][Ee][Oo][Jj][Ss][Oo][Nn])
    2.72 +				MIME_TYPE="application/json"
    2.73  				;;
    2.74  			*)
    2.75  				echo "${CMD}: unknown format \"${FORMAT}\"."
    2.76 -				echo "${CMD}: possible values are \`KMLMAP\, \`GEOJSON' \`HTML', \`KMZMAP', \`XML' (default), or \`KML'"
    2.77 +				echo "${CMD}: possible values are \`KML', \`KMZ', \`GeoJSON', \`XML' (default), \`HTML', or \`TSV'"
    2.78  				exit 2
    2.79  				;;
    2.80  		esac
    2.81  
    2.82 -		EXEC="curl ${CURL_OPTS} -d format='${FORMAT}' --data-urlencode SPARQLQuery='${PREFIXES}${QUERY}' ${URL}"
    2.83 +		EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${PREFIXES}${QUERY}' ${URL}"
    2.84  		;;
    2.85  
    2.86  	update)