FileDocCategorySizeDatePackage
ViewFileCompress.javaAPI DocExample2091Tue Feb 08 11:31:02 GMT 2000None

ViewFileCompress

public class ViewFileCompress extends HttpServlet

Fields Summary
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)


    OutputStream out = null;

    // Select the appropriate content encoding based on the
    // client's Accept-Encoding header.  Choose GZIP if the header 
    // includes "gzip".  Choose ZIP if the header includes "compress".  
    // Choose no compression otherwise.
    String encodings = req.getHeader("Accept-Encoding");
    if (encodings != null && encodings.indexOf("gzip") != -1) {
      // Go with GZIP
      res.setHeader("Content-Encoding", "gzip");
      out = new GZIPOutputStream(res.getOutputStream());
    }
    else if (encodings != null && encodings.indexOf("compress") != -1) {
      // Go with ZIP
      res.setHeader("Content-Encoding", "x-compress");
      out = new ZipOutputStream(res.getOutputStream());
      ((ZipOutputStream)out).putNextEntry(new ZipEntry("dummy name"));
    }
    else {
      // No compression
      out = res.getOutputStream();
    }
    res.setHeader("Vary", "Accept-Encoding");

    // Get the file to view
    String file = req.getPathTranslated();

    // No file, nothing to view
    if (file == null) {
      file = getServletContext().getRealPath("/index.html");
    }

    // Get and set the type of the file
    String contentType = getServletContext().getMimeType(file);
    res.setContentType(contentType);

    // Return the file
    try {
      ServletUtils.returnFile(file, out);
    }
    catch (FileNotFoundException e) { 
      res.sendError(res.SC_NOT_FOUND);
      return;
    }
    catch (IOException e) {
      getServletContext().log(e, "Problem sending file");
      res.sendError(res.SC_INTERNAL_SERVER_ERROR,
                    ServletUtils.getStackTraceAsString(e));
    }

    // Write the compression trailer and close the output stream
    out.close();