001package ca.bc.webarts.servlet;
002
003import com.oreilly.servlet.MultipartResponse;
004import com.oreilly.servlet.ServletUtils;
005import java.awt.*;
006import java.io.*;
007import javax.servlet.*;
008import javax.servlet.http.*;
009
010
011/**
012 *  A very simple servlet to push webcam images to a client.
013 *
014 * @author    tgutwin
015 */
016public class WebcamPush extends HttpServlet
017{
018
019  /**  The default location for the image file. */
020  final static String IMAGELOCATION = "/images/webcam/tvtime-screenshot.png";
021  /**  The instance location of the image file. */
022  String imageLocation = null;
023
024  /**
025   *  Initializes the servlet and loads some init data. The init method
026   *  is called once, automatically, by the network service each time it loads
027   *  the servlet. It is guaranteed to finish before any service requests are
028   *  accepted. <br>
029   *
030   * @param  config                servlet configuration information
031   * @exception  ServletException  if an error occured during initialization
032   */
033  public void init( ServletConfig config )  throws ServletException
034  {
035    super.init( config );// invoked on GenericServlet and saves a reference of
036    System.out.println("WebcamPush:init");
037    // the config for later use
038    imageLocation = getInitParameter( "imageLocation" );
039    System.out.println("WebcamPush:init:imageLocation="+imageLocation);
040
041    if ( imageLocation == null || imageLocation.equals( "" ) || !(new File(imageLocation)).exists() )
042    {
043      imageLocation = IMAGELOCATION;
044    }
045  }
046
047
048  /**
049   *  Initializes the servlet and loads some init data. The init method
050   *  is called once, automatically, by the network service each time it loads
051   *  the servlet. It is guaranteed to finish before any service requests are
052   *  accepted. <br>
053   *
054   */
055  public void init() 
056  {
057    System.out.println("WebcamPush:init()");
058    // the config for later use
059    imageLocation = getInitParameter( "imageLocation" );
060    System.out.println("WebcamPush:init:imageLocation="+imageLocation);
061
062    if ( imageLocation == null || imageLocation.equals( "" ) || !(new File(imageLocation)).exists() )
063    {
064      imageLocation = IMAGELOCATION;
065    }
066  }
067
068
069  /**
070   *  The main servlet processing method.
071   *
072   * @param  req                   Description of the Parameter
073   * @param  res                   Description of the Parameter
074   * @exception  ServletException  Description of the Exception
075   * @exception  IOException       Description of the Exception
076   */
077  public void doGet( HttpServletRequest req, HttpServletResponse res )
078    throws IOException, ServletException
079  {
080    System.out.println("WebcamPush:doGet");
081    ServletOutputStream out = res.getOutputStream();// some binary output
082    
083    res.setContentType("text/html");
084    out.print("<?xml version=\"1.0\"?>\n");
085    out.print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"DTD/xhtml1-strict.dtd\">\n\n");
086    out.print("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n");
087    out.print("  <head>\n");
088    out.print("    <title>Toms Webcam</title></head>\n");
089    out.print("    <body>\n      <h2>Toms Webcam</h2>\n");
090    out.print("      <br />\n");
091    out.print("      "+imageLocation+"<br />\n");
092    out.print("      <img src=\""+imageLocation+"\" width=\"640\" height=\"480\"/>");
093    out.print("    </body>\n</html>");
094  }
095  
096  
097  /**
098   *  The main servlet processing method.
099   *
100   * @param  req                   Description of the Parameter
101   * @param  res                   Description of the Parameter
102   * @exception  ServletException  Description of the Exception
103   * @exception  IOException       Description of the Exception
104   */
105  public void doGetImage( HttpServletRequest req, HttpServletResponse res )
106    throws IOException, ServletException
107  {
108    System.out.println("WebcamPush:doGetImage");
109    ServletOutputStream out = res.getOutputStream();// some binary output
110    
111    // set the response HTTP headers to reload this page 
112    res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
113    res.setHeader("Pragma", "no-cache");
114    res.setHeader("Expires","Thu, 19 Nov 1981 08:52:00 GMT");
115    // 
116    MultipartResponse multi = new MultipartResponse( res );
117      try
118      {
119        multi.startResponse( "image/png" );
120        ServletUtils.returnFile( req.getRealPath( imageLocation ), out );
121        multi.endResponse();
122      }
123      catch ( FileNotFoundException e )
124      {
125        multi.endResponse();
126        multi.startResponse( "text/plain" );
127        out.print("Could not find file: " + e.getMessage() );
128        multi.endResponse();
129        //throw new ServletException( "Could not find file: " + e.getMessage() );
130      }
131
132    // First send a countdown
133    /*
134    for ( int i = 0; i < 10; i++ )
135    {
136      try
137      {
138        multi.startResponse( "image/png" );
139        ServletUtils.returnFile( req.getRealPath( imageLocation ), out );
140        multi.endResponse();
141      }
142      catch ( FileNotFoundException e )
143      {
144        multi.endResponse();
145        multi.startResponse( "text/plain" );
146        out.print("Could not find file: " + e.getMessage() );
147        multi.endResponse();
148      }
149      try
150      {
151        Thread.sleep( 500 );
152      }
153      catch ( InterruptedException e )
154      {}
155    }
156    */
157
158    // Don't forget to end the multipart response
159    multi.finish();
160  }
161  
162  
163  /**
164   *  This method handles the "POST" submission.
165   *
166   * @param  req                   Description of Parameter
167   * @param  res                   Description of Parameter
168   * @exception  ServletException  Description of Exception
169   * @exception  IOException       Description of Exception
170   */
171  public void doPost(HttpServletRequest req, HttpServletResponse res)
172    throws ServletException, IOException
173  {
174    final String methodName = "doPost";
175    System.out.println("WebcamPush:"+methodName);
176    doGet(req,res);
177  }
178}
179        /*
180        multi.startResponse( "text/html" );
181        res.setContentType("text/html");
182        out.print("<?xml version=\"1.0\"?>\n");
183        out.print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"DTD/xhtml1-strict.dtd\">\n\n");
184        out.print("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n");
185        out.print("  <head>\n");
186        out.print("    <title>Toms Webcam</title></head>\n");
187        out.print("    <body>\n      <h2>Toms Webcam</h2>\n");
188        out.print("      <br />\n");
189        out.print("      "+imageLocation+"<br />\n");
190        out.print("      <img src=\""+imageLocation+"\" width=\"640\" height=\"480\"/>");
191        out.print("    </body>\n</html>");
192        */
193
194