Changeset 31337 for other-projects


Ignore:
Timestamp:
2017-01-23T21:24:31+13:00 (7 years ago)
Author:
davidb
Message:

Output the downloaded rsync file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • other-projects/hathitrust/wcsa/vol-checker/src/org/hathitrust/extractedfeatures/VolumeCheck.java

    r31336 r31337  
    22
    33import java.io.BufferedInputStream;
     4import java.io.BufferedOutputStream;
    45import java.io.BufferedReader;
     6import java.io.FileInputStream;
    57import java.io.FileReader;
    68import java.io.IOException;
    79import java.io.InputStream;
    810import java.io.InputStreamReader;
     11import java.io.OutputStream;
    912import java.io.PrintWriter;
    1013import java.io.UnsupportedEncodingException;
     14import java.nio.file.Files;
     15import java.nio.file.Path;
     16import java.nio.file.Paths;
    1117import java.util.ArrayList;
    1218import java.util.HashMap;
     
    2834    protected static int HASHMAP_INIT_SIZE = 13800000;
    2935    protected static HashMap<String,Boolean> id_check_ = null;
    30    
    31     public VolumeCheck() {
    32        
    33     }
    3436
    3537    protected static final String file_ext = ".json.bz2";
     38
     39    public VolumeCheck()
     40    {}
    3641   
    3742    protected static String full_filename_to_tail(String full_filename)
     
    148153    }
    149154
    150     protected void doRsyncDownload(String full_json_filename)
     155    protected BufferedInputStream doRsyncDownload(String full_json_filename)
    151156    {
    152157        String json_filename_tail = full_filename_to_tail(full_json_filename);
    153         //String cmd = "rsync -av data.analytics.hathitrust.org::features/" + full_json_filename + ".";
    154        
    155        
    156         Runtime rt = Runtime.getRuntime();
    157         String[] command = {"rsync","-av","data.analytics.hathitrust.org::features/" + full_json_filename, "."};
    158        
     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, "."};
    159163       
    160164        try {
    161             Process proc = rt.exec(command);
    162 
     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           
    163178            /*
    164179            BufferedReader stdInput = new BufferedReader(new
     
    182197            */
    183198
    184             proc.waitFor();
    185             System.err.println("*** Rsync finished");
    186 
     199           
     200            //return bz_bytes;
    187201              //System.out.println("Done.");
    188202             
     
    191205            e.printStackTrace();
    192206        }
     207       
     208        return bis;
    193209       
    194210    }
     
    197213     */
    198214    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    199         PrintWriter pw = response.getWriter();
     215       
    200216       
    201217        String cgi_ids = request.getParameter("ids");
     
    205221        if (cgi_ids != null) {
    206222            response.setContentType("application/json");
    207    
     223            PrintWriter pw = response.getWriter();
     224           
    208225            String[] ids = cgi_ids.split(",");
    209226            int ids_len = ids.length;
     
    226243        else if (cgi_id != null) {
    227244            response.setContentType("application/json");
    228            
     245            PrintWriter pw = response.getWriter();
     246           
    229247            String id = cgi_id;
    230248            boolean exists = id_check_.get(id);
     
    242260                String full_json_filename = id_to_pairtree_filename(download_id);
    243261               
    244                 doRsyncDownload(full_json_filename);
    245    
     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                }
    246301            }
    247302        }
    248303        else {
    249 
     304            PrintWriter pw = response.getWriter();
     305           
    250306            pw.append("General Info: Number of HTRC Volumes in check-list = " + id_check_.size());
    251307                       
Note: See TracChangeset for help on using the changeset viewer.