Project

General

Profile

« Previous | Next » 

Revision 636

Added by berkley almost 24 years ago

added support to download a data file through metacat using a metacat url or a docid parameter.

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
272 272
      }
273 273
    }
274 274
    else if (action.equals("read")) {
275
      PrintWriter out = response.getWriter();
275
      //PrintWriter out = response.getWriter();
276 276
      try {
277
        handleReadAction(out, params, response);
277
        handleReadAction(/*out,*/ params, response);
278 278
      } catch (ClassNotFoundException e) {
279
        out.println(e.getMessage());
279
        System.out.println(e.getMessage());
280 280
      } catch (SQLException se) {
281
        out.println(se.getMessage());
281
        System.out.println(se.getMessage());
282 282
      }
283 283
    } 
284 284
/*
......
880 880
   * Handle the database read request and return an XML document, 
881 881
   * possibly transformed from XML into HTML
882 882
   */
883
  private void handleReadAction(PrintWriter out, Hashtable params, 
883
  private void handleReadAction(/*PrintWriter out,*/ Hashtable params, 
884 884
               HttpServletResponse response) 
885 885
               throws ClassNotFoundException, IOException, SQLException 
886 886
  {
887
    //out.println(((String[])params.get("docid"))[0]);
887
     PrintWriter out;
888 888
    try {
889 889
      //MetacatURL murl = new MetacatURL(((String[])params.get("docid"))[0]);
890
      URL murl = new URL(((String[])params.get("docid"))[0]);
891
      handleGetRelatedDocumentAction(out, params, response, murl);
890
      if(params.containsKey(new String("qformat")) && 
891
         ((String[])params.get("qformat"))[0].equals("bin"))
892
      {
893
        handleGetData(params, response);
894
      }
895
      else
896
      {
897
        out = response.getWriter();
898
        URL murl = new URL(((String[])params.get("docid"))[0]);
899
        handleGetRelatedDocumentAction(out, params, response, murl);
900
      }
892 901
    } catch (MalformedURLException mue) {
902
      System.out.println("in catch");
903
      out = response.getWriter();
893 904
      handleGetDocumentAction(out, params, response);
894 905
    }
895 906
  }
907
  
908
  /**
909
   * Handle the read of a data file.
910
   */
911
  private void handleGetData(Hashtable params, 
912
                             HttpServletResponse response)
913
  {
914
    String docid = null;
915
    try
916
    {
917
      URL murl = new URL(((String[])params.get("docid"))[0]);
918
      Hashtable murlParams = util.parseQuery(murl.getQuery());
919
      if(murlParams.containsKey("docid"))
920
      {
921
        docid = ((String[])murlParams.get("docid"))[0];
922
      }
923
    }
924
    catch(MalformedURLException mue)
925
    {
926
      docid = ((String[])params.get("docid"))[0];
927
    }
928
    
929
    File f = null;
930
    FileInputStream fin = null;
931
    Connection conn;
932
    
933
    try
934
    {
935
      ServletOutputStream sosout = response.getOutputStream();
936
      response.setContentType("application/octet-stream");
937
      StringBuffer sql = new StringBuffer();
938
      sql.append("select docname from xml_documents where docid like '");
939
      sql.append(docid).append("'");
940
      conn = util.openDBConnection();
941
      PreparedStatement pstmt = conn.prepareStatement(sql.toString());
942
      pstmt.execute();
943
      ResultSet rs = pstmt.getResultSet();
944
      boolean tablehasrows = rs.next();
945
      while(tablehasrows)
946
      {
947
        //get the file stream from the file then send it to the output stream
948
        String filepath = util.getOption("datafilepath");
949
        if(!filepath.endsWith("/"))
950
        {
951
          filepath += "/";
952
        }
953
        f = new File(filepath + rs.getString(1)); 
954
        fin = new FileInputStream(f);
955
        int b = fin.read();
956
        while(b != -1)
957
        {
958
          sosout.write(b);
959
          b = fin.read();
960
        }
961
        tablehasrows = rs.next();
962
      }
963
      
964
      fin.close();
965
      conn.close();
966
    }
967
    catch(Exception e)
968
    {
969
      System.out.println("error in handleGetData: " + e.getMessage());
970
      e.printStackTrace(System.out);
971
    }
972
  }
896 973

  
897 974
  /** 
898 975
   * Handle the database read request and return an XML document, 

Also available in: Unified diff