annotate src/main/java/de/mpiwg/gazetteer/rest/DownloadTabDelimited4File.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 a025dd907626
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
58
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
1 package de.mpiwg.gazetteer.rest;
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
2
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
3 import java.io.PrintWriter;
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
4
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
5 import javax.servlet.http.HttpServletRequest;
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
6 import javax.servlet.http.HttpServletResponse;
96
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
7 import javax.servlet.ServletOutputStream;
58
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
8
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
9 import org.apache.commons.lang.StringUtils;
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
10 import org.json.JSONObject;
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
11
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
12 import de.mpiwg.gazetteer.bo.LGFile;
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
13 import de.mpiwg.gazetteer.utils.DataProvider;
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
14 import de.mpiwg.gazetteer.utils.FileManager;
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
15 import de.mpiwg.gazetteer.utils.HTTPUtils;
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
16
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
17 public class DownloadTabDelimited4File extends AbstractServletMethod {
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
18 public static String name = "downloadTabDelimited4File";
96
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
19
58
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
20 public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{
96
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
21
58
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
22 Long fileId = getQueryLongParam(request, "fileId");
96
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
23
58
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
24 if(fileId != null){
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
25 LGFile file = DataProvider.getInstance().getFile(fileId);
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
26 if(file != null){
96
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
27
58
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
28 String text = HTTPUtils.getTabDelimitedOfFile(fileId); // get text as tab-delimited file
96
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
29
58
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
30 if(StringUtils.isNotEmpty(text)){
96
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
31
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
32 String filename = file.getFileName(); // "112_360452_360453_2015.09.04_12.43.08.924_11.txt";
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
33
58
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
34 //filename extension with csv
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
35 filename = filename.substring(0, filename.length()-3);
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
36 filename += "csv";
96
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
37
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
38 response.setContentType("text/csv; charset=UTF-8");
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
39 response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
40
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
41 byte[] csv = text.getBytes("UTF-8");
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
42
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
43 response.setContentLength( csv.length + 3 );
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
44
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
45 ServletOutputStream aOutputStream = response.getOutputStream();
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
46
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
47 /* Specify the BOM (Byte order Mask) for UTF-8
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
48 * so that Excel can identify the encoding and open it in the correct format
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
49 * */
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
50 aOutputStream.write(new byte[] {(byte)0xef, (byte)0xbb, (byte)0xbf});
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
51
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
52 aOutputStream.write(csv);
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
53
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
54 aOutputStream.flush();
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
55
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
56 aOutputStream.close();
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
57
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
58 }
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
59
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
60
58
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
61 } else{
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
62 response.setContentType("application/json");
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
63 JSONObject json = new JSONObject();
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
64 json.put("status", "error");
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
65 json.put("message", "File no found (" + fileId + ")");
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
66 PrintWriter out = response.getWriter();
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
67 out.print(json.toString());
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
68 out.flush();
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
69 }
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
70 }else{
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
71 response.setContentType("application/json");
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
72 JSONObject json = new JSONObject();
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
73 json.put("status", "error");
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
74 json.put("message", "Following parameters are mandatory: fileId.");
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
75 PrintWriter out = response.getWriter();
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
76 out.print(json.toString());
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
77 out.flush();
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
78 }
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
79 }
96
a025dd907626 Improvement : add extra 3 bytes, make excel can identify the encoding and open csv file in the correct format
Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
parents: 58
diff changeset
80
58
b8ad346e39a0 new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
81 }