Project

General

Profile

« Previous | Next » 

Revision 49

Added by Matt Jones over 24 years ago

added document display function to servlet

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
20 20
  private ServletConfig		config = null;
21 21
  private ServletContext	context = null;
22 22
  DBSimpleQuery		queryobj = null;
23
  DBReader		docreader = null;
23 24
  static  String 	user = "jones";
24 25
  static  String 	password = "your-pw-goes-here";
25 26
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
......
32 33
    
33 34
      try {
34 35
        queryobj = new DBSimpleQuery(user, password, defaultDB);
36
        docreader = new DBReader(user, password, defaultDB);
35 37
      } catch (Exception e) {
36 38
      }
37 39
    } catch ( ServletException ex ) {
......
64 66
    handleGetOrPost(response, params);
65 67
  }
66 68

  
69
  /**
70
   * routine to control servlet response depending on the action requested
71
   * Valid "action" parameters are:
72
   *     query -- query the text values of all elements and attributes
73
   *              and return a result set of nodes
74
   *  getdocument -- display an XML document in XML or HTML given a document ID
75
   */
67 76
  private void handleGetOrPost(HttpServletResponse response, Hashtable params) 
68 77
    throws ServletException, IOException {
69 78

  
70
    // Run the query
71
    String[] query = (String[])params.get("query"); 
72
    Hashtable nodelist = queryobj.findRootNodes(query[0]);
73

  
74
    // set content type and other response header fields first
75
    response.setContentType("text/xml");
76

  
77
    // then write the data of the response
79
    // Get a handle to the output stream back to the client
78 80
    PrintWriter out = response.getWriter();
81
  
82
    String action = ((String[])params.get("action"))[0];
79 83

  
80
    // Print the reulting root nodes
81
    long nodeid;
82
    out.println("<?xml version=\"1.0\"?>\n");
83
    out.println("<resultset>\n");
84
    out.println("  <query>" + query[0] + "</query>");
85
    Enumeration rootlist = nodelist.keys(); 
86
    while (rootlist.hasMoreElements()) {
87
      nodeid = ((Long)rootlist.nextElement()).longValue();
88
      out.println("  <nodeid>" + nodeid + "</nodeid>");
84
    if (action.equals("query")) {
85
      handleQueryAction(out, params, response);
86
    } else if (action.equals("getdocument")) {
87
      handleGetDocumentAction(out, params, response);
89 88
    }
90
    out.println("</resultset>");
91 89

  
90
    // Close the stream to the client
92 91
    out.close();
93 92
  }
93

  
94
  private void handleQueryAction(PrintWriter out, Hashtable params, 
95
               HttpServletResponse response) {
96
      // Run the query
97
      String query = ((String[])params.get("query"))[0]; 
98
      Hashtable nodelist = queryobj.findRootNodes(query);
99
  
100
      // set content type and other response header fields first
101
      response.setContentType("text/xml");
102
  
103
      // Print the resulting root nodes
104
      long nodeid;
105
      out.println("<?xml version=\"1.0\"?>\n");
106
      out.println("<resultset>\n");
107
      out.println("  <query>" + query + "</query>");
108
      Enumeration rootlist = nodelist.keys(); 
109
      while (rootlist.hasMoreElements()) {
110
        nodeid = ((Long)rootlist.nextElement()).longValue();
111
        out.println("  <nodeid>" + nodeid + "</nodeid>");
112
      }
113
      out.println("</resultset>");
114
  }
115

  
116
  private void handleGetDocumentAction(PrintWriter out, Hashtable params, 
117
               HttpServletResponse response) {
118
      // Get the document indicated
119
      String docid = ((String[])params.get("docid"))[0]; 
120
      String doc = docreader.readXMLDocument((new Long(docid)).longValue());
121

  
122
      // set content type and other response header fields first
123
      response.setContentType("text/xml");
124
  
125
      out.println(doc);
126

  
127
      // Print the resulting root nodes
128
      //out.println("<?xml version=\"1.0\"?>\n");
129
      //out.println("<docid>" + docid + "</docid>");
130
  }
94 131
}
contents.html
21 21
<p>
22 22
Simple database query against MetaCat<br>
23 23
<form action="/servlets/MetaCatServlet" target="right" method="POST">
24
  <input type="hidden" name="action" value="query">
24 25
  <input type="text" name="query" size="15">
25 26
  <input type="submit" value="Run query">
26 27
  <br />
27 28
  <input type="checkbox" name="convert" value="true">Results in HTML
28 29
</form>
29 30
<p>
31
<a href="/servlets/MetaCatServlet?action=getdocument&docid=1" 
32
   target="right">Test Document Display (1)</a><br>
33
<a href="/servlets/MetaCatServlet?action=getdocument&docid=31" 
34
   target="right">Test Document Display (31)</a>
30 35
</body>
31 36
</html>
MetaCatServlet.java
20 20
  private ServletConfig		config = null;
21 21
  private ServletContext	context = null;
22 22
  DBSimpleQuery		queryobj = null;
23
  DBReader		docreader = null;
23 24
  static  String 	user = "jones";
24 25
  static  String 	password = "your-pw-goes-here";
25 26
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
......
32 33
    
33 34
      try {
34 35
        queryobj = new DBSimpleQuery(user, password, defaultDB);
36
        docreader = new DBReader(user, password, defaultDB);
35 37
      } catch (Exception e) {
36 38
      }
37 39
    } catch ( ServletException ex ) {
......
64 66
    handleGetOrPost(response, params);
65 67
  }
66 68

  
69
  /**
70
   * routine to control servlet response depending on the action requested
71
   * Valid "action" parameters are:
72
   *     query -- query the text values of all elements and attributes
73
   *              and return a result set of nodes
74
   *  getdocument -- display an XML document in XML or HTML given a document ID
75
   */
67 76
  private void handleGetOrPost(HttpServletResponse response, Hashtable params) 
68 77
    throws ServletException, IOException {
69 78

  
70
    // Run the query
71
    String[] query = (String[])params.get("query"); 
72
    Hashtable nodelist = queryobj.findRootNodes(query[0]);
73

  
74
    // set content type and other response header fields first
75
    response.setContentType("text/xml");
76

  
77
    // then write the data of the response
79
    // Get a handle to the output stream back to the client
78 80
    PrintWriter out = response.getWriter();
81
  
82
    String action = ((String[])params.get("action"))[0];
79 83

  
80
    // Print the reulting root nodes
81
    long nodeid;
82
    out.println("<?xml version=\"1.0\"?>\n");
83
    out.println("<resultset>\n");
84
    out.println("  <query>" + query[0] + "</query>");
85
    Enumeration rootlist = nodelist.keys(); 
86
    while (rootlist.hasMoreElements()) {
87
      nodeid = ((Long)rootlist.nextElement()).longValue();
88
      out.println("  <nodeid>" + nodeid + "</nodeid>");
84
    if (action.equals("query")) {
85
      handleQueryAction(out, params, response);
86
    } else if (action.equals("getdocument")) {
87
      handleGetDocumentAction(out, params, response);
89 88
    }
90
    out.println("</resultset>");
91 89

  
90
    // Close the stream to the client
92 91
    out.close();
93 92
  }
93

  
94
  private void handleQueryAction(PrintWriter out, Hashtable params, 
95
               HttpServletResponse response) {
96
      // Run the query
97
      String query = ((String[])params.get("query"))[0]; 
98
      Hashtable nodelist = queryobj.findRootNodes(query);
99
  
100
      // set content type and other response header fields first
101
      response.setContentType("text/xml");
102
  
103
      // Print the resulting root nodes
104
      long nodeid;
105
      out.println("<?xml version=\"1.0\"?>\n");
106
      out.println("<resultset>\n");
107
      out.println("  <query>" + query + "</query>");
108
      Enumeration rootlist = nodelist.keys(); 
109
      while (rootlist.hasMoreElements()) {
110
        nodeid = ((Long)rootlist.nextElement()).longValue();
111
        out.println("  <nodeid>" + nodeid + "</nodeid>");
112
      }
113
      out.println("</resultset>");
114
  }
115

  
116
  private void handleGetDocumentAction(PrintWriter out, Hashtable params, 
117
               HttpServletResponse response) {
118
      // Get the document indicated
119
      String docid = ((String[])params.get("docid"))[0]; 
120
      String doc = docreader.readXMLDocument((new Long(docid)).longValue());
121

  
122
      // set content type and other response header fields first
123
      response.setContentType("text/xml");
124
  
125
      out.println(doc);
126

  
127
      // Print the resulting root nodes
128
      //out.println("<?xml version=\"1.0\"?>\n");
129
      //out.println("<docid>" + docid + "</docid>");
130
  }
94 131
}
lib/contents.html
21 21
<p>
22 22
Simple database query against MetaCat<br>
23 23
<form action="/servlets/MetaCatServlet" target="right" method="POST">
24
  <input type="hidden" name="action" value="query">
24 25
  <input type="text" name="query" size="15">
25 26
  <input type="submit" value="Run query">
26 27
  <br />
27 28
  <input type="checkbox" name="convert" value="true">Results in HTML
28 29
</form>
29 30
<p>
31
<a href="/servlets/MetaCatServlet?action=getdocument&docid=1" 
32
   target="right">Test Document Display (1)</a><br>
33
<a href="/servlets/MetaCatServlet?action=getdocument&docid=31" 
34
   target="right">Test Document Display (31)</a>
30 35
</body>
31 36
</html>
Makefile
28 28
		DBReader.java \
29 29
		BasicElement.java \
30 30
		ReaderElement.java
31
	cp DBReader.class \
32
		ReaderElement.class \
33
		BasicElement.class \
34
		/home/httpd/servlets/
31 35

  
32 36
query:
33 37
	javac -classpath "$(CPATH)" \
34 38
		DBSimpleQuery.java
39
	cp DBSimpleQuery.class /home/httpd/servlets/
35 40

  
36
servlet:
37
	javac -classpath "$(CPATH)" \
38
		MetaCatServlet.java \
39
		DBSimpleQuery.java
40
	cp MetaCatServlet.class DBSimpleQuery.class /home/httpd/servlets/
41
servlet: query reader
42
	javac -classpath "$(CPATH)" MetaCatServlet.java
43
	cp MetaCatServlet.class /home/httpd/servlets/
41 44

  
42 45
test:
43 46
	java -cp $(CPATH) DBSAXWriter test.xml $(USER) $(PW)

Also available in: Unified diff