source: other-projects/hathitrust/wcsa/vol-checker/src/org/hathitrust/extractedfeatures/VolumeCheck.java@ 31338

Last change on this file since 31338 was 31338, checked in by davidb, 7 years ago

additional close()

  • Property svn:executable set to *
File size: 9.2 KB
Line 
1package org.hathitrust.extractedfeatures;
2
3import java.io.BufferedInputStream;
4import java.io.BufferedOutputStream;
5import java.io.BufferedReader;
6import java.io.FileInputStream;
7import java.io.FileReader;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.InputStreamReader;
11import java.io.OutputStream;
12import java.io.PrintWriter;
13import java.io.UnsupportedEncodingException;
14import java.nio.file.Files;
15import java.nio.file.Path;
16import java.nio.file.Paths;
17import java.util.ArrayList;
18import java.util.HashMap;
19
20import javax.servlet.ServletConfig;
21import javax.servlet.ServletException;
22import javax.servlet.annotation.WebServlet;
23import javax.servlet.http.HttpServlet;
24import javax.servlet.http.HttpServletRequest;
25import javax.servlet.http.HttpServletResponse;
26
27/**
28 * Servlet implementation class VolumeCheck
29 */
30@WebServlet("/VolumeCheck")
31public class VolumeCheck extends HttpServlet {
32 private static final long serialVersionUID = 1L;
33
34 protected static int HASHMAP_INIT_SIZE = 13800000;
35 protected static HashMap<String,Boolean> id_check_ = null;
36
37 protected static final String file_ext = ".json.bz2";
38
39 public VolumeCheck()
40 {}
41
42 protected static String full_filename_to_tail(String full_filename)
43 {
44 String filename_tail = full_filename.substring(full_filename.lastIndexOf("/")+1);
45 return filename_tail;
46 }
47
48 protected static String filename_tail_to_id(String filename_tail)
49 {
50 String id = null;
51 if (filename_tail.endsWith(file_ext)) {
52 id = filename_tail.substring(0,filename_tail.lastIndexOf(file_ext));
53 }
54 else {
55 id = filename_tail;
56 }
57
58 id = id.replaceAll("\\+", ":").replaceAll("=", "/");
59
60 return id;
61 }
62
63 protected static String id_to_pairtree_filename(String id) {
64 // Example :-
65 // id: miun.adx6300.0001.001
66 // pairtree filename: miun/pairtree_root/ad/x6/30/0,/00/01/,0/01/adx6300,0001,001/miun.adx6300,0001,001.json.bz2
67
68 // 1. Map 'difficult' chars:
69 // . => ,
70 // : => +
71 // / => =
72
73 // 2. Process resulting string:
74 // split on first dot
75 // add "pairtree_root"
76 // then split everything else 2 chars at a time
77
78 // 3. Finally add in the (safely transformed) id:
79 // append directory that is prefix-removed id (transformed to be safe)
80 // further append 'id-safe'.json.bz
81
82 int id_dot_pos = id.indexOf(".");
83 String id_prefix = id.substring(0,id_dot_pos);
84 String id_tail = id.substring(id_dot_pos+1);
85 String id_tail_safe = id_tail.replaceAll("\\.", ",").replaceAll(":", "+").replaceAll("/", "=");
86
87 String [] pairs = id_tail_safe.split("(?<=\\G..)");
88 String joined_pairs = String.join("/", pairs);
89
90 String id_safe = id_prefix + "." + id_tail_safe;
91 String main_dir = id_prefix + "/pairtree_root/" + joined_pairs;
92 String filename = main_dir + "/" + id_tail_safe + "/" + id_safe + file_ext;
93
94 return filename;
95 }
96
97 protected void storeIDs(BufferedReader br)
98 {
99 long line_num = 1;
100 String line;
101
102 try {
103
104 System.err.print("Loading hashmap: ");
105 while ((line = br.readLine()) != null) {
106
107 String full_json_filename = line;
108 String json_filename_tail = full_filename_to_tail(full_json_filename);
109 String id = filename_tail_to_id(json_filename_tail);
110
111 id_check_.put(id, true);
112
113 if ((line_num % 100000) == 0) {
114 //System.err.println("sample id = " + id);
115 //System.err.println("Passed line: " + line_num);
116 System.err.print(".");
117 }
118 line_num++;
119
120 }
121 System.err.println(" => done.");
122 }
123 catch (Exception e) {
124 e.printStackTrace();
125 }
126
127 }
128 /**
129 * @see Servlet#init(ServletConfig)
130 */
131 public void init(ServletConfig config) throws ServletException {
132 super.init(config);
133
134 if (id_check_ == null) {
135 id_check_ = new HashMap<String,Boolean>(HASHMAP_INIT_SIZE);
136
137 String htrc_list_file = "htrc-ef-all-files.txt";
138 InputStream is = getServletContext().getResourceAsStream("/WEB-INF/" + htrc_list_file);
139
140 try {
141 System.err.println("INFO: Loading in volume IDS: " + htrc_list_file);
142
143 InputStreamReader isr = new InputStreamReader(is, "UTF-8");
144 BufferedReader br = new BufferedReader(isr);
145
146 storeIDs(br);
147 br.close();
148 }
149 catch (Exception e) {
150 e.printStackTrace();
151 }
152 }
153 }
154
155 protected BufferedInputStream doRsyncDownload(String full_json_filename)
156 {
157 String json_filename_tail = full_filename_to_tail(full_json_filename);
158
159 BufferedInputStream bis = null;
160
161 Runtime runtime = Runtime.getRuntime();
162 String[] rsync_command = {"rsync","-av","data.analytics.hathitrust.org::features/" + full_json_filename, "."};
163
164 try {
165 Process proc = runtime.exec(rsync_command);
166 proc.waitFor();
167 System.err.println("*** Rsync finished");
168
169 FileInputStream fis = new FileInputStream(json_filename_tail);
170 bis = new BufferedInputStream(fis);
171
172 //Path json_filename_path = Paths.get(json_filename_tail);
173 //byte[] bz_bytes = Files.readAllBytes(json_filename_path);
174
175
176 //response.setContentType("application/json");
177
178 /*
179 BufferedReader stdInput = new BufferedReader(new
180 InputStreamReader(proc.getInputStream()));
181
182 BufferedReader stdError = new BufferedReader(new
183 InputStreamReader(proc.getErrorStream()));
184
185 // read the output from the command
186 System.out.println("Here is the standard output of the command:\n");
187 String s = null;
188 while ((s = stdInput.readLine()) != null) {
189 System.out.println(s);
190 }
191
192 // read any errors from the attempted command
193 System.out.println("Here is the standard error of the command (if any):\n");
194 while ((s = stdError.readLine()) != null) {
195 System.out.println(s);
196 }
197 */
198
199
200 //return bz_bytes;
201 //System.out.println("Done.");
202
203 }
204 catch (Exception e) {
205 e.printStackTrace();
206 }
207
208 return bis;
209
210 }
211 /**
212 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
213 */
214 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
215
216
217 String cgi_ids = request.getParameter("ids");
218 String cgi_id = request.getParameter("id");
219 String cgi_download_id = request.getParameter("download-id");
220
221 if (cgi_ids != null) {
222 response.setContentType("application/json");
223 PrintWriter pw = response.getWriter();
224
225 String[] ids = cgi_ids.split(",");
226 int ids_len = ids.length;
227
228 pw.append("{");
229
230 for (int i=0; i<ids_len; i++) {
231 String id = ids[i];
232
233 boolean exists = id_check_.get(id);
234
235 if (i>0) {
236 pw.append(",");
237 }
238 pw.append("\"" + id + "\":" + exists );
239 }
240 pw.append("}");
241
242 }
243 else if (cgi_id != null) {
244 response.setContentType("application/json");
245 PrintWriter pw = response.getWriter();
246
247 String id = cgi_id;
248 boolean exists = id_check_.get(id);
249 pw.append("{'" + id + "':" + exists + "}");
250 }
251 else if (cgi_download_id != null) {
252 String download_id = cgi_download_id;
253 boolean exists = id_check_.get(download_id);
254 if (!exists) {
255 // Error
256 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "The requested volume id does not exist.");
257 }
258 else {
259 // rsync -av data.analytics.hathitrust.org::features/{PATH-TO-FILE} .
260 String full_json_filename = id_to_pairtree_filename(download_id);
261
262 BufferedInputStream bis = doRsyncDownload(full_json_filename);
263
264 if (bis == null) {
265 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Rsync failed");
266 }
267 else {
268 String json_filename_tail = full_filename_to_tail(full_json_filename);
269
270 response.setContentType("application/x-bzip2");
271 response.setHeader("Content-Disposition", "attachment; filename=\"" + json_filename_tail + "\"");
272
273
274 //InputStream is=request.getInputStream();
275 OutputStream os=response.getOutputStream();
276 BufferedOutputStream bos = new BufferedOutputStream(os);
277
278 byte[] buf = new byte[1024];
279
280 //int num_bytes;
281 while (true) {
282 int num_bytes = bis.read(buf);
283 if (num_bytes == -1) {
284 break;
285 }
286 bos.write(buf,0,num_bytes);
287 }
288 /*
289 for (int nChunk = bis.read(buf); nChunk!=-1; nChunk = bis.read(buf))
290 {
291 os.write(buf, 0, nChunk);
292 }
293 */
294
295 //OutputStream os = response.getOutputStream()
296
297 //os.write(bz_bytes);
298
299 bis.close();
300 bos.close();
301 }
302 }
303 }
304 else {
305 PrintWriter pw = response.getWriter();
306
307 pw.append("General Info: Number of HTRC Volumes in check-list = " + id_check_.size());
308
309 }
310 //pw.close();
311
312 }
313
314 /**
315 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
316 */
317 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
318 doGet(request, response);
319 }
320
321}
Note: See TracBrowser for help on using the repository browser.