Strabon

changeset 670:f443dfe30f78

completed implementation of query() method for StrabonEndpoint
author Babis Nikolaou <charnik@di.uoa.gr>
date Tue Oct 30 21:37:56 2012 +0200 (2012-10-30)
parents 130f6aca1765
children 24a6e7d423eb
files endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpoint.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java
line diff
     1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpoint.java	Tue Oct 30 21:18:40 2012 +0200
     1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpoint.java	Tue Oct 30 21:37:56 2012 +0200
     1.3 @@ -9,8 +9,10 @@
     1.4   */
     1.5  package eu.earthobservatory.org.StrabonEndpoint.client;
     1.6  
     1.7 +import java.io.IOException;
     1.8  import java.net.URL;
     1.9  
    1.10 +import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
    1.11  import org.openrdf.rio.RDFFormat;
    1.12  
    1.13  /**
    1.14 @@ -22,7 +24,7 @@
    1.15   */
    1.16  public interface SpatialEndpoint {
    1.17  
    1.18 -	public String query(String sparqlQuery, String format);
    1.19 +	public String query(String sparqlQuery, stSPARQLQueryResultFormat format) throws IOException;
    1.20  	
    1.21  	public boolean store(String data, RDFFormat format);
    1.22  	
     2.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java	Tue Oct 30 21:18:40 2012 +0200
     2.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java	Tue Oct 30 21:37:56 2012 +0200
     2.3 @@ -9,10 +9,13 @@
     2.4   */
     2.5  package eu.earthobservatory.org.StrabonEndpoint.client;
     2.6  
     2.7 +import java.io.IOException;
     2.8  import java.net.URL;
     2.9  
    2.10  import org.apache.commons.httpclient.HttpMethod;
    2.11 +import org.apache.commons.httpclient.HttpStatus;
    2.12  import org.apache.commons.httpclient.methods.PostMethod;
    2.13 +import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
    2.14  import org.openrdf.rio.RDFFormat;
    2.15  
    2.16  /**
    2.17 @@ -28,17 +31,40 @@
    2.18  	}
    2.19  
    2.20  	@Override
    2.21 -	public String query(String sparqlQuery, String format) {
    2.22 +	public String query(String sparqlQuery, stSPARQLQueryResultFormat format) throws IOException {
    2.23  		// create a post method to execute
    2.24 -		HttpMethod post = new PostMethod(getConnectionURL());
    2.25 +		HttpMethod method = new PostMethod(getConnectionURL());
    2.26  		
    2.27  		// set the query parameter
    2.28 -		post.getParams().setParameter("query", sparqlQuery);
    2.29 +		method.getParams().setParameter("query", sparqlQuery);
    2.30  		
    2.31  		// set the accept format
    2.32 -		post.setRequestHeader("Accept", "???");
    2.33 +		method.setRequestHeader("Accept", format.getDefaultMIMEType());
    2.34  		
    2.35 -		return null;
    2.36 +		try {
    2.37 +			// execute the method
    2.38 +			int statusCode = hc.executeMethod(method);
    2.39 +
    2.40 +			// check the status code
    2.41 +			if (statusCode != HttpStatus.SC_OK) {
    2.42 +				System.err.println("Method failed: " + method.getStatusLine());
    2.43 +			}
    2.44 +
    2.45 +			// Read the response body.
    2.46 +			byte[] responseBody = method.getResponseBody();
    2.47 +
    2.48 +			// Deal with the response.
    2.49 +			// Use caution: ensure correct character encoding and is not binary
    2.50 +			// data
    2.51 +			return new String(responseBody);
    2.52 +
    2.53 +		} catch (IOException e) {
    2.54 +			throw e;
    2.55 +			
    2.56 +		} finally {
    2.57 +			// release the connection.
    2.58 +			method.releaseConnection();
    2.59 +		}
    2.60  	}
    2.61  
    2.62  	@Override