package com.vistech.net;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ImageManipControllerServlet extends HttpServlet {

   public void doGet(HttpServletRequest req, HttpServletResponse res)
                  throws ServletException{
      try {
        String command = req.getParameter("command");
        if(command.equalsIgnoreCase("listfiles")){
           String imagepath = ".";
           HttpSession session = req.getSession(false);
           if(session != null) imagepath= (String)session.getValue("imagepath");
           String fileList[] = listFiles(imagepath);
           PrintWriter out = res.getWriter();
           if(fileList != null) System.out.println(" 1" +fileList.length);
           for(int i=0;i<fileList.length;i++) {
               out.println(fileList[i]);
           }
           return;
        }
       if(command.equalsIgnoreCase("manip")){
           ServletContext sc =  getServletContext();
           RequestDispatcher 
           rd = 
           sc.getRequestDispatcher("/jsp/imageviewer/JAIImageManip1.jsp");
           rd.forward(req, res);
           return;
        }
        if(command.equalsIgnoreCase("edgedetect")){
           ServletContext sc =  getServletContext();
           RequestDispatcher rd = 
              sc.getRequestDispatcher("/jsp/imageviewer/JAIEdgeDetect.jsp");
           rd.forward(req, res);
           return;
        }
     } catch(Exception e) {System.out.println(e);}
   }

   public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException {
        doGet(request, response);
   }
   public synchronized String[] listFiles(String dirstr) {
      String fl[] = null;
      try {
         File dir = new File(dirstr);
         fl = dir.list();
      } catch (Exception e){System.out.println(e);}
      return fl;
   }
}
