package servlets.upload;




import java.io.*; 
import java.util.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 


import iDSS.*;
import iDSS.utils.*;



public class UploadHandler extends HttpServlet { 



    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 


        processRequest(req,res);


    } //public void doPost(HttpServletRequest req, HttpServletResponse res)



    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 


        processRequest(req,res);


    } //public void doGet(HttpServletRequest req, HttpServletResponse res)




    //public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 

    public void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 


        String callingComponent = null;

        res.setContentType("text/html"); 
        PrintWriter out = res.getWriter(); 

        try { 

            // Blindly take it on faith this is a multipart/form-data request 

            // Construct a FileUploader to help read the information. 
            // Pass in the request, a directory to saves files to, and the 
            // maximum POST size we should attempt to handle. 
            // Here we (rudely) write to the server root and impose 5 Meg limit. 

            FileUploader multi = new FileUploader(req, IDSSAppConstants.uploadDir, 5 * 1024 * 1024); 


/**
            Enumeration params = multi.getParameterNames(); 

            while (params.hasMoreElements()) { 
                String name = (String)params.nextElement(); 
                String value = multi.getParameter(name); 
                //out.println(name + " = " + value); 

                if(name.equals(IDSSAppConstants.callingComponent)) {

                    callingComponent = value;

                } //if(name.equals(IDSSAppConstants.callingComponent))

            } //while (params.hasMoreElements())


            Enumeration files = multi.getFileNames(); 
            String fileName = null;

            while (files.hasMoreElements()) { 

                String name = (String)files.nextElement(); 
                fileName = multi.getFilesystemName(name); 
                //String type = multi.getContentType(name); 

                //File f = multi.getFile(name);


            } //while (files.hasMoreElements())

            if(fileName != null) {

                File f = new File(IDSSAppConstants.uploadDir,fileName);
                req.setAttribute(IDSSAppConstants.userGridFileName,f.toString());

            } //if(fileName != null)


            //if(callingComponent != null) {
                RequestDispatcher rd = getServletContext().getRequestDispatcher(callingComponent);
                rd.forward(req,res);
            //}

**/

/**/
            out.println("<HTML>"); 
            out.println("<HEAD><TITLE>"+IDSSAppConstants.programTitle+"</TITLE></HEAD>"); 
            out.println("<BODY>"); 
            out.println("<H2>"+IDSSAppConstants.programTitle+"</H2>"); 


            // Print the parameters we received 
            out.println("<H3>Params:</H3>"); 
            out.println("<PRE>"); 
            Enumeration params = multi.getParameterNames(); 

            while (params.hasMoreElements()) { 
                String name = (String)params.nextElement(); 
                String value = multi.getParameter(name); 
                out.println(name + " = " + value);

                if(name.equals(IDSSAppConstants.callingComponent)) {

                    callingComponent = value;

                } //if(name.equals(IDSSAppConstants.callingComponent))

            } //while (params.hasMoreElements())

            out.println("</PRE>"); 

            // Show which files we received 
            out.println("<H3>Files:</H3>"); 
            out.println("<PRE>"); 

            Enumeration xfiles = multi.getFileNames(); 
            String fileName = null;

            while (xfiles.hasMoreElements()) { 

                String name = (String)xfiles.nextElement(); 
                fileName = multi.getFilesystemName(name); 
                String type = multi.getContentType(name); 
                File f = multi.getFile(name); 
                out.println("name: " + name); 
                out.println("fileName: " + fileName); 
                out.println("type: " + type); 

                if (f != null) { 
                    out.println("length: " + f.length()); 
                    out.println(); 
                } 

            } 

            Cookie param = null;

            if(fileName != null) {

                File f = new File(IDSSAppConstants.uploadDir,fileName);
                req.getSession().setAttribute(IDSSAppConstants.userGridFileName,f.toString());

                param = new Cookie(IDSSAppConstants.userGridFileName,f.toString());
                param.setPath("/");
                //param.setMaxAge(0);
                res.addCookie(param);

            } //if(fileName != null)


            out.println("</PRE>"); 



            out.println("<p>" + "Return to callingComponent : <a href="+callingComponent+">" + callingComponent + "</a></p>");

/**
            out.println("<p>" + "req.getSession().getAttribute() : " + (String)req.getSession().getAttribute(IDSSAppConstants.userGridFileName) + "</p>");
            out.println("<p>" + "param.getPath() : " + param.getPath() + "</p>");
            out.println("<p>" + "param.getName() : " + param.getName() + "</p>");
            out.println("<p>" + "param.getValue() : " + param.getValue() + "</p>");
**/




/**/


        } //try


        catch (Exception e) { 
            out.println("<PRE>"); 
            e.printStackTrace(out); 
            out.println("</PRE>"); 
        } 

        out.println("</BODY></HTML>"); 


    } //public void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException


} //public class UploadHandler extends HttpServlet


