<commit_before>package org.waag.ah.spring.view;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

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

import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.parser.ParsedQuery;
import org.openrdf.rio.RDFFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.view.AbstractView;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.WebUtils;
import org.waag.ah.QueryService;
import org.waag.ah.QueryTask;
import org.waag.ah.WriterContentTypeConfig;
import org.waag.ah.rdf.RDFJSONFormat;
import org.waag.ah.rdf.RDFWriterConfig;
import org.waag.ah.rdf.RdfQueryDefinition;
import org.waag.ah.rest.util.MIMEParse;

import com.bigdata.rdf.sparql.ast.QueryType;

public class QueryTaskView extends AbstractView {
	final static Logger logger = LoggerFactory.getLogger(QueryTaskView.class);
	static final UrlPathHelper urlPathHelper = new UrlPathHelper();
	public static String MODEL_QUERY = "queryDefinition";
	
	private QueryService queryService;
	private ExecutorService executor;
	
	public QueryTaskView(QueryService queryService) {
		RDFFormat.register(RDFJSONFormat.RESTAPIJSON);
		
		this.queryService = queryService;
		this.executor = Executors.newCachedThreadPool();
	}

	@Override
	protected void renderMergedOutputModel(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Assert.isTrue(model.containsKey(MODEL_QUERY));
		RdfQueryDefinition query = (RdfQueryDefinition) model.get(MODEL_QUERY);
		RDFWriterConfig config = query.getWriterConfig();
		WriterContentTypeConfig typeConfig = config.getContentTypeConfig();
		
		try {			
			ParsedQuery parsedQuery = queryService.getParsedQuery(query, config);
			QueryType queryType = queryService.getQueryType(parsedQuery);
			
			String contentType = getRequestedContentType(request, typeConfig, queryType);

			if (contentType == null) {
				response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE);
				return;
			}			
			
			config.setContentType(contentType);
						
			if (WriterContentTypeConfig.isJSON(contentType) && config.getJSONPCallback() != null && config.getJSONPCallback().length() > 1) {
				config.setJSONP(true);
				contentType = WriterContentTypeConfig.MIME_APPLICATION_JAVASCRIPT;
			} else {
				// Use text/plain when target is browser:
				if (config.isOverrideResponseContentType()) {
					contentType = config.getResponseContentType();	
				} else if (config.getContentType() == RDFJSONFormat.MIMETYPE) {
					// Or when content-type is our custom "application/x-waag-artsholland-restapi+json",
					// change to default JSON content-type
					contentType = WriterContentTypeConfig.MIME_APPLICATION_JSON;
				}<commit_msg>Added workaround so the overridden content type is used<commit_after>package org.waag.ah.spring.view;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

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

import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.parser.ParsedQuery;
import org.openrdf.rio.RDFFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.view.AbstractView;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.WebUtils;
import org.waag.ah.QueryService;
import org.waag.ah.QueryTask;
import org.waag.ah.WriterContentTypeConfig;
import org.waag.ah.rdf.RDFJSONFormat;
import org.waag.ah.rdf.RDFWriterConfig;
import org.waag.ah.rdf.RdfQueryDefinition;
import org.waag.ah.rest.util.MIMEParse;

import com.bigdata.rdf.sparql.ast.QueryType;

public class QueryTaskView extends AbstractView {
	final static Logger logger = LoggerFactory.getLogger(QueryTaskView.class);
	static final UrlPathHelper urlPathHelper = new UrlPathHelper();
	public static String MODEL_QUERY = "queryDefinition";
	
	private QueryService queryService;
	private ExecutorService executor;
	
	public QueryTaskView(QueryService queryService) {
		RDFFormat.register(RDFJSONFormat.RESTAPIJSON);
		
		this.queryService = queryService;
		this.executor = Executors.newCachedThreadPool();
	}

	@Override
	protected void renderMergedOutputModel(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Assert.isTrue(model.containsKey(MODEL_QUERY));
		RdfQueryDefinition query = (RdfQueryDefinition) model.get(MODEL_QUERY);
		RDFWriterConfig config = query.getWriterConfig();
		WriterContentTypeConfig typeConfig = config.getContentTypeConfig();
		
		try {			
			ParsedQuery parsedQuery = queryService.getParsedQuery(query, config);
			QueryType queryType = queryService.getQueryType(parsedQuery);
			
			String contentType = getRequestedContentType(request, typeConfig, queryType);

			if (contentType == null) {
				response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE);
				return;
			}			
			
			config.setContentType(contentType);
						
			if (WriterContentTypeConfig.isJSON(contentType) && config.getJSONPCallback() != null && config.getJSONPCallback().length() > 1) {
				config.setJSONP(true);
				contentType = WriterContentTypeConfig.MIME_APPLICATION_JAVASCRIPT;
			} else {
				// Use text/plain when target is browser:
				if (config.isOverrideResponseContentType()) {
					// TODO: Shouldn't config.getContentType() return the overridden value?
					contentType = config.getResponseContentType();	
					config.setContentType(contentType);
				} else if (config.getContentType() == RDFJSONFormat.MIMETYPE) {
					// Or when content-type is our custom "application/x-waag-artsholland-restapi+json",
					// change to default JSON content-type
					contentType = WriterContentTypeConfig.MIME_APPLICATION_JSON;
				}
{'fraction_non_alphanumeric': 0.09498432601880878, 'fraction_numerical': 0.0009404388714733542, 'mean_word_length': 5.3937875751503, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4671716', 'n_tokens_mistral': 2105, 'n_tokens_neox': 1903, 'n_words': 354}