Project

General

Profile

« Previous | Next » 

Revision 59

Added by Matt Jones over 24 years ago

further work on XML docuemnt loading facility in the MetaCatServlet

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
14 14
import java.io.IOException;
15 15
import java.io.Reader;
16 16
import java.io.StringReader;
17
import java.io.BufferedReader;
17 18
import java.util.Enumeration;
18 19
import java.util.Hashtable;
19 20
import java.net.URL;
......
87 88
  public void doGet (HttpServletRequest request, HttpServletResponse response)
88 89
    throws ServletException, IOException {
89 90

  
90
    // Get the parameters from the form
91
    String querystring = request.getQueryString();
92
    Hashtable params = HttpUtils.parseQueryString(querystring);
93

  
94 91
    // Process the data and send back the response
95
    handleGetOrPost(response, params);
92
    handleGetOrPost(request, response);
96 93
  }
97 94

  
98 95
  /** Handle "POST" method requests from HTTP clients */
99 96
  public void doPost( HttpServletRequest request, HttpServletResponse response)
100 97
    throws ServletException, IOException {
101 98

  
102
    // Get the input data from the client
103
    ServletInputStream in = request.getInputStream();
104
    int len = request.getContentLength();
105

  
106
    // Parse the input data into a Hashtable
107
    Hashtable params = HttpUtils.parsePostData(len, in);
108

  
109 99
    // Process the data and send back the response
110
    handleGetOrPost(response, params);
100
    handleGetOrPost(request, response);
111 101
  }
112 102

  
113 103
  /**
114 104
   * Control servlet response depending on the action parameter specified
115 105
   */
116
  private void handleGetOrPost(HttpServletResponse response, Hashtable params) 
106
  private void handleGetOrPost(HttpServletRequest request, 
107
    HttpServletResponse response) 
117 108
    throws ServletException, IOException {
118 109

  
119 110
    // Get a handle to the output stream back to the client
120 111
    PrintWriter out = response.getWriter();
121 112
  
113
    String name = null;
114
    String[] value = null;
115
    Hashtable params = new Hashtable();
116
    Enumeration paramlist = request.getParameterNames();
117
    while (paramlist.hasMoreElements()) {
118
      name = (String)paramlist.nextElement();
119
      value = request.getParameterValues(name);
120
      params.put(name,value);
121
    }
122

  
122 123
    String action = ((String[])params.get("action"))[0];
123 124

  
124 125
    if (action.equals("query")) {
......
203 204
   */
204 205
  private void handlePutDocumentAction(PrintWriter out, Hashtable params, 
205 206
               HttpServletResponse response) {
207

  
206 208
      // Get the document indicated
207
      String doctext = ((String[])params.get("doctext"))[0]; 
208
      StringReader xml = new StringReader(doctext);
209
      String[] doctext = (String[])params.get("doctext");
210
      StringReader xml = new StringReader(doctext[0]);
211

  
212
      // write the document to the database
209 213
      try {
210
        DBSAXWriter dbw = new DBSAXWriter(doctext, conn);
214
        DBSAXWriter dbw = new DBSAXWriter(xml, conn);
211 215
      } catch (SQLException e1) {
212
          out.println("Error loading document:<p>\n" + e1.getMessage());
216
          out.println("Error 1 loading document:<p>\n" + e1.getMessage());
213 217
      }catch (IOException e2) {
214
          out.println("Error loading document:<p>\n" + e2.getMessage());
218
          out.println("Error 2 loading document:<p>\n" + e2.getMessage());
215 219
      }catch (ClassNotFoundException e3) {
216
          out.println("Error loading document:<p>\n" + e3.getMessage());
220
          out.println("Error 3 loading document:<p>\n" + e3.getMessage());
217 221
      }
218
      //String doc = docreader.readXMLDocument((new Long(docid)).longValue());
219 222

  
220 223
      // set content type and other response header fields first
221
      response.setContentType("text/html");
224
      response.setContentType("text/xml");
222 225
  
223
      out.println("Finished loading document.");
226
      out.println(doctext[0]);
224 227
  }
225 228
}
MetaCatServlet.java
14 14
import java.io.IOException;
15 15
import java.io.Reader;
16 16
import java.io.StringReader;
17
import java.io.BufferedReader;
17 18
import java.util.Enumeration;
18 19
import java.util.Hashtable;
19 20
import java.net.URL;
......
87 88
  public void doGet (HttpServletRequest request, HttpServletResponse response)
88 89
    throws ServletException, IOException {
89 90

  
90
    // Get the parameters from the form
91
    String querystring = request.getQueryString();
92
    Hashtable params = HttpUtils.parseQueryString(querystring);
93

  
94 91
    // Process the data and send back the response
95
    handleGetOrPost(response, params);
92
    handleGetOrPost(request, response);
96 93
  }
97 94

  
98 95
  /** Handle "POST" method requests from HTTP clients */
99 96
  public void doPost( HttpServletRequest request, HttpServletResponse response)
100 97
    throws ServletException, IOException {
101 98

  
102
    // Get the input data from the client
103
    ServletInputStream in = request.getInputStream();
104
    int len = request.getContentLength();
105

  
106
    // Parse the input data into a Hashtable
107
    Hashtable params = HttpUtils.parsePostData(len, in);
108

  
109 99
    // Process the data and send back the response
110
    handleGetOrPost(response, params);
100
    handleGetOrPost(request, response);
111 101
  }
112 102

  
113 103
  /**
114 104
   * Control servlet response depending on the action parameter specified
115 105
   */
116
  private void handleGetOrPost(HttpServletResponse response, Hashtable params) 
106
  private void handleGetOrPost(HttpServletRequest request, 
107
    HttpServletResponse response) 
117 108
    throws ServletException, IOException {
118 109

  
119 110
    // Get a handle to the output stream back to the client
120 111
    PrintWriter out = response.getWriter();
121 112
  
113
    String name = null;
114
    String[] value = null;
115
    Hashtable params = new Hashtable();
116
    Enumeration paramlist = request.getParameterNames();
117
    while (paramlist.hasMoreElements()) {
118
      name = (String)paramlist.nextElement();
119
      value = request.getParameterValues(name);
120
      params.put(name,value);
121
    }
122

  
122 123
    String action = ((String[])params.get("action"))[0];
123 124

  
124 125
    if (action.equals("query")) {
......
203 204
   */
204 205
  private void handlePutDocumentAction(PrintWriter out, Hashtable params, 
205 206
               HttpServletResponse response) {
207

  
206 208
      // Get the document indicated
207
      String doctext = ((String[])params.get("doctext"))[0]; 
208
      StringReader xml = new StringReader(doctext);
209
      String[] doctext = (String[])params.get("doctext");
210
      StringReader xml = new StringReader(doctext[0]);
211

  
212
      // write the document to the database
209 213
      try {
210
        DBSAXWriter dbw = new DBSAXWriter(doctext, conn);
214
        DBSAXWriter dbw = new DBSAXWriter(xml, conn);
211 215
      } catch (SQLException e1) {
212
          out.println("Error loading document:<p>\n" + e1.getMessage());
216
          out.println("Error 1 loading document:<p>\n" + e1.getMessage());
213 217
      }catch (IOException e2) {
214
          out.println("Error loading document:<p>\n" + e2.getMessage());
218
          out.println("Error 2 loading document:<p>\n" + e2.getMessage());
215 219
      }catch (ClassNotFoundException e3) {
216
          out.println("Error loading document:<p>\n" + e3.getMessage());
220
          out.println("Error 3 loading document:<p>\n" + e3.getMessage());
217 221
      }
218
      //String doc = docreader.readXMLDocument((new Long(docid)).longValue());
219 222

  
220 223
      // set content type and other response header fields first
221
      response.setContentType("text/html");
224
      response.setContentType("text/xml");
222 225
  
223
      out.println("Finished loading document.");
226
      out.println(doctext[0]);
224 227
  }
225 228
}
loadxml.html
17 17
<body bgcolor="white">
18 18
<b>MetaCat XML Loader</b>
19 19
<p>
20
Load an XML document into MetaCat<br>
21
by pasting the XML into this box.<br>
20
Load an XML document into MetaCat by pasting the XML into this box.
21
</p>
22 22
<form action="/servlets/MetaCatServlet" target="right" method="POST">
23 23
  <input type="hidden" name="action" value="putdocument">
24
  <textarea name="doctext" size="50"></textarea>
24
  <textarea name="doctext" cols="55" rows="15"></textarea>
25
  <br />
25 26
  <input type="submit" value="Load XML">
26 27
</form>
27
<p>
28
To view results that are XML formatted in this demo, it is easiest to use 
29
Internet Explorer 5. Although Netscape browsers will allow you to 
30
download XML documents, only IE5 will display XML at this time.
31
<p>
28
<p />
32 29
</body>
33 30
</html>
lib/loadxml.html
17 17
<body bgcolor="white">
18 18
<b>MetaCat XML Loader</b>
19 19
<p>
20
Load an XML document into MetaCat<br>
21
by pasting the XML into this box.<br>
20
Load an XML document into MetaCat by pasting the XML into this box.
21
</p>
22 22
<form action="/servlets/MetaCatServlet" target="right" method="POST">
23 23
  <input type="hidden" name="action" value="putdocument">
24
  <textarea name="doctext" size="50"></textarea>
24
  <textarea name="doctext" cols="55" rows="15"></textarea>
25
  <br />
25 26
  <input type="submit" value="Load XML">
26 27
</form>
27
<p>
28
To view results that are XML formatted in this demo, it is easiest to use 
29
Internet Explorer 5. Although Netscape browsers will allow you to 
30
download XML documents, only IE5 will display XML at this time.
31
<p>
28
<p />
32 29
</body>
33 30
</html>

Also available in: Unified diff