Project

General

Profile

« Previous | Next » 

Revision 1292

Added by Jing Tao almost 22 years ago

Merge cvs branch replication to cvs head.

View differences:

MetaCatServlet.java
317 317
        }
318 318
      } else if (action.equals("export")) {
319 319
        
320
        handleExportAction(params, response, username, groupnames);
320
        handleExportAction(params, response, username, groupnames, password);
321 321
      } else if (action.equals("read")) {
322
        handleReadAction(params, response, username, groupnames);
322
        handleReadAction(params, response, username,password, groupnames);
323 323
      } else if (action.equals("insert") || action.equals("update")) {
324 324
        PrintWriter out = response.getWriter();
325 325
        if ( (username != null) &&  !username.equals("public") ) {
......
364 364
        PrintWriter out = response.getWriter();
365 365
        handleGetMaxDocidAction(out, params, response);
366 366
        out.close();
367
      } else if (action.equals("getrevisionanddoctype")) {
368
        PrintWriter out = response.getWriter();
369
        handleGetRevisionAndDocTypeAction(out, params);
370
        out.close();
367 371
      } else if (action.equals("login") || action.equals("logout")) {
368 372
      } else if (action.equals("protocoltest")) {
369 373
        String testURL = "metacat://dev.nceas.ucsb.edu/NCEAS.897766.9";
......
699 703
   * @param user the username sent the request
700 704
   * @param groups the user's groupnames
701 705
   */
702
  private void handleExportAction(Hashtable params,
703
                    HttpServletResponse response, String user, String[] groups)
706
  private void handleExportAction(Hashtable params, 
707
    HttpServletResponse response, String user, String[] groups, String passWord)
704 708
  {
709
    // Output stream
705 710
    ServletOutputStream out = null;
711
    // Zip output stream
706 712
    ZipOutputStream zOut = null;
707 713
    DocumentImpl docImpls=null;
708 714
    DBQuery queryObj=null;
......
713 719
    try
714 720
    {
715 721
      // read the params
716
      if (params.containsKey("docid")) {
722
      if (params.containsKey("docid")) 
723
      {
717 724
        docs = (String[])params.get("docid");
718
      }
719

  
720
   
721
      queryObj = new DBQuery(saxparser);//"saxparser" have to use though
722
                                              //it is not standard
725
      }//if
726
      // Create a DBuery to handle export
727
      queryObj = new DBQuery(saxparser);
728
      // Get the docid
723 729
      docId=docs[0];
730
      // Make sure the client specify docid
731
      if (docId == null || docId.equals(""))
732
      {
733
        response.setContentType("text/xml"); //MIME type
734
        // Get a printwriter
735
        PrintWriter pw = response.getWriter();
736
        // Send back message
737
        pw.println("<?xml version=\"1.0\"?>");
738
        pw.println("<error>");
739
        pw.println("You didn't specify requested docid");
740
        pw.println("</error>");
741
        // Close printwriter 
742
        pw.close();
743
        return;
744
      }//if
745
      // Get output stream
724 746
      out = response.getOutputStream();
725 747
      response.setContentType("application/zip"); //MIME type
726 748
      zOut = new ZipOutputStream(out);
727
      zOut =queryObj.getZippedPackage(docId, out, user, groups);
749
      zOut =queryObj.getZippedPackage(docId, out, user, groups, passWord);
728 750
      zOut.finish(); //terminate the zip file
729 751
      zOut.close();  //close the zip stream
730 752
      
731
    } 
753
    }//try 
732 754
    catch (Exception e)
733 755
    {
734 756
      try
735 757
      {
736 758
        response.setContentType("text/xml"); //MIME type
759
        // Send error message back
737 760
        if (out != null)
738 761
        {
739 762
            PrintWriter pw = new PrintWriter(out);
......
741 764
            pw.println("<error>");
742 765
            pw.println(e.getMessage());
743 766
            pw.println("</error>");
767
            // Close printwriter
744 768
            pw.close();
745
        }
746

  
769
            // Close output stream
770
            out.close();
771
        }//if
772
        // Close zip output stream 
747 773
        if ( zOut != null )
748 774
        {
749 775
          zOut.close();
750
        }
751
      }
776
        }//if
777
      }//try
752 778
      catch (IOException ioe)
753 779
      {
754 780
        MetaCatUtil.debugMessage("Problem with the servlet output " +
755
                           "in MetacatServlet.handleReadAction: " +
781
                           "in MetacatServlet.handleExportAction: " +
756 782
                           ioe.getMessage(), 30);
757
        
783
      }//catch
758 784

  
759
      }
760

  
761
      MetaCatUtil.debugMessage("Error in MetacatServlet.handleReadAction: " +
785
      MetaCatUtil.debugMessage("Error in MetacatServlet.handleExportAction: " +
762 786
                         e.getMessage(), 30);
787
      e.printStackTrace(System.out);
763 788
      
764 789
    }//catch
765
   
766

  
790
 
767 791
  }//handleExportAction
768 792
  
769 793
  // READ SECTION
......
779 803
   * @param groups the user's groupnames
780 804
   */
781 805
  private void handleReadAction(Hashtable params, HttpServletResponse response,
782
                                String user, String[] groups) 
806
                                String user, String passWord, String[] groups) 
783 807
  {
784 808
    ServletOutputStream out = null;
785 809
    ZipOutputStream zout = null;
810
    PrintWriter pw = null;
811
    boolean zip = false;
786 812
    
787 813
    try {
788 814
      String[] docs = new String[0];
789 815
      String docid = "";
790 816
      String qformat = "";
791 817
      String abstrpath = null;
792
      boolean zip = false;
818
      
793 819
      // read the params
794 820
      if (params.containsKey("docid")) {
795 821
        docs = (String[])params.get("docid");
......
857 883
      }
858 884
      
859 885
  
860
    } catch (Exception e) {
886
    }
887
    // To handle doc not found exception
888
    catch (McdbDocNotFoundException notFoundE)
889
    {
890
      // the docid which didn't be found
891
      String notFoundDocId = notFoundE.getUnfoundDocId();
892
      String notFoundRevision = notFoundE.getUnfoundRevision();
893
      MetaCatUtil.debugMessage("Missed id: "+ notFoundDocId, 30);
894
      MetaCatUtil.debugMessage("Missed rev: "+ notFoundRevision, 30);
895
      try
896
      {
897
        // read docid from remote server
898
        readFromRemoteMetaCat(response, notFoundDocId, notFoundRevision,
899
                                              user, passWord, out, zip, zout);
900
        // Close zout outputstream
901
        if ( zout != null)
902
        {
903
          zout.close();
904
        }
905
        // close output stream
906
        if (out != null)
907
        {
908
          out.close();
909
        }
910
        
911
      }//try
912
      catch ( Exception exc)
913
      {
914
        MetaCatUtil.debugMessage("Erorr in MetacatServlet.hanldReadAction: "+
915
                                      exc.getMessage(), 30);
916
        try
917
        {
918
          if (out != null) 
919
          {
920
            response.setContentType("text/xml");
921
            // Send back error message by printWriter
922
            pw = new PrintWriter(out);
923
            pw.println("<?xml version=\"1.0\"?>");
924
            pw.println("<error>");
925
            pw.println(notFoundE.getMessage());
926
            pw.println("</error>");
927
            pw.close();
928
            out.close();
929
            
930
          }
931
          else
932
          {
933
           response.setContentType("text/xml"); //MIME type
934
           // Send back error message if out = null
935
           if (pw == null)
936
           {
937
             // If pw is null, open the respnose
938
            pw = response.getWriter();
939
           }
940
           pw.println("<?xml version=\"1.0\"?>");
941
           pw.println("<error>");
942
           pw.println(notFoundE.getMessage());
943
           pw.println("</error>");
944
           pw.close();
945
        }
946
        // close zout
947
        if ( zout != null ) 
948
        { 
949
          zout.close(); 
950
        }
951
        }//try
952
        catch (IOException ie)
953
        {
954
          MetaCatUtil.debugMessage("Problem with the servlet output " +
955
                           "in MetacatServlet.handleReadAction: " +
956
                           ie.getMessage(), 30);
957
        }//cathch
958
      }//catch
959
    }// catch McdbDocNotFoundException
960
    catch (Exception e) 
961
    {
861 962
      try {
862
        response.setContentType("text/xml"); //MIME type
863
        //PrintWriter pw = response.getWriter();
963
       
864 964
        if (out != null) {
865
            PrintWriter pw = new PrintWriter(out);
965
            response.setContentType("text/xml"); //MIME type
966
            pw = new PrintWriter(out);
866 967
            pw.println("<?xml version=\"1.0\"?>");
867 968
            pw.println("<error>");
868 969
            pw.println(e.getMessage());
869 970
            pw.println("</error>");
870 971
            pw.close();
972
            out.close();
871 973
        }
872
        //if ( out != null ) { out.close(); }
974
        else
975
        {
976
           response.setContentType("text/xml"); //MIME type
977
           // Send back error message if out = null
978
           if ( pw == null)
979
           {
980
            pw = response.getWriter();
981
           }
982
           pw.println("<?xml version=\"1.0\"?>");
983
           pw.println("<error>");
984
           pw.println(e.getMessage());
985
           pw.println("</error>");
986
           pw.close();
987
          
988
        }
989
        // Close zip output stream
873 990
        if ( zout != null ) { zout.close(); }
991
        
874 992
      } catch (IOException ioe) {
875
        System.out.println("Problem with the servlet output " +
993
        MetaCatUtil.debugMessage("Problem with the servlet output " +
876 994
                           "in MetacatServlet.handleReadAction: " +
877
                           ioe.getMessage());
995
                           ioe.getMessage(), 30);
878 996
        ioe.printStackTrace(System.out);
879 997
        
880 998
      }
......
910 1028
     
911 1029
      if ( doc.getRootNodeID() == 0 ) {
912 1030
        // this is data file
913
        ServletOutputStream out = response.getOutputStream(); 
914 1031
        String filepath = util.getOption("datafilepath");
915 1032
        if(!filepath.endsWith("/")) {
916 1033
          filepath += "/";
917 1034
        }
918
        String filename = filepath + docid;      //MIME type
1035
        String filename = filepath + docid;
1036
        FileInputStream fin = null;
1037
        fin = new FileInputStream(filename);
1038
        
1039
        //MIME type
919 1040
        String contentType = getServletContext().getMimeType(filename);
920 1041
        if (contentType == null) {
921 1042
          if (filename.endsWith(".xml")) {
......
940 1061
        response.setContentType(contentType);
941 1062
        // if we decide to use "application/octet-stream" for all data returns
942 1063
        // response.setContentType("application/octet-stream");
943
        FileInputStream fin = null;
1064
       
944 1065
        try {
945
          fin = new FileInputStream(filename);
1066
          
1067
          ServletOutputStream out = response.getOutputStream(); 
946 1068
          byte[] buf = new byte[4 * 1024]; // 4K buffer
947 1069
          int b = fin.read(buf);
948 1070
          while (b != -1) {
......
974 1096
        }
975 1097
      
976 1098
      }
977
    } catch (Exception except) {
1099
    }
1100
    catch (Exception except) 
1101
    {
978 1102
      throw except;
979 1103
    
980 1104
    }
......
1080 1204
          if(!filepath.endsWith("/")) {
1081 1205
            filepath += "/";
1082 1206
          }
1083
          String filename = filepath + doc.getDocname();
1084
          zentry = new ZipEntry(filename);
1085
          zout.putNextEntry(zentry);
1207
          String filename = filepath + docid;
1086 1208
          FileInputStream fin = null;
1209
          fin = new FileInputStream(filename);
1087 1210
          try {
1088
            fin = new FileInputStream(filename);
1211
            
1212
            zentry = new ZipEntry(docid);
1213
            zout.putNextEntry(zentry);
1089 1214
            byte[] buf = new byte[4 * 1024]; // 4K buffer
1090 1215
            int b = fin.read(buf);
1091 1216
            while (b != -1) {
......
1144 1269
   
1145 1270
    }
1146 1271
  }
1272
  /**
1273
   * If metacat couldn't find a data file or document locally, it will read this
1274
   * docid from its home server. This is for the replication feature
1275
   */
1276
  private void readFromRemoteMetaCat(HttpServletResponse response, String docid, 
1277
                     String rev, String user, String password, 
1278
                     ServletOutputStream out, boolean zip, ZipOutputStream zout)
1279
                        throws Exception
1280
 {
1281
   // Create a object of RemoteDocument, "" is for zipEntryPath
1282
   RemoteDocument remoteDoc = 
1283
                        new RemoteDocument (docid, rev,user, password, "");
1284
   // If it is zip format
1285
   if (zip)
1286
   {
1287
     remoteDoc.readDocumentFromRemoteServerByZip(zout);
1288
   }//if 
1289
   else
1290
   {
1291
     if (out == null)
1292
     {
1293
       out = response.getOutputStream();
1294
     }//if
1295
     response.setContentType("application/octet-stream");
1296
     remoteDoc.readDocumentFromRemoteServer(out);
1297
   }//else (not zip)                   
1298
 }//readFromRemoteMetaCat
1299
  
1147 1300
  // END OF READ SECTION
1148 1301
    
1149 1302
  // INSERT/UPDATE SECTION
......
1202 1355
                  getDBConnection("MetaCatServlet.handleInsertOrUpdateAction");
1203 1356
          serialNumber=dbConn.getCheckOutSerialNumber();
1204 1357
          
1358
          
1205 1359
          // write the document to the database
1206 1360
          try 
1207 1361
          {
1208 1362
            String accNumber = docid[0];
1363
            MetaCatUtil.debugMessage(""+ doAction + " " + accNumber +"...", 10);
1209 1364
            if (accNumber.equals("")) 
1210 1365
            {
1211 1366
              accNumber = null;
......
1431 1586
 
1432 1587
  // OTHER ACTION HANDLERS
1433 1588
  
1589
  /**
1590
   * Handle "getrevsionanddoctype" action
1591
   * Given a docid, return it's current revision and doctype from data base
1592
   * The output is String look like "rev;doctype"
1593
   */
1594
  private void handleGetRevisionAndDocTypeAction(PrintWriter out, 
1595
                                                              Hashtable params)
1596
  {
1597
    // To store doc parameter
1598
    String [] docs = new String[10];
1599
    // Store a single doc id
1600
    String givenDocId = null;
1601
    // Get docid from parameters
1602
    if (params.containsKey("docid")) 
1603
    {
1604
      docs = (String[])params.get("docid");
1605
    }
1606
    // Get first docid form string array
1607
    givenDocId = docs[0];
1608
   
1609
    try 
1610
    {
1611
      // Make sure there is a docid
1612
      if (givenDocId == null || givenDocId.equals(""))
1613
      {
1614
        throw new Exception("User didn't specify docid!");
1615
      }//if
1616
      
1617
      // Create a DBUtil object
1618
      DBUtil dbutil = new DBUtil();
1619
      // Get a rev and doctype
1620
      String revAndDocType = 
1621
                dbutil.getCurrentRevisionAndDocTypeForGivenDocument(givenDocId);
1622
      out.println(revAndDocType);
1623

  
1624
    }//try 
1625
    catch (Exception e) 
1626
    {
1627
      // Handle exception
1628
      out.println("<?xml version=\"1.0\"?>");
1629
      out.println("<error>");
1630
      out.println(e.getMessage());
1631
      out.println("</error>");
1632
    }//catch 
1633
    
1634
  }//handleGetRevisionAndDocTypeAction
1635
  
1434 1636
  /** 
1435 1637
   * Handle "getaccesscontrol" action.
1436 1638
   * Read Access Control List from db connection in XML format
......
1691 1893
                           ise.getMessage());
1692 1894
      }
1693 1895
    }  
1694

  
1695
    if ( action.equals("upload")) {
1696
      if (username != null &&  !username.equals("public")) {
1697
        handleUploadAction(request, response, params, fileList, 
1698
                           username, groupnames);
1699
      } else {
1700
        try {
1896
    
1897
    // Get the out stream
1898
    try {
1701 1899
          out = response.getWriter();
1702 1900
        } catch (IOException ioe2) {
1703 1901
          util.debugMessage("Fatal Error: couldn't get response "+
1704 1902
                                                              "output stream.");
1705 1903
        }
1904
    
1905
    if ( action.equals("upload")) {
1906
      if (username != null &&  !username.equals("public")) {
1907
        handleUploadAction(request, out, params, fileList, 
1908
                           username, groupnames);
1909
      } else {
1910
       
1706 1911
        out.println("<?xml version=\"1.0\"?>");
1707 1912
        out.println("<error>");
1708 1913
        out.println("Permission denied for " + action);
1709 1914
        out.println("</error>");
1710 1915
      }
1711 1916
    } else {
1712
      try {
1917
      /*try {
1713 1918
        out = response.getWriter();
1714 1919
      } catch (IOException ioe2) {
1715 1920
        System.err.println("Fatal Error: couldn't get response output stream.");
1716
      }
1921
      }*/
1717 1922
      out.println("<?xml version=\"1.0\"?>");
1718 1923
      out.println("<error>");
1719 1924
      out.println("Error: action not registered.  Please report this error.");
......
1727 1932
   * registering it in the Metacat db
1728 1933
   */
1729 1934
  private void handleUploadAction(HttpServletRequest request,
1730
                                  HttpServletResponse response, 
1935
                                  PrintWriter out, 
1731 1936
                                  Hashtable params, Hashtable fileList, 
1732 1937
                                  String username, String[] groupnames)
1733 1938
  {
1734
    PrintWriter out = null;
1939
    //PrintWriter out = null;
1735 1940
    //Connection conn = null;
1736 1941
    String action = null;
1737 1942
    String docid = null;
1738 1943
  
1739
    response.setContentType("text/xml");
1944
    /*response.setContentType("text/xml");
1740 1945
    try 
1741 1946
    {
1742 1947
      out = response.getWriter();
......
1744 1949
    catch (IOException ioe2) 
1745 1950
    {
1746 1951
      System.err.println("Fatal Error: couldn't get response output stream.");
1747
    }
1952
    }*/
1748 1953

  
1749 1954
    if (params.containsKey("docid")) 
1750 1955
    {
......
1764 1969
  
1765 1970
        try 
1766 1971
        {
1972
           //MetaCatUtil.debugMessage("Upload datafile " + docid +"...", 10);
1767 1973
           //If document get lock data file grant
1768 1974
           if (DocumentImpl.getDataFileLockGrant(docid))
1769 1975
           {
......
1775 1981
              dataDirectory.mkdirs();
1776 1982
              File newFile = new File(dataDirectory, docid);
1777 1983
              long size = filePart.writeTo(newFile);
1778
              if ((util.getOption("replicationsenddata")).equals("on"))
1779
              {
1780
                // force replcation data file if metacat was configured
1781
                if (ReplicationHandler.getServerLocation(docid)==1)
1782
                {//local host document
1783
                  //conn=util.getConnection();
1784
                  ForceReplicationHandler frh = new ForceReplicationHandler
1785
             (docid, "insert", false, ReplicationHandler.buildServerList());
1786
                  //conn.close();
1787
                }//if
1788
                else
1789
                {
1790
                  //if it is super hub, replication to server list
1791
                  if ((util.getOption("hub")).equals("super"))
1792
                  {
1793
                    ForceReplicationHandler frh = new ForceReplicationHandler
1794
                    (docid,true, ReplicationHandler.buildServerList());
1795
                    //conn.close();
1796
                  }//if
1797
                  else
1798
                  {
1799
                    //only replicate to home host of the document
1800
                    String id=util.getDocIdFromString(docid);
1801
                    ForceReplicationHandler frh = new ForceReplicationHandler
1802
                    (docid, true, ReplicationHandler.getHomeServer(id));
1803
                  }//else
1804
                }//else
1805
              }//if
1984
              
1985
              // Force replication this data file
1986
              // To data file, "insert" and update is same
1987
              // The fourth parameter is null. Because it is notification server 
1988
              // and this method is in MetaCatServerlet. It is original command, 
1989
              // not get force replication info from another metacat
1990
              ForceReplicationHandler frh = new ForceReplicationHandler
1991
                                                (docid, "insert", false, null);
1992
             
1806 1993
              // set content type and other response header fields first
1807 1994
              out.println("<?xml version=\"1.0\"?>");
1808 1995
              out.println("<success>");

Also available in: Unified diff