Index: src/edu/ucsb/nceas/metacat/MetaCatServlet.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 340)
+++ src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 341)
@@ -21,7 +21,7 @@
 import java.io.FileInputStream;
 import java.util.Enumeration;
 import java.util.Hashtable;
-import java.util.ResourceBundle;
+import java.util.ResourceBundle; 
 import java.util.PropertyResourceBundle;
 import java.net.URL;
 import java.net.MalformedURLException;
@@ -226,7 +226,7 @@
     } else if (action.equals("Logout")) {
       HttpSession sess = request.getSession(false);
       if (sess != null) { sess.invalidate();  }    
-      response.sendRedirect("/xmltodb/login.html"); 
+      response.sendRedirect("/xmltodb/lib/login.html"); 
     // aware of session expiration on every request  
     } else {    
       HttpSession sess = request.getSession(true);
@@ -233,7 +233,7 @@
       if (sess.isNew()) { 
         // session expired or has not been stored b/w user requests
         // redirect to default page for query only access
-        response.sendRedirect("/xmltodb/sexpire.html"); 
+        response.sendRedirect("/xmltodb/lib/sexpire.html"); 
       } 
     }    
 // End of Jivka added
@@ -299,7 +299,7 @@
                     return;
                 } else {
                     response.sendRedirect(
-                    response.encodeRedirectUrl("/xmltodb/index.html"));
+                    response.encodeRedirectUrl("/xmltodb/lib/index.html"));
                 }    
             } catch ( java.io.IOException ioe) {
                 sess.disconnect();            
@@ -323,7 +323,7 @@
                     out.println("</success>");
                 } else {
                     response.sendRedirect(
-                    response.encodeRedirectUrl("/xmltodb/metacat.html"));
+                    response.encodeRedirectUrl("/xmltodb/lib/metacat.html"));
                 }    
             } catch ( java.io.IOException ioe) {
                 sess.disconnect();            
@@ -350,18 +350,140 @@
             out.println("<?xml version=\"1.0\"?>");
             out.println("<error>");
             out.println("SRB Connection failed. " + re.getMessage());
-            out.println("</error>");
+            out.println("</error>"); 
     }        
   }
 
+  /**
+    *Create the squery xml and return it as a String.
+    * @param params is the Hashtable of parameters that should be included
+    * in the squery.
+    */
+  private String handleSQuery(Hashtable params)
+  {
+    //create the squery and return it to be processed
+    String doctype = null;
+    String[] doctypeArr = null;
+    doctypeArr = (String[])params.get("doctype");
+    doctype = null;
+    if (doctypeArr != null) 
+    {
+      doctype = ((String[])params.get("doctype"))[0]; 
+    }
+    else
+    {
+      doctype="ANY"; 
+    }
+    return DBQuery.createSQuery(params, doctype);
+  }
+  
+   /**
+    *Create the query xml and return it as a String.
+    * @param query is the free text query parameter returned through the CGI.
+    * @param doctype is the doctype parameter returned through the CGI. 
+    * If no doctype filter is required, set it to null or "".
+    */
+  private String handleQuery(Hashtable params)
+  {
+    String doctype=null;
+    String[] doctypeArr=null;
+    String query = ((String[])params.get("query"))[0]; 
+    doctypeArr = (String[])params.get("doctype");
+    doctype = null;
+    if (doctypeArr != null) 
+    {
+      doctype = ((String[])params.get("doctype"))[0]; 
+    }
+    else
+    {
+      doctype="ANY"; 
+    }
+    return DBQuery.createQuery(query,doctype);
+  }
+  
+  /**
+    * Run the query and return a hashtable of results.
+    */
+  private Hashtable runQuery(Reader xmlquery)
+  {
+    Hashtable doclist=null;
+    Connection conn = null;
+    try
+    {
+        conn = util.getConnection();
+        DBQuery queryobj = new DBQuery(conn, saxparser);
+        doclist = queryobj.findDocuments(xmlquery);
+        util.returnConnection(conn);
+        return doclist;
+    } 
+    catch (Exception e) 
+    {
+      if (conn != null) 
+      {
+        util.returnConnection(conn); 
+      }
+      util.debugMessage("Error in runQuery: " + e.getMessage());
+      doclist = null;
+      return doclist;
+    }    
+  }
+  
+  private void transformDocument(Hashtable doclist, String qformat, 
+                                 String xmlquery, PrintWriter out,
+                                 HttpServletResponse response)
+  {
+    // Create a buffer to hold the xml result
+    StringBuffer resultset = new StringBuffer();
+ 
+    // Print the resulting root nodes
+    String docid = null;
+    String document = null;
+    resultset.append("<?xml version=\"1.0\"?>\n");
+    resultset.append("<resultset>\n");
+    resultset.append("  <query>" + xmlquery + "</query>");   
+    Enumeration doclistkeys = doclist.keys(); 
+    while (doclistkeys.hasMoreElements()) 
+    {
+      docid = (String)doclistkeys.nextElement();
+      document = (String)doclist.get(docid);
+      resultset.append("  <document>" + document + "</document>");
+    }
+    resultset.append("</resultset>");
+
+    if(qformat.equals("xml")) 
+    {
+      // set content type and other response header fields first
+      response.setContentType("text/xml");
+      out.println(resultset.toString());
+    } 
+    else if(qformat.equals("html")) 
+    {
+      // set content type and other response header fields first
+      response.setContentType("text/html");
+      XMLDocumentFragment htmldoc = null;
+      try 
+      {
+        XSLStylesheet style = new XSLStylesheet(
+                              new URL(resultStyleURL), null);
+        htmldoc = (new XSLProcessor()).processXSL(style, 
+                  (Reader)(new StringReader(resultset.toString())),null);
+        htmldoc.print(out);
+      } 
+      catch (Exception e) 
+      {
+        out.println("Error transforming document:\n" + e.getMessage());
+      }
+    }
+  }
+                              
   /** 
    * Handle the database query request and return a result set, possibly
    * transformed from XML into HTML
    */
   private void handleQueryAction(PrintWriter out, Hashtable params, 
-               HttpServletResponse response) {
+                                 HttpServletResponse response) 
+  {
       String action = ((String[])params.get("action"))[0];
-      String query = ((String[])params.get("query"))[0]; 
       Hashtable doclist = null;
       String[] doctypeArr = null;
       String doctype = null;
@@ -368,81 +490,19 @@
       Reader xmlquery = null;
       Connection conn = null;
 
-      // Run the query if it is a structured query
-      // or, if it is a free-text, simple query,
-      // format it first as a structured query and then run it
-      if (action.equals("query")) {
-        doctypeArr = (String[])params.get("doctype");
-        doctype = null;
-        if (doctypeArr != null) {
-          doctype = ((String[])params.get("doctype"))[0]; 
-        }
-
-        if (doctype != null) {
-          xmlquery = new StringReader(DBQuery.createQuery(query,doctype));
-        } else {
-          xmlquery = new StringReader(DBQuery.createQuery(query));
-        }
-      } else if (action.equals("squery")) {
-        xmlquery = new StringReader(query);
-      } else {
-        System.err.println("Error handling query -- illegal action value");
+      if(action.equals("query"))
+      {
+        xmlquery = new StringReader(handleQuery(params)); 
       }
-
-      try {
-
-        conn = util.getConnection();
-        DBQuery queryobj = new DBQuery(conn, saxparser);
-        doclist = queryobj.findDocuments(xmlquery);
-        util.returnConnection(conn);
-
-      } catch (Exception e) {
-        if ( conn != null ) { util.returnConnection(conn); }
-        response.setContentType("text/html");
-        out.println(e.getMessage());
-        return;
-      }    
-
-      // Create a buffer to hold the xml result
-      StringBuffer resultset = new StringBuffer();
- 
-      // Print the resulting root nodes
-      String docid;
-      String document = null;
-      resultset.append("<?xml version=\"1.0\"?>\n");
-      //resultset.append("<!DOCTYPE resultset PUBLIC " +
-      //               "\"-//NCEAS//resultset//EN\" \"resultset.dtd\">\n");
-      resultset.append("<resultset>\n");
-  // following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
-  //    resultset.append("  <query>" + query + "</query>");   
-      Enumeration doclistkeys = doclist.keys(); 
-      while (doclistkeys.hasMoreElements()) {
-        docid = (String)doclistkeys.nextElement();
-        document = (String)doclist.get(docid);
-        resultset.append("  <document>" + document + "</document>");
+      else if(action.equals("squery"))
+      {
+        xmlquery = new StringReader(handleSQuery(params)); 
       }
-      resultset.append("</resultset>");
-
+      
+      doclist = runQuery(xmlquery);
+      
       String qformat = ((String[])params.get("qformat"))[0]; 
-      if (qformat.equals("xml")) {
-        // set content type and other response header fields first
-        response.setContentType("text/xml");
-        out.println(resultset.toString());
-      } else if (qformat.equals("html")) {
-        // set content type and other response header fields first
-        response.setContentType("text/html");
-        //out.println("Converting to HTML...");
-        XMLDocumentFragment htmldoc = null;
-        try {
-          XSLStylesheet style = new XSLStylesheet(
-                                    new URL(resultStyleURL), null);
-          htmldoc = (new XSLProcessor()).processXSL(style, 
-                     (Reader)(new StringReader(resultset.toString())),null);
-          htmldoc.print(out);
-        } catch (Exception e) {
-          out.println("Error transforming document:\n" + e.getMessage());
-        }
-      }
+      transformDocument(doclist, qformat, xmlquery.toString(), out, response);
   }
 
   /** 
@@ -807,6 +867,9 @@
 
 /**
  * '$Log$
+ * 'Revision 1.61  2000/08/10 18:56:48  bojilova
+ * 'added "anonymous" user connection
+ * '
  * 'Revision 1.60  2000/08/09 00:39:47  jones
  * '-Reorganized xmltodb module to support new install process for the new
  * 'linux server (dev.nceas.ucsb.edu).  Added "build.sh" shell script that
