package edu.pdx.cs410J.family;

import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.List;
import java.util.Iterator;

import edu.pdx.cs410J.family.web.FamilyTreeManager;

/**
 * A servlet that uploads a family tree XML file and stores it in the session.
 */
public class FamilyTreeUploadServlet extends HttpServlet {
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/plain");

    PrintWriter pw = response.getWriter();

    if (ServletFileUpload.isMultipartContent(request)) {
      FileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      List items;
      try {
        items = upload.parseRequest(request);

      } catch (FileUploadException ex) {
        throw new ServletException("Could not upload file", ex);
      }

      FileItem file = null;
      for (Iterator iter = items.iterator(); iter.hasNext(); ) {
        FileItem item = (FileItem) iter.next();
        if (!item.isFormField()) {
          if (file != null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Only one file can be uploaded");
            return;

          } else {
            file = item;
          }
        }
      }

      if (file == null) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No file specified");
        return;
      }

      XmlParser parser = new XmlParser(new InputStreamReader(file.getInputStream()));

      pw.println("Uploading " + file.getName());

      FamilyTree tree;
      try {
        tree = parser.parse();

      } catch (FamilyTreeException ex) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Couldn't parse " + file.getName());
        return;
      }

      FamilyTreeManager.setFamilyTree(tree, request);
      pw.println("Uploaded " + file.getName());

    } else {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No files specified");
    }
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/plain");

    PrintWriter pw = response.getWriter();

    FamilyTree tree = FamilyTreeManager.getFamilyTree(request);
    if (tree == null) {
      pw.println("No tree has been uploaded");

    } else {
      TextDumper dumper = new TextDumper(pw);
      dumper.dump(tree);
    }
  }
}


@@ -25,16 +25,68 @@
 amily;%0A%0A
+import edu.pdx.cs410J.family.web.FamilyTreeManager;%0A
 import o
@@ -114,39 +114,32 @@
 eupload.FileItem
-Factory
 ;%0Aimport org.apa
@@ -161,32 +161,39 @@
 eupload.FileItem
+Factory
 ;%0Aimport org.apa
@@ -364,16 +364,55 @@
 pload;%0A%0A
+import javax.servlet.ServletException;%0A
 import j
@@ -539,47 +539,8 @@
 se;%0A
-import javax.servlet.ServletException;%0A
 impo
@@ -651,88 +651,8 @@
 ist;
-%0Aimport java.util.Iterator;%0A%0Aimport edu.pdx.cs410J.family.web.FamilyTreeManager;
 %0A%0A/*
@@ -795,16 +795,28 @@
 rvlet %7B%0A
+  @Override%0A
   protec
@@ -1207,16 +1207,26 @@
     List
+%3CFileItem%3E
  items;%0A
@@ -1442,70 +1442,8 @@
 or (
-Iterator iter = items.iterator(); iter.hasNext(); ) %7B%0A        
 File
@@ -1456,33 +1456,18 @@
 tem 
-= (FileI
+: i
 tem
+s
 ) 
-iter.next();
+%7B
 %0A   
@@ -2442,16 +2442,28 @@
  %7D%0A  %7D%0A%0A
+  @Override%0A
   protec

Remove some warnings.