view src/main/java/de/mpiwg/gazetteer/rest/DownloadFullTextSearchCsvFile.java @ 105:16a0796e3871 default tip

remove "console.log" from general.js
author Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
date Fri, 29 Sep 2017 16:18:02 +0200
parents fc5116de601f
children
line wrap: on
line source

package de.mpiwg.gazetteer.rest;

import java.io.PrintWriter;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

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

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import de.mpiwg.gazetteer.utils.PropertiesUtils;

public class DownloadFullTextSearchCsvFile extends AbstractServletMethod {

	public static String name = "downloadFullTextSearchCsvFile";
	
	public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		//String filename = getQueryLongParam(request, "file");
		String filename = request.getParameter("file");
		
		/*
		byte[] bytes = filename.getBytes(StandardCharsets.ISO_8859_1);
		
		filename = new String(bytes, StandardCharsets.UTF_8);
		*/
		
		System.out.println("%%%%% DownloadFullTextSearchCsvFile [filename=" + filename + "]");
		
	
		if(StringUtils.isNotEmpty(filename)){
			
			PrintWriter out = response.getWriter();  

			response.setContentType("text/csv; charset=UTF-8"); 
			
			String downloadFilename = filename.split("_")[1];	// Strip the user's id at the beginning of the filename, eg 11_ASDF.csv
			response.setHeader("Content-Disposition","attachment; filename=\"" + URLEncoder.encode(downloadFilename, "UTF-8") + "\"");   
		
			String absolutePath = PropertiesUtils.getPropValue("lgmap_datasets") + "/" + filename;
			
			byte[] encoded = Files.readAllBytes(Paths.get(absolutePath));
			String text = new String(encoded, "UTF8");		
			
			out.print(text);
			out.flush();
			
		}else{
			response.setContentType("application/json");
			JSONObject json = new JSONObject();
			json.put("status", "error");
			json.put("message", "Following parameters are mandatory: fileId.");
			PrintWriter out = response.getWriter();
			out.print(json.toString());
			out.flush();
		}
	}
	
}