<commit_before>package uk.ac.ebi.quickgo.controller;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import uk.ac.ebi.quickgo.service.annotation.AnnotationService;
import uk.ac.ebi.quickgo.service.annotation.parameter.AnnotationParameters;
import uk.ac.ebi.quickgo.web.util.FileService;
import uk.ac.ebi.quickgo.web.util.annotation.AnnotationColumn;
import uk.ac.ebi.quickgo.web.util.annotation.AppliedFilterSet;

/**
 * Controller for the download of annotations
 * @author cbonill
 *
 */

@Controller
public class DownloadController {	
	
	@Autowired
	AnnotationService annotationService;
	
	@Autowired
	FileService fileService;
	
	private static final Logger logger = Logger.getLogger(DownloadController.class);	
	
	@RequestMapping(value = { "/", "annotation" }, method = {RequestMethod.POST, RequestMethod.GET }, params = { "format" })
	public void download(
			@RequestParam(value = "format", defaultValue = "tab") String format,
			HttpSession session, HttpServletResponse httpServletResponse) {

		// Get columns to display		
		AnnotationColumn[] visibleColumns = (AnnotationColumn[]) session.getAttribute("visibleAnnotationsColumns");
		
		// Get applied filters and convert them into Solr query
		AnnotationParameters annotationParameters = new AnnotationParameters();		
		AppliedFilterSet appliedFilterSet = (AppliedFilterSet) session.getAttribute("appliedFilters");
		annotationParameters.setParameters(appliedFilterSet.getParameters());
		String query = annotationParameters.toSolrQuery();

		// Get total number annotations
		long totalAnnotations = annotationService.getTotalNumberAnnotations(query);
		int numResults = 10000; //TODO Add parameter in request to specify limit
		
		// Check file format
		StringBuffer sb = null;
		try {				
			switch (FileService.FILE_FORMAT.valueOf(format.toUpperCase())) {
			case TSV:
				sb = fileService.generateTSVfile(FileService.FILE_FORMAT.TSV, "", query, totalAnnotations, visibleColumns, numResults);
				break;
			case FASTA:
				sb = fileService.generateFastafile(query, totalAnnotations, numResults);
				break;
			case GAF:										
				sb = fileService.generateGAFFile(query, totalAnnotations, numResults);
				break;
			case GPAD:
				sb = fileService.generateGPADFile(query, totalAnnotations, numResults);
				break;
			case PROTEINLIST:
				sb = fileService.generateProteinListFile(query, totalAnnotations, numResults);
				break;
			case GENE2GO:
				sb = fileService.generateGene2GoFile(query, totalAnnotations, 10000);
				break;
			}
			InputStream in = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
			IOUtils.copy(in, httpServletResponse.getOutputStream());
			
			// Set response header and content
			httpServletResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
			httpServletResponse.setHeader("Content-Disposition","attachment; filename=annotations." + format);
			httpServletResponse.setContentLength(sb.length());
			
			httpServletResponse.flushBuffer();		
		} catch (IOException e) {
			logger.error(e.getMessage());
		}		
	}
}<commit_msg>Add json response.<commit_after>package uk.ac.ebi.quickgo.controller;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import uk.ac.ebi.quickgo.service.annotation.AnnotationService;
import uk.ac.ebi.quickgo.service.annotation.parameter.AnnotationParameters;
import uk.ac.ebi.quickgo.web.util.FileService;
import uk.ac.ebi.quickgo.web.util.annotation.AnnotationColumn;
import uk.ac.ebi.quickgo.web.util.annotation.AppliedFilterSet;

/**
 * Controller for the download of annotations
 * @author cbonill
 *
 */

@Controller
public class DownloadController {

	@Autowired
	AnnotationService annotationService;

	@Autowired
	FileService fileService;

	private static final Logger logger = Logger.getLogger(DownloadController.class);

	@RequestMapping(value = { "/", "annotation" }, method = {RequestMethod.POST, RequestMethod.GET }, params = { "format" })
	public void download(
			@RequestParam(value = "format", defaultValue = "tab") String format,
			HttpSession session, HttpServletResponse httpServletResponse) {

		// Get columns to display
		AnnotationColumn[] visibleColumns = (AnnotationColumn[]) session.getAttribute("visibleAnnotationsColumns");

		// Get applied filters and convert them into Solr query
		AnnotationParameters annotationParameters = new AnnotationParameters();
		AppliedFilterSet appliedFilterSet = (AppliedFilterSet) session.getAttribute("appliedFilters");
		annotationParameters.setParameters(appliedFilterSet.getParameters());
		String query = annotationParameters.toSolrQuery();

		// Get total number annotations
		long totalAnnotations = annotationService.getTotalNumberAnnotations(query);
		int numResults = 10000; //TODO Add parameter in request to specify limit

		// Check file format
		StringBuffer sb = null;
		try {
			switch (FileService.FILE_FORMAT.valueOf(format.toUpperCase())) {
			case TSV:
				sb = fileService.generateTSVfile(FileService.FILE_FORMAT.TSV, "", query, totalAnnotations, visibleColumns, numResults);
				break;
			case FASTA:
				sb = fileService.generateFastafile(query, totalAnnotations, numResults);
				break;
			case GAF:
				sb = fileService.generateGAFFile(query, totalAnnotations, numResults);
				break;
			case GPAD:
				sb = fileService.generateGPADFile(query, totalAnnotations, numResults);
				break;
			case PROTEINLIST:
				sb = fileService.generateProteinListFile(query, totalAnnotations, numResults);
				break;
			case GENE2GO:
				sb = fileService.generateGene2GoFile(query, totalAnnotations, 10000);
				break;
			case JSON:
				sb = fileService.generateJsonFile(query, totalAnnotations, numResults);
				break;
			}
			InputStream in = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
			IOUtils.copy(in, httpServletResponse.getOutputStream());

			// Set response header and content
			httpServletResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
			httpServletResponse.setHeader("Content-Disposition","attachment; filename=annotations." + format);
			httpServletResponse.setContentLength(sb.length());

			httpServletResponse.flushBuffer();
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}
}

{'fraction_non_alphanumeric': 0.10108800440710645, 'fraction_numerical': 0.0038562181517697285, 'mean_word_length': 5.3202785030461275, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1222919', 'n_tokens_mistral': 2247, 'n_tokens_neox': 1992, 'n_words': 415}