Index: src/edu/ucsb/nceas/metacat/harvesterClient/MetUpload.java
===================================================================
--- src/edu/ucsb/nceas/metacat/harvesterClient/MetUpload.java	(revision 1948)
+++ src/edu/ucsb/nceas/metacat/harvesterClient/MetUpload.java	(revision 1949)
@@ -1,3 +1,7 @@
+package edu.ucsb.nceas.metacat.harvesterClient;
+
+import java.io.*;
+import java.util.*;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
@@ -8,119 +12,158 @@
 import javax.servlet.http.HttpSession;
 import javax.servlet.http.HttpUtils;
 import javax.servlet.ServletOutputStream;
-
-//import org.ecoinformatics.eml.EMLParser;
-
-import java.io.*;
-import java.util.*;
-
 import com.oreilly.servlet.multipart.FilePart;
 import com.oreilly.servlet.multipart.MultipartParser;
 import com.oreilly.servlet.multipart.ParamPart;
 import com.oreilly.servlet.multipart.Part;
-
 import edu.ucsb.nceas.metacat.client.*;
 import edu.ucsb.nceas.utilities.IOUtil;
-//import edu.ucsb.nceas.metacat.*;
-//import edu.ucsb.nceas.metacat.MetaCatUtil;
 
+/**
+ *  MetUpload implements a Harvester servlet to upload a single file to Metacat
+ */
+public class MetUpload extends HttpServlet {
+    private Metacat m;
+    private String  insertXML = "";
+    private String  metacatUrl;
+    private String  userName = System.getProperty("user.name");
+    private String  tmpDirPath = "/tmp/" + userName;
+    private File    tmpDir = new File(tmpDirPath);
 
-public class MetUpload extends HttpServlet
-{
-	 private String insertXML = "";
-	 private String metacatUrl =
-	 	 "http://knb.lternet.edu:8098/knb/servlet/metacat";
-	 private Metacat m;
-public void service(HttpServletRequest request, HttpServletResponse response)
-        throws IOException
-{
-	PrintWriter out = null;
-	try {
-  		int id=0;
+    /**
+     *  Service requests made to the Harvester MetUpload servlet
+     *
+     *  @param  req   The request
+     *  @param  res   The response
+     *  @throws IOException
+     */
+    public void service(HttpServletRequest req,
+                        HttpServletResponse res) throws IOException {
+        int             strLen;
+        File            tmpFile;
+        FilePart        fPart = null;
+        FileReader      fr;
+        Hashtable       formElements = new Hashtable();
+        HttpSession     sess;
+        MultipartParser parser;
+        ParamPart       pPart;
+        Part            aPart;
+        PrintWriter     out = null;
+        String          docid = "";
+        String          tmpFileName = "";
+        String          tmpFilePath = "";
+        String          fieldName;
+        String          fieldValue;
+        String          metacatResponse;
+        String          password = null;
+        String          revision;
+        String          updateResult;
+        String          username = null;
+        StringReader    sr;
 
-		out = response.getWriter();
+        if (userName.equals("harvest")) {
+            metacatUrl = "http://knb.lternet.edu:8888/knb/servlet/metacat";
+        }
+        else {
+            metacatUrl = "http://knb.lternet.edu:8088/knb/servlet/metacat";
+        }
 
-  		String file = new String("");
-		String fileName = null;
-		FilePart fPart = null;
-      		MultipartParser parser = new MultipartParser((HttpServletRequest)request, 1024 * 1024);
-      		//MultipartParser parser = new MultipartParser(request, 1024 * 1024);
-      		Part aPart;
-		String eml_filename=" ",docid=" ";
-	    	Hashtable formElements = new Hashtable();
- 	    	while( (aPart = parser.readNextPart()) != null ) {
-	      		if( aPart.isParam() == true ) {
-          			ParamPart pPart = (ParamPart)aPart;
-	        		String field = pPart.getName();
-          			String value = pPart.getStringValue();
-          			if(field != null) {
-		        		if(value == null)
-			        		value = "";
-            				formElements.put(field, value);
-					if (field.equals("docid"))
-						docid=value;
-		      		}
-	      		}
-        		else if( aPart.isFile() == true ) {
-		      		fPart = (FilePart)aPart;
-          			String field = fPart.getName();
-		      		fileName = fPart.getFileName();
-          			if(fileName != null) {
-		        		//write the image to a file
-		        		String dir = "/tmp/Upload_data";  
-		        		File theFile = new File(dir, fileName);
-					fPart.writeTo(theFile);
-					eml_filename = dir+"/"+fileName;
-		        		formElements.put(field, fileName);
-					break;
-           			}
-        		}
-      		}
-/*****************************************************/
+        try {
+            out = res.getWriter();
+            parser = new MultipartParser(req, 1024 * 1024);
 
+            while ((aPart = parser.readNextPart()) != null) {
+                if (aPart.isParam() == true) {
+                    pPart = (ParamPart) aPart;
+                    fieldName = pPart.getName();
+                    fieldValue = pPart.getStringValue();
 
-/* Change for input to metacat */
+                    if (fieldName != null) {
+                        if (fieldValue == null) {
+                            fieldValue = "";
+                        }
 
-	HttpSession sess = request.getSession(true);
-	String username = null,password=null;
-	username = (String)sess.getAttribute("Musername");
-	password = (String)sess.getAttribute("Mpassword");
+                        formElements.put(fieldName, fieldValue);
 
-	 //out.println(username);
-	 out.println("Username is "+username+" <BR>");
-	try {
-	       FileReader fr = new FileReader(eml_filename);
-	      insertXML = IOUtil.getAsString(fr, true);
-	       } catch (IOException ioe) {
-	          out.println("Can't read test data to run the test: " + eml_filename);
-	    }
+                        if (fieldName.equals("docid")) {
+                            docid = fieldValue;
+                        }
+                    }
+                }
+                else if (aPart.isFile() == true) {
+                    fPart = (FilePart)aPart;
+                    fieldName = fPart.getName();
+                    tmpFileName = fPart.getFileName();
 
-	 try {
-	       m = MetacatFactory.createMetacatConnection(metacatUrl);
-	    } catch (MetacatInaccessibleException mie) {
-	             out.println("Metacat connection failed." + mie.getMessage());
-	   }
-	try {
-	           String identifier = docid + ".1";
-		   m.login(username, password);
-	           String response1 = m.insert(identifier,
-	           new StringReader(insertXML), null);
-	           out.println(response1);
+                    if (tmpFileName != null) {
+                        // Create the temporary directory if it doesn't exist
+                        try {
+                            if (!tmpDir.exists()) {
+                                tmpDir.mkdirs();
+                            }
+                        } catch (SecurityException se) {
+                            out.println("Can't create temporary directory: " +
+                                        tmpDir.getPath());
+                            out.println(se.getMessage());
+                        }
+                        // Write the image to a file
+                        tmpFile = new File(tmpDirPath, tmpFileName);
+                        fPart.writeTo(tmpFile);
+                        tmpFilePath = tmpDirPath + "/" + tmpFileName;
+                        formElements.put(fieldName, tmpFileName);
+                        break;
+                    }
+                }
+            }
 
-           } catch (Exception e) {
-	                out.println("General exception:\n" + e.getMessage());
-       }
+            sess = req.getSession(true);
+            username = (String) sess.getAttribute("Musername");
+            password = (String) sess.getAttribute("Mpassword");
 
+            try {
+                fr = new FileReader(tmpFilePath);
+                insertXML = IOUtil.getAsString(fr, true);
+            } catch (IOException ioe) {
+                out.println("Error reading file: " + tmpFilePath);
+                out.println(ioe.getMessage());
+            }
 
+            try {
+                m = MetacatFactory.createMetacatConnection(metacatUrl);
+            } catch (MetacatInaccessibleException mie) {
+                out.println("Metacat connection failed.");
+                out.println(mie.getMessage());
+            }
 
-/****************************************************/
+            try {
+                m.login(username, password);
+                strLen = docid.length();
+                revision = docid.substring((strLen - 2), strLen);
+                sr = new StringReader(insertXML);
+                
+                if (revision.equals(".1")) {
+                    metacatResponse = m.insert(docid, sr, null);
+                }
+                else {
+                    metacatResponse = m.update(docid, sr, null);
+                }
 
+                out.println(metacatResponse);
+            } catch (Exception e) {
+				out.println("General exception:\n" + e.getMessage());
+            }
+            
+            // Clean up the temporary file
+            if (!tmpFileName.equals("")) {
+                tmpFile = new File(tmpDirPath, tmpFileName);
+                tmpFile.delete();
+            }
 
-  	} catch (Exception e) {
-		out.println("An error occurred.");
-		e.printStackTrace(out);
-  	}
-	
-	out.flush();
+        } catch (Exception e) {
+            out.println("An error occurred while attempting to upload.");
+            e.printStackTrace(out);
+        }
+
+        out.flush();
+    }
 }
-}
Index: src/edu/ucsb/nceas/metacat/harvesterClient/LoginServlet.java
===================================================================
--- src/edu/ucsb/nceas/metacat/harvesterClient/LoginServlet.java	(revision 1948)
+++ src/edu/ucsb/nceas/metacat/harvesterClient/LoginServlet.java	(revision 1949)
@@ -1,6 +1,6 @@
+package edu.ucsb.nceas.metacat.harvesterClient;
+
 import java.io.PrintWriter;
-
-
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
@@ -11,81 +11,92 @@
 import javax.servlet.http.HttpSession;
 import javax.servlet.http.HttpUtils;
 import javax.servlet.ServletOutputStream;
-
 import edu.ucsb.nceas.metacat.AuthSession;
 
+/**
+ *  LoginServlet implements a Harvester servlet to login to Metacat
+ */
 public class LoginServlet extends HttpServlet {
 
+    public void destroy() {
+        // Close all connections
+        System.out.println("Destroying LoginServlet");
+    }
 
-public void destroy() {
-     // Close all connections
-         System.out.println("Destroying LoginServlet");
-		                     }
+    /**
+     *  Handle "GET" method requests from HTTP clients
+     *
+     *  @param  request   The request
+     *  @param  response  The response
+     *  @throws ServletException, java.io.IOException
+     */
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+                throws ServletException, java.io.IOException {
+        // Process the data and send back the response
+        handleGetOrPost(request, response);
+    }
 
- /** Handle "GET" method requests from HTTP clients */
-  public void doGet (HttpServletRequest request, HttpServletResponse response)
-      throws ServletException,java.io.IOException{
+    /**
+     *  Handle "POST" method requests from HTTP clients
+     *
+     *  @param  request   The request
+     *  @param  response  The response
+     *  @throws ServletException, java.io.IOException
+     */
+    public void doPost(HttpServletRequest request, HttpServletResponse response)
+                throws ServletException, java.io.IOException {
+        // Process the data and send back the response
+        handleGetOrPost(request, response);
+    }
 
-	// Process the data and send back the response
-	handleGetOrPost(request, response);
-	}
-		  
+    /**
+     *  Handle "GET" or "POST" method requests from HTTP clients
+     *
+     *  @param  request   The request
+     *  @param  response  The response
+     *  @throws ServletException, java.io.IOException
+     */
+    private void handleGetOrPost(HttpServletRequest request,
+                                 HttpServletResponse response)
+                 throws ServletException, java.io.IOException {
+        String user = "skr";
+        // String group = "skr";
+        String passwd = "skr";
+        AuthSession sess = null;
+        boolean isValid;
+        PrintWriter out1;
 
-/** Handle "POST" method requests from HTTP clients */
-public void doPost( HttpServletRequest request, HttpServletResponse response)
-throws ServletException,java.io.IOException{
+        user = request.getParameter("user");
+        // group = request.getParameter("group");
+        passwd = request.getParameter("passwd");
 
-// Process the data and send back the response
-handleGetOrPost(request, response);
-}
+        try {
+            sess = new AuthSession();
+        } catch (Exception e) {
+            System.out.println(
+                "Error in LoginServlet.handleGetOrPost AuthSession" +
+                e.getMessage());
+            return;
+        }
 
- private void handleGetOrPost(HttpServletRequest request,
-		HttpServletResponse response)
- 		throws ServletException,java.io.IOException
-{
+        isValid = sess.authenticate(request, user, passwd);
+        out1 = response.getWriter();
+        System.out.println("Sess.authenticate " + sess.getMessage());
+        out1.println("Sess.authenticate " + sess.getMessage());
 
-	String user="skr";
-//	String group="skr";
-	String passwd="skr";
-	AuthSession sess =null;
+        /*****************************************************/
+        HttpSession sess1 = request.getSession(true);
 
-		user = request.getParameter("user");
-//		group = request.getParameter("group");
-		passwd = request.getParameter("passwd");
-	
-	try{
-		sess = new AuthSession();
-	} catch (Exception e) {
-		System.out.println("Error in LoginServlet.handleGetorPost Authsession" +
-				e.getMessage());
-		return;
-	}
+        // if (sess.isNew()) {
+        sess1.putValue("Musername", user);
+        sess1.putValue("Mpassword", passwd);
+        // sess.putValue("groupnames", group);
+        // }
 
+        // PrintWriter out = response.getWriter();
+        // out.println(sess.getValue("username"));
+        /*****************************************************/
 
-	boolean isValid = sess.authenticate(request, user,passwd);
-		PrintWriter out1 = response.getWriter();
-	System.out.println("Sess.authenticate "+sess.getMessage());
-	out1.println("Sess.authenticate "+sess.getMessage());
-
-
-
-/*****************************************************/
-		HttpSession sess1 = request.getSession(true);
-	
-		/*if (sess.isNew()) {*/
-		 sess1.putValue("Musername",user);
-		 sess1.putValue("Mpassword",passwd);
-//			 sess.putValue("groupnames", group);
-
-		//}
-
-		
-	//	PrintWriter out = response.getWriter();
-	//	out.println(sess.getValue("username"));
-/*****************************************************/
-	response.sendRedirect("../uploademl.html");
-
-
-		
+        response.sendRedirect("../harvesterUpload.html");
+    }
 }
-}
