<commit_before>/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.controller.ajax;

import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hp.hpl.jena.query.DataSource;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;

import edu.cornell.mannlib.vedit.beans.LoginStatusBean;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;

/**
 * Handle an AJAX request for a SPARQL query. On entry, the "query" parameter
 * contains the query string.
 * 
 * The result is delivered in JSON format.
 */
public class SparqlQueryAjaxController extends VitroAjaxController {
	private static final Log log = LogFactory
			.getLog(SparqlQueryAjaxController.class);

	private static final String PARAMETER_QUERY = "query";
	private static final String RESPONSE_MIME_TYPE = "application/javascript";

	/**
	 * If you are logged in, you can use this servlet.
	 */
	@Override
	protected boolean testIsAuthorized(HttpServletRequest request) {
		return LoginStatusBean.getBean(request).isLoggedIn();
	}

	@Override
	protected void doRequest(VitroRequest vreq, HttpServletResponse response)
			throws ServletException, IOException {

		Model model = vreq.getJenaOntModel();
		if (model == null) {
			log.error("JenaOntModel not found.");
			response.sendError(SC_INTERNAL_SERVER_ERROR,
					"JenaOntModel not found");
			return;
		}

		String queryParam = vreq.getParameter(PARAMETER_QUERY);
		log.debug("queryParam was : " + queryParam);
		if ((queryParam == null) || queryParam.isEmpty()) {
			response.sendError(SC_NOT_FOUND, "'" + PARAMETER_QUERY
					+ "' parameter is required");
		}

		Query query = QueryFactory.create(queryParam, Syntax.syntaxARQ);
		if (!query.isSelectType()) {
			log.debug("Not a 'select' query.");
			response.sendError(SC_NOT_FOUND,
					"Only 'select' queries are allowed.");
		}

		Dataset dataset = chooseDatasetToQueryAgainst(vreq);

		executeQuery(response, query, dataset);
		return;
	}

	private Dataset chooseDatasetToQueryAgainst(VitroRequest vreq) {
		Dataset dataset = vreq.getDataset();
		if (dataset != null) {
			return dataset;
		}

		DataSource dataSource = DatasetFactory.create();
		dataSource.setDefaultModel(vreq.getJenaOntModel());
		return dataSource;<commit_msg>NIHVIVO-1363 adapt the controller to use the jenaOntModel from the request.<commit_after>/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.controller.ajax;

import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hp.hpl.jena.query.DataSource;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;

import edu.cornell.mannlib.vedit.beans.LoginStatusBean;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;

/**
 * Handle an AJAX request for a SPARQL query. On entry, the "query" parameter
 * contains the query string.
 * 
 * The result is delivered in JSON format.
 */
public class SparqlQueryAjaxController extends VitroAjaxController {
	private static final Log log = LogFactory
			.getLog(SparqlQueryAjaxController.class);

	private static final String PARAMETER_QUERY = "query";
	private static final String RESPONSE_MIME_TYPE = "application/javascript";

	/**
	 * If you are logged in, you can use this servlet.
	 */
	@Override
	protected boolean testIsAuthorized(HttpServletRequest request) {
		return LoginStatusBean.getBean(request).isLoggedIn();
	}

	@Override
	protected void doRequest(VitroRequest vreq, HttpServletResponse response)
			throws ServletException, IOException {

		Model model = vreq.getJenaOntModel();
		if (model == null) {
			log.error("JenaOntModel not found.");
			response.sendError(SC_INTERNAL_SERVER_ERROR,
					"JenaOntModel not found");
			return;
		}

		String queryParam = vreq.getParameter(PARAMETER_QUERY);
		log.debug("queryParam was : " + queryParam);
		if ((queryParam == null) || queryParam.isEmpty()) {
			response.sendError(SC_NOT_FOUND, "'" + PARAMETER_QUERY
					+ "' parameter is required");
		}

		Query query = QueryFactory.create(queryParam, Syntax.syntaxARQ);
		if (!query.isSelectType()) {
			log.debug("Not a 'select' query.");
			response.sendError(SC_NOT_FOUND,
					"Only 'select' queries are allowed.");
		}

		executeQuery(response, query, DatasetFactory.create(model));
		return;
{'fraction_non_alphanumeric': 0.10902837489251935, 'fraction_numerical': 0.0006878761822871883, 'mean_word_length': 4.965128205128205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2281847', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1862, 'n_words': 400}