comparison src/main/java/de/mpiwg/gazetteer/rest/DownloadTabDelimited4File.java @ 58:b8ad346e39a0

new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Mon, 02 May 2016 12:03:30 +0200
parents
children a025dd907626
comparison
equal deleted inserted replaced
57:5cbe567a9c52 58:b8ad346e39a0
1 package de.mpiwg.gazetteer.rest;
2
3 import java.io.PrintWriter;
4
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7
8 import org.apache.commons.lang.StringUtils;
9 import org.json.JSONObject;
10
11 import de.mpiwg.gazetteer.bo.LGFile;
12 import de.mpiwg.gazetteer.utils.DataProvider;
13 import de.mpiwg.gazetteer.utils.FileManager;
14 import de.mpiwg.gazetteer.utils.HTTPUtils;
15
16 public class DownloadTabDelimited4File extends AbstractServletMethod {
17 public static String name = "downloadTabDelimited4File";
18
19 public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{
20
21 Long fileId = getQueryLongParam(request, "fileId");
22
23 if(fileId != null){
24 LGFile file = DataProvider.getInstance().getFile(fileId);
25 if(file != null){
26
27 String text = HTTPUtils.getTabDelimitedOfFile(fileId); // get text as tab-delimited file
28
29 if(StringUtils.isNotEmpty(text)){
30
31 String filename = file.getFileName(); // "112_360452_360453_2015.09.04_12.43.08.924_11.txt";
32
33 //filename extension with csv
34 filename = filename.substring(0, filename.length()-3);
35 filename += "csv";
36
37 response.setContentType("text/csv; charset=UTF-8");
38 response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
39
40
41 PrintWriter out = response.getWriter();
42 out.print(text);
43 out.flush();
44
45 }
46
47
48 } else{
49 response.setContentType("application/json");
50 JSONObject json = new JSONObject();
51 json.put("status", "error");
52 json.put("message", "File no found (" + fileId + ")");
53 PrintWriter out = response.getWriter();
54 out.print(json.toString());
55 out.flush();
56 }
57 }else{
58 response.setContentType("application/json");
59 JSONObject json = new JSONObject();
60 json.put("status", "error");
61 json.put("message", "Following parameters are mandatory: fileId.");
62 PrintWriter out = response.getWriter();
63 out.print(json.toString());
64 out.flush();
65 }
66 }
67
68 }