Strabon
changeset 1166:8f35157e8fc2
SPARQL Endpoint client: added store operation that stores data expressed as string
author | Kallirroi Dogani <kallirroi@di.uoa.gr> |
---|---|
date | Fri May 10 17:46:27 2013 +0300 (2013-05-10) |
parents | 8f33dfbb8c50 |
children | db7c1e492a84 |
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 |
line diff
1.1 --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SPARQLEndpoint.java Fri May 10 15:11:53 2013 +0300 1.2 +++ b/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SPARQLEndpoint.java Fri May 10 17:46:27 2013 +0300 1.3 @@ -144,10 +144,59 @@ 1.4 * @param format 1.5 * @param namedGraph 1.6 * @return <code>true</code> if store was successful, <code>false</code> otherwise 1.7 + * @throws IOException 1.8 */ 1.9 1.10 - public boolean store(String data, RDFFormat format, URL namedGraph) { 1.11 - throw new UnsupportedOperationException(); 1.12 + public boolean store(String data, RDFFormat format, URL namedGraph) throws IOException { 1.13 +assert(format != null); 1.14 + 1.15 + // create a post method to execute 1.16 + HttpPost method = new HttpPost(getConnectionURL()); 1.17 + 1.18 + // set the url and fromurl parameters 1.19 + List<NameValuePair> params = new ArrayList<NameValuePair>(); 1.20 + params.add(new BasicNameValuePair("data", data)); 1.21 + UrlEncodedFormEntity encodedEntity = new UrlEncodedFormEntity(params, Charset.defaultCharset()); 1.22 + method.setEntity(encodedEntity); 1.23 + 1.24 + // set the content type 1.25 + method.setHeader("Content-Type", "application/x-www-form-urlencoded"); 1.26 + 1.27 + // set the accept format 1.28 + method.addHeader("Accept", format.getDefaultMIMEType()); 1.29 + 1.30 + //set username and password 1.31 + if (getUser()!=null && getPassword()!=null){ 1.32 + 1.33 + String userPass = getUser()+":"+ getPassword(); 1.34 + String encoding = Base64.encode(userPass.getBytes()); 1.35 + method.setHeader("Authorization", "Basic "+ encoding); 1.36 + } 1.37 + 1.38 + try { 1.39 + // response that will be filled next 1.40 + // String responseBody = ""; 1.41 + 1.42 + // execute the method 1.43 + HttpResponse response = hc.execute(method); 1.44 + int statusCode = response.getStatusLine().getStatusCode(); 1.45 + 1.46 + if (statusCode==200) 1.47 + return true; 1.48 + else{ 1.49 + System.err.println("Status code " + statusCode); 1.50 + return false; 1.51 + } 1.52 + 1.53 + 1.54 + 1.55 + } catch (IOException e) { 1.56 + throw e; 1.57 + 1.58 + } finally { 1.59 + // release the connection. 1.60 + method.releaseConnection(); 1.61 + } 1.62 } 1.63 1.64 /**
2.1 --- a/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointStoreWithStrabon.java Fri May 10 15:11:53 2013 +0300 2.2 +++ b/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSPARQLEndpointStoreWithStrabon.java Fri May 10 17:46:27 2013 +0300 2.3 @@ -59,19 +59,38 @@ 2.4 } 2.5 2.6 /** 2.7 - * Test method for {@link eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint#query(java.lang.String, org.openrdf.query.resultio.stSPARQLQueryResultFormat)}. 2.8 + * Test method for {@link eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint#store(java.net.URL, org.openrdf.rio.RDFFormat, java.net.URL)}. 2.9 * @throws IOException 2.10 */ 2.11 @Test 2.12 public void testStoreFromUrl() throws IOException { 2.13 2.14 - Boolean response = endpoint.store(data, RDFFormat.NTRIPLES , null); 2.15 - 2.16 - if (response != true) 2.17 - System.err.println("Error"); 2.18 - 2.19 - 2.20 - assertTrue(response == true); 2.21 + Boolean response = endpoint.store(data, RDFFormat.NTRIPLES , null); 2.22 + 2.23 + if (response != true) 2.24 + System.err.println("Error"); 2.25 + 2.26 + 2.27 + assertTrue(response == true); 2.28 + 2.29 + } 2.30 + 2.31 + 2.32 + /** 2.33 + * Test method for {@link eu.earthobservatory.org.StrabonEndpoint.client.SPARQLEndpoint#store(java.lang.String, org.openrdf.rio.RDFFormat, java.net.URL)}. 2.34 + * @throws IOException 2.35 + */ 2.36 + @Test 2.37 + public void testStore() throws IOException { 2.38 + 2.39 + String data = "<http://geo.linkedopendata.gr/map/id/l22> <http://geo.linkedopendata.gr/map/hasName> \"layer22\" . "; 2.40 + Boolean response = endpoint.store(data, RDFFormat.NTRIPLES , null); 2.41 + 2.42 + if (response != true) 2.43 + System.err.println("Error"); 2.44 + 2.45 + 2.46 + assertTrue(response == true); 2.47 2.48 } 2.49