view src/main/java/de/mpiwg/gazetteer/rest/DownloadFileText.java @ 102:6a508b605b5f

1. add new page : footer.jsp. 2. embed footer.jsp into other pages.
author Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
date Fri, 29 Sep 2017 16:03:06 +0200
parents b8ad346e39a0
children
line wrap: on
line source

package de.mpiwg.gazetteer.rest;

import java.io.FileInputStream;
import java.io.PrintWriter;

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

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

import de.mpiwg.gazetteer.bo.LGBranch;
import de.mpiwg.gazetteer.bo.LGFile;
import de.mpiwg.gazetteer.utils.DataProvider;
import de.mpiwg.gazetteer.utils.FileManager;
import de.mpiwg.gazetteer.utils.PropertiesUtils;
import de.mpiwg.gazetteer.utils.exceptions.GazetteerException;

public class DownloadFileText extends AbstractServletMethod {

	public static String name = "downloadFileText";
	
	public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Long fileId = getQueryLongParam(request, "fileId");
		
		System.out.println("%%%%% DownloadFileText [fileId=" + fileId + "]");
		
	
		if(fileId != null){
	
			response.setContentType("text/html; charset=UTF-8");  
			PrintWriter out = response.getWriter();  
						
			LGFile file = DataProvider.getInstance().getFile(fileId);
			if(file != null){
				String filename = file.getFileName(); // "112_360452_360453_2015.09.04_12.43.08.924_11.txt";   
			
				//filename extension with xml
				filename = filename.substring(0, filename.length()-3);
				filename += "xml";
				
				response.setContentType("text/xml; charset=UTF-8"); 
				response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");   
			
				String text = FileManager.getFileAsText(file);
				out.print(text);
				out.flush();
				
			}else{
				
				response.setContentType("application/json");
				JSONObject json = new JSONObject();
				json.put("status", "error");
				json.put("message", "File no found (" + fileId + ")");
				json.put("code", GazetteerException.CODE);
				out = response.getWriter();
				out.print(json.toString());
				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();
		}
	}
	
}