KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > components > guestbook > GuestBookApp


1
2 package components.guestbook;
3
4 import java.io.BufferedReader JavaDoc;
5 import java.io.File JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.PrintWriter JavaDoc;
8 import java.util.Enumeration JavaDoc;
9 import java.util.Hashtable JavaDoc;
10
11 import javax.servlet.http.HttpServlet JavaDoc;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14
15 import org.jahia.params.ParamBean;
16 import org.jahia.utils.FileUtils;
17
18
19 public class GuestBookApp extends HttpServlet JavaDoc {
20
21
22     private String JavaDoc version = "1.5";
23
24     private static final String JavaDoc ENTRIES_PATH = File.separator + "guestbook" + File.separator + "data" + File.separator;
25     private static final String JavaDoc ENTRIES_FILE = "entries.txt";
26     private static final String JavaDoc TEMPLATE_PATH = File.separator + "guestbook" + File.separator + "templates" + File.separator;
27     private static final String JavaDoc TEMPLATE_FILE = "std.html";
28
29     /***
30         * doGet
31         * EV 10.12.2000
32         *
33         */

34     public void doGet( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response ) throws IOException JavaDoc
35     {
36         PrintWriter JavaDoc out = response.getWriter();
37
38
39         //System.out.println( "GuestBook App ver" + version + " responding to " + request.getRemoteAddr() + " GET METHOD ");
40
ParamBean jParams = (ParamBean) request.getAttribute( "org.jahia.params" );
41         if (jParams == null)
42         return;
43         checkEntriesFile( jParams );
44         if (request.getParameter("gb_flush") != null) {
45             //System.out.println("Guestbook. Flushing entries...");
46
flushEntries( request, jParams );
47         }
48         out.println(drawForm( request, response, jParams ) );
49     } // end doGet
50

51
52
53     /***
54         * doPost
55         * EV 10.12.2000
56         *
57         */

58     public void doPost( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response ) throws IOException JavaDoc
59     {
60         //System.out.println( "GuestBook App ver" + version + " responding to " + request.getRemoteAddr() + " POST METHOD ");
61
BufferedReader JavaDoc requestReader = null;
62
63         try {
64             requestReader = request.getReader();
65         } catch (IOException JavaDoc ioe) {
66         //System.out.println("Guestbook : IOException while trying to recuperate input stream");
67
} catch (IllegalStateException JavaDoc ise) {
68             //System.out.println("Guestbook : IllegalStateException means InputStream has already been called on the request body");
69
}
70
71             if (requestReader == null) {
72             //System.out.println("Guestbook : ERROR RETRIEVING INPUT STREAM !");
73
} else {
74             try {
75                 requestReader.reset();
76         } catch (IOException JavaDoc ioe) {
77             //System.out.println("Guestbook : IOException while resetting input stream");
78
}
79         try {
80                 //System.out.println("Guestbook : REQUEST BODY START ----- ");
81
int nc = requestReader.read();
82             while (nc != -1) {
83                     //System.out.print((char)nc);
84
nc = requestReader.read();
85             }
86                 //System.out.println("Guestbook : REQUEST BODY END ----- ");
87
} catch (IOException JavaDoc ioe) {
88             //System.out.println("Guestbook : IOException while reading body of request");
89
}
90
91         }
92
93         ParamBean jParams = (ParamBean) request.getAttribute( "org.jahia.params" );
94         if (jParams == null)
95         return;
96         saveEntry( request, jParams );
97         doGet( request, response );
98     } // end doPost
99

100
101
102     /***
103         * flushEntries
104         * EV 11.12.2000
105         *
106         */

107     public void flushEntries( HttpServletRequest JavaDoc request, ParamBean jParams )
108     {
109         if (request.getParameter("gb_flush").equals("true")) {
110             //System.out.println( "Flushing !!!! \n" ) ;
111
String JavaDoc fileName = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE;
112             try {
113                 FileUtils.getInstance().writeFile( fileName, " " );
114             } catch (Exception JavaDoc e) {
115                 //System.out.println( "GuestBook App > Error in trying to flush data file : " + e );
116
}
117         }
118     } // end flushEntries
119

120
121
122     /***
123         * checkEntriesFile
124         * EV 11.12.2000
125         *
126         */

127     public void checkEntriesFile( ParamBean jParams )
128     {
129         String JavaDoc filePath = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH;
130         String JavaDoc fileName = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE;
131         File JavaDoc entriesPath = new File JavaDoc( filePath );
132         if (!entriesPath.exists()) {
133             entriesPath.mkdirs();
134         }
135         File JavaDoc entriesFile = new File JavaDoc( fileName );
136         if (!entriesFile.exists()) {
137             try {
138                 FileUtils.getInstance().writeFile( fileName, " " );
139             } catch (Exception JavaDoc e) {
140                 //System.out.println( "GuestBook App > Error in trying to create data file : " + e );
141
}
142         }
143     } // end
144

145
146
147     /***
148         * saveEntry
149         * EV 10.12.2000
150         *
151         */

152     public void saveEntry( HttpServletRequest JavaDoc request, ParamBean jParams )
153     {
154         String JavaDoc contents = "";
155         String JavaDoc filePath = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE;
156         try {
157             contents = FileUtils.getInstance().readFile( filePath );
158         } catch (Exception JavaDoc e) {
159             //System.out.println( "GuestBook App > Error occured : " + e );
160
}
161         String JavaDoc comment = request.getParameter( "gb_comment" );
162         String JavaDoc author = request.getParameter( "gb_author" );
163         String JavaDoc line = "<table border=\"0\" width=\"100%\">\n";
164         line += "<tr><td width=\"100%\"><font face=\"arial\" size=\"2\" color=\"white\">\n";
165         line += comment + "\n";
166         line += "</font></td></tr>\n";
167         line += "<tr><td align=\"right\">\n";
168         line += "<font face=\"arial\" size=\"1\" color=\"white\"><i>" + author + "</i></font>\n";
169         line += "</td></tr>\n";
170         line += "<tr><td><hr noshade></td></tr></table>\n";
171         contents += line;
172         try {
173             FileUtils.getInstance().writeFile( filePath, contents );
174         } catch (Exception JavaDoc e) {
175             //System.out.println( "GuestBook App > Error occured : " + e );
176
}
177     } // end saveEntry
178

179
180
181     /***
182         * loadEntries
183         * EV 10.12.2000
184         *
185         */

186     public String JavaDoc loadEntries( HttpServletRequest JavaDoc request, ParamBean jParams )
187     {
188         String JavaDoc contents = "";
189         String JavaDoc filePath = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE;
190         try {
191             contents = FileUtils.getInstance().readFile( filePath );
192         } catch (Exception JavaDoc e) {
193             //System.out.println( "GuestBook App > Error occured : " + e );
194
contents = "- Error in reading data file -";
195         }
196         return contents;
197     } // end loadEntries
198

199
200
201     /***
202         * drawForm
203         * EV 10.12.2000
204         *
205         */

206     public String JavaDoc drawForm( HttpServletRequest JavaDoc request,
207                     HttpServletResponse JavaDoc response,
208                     ParamBean jParams )
209     {
210             String JavaDoc html = "";
211             String JavaDoc entries = loadEntries( request, jParams );
212             String JavaDoc commentField = "<textarea name=\"gb_comment\" rows=\"3\" cols=\"25\" wrap=\"soft\"></textarea>\n";
213             String JavaDoc authorField = "<input type=\"text\" name=\"gb_author\" size=\"20\" maxlength=\"200\">\n";
214             String JavaDoc submitField = "<input type=\"submit\" value=\"Add Entry\">\n";
215             String JavaDoc filePath = jParams.settings().getComponentsDiskPath() + TEMPLATE_PATH + TEMPLATE_FILE;
216
217             String JavaDoc actionURL;
218             if (request.getContextPath().equals("/")) {
219                 actionURL = request.getServletPath();
220             } else {
221             actionURL = request.getContextPath() + request.getServletPath();
222             }
223
224             String JavaDoc flushField = "";
225             if (jParams.getOperationMode() == ParamBean.EDIT) {
226                 int matrix = (int)(Math.random() * 10000);
227                 flushField += "<input type=\"button\" name=\"flush\" value=\"Flush Entries\" ";
228                 flushField += "onClick=\"location.href='" + response.encodeURL(actionURL + "?gb_flush=true&matrix=" + matrix ) + "'\">\n";
229             }
230
231             Hashtable JavaDoc tokens = new Hashtable JavaDoc();
232             tokens.put( "entries", entries );
233             tokens.put( "commentField", commentField );
234             tokens.put( "authorField", authorField );
235             tokens.put( "submitField", submitField );
236             tokens.put( "flushField", flushField );
237
238             html = "<form method=\"POST\" action=\"" + response.encodeURL(actionURL) + "\">\n";
239             html += composeSource( filePath, tokens );
240             html += "</form>\n";
241             return html;
242     } // end drawForm
243

244
245
246     /***
247         * composeSource
248         * EV 10.12.2000
249         *
250         */

251     public String JavaDoc composeSource( String JavaDoc filePath, Hashtable JavaDoc tokens )
252     {
253         String JavaDoc contents = "";
254         try {
255             contents = FileUtils.getInstance().readFile( filePath );
256         } catch (Exception JavaDoc e) {
257             //System.out.println( "GuestBookApp > Error occured : " + e );
258
contents = "- Error in reading template file - ";
259             return contents;
260         }
261
262         int pos = 0;
263         String JavaDoc theKey = "";
264         Enumeration JavaDoc keys = tokens.keys();
265         while (keys.hasMoreElements()) {
266             theKey = (String JavaDoc) keys.nextElement();
267             pos = contents.indexOf( "<!--" + theKey + "-->" );
268             if (pos > -1) {
269                 contents = contents.substring( 0, pos )
270                             + tokens.get(theKey)
271                             + contents.substring( pos, contents.length() );
272             }
273         }
274
275         return contents;
276     } // end composeSource
277

278
279
280 } // end GuestBookApp
281
Popular Tags