1 2 package components.guestbook; 3 4 import java.io.BufferedReader ; 5 import java.io.File ; 6 import java.io.IOException ; 7 import java.io.PrintWriter ; 8 import java.util.Enumeration ; 9 import java.util.Hashtable ; 10 11 import javax.servlet.http.HttpServlet ; 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.http.HttpServletResponse ; 14 15 import org.jahia.params.ParamBean; 16 import org.jahia.utils.FileUtils; 17 18 19 public class GuestBookApp extends HttpServlet { 20 21 22 private String version = "1.5"; 23 24 private static final String ENTRIES_PATH = File.separator + "guestbook" + File.separator + "data" + File.separator; 25 private static final String ENTRIES_FILE = "entries.txt"; 26 private static final String TEMPLATE_PATH = File.separator + "guestbook" + File.separator + "templates" + File.separator; 27 private static final String TEMPLATE_FILE = "std.html"; 28 29 34 public void doGet( HttpServletRequest request, HttpServletResponse response ) throws IOException  35 { 36 PrintWriter out = response.getWriter(); 37 38 39 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 flushEntries( request, jParams ); 47 } 48 out.println(drawForm( request, response, jParams ) ); 49 } 51 52 53 58 public void doPost( HttpServletRequest request, HttpServletResponse response ) throws IOException  59 { 60 BufferedReader requestReader = null; 62 63 try { 64 requestReader = request.getReader(); 65 } catch (IOException ioe) { 66 } catch (IllegalStateException ise) { 68 } 70 71 if (requestReader == null) { 72 } else { 74 try { 75 requestReader.reset(); 76 } catch (IOException ioe) { 77 } 79 try { 80 int nc = requestReader.read(); 82 while (nc != -1) { 83 nc = requestReader.read(); 85 } 86 } catch (IOException ioe) { 88 } 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 } 100 101 102 107 public void flushEntries( HttpServletRequest request, ParamBean jParams ) 108 { 109 if (request.getParameter("gb_flush").equals("true")) { 110 String fileName = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE; 112 try { 113 FileUtils.getInstance().writeFile( fileName, " " ); 114 } catch (Exception e) { 115 } 117 } 118 } 120 121 122 127 public void checkEntriesFile( ParamBean jParams ) 128 { 129 String filePath = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH; 130 String fileName = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE; 131 File entriesPath = new File ( filePath ); 132 if (!entriesPath.exists()) { 133 entriesPath.mkdirs(); 134 } 135 File entriesFile = new File ( fileName ); 136 if (!entriesFile.exists()) { 137 try { 138 FileUtils.getInstance().writeFile( fileName, " " ); 139 } catch (Exception e) { 140 } 142 } 143 } 145 146 147 152 public void saveEntry( HttpServletRequest request, ParamBean jParams ) 153 { 154 String contents = ""; 155 String filePath = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE; 156 try { 157 contents = FileUtils.getInstance().readFile( filePath ); 158 } catch (Exception e) { 159 } 161 String comment = request.getParameter( "gb_comment" ); 162 String author = request.getParameter( "gb_author" ); 163 String 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 e) { 175 } 177 } 179 180 181 186 public String loadEntries( HttpServletRequest request, ParamBean jParams ) 187 { 188 String contents = ""; 189 String filePath = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE; 190 try { 191 contents = FileUtils.getInstance().readFile( filePath ); 192 } catch (Exception e) { 193 contents = "- Error in reading data file -"; 195 } 196 return contents; 197 } 199 200 201 206 public String drawForm( HttpServletRequest request, 207 HttpServletResponse response, 208 ParamBean jParams ) 209 { 210 String html = ""; 211 String entries = loadEntries( request, jParams ); 212 String commentField = "<textarea name=\"gb_comment\" rows=\"3\" cols=\"25\" wrap=\"soft\"></textarea>\n"; 213 String authorField = "<input type=\"text\" name=\"gb_author\" size=\"20\" maxlength=\"200\">\n"; 214 String submitField = "<input type=\"submit\" value=\"Add Entry\">\n"; 215 String filePath = jParams.settings().getComponentsDiskPath() + TEMPLATE_PATH + TEMPLATE_FILE; 216 217 String actionURL; 218 if (request.getContextPath().equals("/")) { 219 actionURL = request.getServletPath(); 220 } else { 221 actionURL = request.getContextPath() + request.getServletPath(); 222 } 223 224 String 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 tokens = new Hashtable (); 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 } 244 245 246 251 public String composeSource( String filePath, Hashtable tokens ) 252 { 253 String contents = ""; 254 try { 255 contents = FileUtils.getInstance().readFile( filePath ); 256 } catch (Exception e) { 257 contents = "- Error in reading template file - "; 259 return contents; 260 } 261 262 int pos = 0; 263 String theKey = ""; 264 Enumeration keys = tokens.keys(); 265 while (keys.hasMoreElements()) { 266 theKey = (String ) 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 } 278 279 280 } | Popular Tags |