Strabon

changeset 1163:2be7757ab6b7

SPARQL Endpoint Client supports now the store operation from url. Virtuoso and parliament endpoints are not yet tested.
author Kallirroi Dogani <kallirroi@di.uoa.gr>
date Fri May 10 14:31:05 2013 +0300 (2013-05-10)
parents 355ab4e7cf59
children 42caaa72ddf8
files endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SPARQLEndpoint.java
line diff
     1.1 --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SPARQLEndpoint.java	Thu May 09 18:42:32 2013 +0300
     1.2 +++ b/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SPARQLEndpoint.java	Fri May 10 14:31:05 2013 +0300
     1.3 @@ -3,7 +3,7 @@
     1.4   * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.5   * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.6   *
     1.7 - * Copyright (C) 2012, Pyravlos Team
     1.8 + * Copyright (C) 2012, 2013 Pyravlos Team
     1.9   *
    1.10   * http://www.strabon.di.uoa.gr/
    1.11   */
    1.12 @@ -24,6 +24,7 @@
    1.13  import org.apache.http.client.entity.UrlEncodedFormEntity;
    1.14  import org.apache.http.client.methods.HttpPost;
    1.15  import org.apache.http.message.BasicNameValuePair;
    1.16 +import org.apache.xerces.impl.dv.util.Base64;
    1.17  import org.openrdf.query.resultio.TupleQueryResultFormat;
    1.18  import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
    1.19  import org.openrdf.rio.RDFFormat;
    1.20 @@ -160,9 +161,60 @@
    1.21  	 * @return <code>true</code> if store was successful, <code>false</code> otherwise
    1.22  	 */
    1.23  
    1.24 -	public boolean store(URL data, RDFFormat format, URL namedGraph) {
    1.25 -		throw new UnsupportedOperationException();
    1.26 +	public boolean store(URL data, RDFFormat format, URL namedGraph) throws IOException{
    1.27 +		
    1.28 +		assert(format != null);
    1.29 +		
    1.30 +		// create a post method to execute
    1.31 +		HttpPost method = new HttpPost(getConnectionURL());
    1.32 +		
    1.33 +		// set the url and fromurl parameters
    1.34 +		List<NameValuePair> params = new ArrayList<NameValuePair>();
    1.35 +		params.add(new BasicNameValuePair("url", data.toString()));
    1.36 +		params.add(new BasicNameValuePair("fromurl", ""));
    1.37 +		UrlEncodedFormEntity encodedEntity = new UrlEncodedFormEntity(params, Charset.defaultCharset());
    1.38 +		method.setEntity(encodedEntity);
    1.39 +		
    1.40 +		// set the content type
    1.41 +		method.setHeader("Content-Type", "application/x-www-form-urlencoded");
    1.42 +		
    1.43 +		// set the accept format
    1.44 +		method.addHeader("Accept", format.getDefaultMIMEType());
    1.45 +		
    1.46 +		//set username and password
    1.47 +		if (getUser()!=null && getPassword()!=null){
    1.48 +			
    1.49 +			String userPass = getUser()+":"+ getPassword();
    1.50 +			String encoding = Base64.encode(userPass.getBytes());
    1.51 +			method.setHeader("Authorization", "Basic "+ encoding);
    1.52 +		}
    1.53 +		
    1.54 +		try {
    1.55 +			// response that will be filled next
    1.56 +		//	String responseBody = "";
    1.57 +			
    1.58 +			// execute the method
    1.59 +			HttpResponse response = hc.execute(method);
    1.60 +			int statusCode = response.getStatusLine().getStatusCode();
    1.61 +			
    1.62 +			if (statusCode==200)
    1.63 +				return true;
    1.64 +			else{
    1.65 +				System.err.println("Status code " + statusCode);
    1.66 +				return false;
    1.67 +			}
    1.68 +				
    1.69 +			
    1.70 +
    1.71 +		} catch (IOException e) {
    1.72 +			throw e;
    1.73 +			
    1.74 +		} finally {
    1.75 +			// release the connection.
    1.76 +			method.releaseConnection();
    1.77 +		}
    1.78  	}
    1.79 +		
    1.80  
    1.81  	/**
    1.82  	 * Executes the SPARQL Update query specified in <code>sparqlUpdate</code>.