Project

General

Profile

« Previous | Next » 

Revision 699

Added by bojilova almost 24 years ago

implemented interface for download of DTD or Schema file from Metacat file system
through the params:
action="getdtdschema"
doctype

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
94 94
 * action=getdatadoc -- retrieve a stored datadocument<br>
95 95
 * action=getaccesscontrol -- retrieve acl info for Metacat document<br>
96 96
 * action=getdoctypes -- retrieve all doctypes (publicID)<br>
97
 * action=getdtdschema -- retrieve a DTD or Schema file<br>
97 98
 * action=getdataguide -- retrieve a Data Guide<br>
98 99
 * datadoc -- data document name (id)<br>
99 100
 * <p>
......
370 371
        out.println("You must be authenticated to perform the getdataport " +
371 372
                    "action!");
372 373
      }
374
    } else if (action.equals("getdtdschema")) {
375
      PrintWriter out = response.getWriter();
376
      handleGetDTDSchemaAction(out, params, response);  
373 377
    } else if (action.equals("getdataguide")) {
374 378
      PrintWriter out = response.getWriter();
375 379
      handleGetDataGuideAction(out, params, response);  
......
1621 1625
  }
1622 1626

  
1623 1627
  /** 
1628
   * Handle the "getdtdschema" action.
1629
   * Read DTD or Schema file for a given doctype from Metacat catalog system
1630
   */
1631
  private void handleGetDTDSchemaAction(PrintWriter out, Hashtable params,
1632
                                        HttpServletResponse response) {
1633

  
1634
    Connection conn = null;
1635
    String doctype = null;
1636
    String[] doctypeArr = (String[])params.get("doctype");
1637

  
1638
    // get only the first doctype specified in the list of doctypes
1639
    // it could be done for all doctypes in that list
1640
    if (doctypeArr != null) {
1641
        doctype = ((String[])params.get("doctype"))[0]; 
1642
    }
1643

  
1644
    try {
1645

  
1646
        // get connection from the pool
1647
        conn = util.getConnection();
1648
        DBUtil dbutil = new DBUtil(conn);
1649
        String dtdschema = dbutil.readDTDSchema(doctype);
1650
        out.println(dtdschema);
1651

  
1652
    } catch (Exception e) {
1653
      out.println("<?xml version=\"1.0\"?>");
1654
      out.println("<error>");
1655
      out.println(e.getMessage());
1656
      out.println("</error>");
1657
    } finally {
1658
      util.returnConnection(conn);
1659
    }  
1660
    
1661
  }
1662

  
1663
  /** 
1624 1664
   * Handle the "getdataguide" action.
1625 1665
   * Read Data Guide for a given doctype from db connection in XML format
1626 1666
   */
src/edu/ucsb/nceas/metacat/DBUtil.java
2 2
 *  '$RCSfile$'
3 3
 *    Purpose: A Class that implements utility methods like:
4 4
 *             1/ Reding all doctypes from db connection
5
 *             2/ Reading Lore type Data Guide from db connection
5
 *             2/ Reading DTD or Schema file from Metacat catalog system
6
 *             3/ Reading Lore type Data Guide from db connection
6 7
 *  Copyright: 2000 Regents of the University of California and the
7 8
 *             National Center for Ecological Analysis and Synthesis
8 9
 *    Authors: Jivka Bojilova
......
33 34
import java.sql.SQLException;
34 35
import java.sql.PreparedStatement;
35 36
import java.sql.ResultSet;
37

  
38
import java.io.BufferedInputStream;
39
import java.io.InputStream;
40
import java.io.IOException;
41
import java.net.URL;
42
import java.net.URLConnection;
43
import java.net.MalformedURLException;
44

  
36 45
import java.util.Enumeration;
37 46
import java.util.Vector;
38 47
import java.util.Stack;
......
57 66
     if (args.length < 1)
58 67
     {
59 68
        System.err.println("Wrong number of arguments!!!");
60
        System.err.println("USAGE: java DBUtil <-dt | -dg [doctype]>");
69
        System.err.println(
70
        "USAGE: java DBUtil <-dt | -ds [doctype] | -dg [doctype]>");
61 71
        return;
62 72
     } else {
63 73
        try {
......
76 86
            if ( args.length == 2 ) { doctype = args[1]; }
77 87
            String dataguide = dbutil.readDataGuide(doctype);
78 88
            System.out.println(dataguide);
89
          } else if ( args[0].equals("-ds") ) {
90
            String doctype = null;
91
            if ( args.length == 2 ) { doctype = args[1]; }
92
            String dtdschema = dbutil.readDTDSchema(doctype);
93
            System.out.println(dtdschema);
79 94
          } else {
80
            System.err.println("USAGE: java DBUtil <-dt | -dg [doctype]>");
95
            System.err.println(
96
            "USAGE: java DBUtil <-dt | -ds [doctype] | -dg [doctype]>");
81 97
          }  
82 98

  
83 99
        } catch (Exception e) {
84
          System.err.println("error in DBUtil.main");
85
          System.err.println(e.getMessage());
100
          //System.err.println("error in DBUtil.main");
101
          //System.err.println(e.getMessage());
86 102
          e.printStackTrace(System.err);
87 103
        }
88 104
     }
......
121 137
      pstmt.close();
122 138

  
123 139
    } catch (SQLException e) {
124
      System.out.println("DBUtil.readDoctypes(): " + e.getMessage());
125
      throw e;
140
      throw new SQLException("DBUtil.readDoctypes(). " + e.getMessage());
126 141
    }
127 142

  
128 143
    return formatToXML(doctypeList, "doctype");
129 144
  }
130 145

  
131 146
  /**
147
   * read DTD or Schema file from Metacat's XML catalog system
148
   */
149
  public String readDTDSchema(String doctype)
150
        throws SQLException, MalformedURLException, IOException
151
  {
152
    String systemID = null;
153
    PreparedStatement pstmt;
154
    StringBuffer cbuff = new StringBuffer();
155
    
156
    // get doctype's System ID from db catalog
157
    try {
158
      pstmt = conn.prepareStatement("SELECT system_id " + 
159
                                    "FROM xml_catalog " +
160
                                    "WHERE entry_type in ('DTD','Schema') " +
161
                                    "AND public_id LIKE ?");
162
      pstmt.setString(1, doctype);
163
      pstmt.execute();
164
      ResultSet rs = pstmt.getResultSet();
165
      boolean hasRow = rs.next();
166
      if (hasRow) {
167
        systemID = rs.getString(1);
168
      } else {
169
        throw new SQLException("Non-registered doctype: " + doctype);
170
      }
171
      pstmt.close();
172

  
173
    } catch (SQLException e) {
174
      throw new SQLException("DBUtil.readDTD(). " + e.getMessage());
175
    }
176

  
177
    // read from URL stream as specified by the System ID.
178
    try {
179
      // open a connection to this URL and return an InputStream
180
      // for reading from that connection
181
      InputStream istream = new URL(systemID).openStream();
182
      // create a buffering character-input stream
183
      // that uses a default-sized input buffer
184
      BufferedInputStream in = new BufferedInputStream(istream);
185

  
186
      // read the input and write into the string buffer
187
	    int inputByte;
188
	    while ( (inputByte = in.read()) != -1 ) {
189
        cbuff.append((char)inputByte);
190
	    }
191

  
192
      // the input stream must be closed
193
	    in.close();
194
	    
195
    } catch (MalformedURLException e) {
196
      throw new MalformedURLException
197
      ("DBUtil.readDTD(). " + e.getMessage());
198
    } catch (IOException e) {
199
      throw new IOException
200
      ("DBUtil.readDTD(). " + e.getMessage());
201
    } catch (SecurityException e) {
202
      throw new IOException
203
      ("DBUtil.readDTD(). " + e.getMessage());
204
    }
205
    
206
   return cbuff.toString();
207
  }
208

  
209
  /**
132 210
   * read Data Guide for a given doctype from db connection in XML format
133 211
   * select all distinct absolute paths from xml_index table
134 212
   */
......
169 247
        pstmt.close();
170 248

  
171 249
    } catch (SQLException e) {
172
      System.out.println("DBUtil.readDataGuide(): " + e.getMessage());
173
      throw e;
250
      throw new SQLException("DBUtil.readDataGuide(). " + e.getMessage());
174 251
    }
175 252

  
176 253
    return formatToXML(dataguide);

Also available in: Unified diff