Strabon

changeset 1178:7557f1f6f2f5

SPARQLEndpoint Client supports now the update operation (only for strabon endpoint)
author Kallirroi Dogani <kallirroi@di.uoa.gr>
date Sat May 18 21:43:06 2013 +0300 (2013-05-18)
parents b21554259954
children 6f55c6bd01ce
files endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SPARQLEndpoint.java endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointStoreWithStrabon.java endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointUpdateWithStrabon.java
line diff
     1.1 --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SPARQLEndpoint.java	Sat May 18 21:11:58 2013 +0300
     1.2 +++ b/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SPARQLEndpoint.java	Sat May 18 21:43:06 2013 +0300
     1.3 @@ -139,6 +139,14 @@
     1.4  		}
     1.5  	}
     1.6  
     1.7 +	
     1.8 +	/* Functions 'store' and 'update' do not follow the SPARQL Protocol.
     1.9 +	 * This means that only Strabon Endpoint supports these operations.
    1.10 +	 * In future they must be modified in order to be compatible with
    1.11 +	 * Virtuso and Parliament endpoints.
    1.12 +	 */
    1.13 +	
    1.14 +	
    1.15  	/**
    1.16  	 * Stores the RDF <code>data</code> which are in the RDF format
    1.17  	 * <code>format</code> in the named graph specified by the URL
    1.18 @@ -287,10 +295,58 @@
    1.19  	 * 
    1.20  	 * @param sparqlUpdate
    1.21  	 * @return <code>true</code> if store was successful, <code>false</code> otherwise
    1.22 +	 * @throws IOException 
    1.23  	 */
    1.24  
    1.25 -	public boolean update(String sparqlUpdate) {
    1.26 -		throw new UnsupportedOperationException();
    1.27 +	public boolean update(String sparqlUpdate) throws IOException {
    1.28 +		
    1.29 +		// create a post method to execute
    1.30 +		HttpPost method = new HttpPost(getConnectionURL());
    1.31 +		
    1.32 +		// set the query parameter
    1.33 +		List<NameValuePair> params = new ArrayList<NameValuePair>();
    1.34 +		params.add(new BasicNameValuePair("query", sparqlUpdate));
    1.35 +		UrlEncodedFormEntity encodedEntity = new UrlEncodedFormEntity(params, Charset.defaultCharset());
    1.36 +		method.setEntity(encodedEntity);
    1.37 +		
    1.38 +		// set the content type
    1.39 +		method.setHeader("Content-Type", "application/x-www-form-urlencoded");
    1.40 +		
    1.41 +		// set the accept format
    1.42 +		method.addHeader("Accept", "text/xml");
    1.43 +		
    1.44 +		//set username and password
    1.45 +		if (getUser()!=null && getPassword()!=null){
    1.46 +			
    1.47 +			String userPass = getUser()+":"+ getPassword();
    1.48 +			String encoding = Base64.encode(userPass.getBytes());
    1.49 +			method.setHeader("Authorization", "Basic "+ encoding);
    1.50 +		}
    1.51 +		
    1.52 +		try {
    1.53 +			// response that will be filled next
    1.54 +			
    1.55 +			// execute the method
    1.56 +			HttpResponse response = hc.execute(method);
    1.57 +			int statusCode = response.getStatusLine().getStatusCode();
    1.58 +			
    1.59 +			if (statusCode==200)
    1.60 +				return true;
    1.61 +			else{
    1.62 +				System.err.println("Status code " + statusCode);
    1.63 +				return false;
    1.64 +			}
    1.65 +				
    1.66 +			
    1.67 +
    1.68 +		} catch (IOException e) {
    1.69 +			throw e;
    1.70 +			
    1.71 +		} finally {
    1.72 +			// release the connection.
    1.73 +			method.releaseConnection();
    1.74 +		}
    1.75 +		
    1.76  	}
    1.77  
    1.78  	public EndpointResult describe(String sparqlDescribe) {
    1.79 @@ -308,7 +364,7 @@
    1.80  	
    1.81  	public static void main(String args[]) {
    1.82  		if (args.length < 4) {
    1.83 -			System.err.println("Usage: eu.earthobservatory.org.StrabonEndpoint.client.StrabonEndpoint <HOST> <PORT> <APPNAME> [<FORMAT>]");
    1.84 +			System.err.println("Usage: eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint <HOST> <PORT> <APPNAME> [<FORMAT>]");
    1.85  			System.err.println("       where <HOST>       is the hostname of the Strabon Endpoint");
    1.86  			System.err.println("             <PORT>       is the port to connect to on the host");
    1.87  			System.err.println("             <APPNAME>    is the application name of Strabon Endpoint as deployed in the Tomcat container");
     2.1 --- a/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointStoreWithStrabon.java	Sat May 18 21:11:58 2013 +0300
     2.2 +++ b/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointStoreWithStrabon.java	Sat May 18 21:43:06 2013 +0300
     2.3 @@ -29,8 +29,6 @@
     2.4  public class TestSPARQLEndpointStoreWithStrabon {
     2.5  
     2.6  	private SPARQLEndpoint endpoint; 
     2.7 -	private String query;
     2.8 -	private Vector<stSPARQLQueryResultFormat> formats = new Vector<stSPARQLQueryResultFormat>();
     2.9  	private URL data;
    2.10  	
    2.11  	@Before
    2.12 @@ -49,12 +47,6 @@
    2.13  		endpoint.setUser("endpoint");
    2.14  		endpoint.setPassword("3ndpo1nt");
    2.15  		
    2.16 -		// initialized formats
    2.17 -		for (TupleQueryResultFormat format : stSPARQLQueryResultFormat.values()) {
    2.18 -				if (format instanceof stSPARQLQueryResultFormat) {
    2.19 -					formats.add((stSPARQLQueryResultFormat) format);
    2.20 -				}
    2.21 -		}
    2.22  				
    2.23  	}
    2.24  	
    2.25 @@ -95,14 +87,4 @@
    2.26  			assertTrue(response == true);
    2.27  		
    2.28  	}
    2.29 -	
    2.30 -	/**
    2.31 -	 * Test method for testing that method {@link eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}.
    2.32 -	 * returns an IOException when it should do so.
    2.33 -	 */
    2.34 -	@Test(expected= IOException.class)
    2.35 -	public void testIOException() throws Exception {
    2.36 -		SPARQLEndpoint ep = new SPARQLEndpoint("blabla.dgr", 80, "bla");
    2.37 -		ep.query(query, formats.get(0));
    2.38 -	}
    2.39  }
    2.40 \ No newline at end of file
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointUpdateWithStrabon.java	Sat May 18 21:43:06 2013 +0300
     3.3 @@ -0,0 +1,51 @@
     3.4 +package eu.earthobservatory.org.StrabonEndpoint.client;
     3.5 +
     3.6 +import static org.junit.Assert.assertTrue;
     3.7 +
     3.8 +import java.io.IOException;
     3.9 +import java.net.MalformedURLException;
    3.10 +import java.net.URL;
    3.11 +
    3.12 +import org.junit.Before;
    3.13 +import org.junit.Test;
    3.14 +import org.openrdf.rio.RDFFormat;
    3.15 +
    3.16 +public class TestSPARQLEndpointUpdateWithStrabon {
    3.17 +	
    3.18 +	private SPARQLEndpoint endpoint; 
    3.19 +	private String query;
    3.20 +	
    3.21 +	@Before
    3.22 +	public void init() {
    3.23 +		// initialize endpoint
    3.24 +		endpoint = new SPARQLEndpoint("luna.di.uoa.gr", 8080, "sextant-endpoint/Update");
    3.25 +		
    3.26 +		// set url data
    3.27 +		query = "insert data" +
    3.28 +				"{graph <http://example.com/update> " +
    3.29 +				"{<http://example.com/map/map> <http://example.com/map/hasLayer>  <http://example.com/map/layer>}  }";
    3.30 +		
    3.31 +		endpoint.setUser("endpoint");
    3.32 +		endpoint.setPassword("3ndpo1nt");
    3.33 +		
    3.34 +				
    3.35 +	}
    3.36 +	
    3.37 +	/**
    3.38 +	 * Test method for {@link eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint#update(java.net.URL, org.openrdf.rio.RDFFormat, java.net.URL)}.
    3.39 +	 * @throws IOException 
    3.40 +	 */
    3.41 +	@Test
    3.42 +	public void testUpdate() throws IOException {
    3.43 +		
    3.44 +			Boolean response = endpoint.update(query);
    3.45 +			
    3.46 +			if (response != true) 
    3.47 +				System.err.println("Error");
    3.48 +			
    3.49 +			
    3.50 +			assertTrue(response == true);
    3.51 +		
    3.52 +	}
    3.53 +
    3.54 +}