<commit_before>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright (C) 2012, 2013 Pyravlos Team
 *
 * http://www.strabon.di.uoa.gr/
 */
package eu.earthobservatory.org.StrabonEndpoint.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.xerces.impl.dv.util.Base64;
import org.openrdf.query.resultio.TupleQueryResultFormat;
import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
import org.openrdf.rio.RDFFormat;

/**
 * This class is the implementation of a java client for accessing
 * SPARQLEndpoint instances.
 * 
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>.
 */
public class SPARQLEndpoint extends HTTPClient{

	public SPARQLEndpoint(String host, int port) {
		super(host, port);
	}
	
	public SPARQLEndpoint(String host, int port, String endpointName) {
		super(host, port, endpointName);
	}

	
	/**
	 * Executes a SPARQL query on the Endpoint and get the results
	 * in the format specified by stSPARQLQueryResultFormat, which is
	 * an instance of class (or a subclass) {@link TupleQueryResultFormat}.   
	 * 
	 * @param sparqlQuery
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public EndpointResult query(String sparqlQuery, stSPARQLQueryResultFormat format) throws IOException {
		assert(format != null);
		
		// create a post method to execute
		HttpPost method = new HttpPost(getConnectionURL());
		
		// set the query parameter
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("query", sparqlQuery));
		UrlEncodedFormEntity encodedEntity = new UrlEncodedFormEntity(params, Charset.defaultCharset());
		method.setEntity(encodedEntity);
		
		// set the content type
		method.setHeader("Content-Type", "application/x-www-form-urlencoded");
		
		// set the accept format
		method.addHeader("Accept", format.getDefaultMIMEType());
		
		try {
			// response that will be filled next
			String responseBody = "";
			
			// execute the method
			HttpResponse response = hc.execute(method);
			int statusCode = response.getStatusLine().getStatusCode();
			
			// If the response does not enclose an entity, there is no need
			// to worry about connection release
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();
				try {

					BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
					StringBuffer strBuf = new StringBuffer();
					
					// do something useful with the response
					String nextLine;
					while ((nextLine = reader.readLine()) != null) {
						strBuf.append(nextLine + "\n");
					}
					
					// remove last newline character
					if (strBuf.length() > 0) {
						strBuf.setLength(strBuf.length() - 1);
					}
					
					responseBody = strBuf.toString();

				} catch (IOException ex) {
					// In case of an IOException the connection will be released
					// back to the connection manager automatically
					throw ex;

				} catch (RuntimeException ex) {
					// In case of an unexpected exception you may want to abort
					// the HTTP request in order to shut down the underlying
					// connection and release it back to the connection manager.
					method.abort();
					throw ex;

				} finally {
					// Closing the input stream will trigger connection release
					instream.close();
				}
			}
			 
			return new EndpointResult(statusCode, response.getStatusLine().getReasonPhrase(), responseBody);

		} catch (IOException e) {
			throw e;
			
		} finally {
			// release the connection.
			method.releaseConnection();
		}
	}

	/**
	 * Stores the RDF <code>data</code> which are in the RDF format
	 * <code>format</code> in the named graph specified by the URL
	 * <code>namedGraph</code>.
	 * 
	 * @param data 
	 * @param format
	 * @param namedGraph
	 * @return <code>true</code> if store was successful, <code>false</code> otherwise
	 */
	
	public boolean store(String data, RDFFormat format, URL namedGraph) {
		throw new UnsupportedOperationException();
	}

	/**
	 * Stores the RDF data located at <code>data</code> which are in the
	 * RDF format <code>format</code> in the named graph specified by the
	 * URL <code>namedGraph</code>.
	 * 
	 * @param data
	 * @param format
	 * @param namedGraph
	 * @return <code>true</code> if store was successful, <code>false</code> otherwise
	 */

	public boolean store(URL data, RDFFormat format, URL namedGraph) throws IOException{
		
		assert(format != null);
		
		// create a post method to execute<commit_msg>added an important comment for sparql endpoint client<commit_after>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright (C) 2012, 2013 Pyravlos Team
 *
 * http://www.strabon.di.uoa.gr/
 */
package eu.earthobservatory.org.StrabonEndpoint.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.xerces.impl.dv.util.Base64;
import org.openrdf.query.resultio.TupleQueryResultFormat;
import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
import org.openrdf.rio.RDFFormat;

/**
 * This class is the implementation of a java client for accessing
 * SPARQLEndpoint instances.
 * 
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>.
 */
public class SPARQLEndpoint extends HTTPClient{

	public SPARQLEndpoint(String host, int port) {
		super(host, port);
	}
	
	public SPARQLEndpoint(String host, int port, String endpointName) {
		super(host, port, endpointName);
	}

	
	/**
	 * Executes a SPARQL query on the Endpoint and get the results
	 * in the format specified by stSPARQLQueryResultFormat, which is
	 * an instance of class (or a subclass) {@link TupleQueryResultFormat}.   
	 * 
	 * @param sparqlQuery
	 * @param format
	 * @return
	 * @throws IOException
	 */
	public EndpointResult query(String sparqlQuery, stSPARQLQueryResultFormat format) throws IOException {
		assert(format != null);
		
		// create a post method to execute
		HttpPost method = new HttpPost(getConnectionURL());
		
		// set the query parameter
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("query", sparqlQuery));
		UrlEncodedFormEntity encodedEntity = new UrlEncodedFormEntity(params, Charset.defaultCharset());
		method.setEntity(encodedEntity);
		
		// set the content type
		method.setHeader("Content-Type", "application/x-www-form-urlencoded");
		
		// set the accept format
		method.addHeader("Accept", format.getDefaultMIMEType());
		
		try {
			// response that will be filled next
			String responseBody = "";
			
			// execute the method
			HttpResponse response = hc.execute(method);
			int statusCode = response.getStatusLine().getStatusCode();
			
			// If the response does not enclose an entity, there is no need
			// to worry about connection release
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();
				try {

					BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
					StringBuffer strBuf = new StringBuffer();
					
					// do something useful with the response
					String nextLine;
					while ((nextLine = reader.readLine()) != null) {
						strBuf.append(nextLine + "\n");
					}
					
					// remove last newline character
					if (strBuf.length() > 0) {
						strBuf.setLength(strBuf.length() - 1);
					}
					
					responseBody = strBuf.toString();

				} catch (IOException ex) {
					// In case of an IOException the connection will be released
					// back to the connection manager automatically
					throw ex;

				} catch (RuntimeException ex) {
					// In case of an unexpected exception you may want to abort
					// the HTTP request in order to shut down the underlying
					// connection and release it back to the connection manager.
					method.abort();
					throw ex;

				} finally {
					// Closing the input stream will trigger connection release
					instream.close();
				}
			}
			 
			return new EndpointResult(statusCode, response.getStatusLine().getReasonPhrase(), responseBody);

		} catch (IOException e) {
			throw e;
			
		} finally {
			// release the connection.
			method.releaseConnection();
		}
	}

	/**
	 * Stores the RDF <code>data</code> which are in the RDF format
	 * <code>format</code> in the named graph specified by the URL
	 * <code>namedGraph</code>.
	 * 
	 * @param data 
	 * @param format
	 * @param namedGraph
	 * @return <code>true</code> if store was successful, <code>false</code> otherwise
	 */
	
	public boolean store(String data, RDFFormat format, URL namedGraph) {
		throw new UnsupportedOperationException();
	}

	/**
	 * Stores the RDF data located at <code>data</code> which are in the
	 * RDF format <code>format</code> in the named graph specified by the
	 * URL <code>namedGraph</code>.
	 * 
	 * @param data
	 * @param format
	 * @param namedGraph
	 * @return <code>true</code> if store was successful, <code>false</code> otherwise
	 */


	/*
	 * Comment: in order to make queries to strabon endpoint we must create a SPARQLEndpoint
	 * with endpointName ".../Query". To store data to strabon endpoint the endpointName of
	 * SPARQLEndpoint must be something like ".../Store". This means that the same object of
	 * SPARQLEndpoint cannot execute store and query operations for strabon endpoints.
	 * This is wrong and must be fixed. Also, the main idea is that the SPARQLEndpoint client 
	 * should be a general endpoint client that supports strabon, virtuoso and parliament endpoints.
	 */
	public boolean store(URL data, RDFFormat format, URL namedGraph) throws IOException{
		
		assert(format != null);
		
		// create a post method to execute
{'fraction_non_alphanumeric': 0.10202759003795944, 'fraction_numerical': 0.0029626886399407464, 'mean_word_length': 3.5272422464375524, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7492771', 'n_tokens_mistral': 3620, 'n_tokens_neox': 3062, 'n_words': 1017}