Project

General

Profile

« Previous | Next » 

Revision 458

Added by berkley over 23 years ago

Updated the download data function. the download data function now pulls a document from the database, zips it up and sends the zip stream to the client. It also zips up any related files. a relation parameter is used to pass related files to the servlet. If only one document should be zipped and sent, a single docid in the param hashtable is sufficient.

View differences:

src/edu/ucsb/nceas/metacat/metacatURL.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements a URL for use in identifying metacat
4
 *             files. It also handles http urls
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Chad Berkley
8
 *    Release: @release@
9
 *
10
 *   '$Author$'
11
 *     '$Date$'
12
 * '$Revision$'
13
 */
14

  
1 15
package edu.ucsb.nceas.metacat;
2 16

  
3 17
import java.net.MalformedURLException;
......
51 65
    {//if this is an http url
52 66
      params[0][0] = "httpurl";
53 67
      params[0][1] = url.substring(0, url.length());
68
      for(int i=url.length()-1; i>0; i--)
69
      {
70
        if(url.charAt(i) == '/')
71
        {
72
          i=0;
73
        }
74
        else
75
        {
76
          String exchange = temp;
77
          temp = "";
78
          temp += url.charAt(i);
79
          temp += exchange;
80
        }
81
      }
82
      params[1][0] = "filename";
83
      params[1][1] = temp;
54 84
    }
55 85
    else
56 86
    {//other urls that meet the metacat type url structure.
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
20 20
import java.io.BufferedReader;
21 21
import java.io.File;
22 22
import java.io.FileInputStream;
23
import java.io.FileOutputStream;
24
import java.io.InputStreamReader;
23 25
import java.io.DataInputStream;
24 26
import java.util.Enumeration;
25 27
import java.util.Hashtable;
......
33 35
import java.sql.SQLException;
34 36
import java.lang.reflect.*;
35 37
import java.net.*;
38
import java.util.zip.*;
36 39

  
37 40
import javax.servlet.ServletConfig;
38 41
import javax.servlet.ServletContext;
......
43 46
import javax.servlet.http.HttpServletResponse;
44 47
import javax.servlet.http.HttpSession;
45 48
import javax.servlet.http.HttpUtils;
49
import javax.servlet.ServletOutputStream;
46 50

  
47 51
import oracle.xml.parser.v2.XSLStylesheet;
48 52
import oracle.xml.parser.v2.XSLException;
......
180 184
      }
181 185
    }    
182 186
    // Get a handle to the output stream back to the client
183
    PrintWriter out = response.getWriter();
187
    //PrintWriter out = response.getWriter();
184 188
    //response.setContentType("text/html");
185 189
  
186 190
    String name = null;
......
222 226
    String username = null;
223 227
    String groupname = null;
224 228
    if (action.equals("Login") || action.equals("Login Client")) {
225
      handleLoginAction(out, params, request, response);
229
      handleLoginAction(response.getWriter(), params, request, response);
226 230
    // handle logout action  
227 231
    } else if (action.equals("Logout") || action.equals("Logout Client")) {
228 232
      HttpSession sess = request.getSession(false);
229 233
      if (sess != null) { sess.invalidate();  }    
230 234
      if (action.equals("Logout Client")) {
235
        PrintWriter out = response.getWriter();
231 236
        out.println("<?xml version=\"1.0\"?>");
232 237
        out.println("<success>");
233 238
        out.println("User logout.");
......
255 260
    // to a particular action handler
256 261
    if(action.equals("query"))
257 262
    {
258
      handleQuery(out, params, response, username, groupname); 
263
      handleQuery(response.getWriter(), params, response, username, groupname); 
259 264
    } 
260 265
    else if(action.equals("squery"))
261 266
    {
262 267
      if(params.containsKey("query"))
263 268
      {
264
        handleSQuery(out, params, response, username, groupname); 
269
        handleSQuery(response.getWriter(), params, response, username, groupname); 
265 270
      }
266 271
      else
267 272
      {
273
        PrintWriter out = response.getWriter();
268 274
        out.println("Illegal action squery without \"query\" parameter");
269 275
      }
270 276
    }
271 277
    else if (action.equals("getdocument")) {
278
      PrintWriter out = response.getWriter();
272 279
      try {
273 280
        handleGetDocumentAction(out, params, response);
274 281
      } catch (ClassNotFoundException e) {
......
278 285
      }
279 286
    } 
280 287
    else if (action.equals("getrelateddocument")) {
288
      PrintWriter out = response.getWriter();
281 289
      try {
282 290
        handleGetRelatedDocumentAction(out, params, response);
283 291
      } catch (ClassNotFoundException e) {
......
287 295
      }
288 296
    }
289 297
    else if (action.equals("insert") || action.equals("update")) {
298
      PrintWriter out = response.getWriter();
290 299
      if ( !username.equals("public") && (username != null) ) {
291 300
        handleInsertOrUpdateAction(out, params, response, username, groupname);
292 301
      } else {  
293 302
        out.println("Permission denied for " + action);
294 303
      }  
295 304
    } else if (action.equals("delete")) {
305
      PrintWriter out = response.getWriter();
296 306
      if ( !username.equals("public") && (username != null) ) {
297 307
        handleDeleteAction(out, params, response, username, groupname);
298 308
      } else {  
299 309
        out.println("Permission denied for " + action);
300 310
      }  
301 311
    } else if (action.equals("validate")) {
312
      PrintWriter out = response.getWriter();
302 313
      handleValidateAction(out, params, response); 
303 314
    } else if (action.equals("getabstract")) {
315
      PrintWriter out = response.getWriter();
304 316
      try{
305 317
        handleViewAbstractAction(out, params, response);
306 318
      }
......
309 321
        out.println("error viewing abstract: " + e.getMessage());
310 322
      }
311 323
    } else if (action.equals("getdatadoc")) {
324
      response.setContentType("application/zip");
325
      ServletOutputStream out = response.getOutputStream();
312 326
      handleGetDataDocumentAction(out, params, response);  
313 327
    } else if (action.equals("getdoctypes")) {
328
      PrintWriter out = response.getWriter();
314 329
      handleGetDoctypesAction(out, params, response);  
315 330
    } else if (action.equals("getdataguide")) {
331
      PrintWriter out = response.getWriter();
316 332
      handleGetDataGuideAction(out, params, response);  
317 333
    } else if (action.equals("Login") || action.equals("Login Client")) {
318 334
    } else {
335
      PrintWriter out = response.getWriter();
319 336
      out.println("Error: action not registered.  Please report this error.");
320 337
    }
321 338

  
322 339
    // Close the stream to the client
323
    out.close();
340
    //out.close();
324 341
  }
325 342
  
326 343
  /**
......
653 670
          if(murlParams[0].equals("httpurl"))
654 671
          {//httpurl is the param name for an http url.
655 672
            URL urlconn = new URL(murlParams[1]);  //create a new url obj.
656
            DataInputStream htmldoc = new DataInputStream(urlconn.openStream());
673
            //DataInputStream htmldoc = new DataInputStream(urlconn.openStream());
674
            BufferedReader htmldoc = new BufferedReader(
675
                                   new InputStreamReader(urlconn.openStream()));
657 676
            //bind a data stream.
658 677
            try
659 678
            { //display the document
......
925 944

  
926 945
  /** 
927 946
   * Handle the document request and return the results to the requestor
947
   * If a docid is passed in through the params then that document
948
   * will be retrieved form the DB and put in the zip file.
949
   * In addition if 1 or more relations parameters are passed, those file
950
   * will be zipped as well.  Currently this is only implemented for 
951
   * metacat:// and http:// files.  Support should be added for srb:// files
952
   * as well.
928 953
   */
929
  private void handleGetDataDocumentAction(PrintWriter out, Hashtable params, 
954
  private void handleGetDataDocumentAction(ServletOutputStream out, 
955
               Hashtable params, 
930 956
               HttpServletResponse response) {
957
  //find the related files, get them from their source and zip them into 
958
  //a zip file.
959
  try
960
  {
961
    Connection conn = util.getConnection();
962
    String currentDocid = ((String[])params.get("docid"))[0];
963
    ZipOutputStream zout = new ZipOutputStream(out);
964
    byte[] bytestring = null;
965
    ZipEntry zentry = null;
966
    DocumentImpl xmldoc = null;
967
    String[] reldocs = null;
968
    
969
    if(params.containsKey("relation"))
970
    { //get the relations from the parameters.
971
      reldocs = ((String[])params.get("relation"));
972
    }
973
    else
974
    { //let the for loop know that there are no relations to zip
975
      reldocs = new String[0];
976
    }
977

  
978
    //write the base file to the zip file.
979
    xmldoc = new DocumentImpl(conn, currentDocid);
980
    bytestring = (xmldoc.toString()).getBytes();
981
    zentry = new ZipEntry(currentDocid + ".xml");
982
    //create a new zip entry and write the file to the stream
983
    zentry.setSize(bytestring.length);
984
    zout.putNextEntry(zentry);
985
    zout.write(bytestring, 0, bytestring.length);
986
    zout.closeEntry(); //get ready for the next entry. 
987

  
988
    //zip up the related documents
989
    for(int i=0; i<reldocs.length; i++)
990
    {
991
      metacatURL murl = new metacatURL(((String)reldocs[i]));
992
      if(murl.getURLType().equals("metacat"))
993
      {
994
        //get the document from the database
995
        xmldoc = new DocumentImpl(conn, (murl.getParam(0))[1]);
996
        bytestring = (xmldoc.toString()).getBytes();
997
        zentry = new ZipEntry((murl.getParam(0))[1] + ".xml");
998
        //create a new zip entry and write the file to the stream
999
        zentry.setSize(bytestring.length);
1000
        zout.putNextEntry(zentry);
1001
        zout.write(bytestring, 0, bytestring.length);
1002
        zout.closeEntry(); //get ready for the next entry.
1003
      }
1004
      else if(murl.getURLType().equals("http"))
1005
      {
1006
        String[] murlParams = murl.getParam(0);
1007
        if(murlParams[0].equals("httpurl"))
1008
        {//httpurl is the param name for an http url.
1009
          URL urlconn = new URL(murlParams[1]);  //create a new url obj.
1010
          BufferedReader htmldoc = new BufferedReader(
1011
                                   new InputStreamReader(urlconn.openStream()));
1012
          //get the data from the web server
1013
          try
1014
          { //zip the document
1015
            String line=null;
1016
            zentry = new ZipEntry((murl.getParam(1))[1]);
1017
            //get just the filename from the URL.
1018
            zout.putNextEntry(zentry);
1019
            //make a new entry in the zip file stream
1020
            while((line = htmldoc.readLine()) != null)
1021
            {
1022
              bytestring = (line.toString()).getBytes();
1023
              zout.write(bytestring, 0, bytestring.length);
1024
              //write out the file line by line
1025
            }
1026
            zout.closeEntry(); //close the entry in the file
1027
          }
1028
          catch(Exception e)
1029
          {
1030
            util.debugMessage("error downloading html document"); 
1031
          }
1032
        }
1033
      }
1034
    }
1035
    zout.finish();  //terminate the zip file
1036
    zout.close();   //close the stream.
1037
    util.returnConnection(conn); //return the connection to the pool
1038
  }
1039
  catch(Exception e)
1040
  {
1041
    System.out.println("Error creating zip file: " + e.getMessage()); 
1042
    e.printStackTrace(System.out);
1043
  }
1044
           
1045
   /*
1046
   //////////old code using a shell script/////////////////////////////////
1047
   
931 1048
      boolean error_flag = false;
932 1049
      String error_message = "";
933 1050
      // Get the document indicated
......
975 1092
                        e.getMessage();
976 1093
      }
977 1094
    } // end defaultdatapath not null if
1095
    */
978 1096
  }
979 1097
  
980 1098
  /** 

Also available in: Unified diff