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

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

Cody tidy-up

  • Property svn:executable set to *
File size: 8.0 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 }
173 catch (Exception e) {
174 e.printStackTrace();
175 }
176
177 return bis;
178
179 }
180 /**
181 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
182 */
183 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
184
185
186 String cgi_ids = request.getParameter("ids");
187 String cgi_id = request.getParameter("id");
188 String cgi_download_id = request.getParameter("download-id");
189
190 if (cgi_ids != null) {
191 response.setContentType("application/json");
192 PrintWriter pw = response.getWriter();
193
194 String[] ids = cgi_ids.split(",");
195 int ids_len = ids.length;
196
197 pw.append("{");
198
199 for (int i=0; i<ids_len; i++) {
200 String id = ids[i];
201
202 boolean exists = id_check_.get(id);
203
204 if (i>0) {
205 pw.append(",");
206 }
207 pw.append("\"" + id + "\":" + exists );
208 }
209 pw.append("}");
210
211 }
212 else if (cgi_id != null) {
213 response.setContentType("application/json");
214 PrintWriter pw = response.getWriter();
215
216 String id = cgi_id;
217 boolean exists = id_check_.get(id);
218 pw.append("{'" + id + "':" + exists + "}");
219 }
220 else if (cgi_download_id != null) {
221 String download_id = cgi_download_id;
222 boolean exists = id_check_.get(download_id);
223 if (!exists) {
224 // Error
225 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "The requested volume id does not exist.");
226 }
227 else {
228 // rsync -av data.analytics.hathitrust.org::features/{PATH-TO-FILE} .
229 String full_json_filename = id_to_pairtree_filename(download_id);
230
231 BufferedInputStream bis = doRsyncDownload(full_json_filename);
232
233 if (bis == null) {
234 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Rsync failed");
235 }
236 else {
237 String json_filename_tail = full_filename_to_tail(full_json_filename);
238
239 response.setContentType("application/x-bzip2");
240 response.setHeader("Content-Disposition", "attachment; filename=\"" + json_filename_tail + "\"");
241
242 OutputStream os=response.getOutputStream();
243 BufferedOutputStream bos = new BufferedOutputStream(os);
244
245 byte[] buf = new byte[1024];
246
247 while (true) {
248 int num_bytes = bis.read(buf);
249 if (num_bytes == -1) {
250 break;
251 }
252 bos.write(buf,0,num_bytes);
253 //total_num_bytes += num_bytes;
254 }
255
256 bis.close();
257 bos.close();
258 }
259 }
260 }
261 else {
262 PrintWriter pw = response.getWriter();
263
264 pw.append("General Info: Number of HTRC Volumes in check-list = " + id_check_.size());
265
266 }
267 //pw.close();
268
269 }
270
271 /**
272 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
273 */
274 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
275 doGet(request, response);
276 }
277
278}
Note: See TracBrowser for help on using the repository browser.