001package bradleyross.j2ee.servlets;
002import java.io.*;
003import javax.servlet.*;
004import javax.servlet.http.*;
005//
006/** 
007* Sample program to get listing of files in directory (Needs work
008* to become more generic).
009*/
010class FileSample extends HttpServlet
011    {
012    /**
013         * 
014         */
015        private static final long serialVersionUID = 1L;
016        //
017    private static void directory (String directory)
018       {
019       int rowNumber;
020       String prefix = " ";
021       String testPrefix = " ";
022       String fullname;
023       String latestName = " ";
024       File listing = new File(directory);
025           String[] fileList;
026           System.out.println("Checking for " + directory);
027           if (listing.exists())
028              { 
029                  System.out.println("Directory exists");
030                  fileList = listing.list();
031                  System.out.println(String.valueOf(fileList.length) + " entries"); 
032                  }
033           else 
034              { 
035                  System.out.println("Directory does not exist"); 
036                  return;
037                  }
038       // Page header
039       //
040       System.out.println("<html><head>");
041       System.out.print("<title>Directory of ");
042       System.out.print(directory);
043       System.out.println("</title>");
044       System.out.println("</head>");
045       //
046       System.out.println("<body>");
047       System.out.print("<p>This is a list of reports in the ");
048       System.out.print(directory);
049       System.out.println(" directory.  The latest version is shown");
050       System.out.println("in the third column.  Click on the version name ");
051       System.out.println("to get the latest version.  To get a list of all");
052       System.out.println("versions, push the button for the report marked");
053       System.out.println("<b>Archive</b>.");
054       System.out.println("</p>");
055       System.out.println("<table border>");
056       //
057       // Print report header
058       System.out.print("<tr>");
059       System.out.print("<th></th>");
060       System.out.print("<th>Report name</th>");
061       System.out.print("<th>Click for latest version</th>");
062       System.out.println("<th>Click for list of archived versions</th>");
063       System.out.println("</tr>");
064       //
065       rowNumber = 1;
066       for (int i = 0; i < fileList.length; i++)
067          { // start of loop
068          fullname = fileList[i];
069          if (!fullname.startsWith(".")) // If block A
070             {
071                         if (fullname.indexOf(".") < 0)
072                            {
073                                System.out.println(fullname + " does not contain period");
074                                continue;
075                                }
076             testPrefix = fullname.substring(0,fullname.indexOf("."));
077             //
078             if (prefix.compareTo(testPrefix) != 0) // If block B
079                {
080                if (!prefix.equals(" ")) // If block C
081                   {
082                   System.out.println("<!-- *****  *****  ***** -->");
083                   System.out.println("<tr>");
084                   //
085                   // Sequence number
086                   System.out.print("<td align=\"right\">");
087                   System.out.print(rowNumber);
088                   System.out.println("</td>");
089                   //
090                   // Name of report
091                   System.out.print("<td>");
092                   System.out.print(latestName.substring(0,latestName.indexOf(".")));
093                   System.out.println("</td>");
094                   //
095                   // Name of report version
096                   // The button to select the latest version will go here
097                   System.out.print("<td>");
098                   System.out.print(latestName.substring(latestName.indexOf(".")+1));
099                   System.out.println("</td>");
100                   //
101                   // Button to get list of archived versions will go here
102                   System.out.print("<td>");
103                   System.out.print("<b>Archive</b>");
104                   System.out.println("</td>");
105                   //
106                   latestName = " ";
107                   rowNumber = rowNumber + 1;
108                   //
109                   System.out.println("</tr>");
110                   //
111                   } // end of If block C
112                prefix = testPrefix;
113                } // End of If block B
114             //
115             if (fullname.compareTo(latestName) > 0)
116                {
117                latestName = fullname;
118                }
119             } // end of If block A
120          } // end of loop
121       if (!prefix.equals(" "))
122          {
123                   System.out.println("<!-- *****  *****  ***** -->");
124                   System.out.println("<!-- Final pass -->");
125                   System.out.println("<tr>");
126                   //
127                   // Sequence number
128                   System.out.print("<td>");
129                   System.out.print(rowNumber);
130                   System.out.println("</td>");
131                   //
132                   // Name of report
133                   System.out.print("<td>");
134                   System.out.print(latestName.substring(0,latestName.indexOf(".")));
135                   System.out.println("</td>");
136                   //
137                   // Name of latest report version
138                   // The button to select the latest version will go here
139                   System.out.print("<td>");
140                   System.out.print(latestName.substring(latestName.indexOf(".")+1));
141                   System.out.println("</td>");
142                   //
143                   // Button to get list of archived versions will go here
144                   System.out.print("<td>");
145                   System.out.print("<b>Archive</b>");
146                   System.out.println("</td>");
147                   //
148                   //
149                   System.out.println("</tr>");
150          }
151       System.out.println("</table>");
152       System.out.println("</body>");
153       System.out.println("</html>");
154       // Page trailer
155       }
156    public static void main(String args[])
157       {
158       // directory("/home/bross/mrs_rpt/dailyrd");
159           String temp = "/Users/bradleyross/repository/dailyrd";
160           if (args.length > 0)
161              { temp = args[0]; }
162       directory(temp);
163       }
164    public void init(ServletConfig config) throws ServletException
165       {
166       super.init(config);
167       }
168    public void destroy()
169       {
170       super.destroy();
171       }
172    public void service (HttpServletRequest req,
173           HttpServletResponse res) throws IOException
174       {
175       PrintWriter output;
176       res.setContentType ("text/html");
177       output = res.getWriter();
178       output.println("<html><head>");
179       output.println("</head>");
180       output.println("<body>");
181       output.println("<p>Test message</p>");
182       output.println("</body></html>");
183       }
184    //
185    }