Mercurial > hg > LGDataverses
comparison src/main/java/de/mpiwg/gazetteer/servlet/GetDatafile.java @ 12:c2e2d794847f
new: add config.properties file for gazetteer
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 30 Sep 2015 13:43:54 +0200 |
parents | |
children | be7787c36e58 |
comparison
equal
deleted
inserted
replaced
11:08c950a22cee | 12:c2e2d794847f |
---|---|
1 package de.mpiwg.gazetteer.servlet; | |
2 | |
3 import static edu.harvard.iq.dataverse.util.JsfHelper.JH; | |
4 | |
5 import java.io.BufferedReader; | |
6 import java.io.File; | |
7 import java.io.FileInputStream; | |
8 import java.io.FileReader; | |
9 import java.io.IOException; | |
10 import java.io.InputStreamReader; | |
11 import java.nio.charset.Charset; | |
12 import java.nio.file.Files; | |
13 import java.nio.file.Path; | |
14 import java.nio.file.Paths; | |
15 import java.util.LinkedList; | |
16 import java.util.List; | |
17 import java.util.Scanner; | |
18 import java.util.logging.Level; | |
19 import java.util.logging.Logger; | |
20 | |
21 import javax.ejb.EJB; | |
22 import javax.servlet.ServletException; | |
23 import javax.servlet.http.HttpServletRequest; | |
24 import javax.servlet.http.HttpServletResponse; | |
25 import javax.ws.rs.WebApplicationException; | |
26 import javax.ws.rs.core.Response; | |
27 | |
28 import org.apache.commons.lang.StringUtils; | |
29 import org.primefaces.json.JSONArray; | |
30 import org.primefaces.json.JSONException; | |
31 import org.primefaces.json.JSONObject; | |
32 | |
33 import edu.harvard.iq.dataverse.DataFile; | |
34 import edu.harvard.iq.dataverse.DataFileServiceBean; | |
35 import edu.harvard.iq.dataverse.FileMetadata; | |
36 import edu.harvard.iq.dataverse.api.DownloadInfo; | |
37 import edu.harvard.iq.dataverse.api.DownloadInstance; | |
38 import edu.harvard.iq.dataverse.authorization.AuthenticationRequest; | |
39 import edu.harvard.iq.dataverse.authorization.CredentialsAuthenticationProvider; | |
40 import edu.harvard.iq.dataverse.authorization.exceptions.AuthenticationFailedException; | |
41 import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser; | |
42 import edu.harvard.iq.dataverse.export.DDIExportServiceBean; | |
43 | |
44 | |
45 | |
46 public class GetDatafile extends AbstractMonographServlet{ | |
47 | |
48 | |
49 private static final Logger logger = Logger.getLogger(GetDatafile.class.getName()); | |
50 | |
51 @EJB | |
52 DataFileServiceBean dataFileService; | |
53 | |
54 | |
55 | |
56 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
57 // Set response content type | |
58 response.setContentType("application/json"); | |
59 response.setCharacterEncoding("UTF-8"); | |
60 | |
61 JSONObject jsonResponse = new JSONObject(); | |
62 | |
63 Long fileId = Long.parseLong(request.getParameter("fileId")); | |
64 | |
65 if(fileId != null){ | |
66 // get datafile by fileId | |
67 System.out.println("getting file... fileId=" + fileId); | |
68 | |
69 DataFile df = dataFileService.find(fileId); | |
70 try { | |
71 if (df == null) { | |
72 logger.warning("Access: datafile service could not locate a DataFile object for id "+fileId+"!"); | |
73 jsonResponse.put("state", "error"); | |
74 | |
75 } else { | |
76 | |
77 JSONObject jsonFile = new JSONObject(); | |
78 | |
79 jsonFile.put("fileLabel", df.getFileMetadata().getLabel()); | |
80 jsonFile.put("fileName", df.getFileSystemName()); | |
81 jsonFile.put("fileSize", df.getFilesize()); | |
82 | |
83 // get file data from file system | |
84 //Path filePath = df.getSavedOriginalFile(); // only works on tab-data | |
85 | |
86 Path filePath = null; | |
87 if (df.getFileSystemName() != null && df.getFileSystemName().startsWith("/")) { | |
88 filePath = Paths.get(df.getFileSystemName()); | |
89 } else { | |
90 Path studyDirectoryPath = df.getOwner().getFileSystemDirectory(); | |
91 if (studyDirectoryPath == null) { | |
92 jsonResponse.put("state", "error"); | |
93 //return; | |
94 } | |
95 String studyDirectory = studyDirectoryPath.toString(); | |
96 | |
97 filePath = Paths.get(studyDirectory, df.getFileSystemName()); | |
98 } | |
99 | |
100 if (filePath != null) { | |
101 String dataString = ""; | |
102 File fileDir = new File(filePath.toString()); | |
103 BufferedReader in = new BufferedReader( new InputStreamReader( | |
104 new FileInputStream(fileDir), "UTF8")); | |
105 String str; | |
106 while ((str = in.readLine()) != null) { | |
107 // TODO file may not be from LGServices which are not in the right format we can parse here | |
108 | |
109 dataString += str.replace("\t", ","); | |
110 dataString += "\n"; | |
111 } | |
112 in.close(); | |
113 | |
114 jsonFile.put("dataString", dataString); | |
115 | |
116 jsonResponse.put("file", jsonFile); | |
117 | |
118 jsonResponse.put("state", "ok"); | |
119 } | |
120 | |
121 } | |
122 } catch (JSONException e) { | |
123 | |
124 e.printStackTrace(); | |
125 } | |
126 } | |
127 | |
128 | |
129 // ====================== | |
130 | |
131 response.setHeader("Access-Control-Allow-Origin", "*"); | |
132 | |
133 java.io.PrintWriter out = response.getWriter(); | |
134 out.print(jsonResponse); | |
135 out.flush(); | |
136 | |
137 | |
138 | |
139 } | |
140 } |