package net.happygiraffe.tclogview;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * A viewer for tomcat log files.
 * 
 * @author dom
 */
public class TomcatLogViewer extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setAttribute("contextPath", req.getContextPath());

        FileBean logDir = getLogDir();
        req.setAttribute("logDir", logDir);

        String servletPath = req.getServletPath();
        if ("/".equals(servletPath)) {
            serveIndexPage(logDir, req, resp);
        } else {
            final File logFile = getLogFile(logDir, servletPath.substring(1));
            serveLogFile(logFile, req, resp);
        }
    }

    private FileBean getLogDir() {
        return new FileBean(System.getProperty("catalina.base"), "logs");
    }

    private File getLogFile(FileBean logDir, String fileName) {
        File file = new File(logDir.getFile(), fileName);
        return file;
    }

    private List<FileBean> getLogFiles(FileBean logDir) {
        List<FileBean> logFiles = logDir.listFiles();
        // Sort in reverse date order.
        Collections.sort(logFiles, new Comparator<FileBean>() {

            public int compare(FileBean f1, FileBean f2) {
                return f2.getLastModified().compareTo(f1.getLastModified());
            }
        });
        return logFiles;
    }

    private void serveIndexPage(FileBean logDir, HttpServletRequest req,
            HttpServletResponse resp) throws ServletException, IOException {
        List<FileBean> logFiles = getLogFiles(logDir);
        req.setAttribute("logFiles", logFiles);
        req.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(req, resp);
    }

    private void serveLogFile(File file,
            HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/plain");
        resp.setCharacterEncoding("UTF-8");
        PrintWriter w = resp.getWriter();
        InputStreamReader isr = new InputStreamReader(new BufferedInputStream(
                new FileInputStream(file)), "UTF-8");
        int c;
        while ((c = isr.read()) != -1) {
            w.print((char)c);
        }
    }
}

@@ -876,85 +876,23 @@
     
-String servletPath = req.getServletPath();%0A        if (%22/%22.equals(servletPath
+if (isIndex(req
 )) %7B
@@ -972,29 +972,21 @@
     
-final File l
+serveL
 ogFile
- = 
+(
 getL
@@ -996,86 +996,268 @@
 ile(
-logDir, servletPath.substring(1));%0A            serveLogFile(logFile, req, resp
+req), req, resp);%0A        %7D%0A    %7D%0A%0A    @Override%0A    protected long getLastModified(HttpServletRequest req) %7B%0A        if (isIndex(req)) %7B%0A            return getLogDir().getFile().lastModified();%0A        %7D else %7B%0A            return getLogFile(req).lastModified(
 );%0A 
@@ -1420,40 +1420,30 @@
 ile(
-FileBean logDir, String fileName
+HttpServletRequest req
 ) %7B%0A
@@ -1454,19 +1454,14 @@
     
-File file =
+return
  new
@@ -1466,22 +1466,27 @@
 ew File(
-l
+getL
 ogDir
+()
 .getFile
@@ -1493,38 +1493,33 @@
 (), 
-fileName);%0A        return file
+getRequestedLogFile(req))
 ;%0A  
@@ -1925,32 +1925,264 @@
 ogFiles;%0A    %7D%0A%0A
+    private String getRequestedLogFile(HttpServletRequest req) %7B%0A        return req.getServletPath().substring(1);%0A    %7D%0A%0A    private boolean isIndex(HttpServletRequest req) %7B%0A        return %22/%22.equals(req.getServletPath());%0A    %7D%0A%0A
     private void
@@ -2972,16 +2972,17 @@
 t((char)
+ 
 c);%0A    

Set the Last-Modified header.