Index: /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/FileLoaderServlet.java
===================================================================
--- /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/FileLoaderServlet.java	(revision 25054)
+++ /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/FileLoaderServlet.java	(revision 25054)
@@ -0,0 +1,181 @@
+package org.greenstone.gsdl3;
+
+import java.io.*;
+import java.nio.channels.FileChannel;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
+import org.greenstone.gsdl3.util.GSParams;
+import org.greenstone.util.GlobalProperties;
+
+public class FileLoaderServlet extends LibraryServlet
+{
+	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+	{
+		if (request.getMethod().equals("POST"))
+		{
+			DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
+
+			int sizeLimit = System.getProperties().containsKey("servlet.upload.filesize.limit") ? Integer.parseInt(System.getProperty("servlet.upload.filesize.limit")) : 20 * 1024 * 1024;
+
+			fileItemFactory.setSizeThreshold(sizeLimit);
+			fileItemFactory.setRepository(new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp"));
+
+			ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
+
+			String storageLocation = "";
+			File uploadedFile = null;
+			Boolean ajaxUpload = false;
+			StringBuilder json = new StringBuilder("[");
+			try
+			{
+				List items = uploadHandler.parseRequest(request);
+				Iterator iter = items.iterator();
+				while (iter.hasNext())
+				{
+					FileItem current = (FileItem) iter.next();
+					if (current.isFormField())
+					{
+						if (current.getFieldName().equals(GSParams.FILE_LOCATION))
+						{
+							storageLocation = current.getString();
+						}
+						
+						if (current.getFieldName().equals("ajaxUpload") && current.getString().equals("true"))
+						{
+							ajaxUpload = true;
+						}
+					}
+					else
+					{
+						File file = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp" + File.separator + current.getName());
+						File tmpFolder = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp");
+						if (!tmpFolder.exists())
+						{
+							tmpFolder.mkdirs();
+						}
+						current.write(file);
+
+						uploadedFile = file;
+						
+						if(!json.toString().equals("["))
+						{
+							json.append(",");
+						}
+						
+						json.append("{");
+						json.append("\"name\":\"" + file.getName() + "\",");
+						json.append("\"size\":\"" + file.length() + "\",");
+						json.append("\"url\":\"" + "tmp/" + current.getName() + "\",");
+						json.append("\"thumbnail_url\":\"" + "tmp/" + current.getName() + "\",");
+						json.append("\"delete_url\":\"\",");
+						json.append("\"delete_type\":\"\"");
+						json.append("}");
+					}
+				}
+				json.append("]");
+				
+				if(ajaxUpload)
+				{
+					response.setContentType("application/json");
+					PrintWriter writer = response.getWriter();
+					writer.write(json.toString());
+					writer.flush();
+					
+					return;
+				}
+
+				if (!storageLocation.equals("") && uploadedFile != null)
+				{
+					String[] locations = storageLocation.split(":");
+
+					for (String location : locations)
+					{
+						File toFile = new File(GlobalProperties.getGSDL3Home() + location);
+						if (toFile.exists())
+						{
+							File backupFile = new File(toFile.getAbsolutePath() + System.currentTimeMillis());
+
+							logger.info("Backing up file (" + toFile.getAbsolutePath() + ") to " + backupFile.getAbsolutePath());
+							toFile.renameTo(backupFile);
+						}
+
+						FileChannel source = null;
+						FileChannel destination = null;
+						try
+						{
+							logger.info("Moving uploaded file (" + uploadedFile.getAbsolutePath() + ") to " + toFile.getAbsolutePath());
+							source = new FileInputStream(uploadedFile).getChannel();
+							destination = new FileOutputStream(toFile).getChannel();
+							destination.transferFrom(source, 0, source.size());
+						}
+						finally
+						{
+							if (source != null)
+							{
+								source.close();
+							}
+							if (destination != null)
+							{
+								destination.close();
+							}
+						}
+
+					}
+				}
+			}
+			catch (Exception e)
+			{
+				logger.error("Exception in LibraryServlet -> " + e.getMessage());
+			}
+		}
+		else
+		{
+			Map<String, String[]> queryMap = request.getParameterMap();
+			Iterator<String> queryIter = queryMap.keySet().iterator();
+			
+			while(queryIter.hasNext())
+			{
+				String q = queryIter.next();
+				if (q.equals("downloadFile"))
+				{
+					String fileLocation = queryMap.get(q)[0];
+					File fileToGet = new File(GlobalProperties.getGSDL3Home() + File.separator + fileLocation);
+	
+					if (fileToGet.exists())
+					{
+						response.setContentType("application/octet-stream");
+						response.addHeader("Content-Disposition","attachment;filename=" + fileToGet.getName());
+						FileInputStream fis = new FileInputStream(fileToGet);
+						ServletOutputStream sos = response.getOutputStream();
+	
+						byte[] buffer = new byte[4096];
+						int len;
+						while ((len = fis.read(buffer)) != -1)
+						{
+							sos.write(buffer, 0, len);
+						}
+						sos.flush();
+						fis.close();
+						sos.close();
+						
+						return;
+					}
+				}
+			}
+			super.doGet(request, response);
+		}
+	}
+
+	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+	{
+		doGet(request, response);
+	}
+}
Index: /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java
===================================================================
--- /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java	(revision 25053)
+++ /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java	(revision 25054)
@@ -356,144 +356,26 @@
 	{
 		logUsageInfo(request);
-
-		String query_string;
-		if (request.getMethod().equals("GET"))
-		{
-			query_string = request.getQueryString();
-		}
-		else if (request.getMethod().equals("POST"))
-		{
-			query_string = "";
-			Map paramMap = request.getParameterMap();
-			Iterator keyIter = paramMap.keySet().iterator();
-
-			while (keyIter.hasNext())
-			{
-				String current = (String) keyIter.next();
-				query_string += current + "=" + ((String[]) paramMap.get(current))[0];
-				if (keyIter.hasNext())
-				{
-					query_string += "&";
-				}
-			}
-
-			DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
-
-			int sizeLimit = System.getProperties().containsKey("servlet.upload.filesize.limit") ? Integer.parseInt(System.getProperty("servlet.upload.filesize.limit")) : 20 * 1024 * 1024;
-
-			fileItemFactory.setSizeThreshold(sizeLimit);
-			fileItemFactory.setRepository(new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp"));
-
-			ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
-
-			String storageLocation = "";
-			File uploadedFile = null;
-			try
-			{
-				List items = uploadHandler.parseRequest(request);
-				Iterator iter = items.iterator();
-				while (iter.hasNext())
-				{
-					FileItem current = (FileItem) iter.next();
-					if (current.isFormField())
-					{
-						query_string += current.getFieldName() + "=" + current.getString();
-						if (iter.hasNext())
-						{
-							query_string += "&";
-						}
-
-						if (current.getFieldName().equals(GSParams.FILE_LOCATION))
-						{
-							storageLocation = current.getString();
-						}
-					}
-					else
-					{
-						File file = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp" + File.separator + current.getName());
-						File tmpFolder = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp");
-						if (!tmpFolder.exists())
-						{
-							tmpFolder.mkdirs();
-						}
-						current.write(file);
-
-						uploadedFile = file;
-					}
-				}
-
-				if (!storageLocation.equals("") && uploadedFile != null)
-				{
-					String[] locations = storageLocation.split(":");
-
-					for (String location : locations)
-					{
-						File toFile = new File(GlobalProperties.getGSDL3Home() + location);
-						if (toFile.exists())
-						{
-							File backupFile = new File(toFile.getAbsolutePath() + System.currentTimeMillis());
-
-							logger.info("Backing up file (" + toFile.getAbsolutePath() + ") to " + backupFile.getAbsolutePath());
-							toFile.renameTo(backupFile);
-						}
-
-						FileChannel source = null;
-						FileChannel destination = null;
-						try
-						{
-							logger.info("Moving uploaded file (" + uploadedFile.getAbsolutePath() + ") to " + toFile.getAbsolutePath());
-							source = new FileInputStream(uploadedFile).getChannel();
-							destination = new FileOutputStream(toFile).getChannel();
-							destination.transferFrom(source, 0, source.size());
-						}
-						finally
-						{
-							if (source != null)
-							{
-								source.close();
-							}
-							if (destination != null)
-							{
-								destination.close();
-							}
-						}
-
-					}
-				}
-			}
-			catch (Exception e)
-			{
-				logger.error("Exception in LibraryServlet -> " + e.getMessage());
-			}
-
-			if (query_string.equals(""))
-			{
-				query_string = null;
-			}
-		}
-		else
-		{
-			query_string = null;
-		}
-
-		if (query_string != null)
-		{
-			String[] query_arr = StringUtils.split(query_string, "&");
+		
+		Map<String,String[]> queryMap = request.getParameterMap();
+		if (queryMap != null)
+		{
+			Iterator<String> queryIter = queryMap.keySet().iterator();
 			boolean redirect = false;
 			String href = null;
 			String rl = null;
 
-			for (int i = 0; i < query_arr.length; i++)
-			{
-				if (query_arr[i].startsWith("el="))
-				{
-					if (query_arr[i].substring(query_arr[i].indexOf("=") + 1, query_arr[i].length()).equals("direct"))
+			while (queryIter.hasNext())
+			{
+				String q = queryIter.next();
+				if (q.equals("el"))
+				{
+					if ((queryMap.get(q)[0]).equals("direct"))
 					{
 						redirect = true;
 					}
 				}
-				else if (query_arr[i].startsWith("href="))
-				{
-					href = query_arr[i].substring(query_arr[i].indexOf("=") + 1, query_arr[i].length());
+				else if (q.equals("href"))
+				{
+					href = queryMap.get(q)[0];
 					href = StringUtils.replace(href, "%2f", "/");
 					href = StringUtils.replace(href, "%7e", "~");
@@ -501,40 +383,7 @@
 					href = StringUtils.replace(href, "%3A", "\\:");
 				}
-				else if (query_arr[i].startsWith("rl="))
-				{
-					rl = query_arr[i].substring(query_arr[i].indexOf("=") + 1, query_arr[i].length());
-				}
-			}
-
-			for (String arg : query_arr)
-			{
-				if (arg.startsWith("downloadFile"))
-				{
-					int index = arg.indexOf("=");
-					if (index > -1 && index < arg.length() - 1)
-					{
-						String fileLocation = arg.substring(index + 1);
-						File fileToGet = new File(GlobalProperties.getGSDL3Home() + File.separator + fileLocation);
-
-						if (fileToGet.exists())
-						{
-							response.setContentType("application/octet-stream");
-							response.addHeader("Content-Disposition","attachment;filename=" + fileToGet.getName());
-							FileInputStream fis = new FileInputStream(fileToGet);
-							ServletOutputStream sos = response.getOutputStream();
-
-							byte[] buffer = new byte[4096];
-							int len;
-							while ((len = fis.read(buffer)) != -1)
-							{
-								sos.write(buffer, 0, len);
-							}
-							sos.flush();
-							fis.close();
-							sos.close();
-							
-							return;
-						}
-					}
+				else if (q.equals("rl"))
+				{
+					rl = queryMap.get(q)[0];
 				}
 			}
