| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.phoenix.templategen; |
|---|
| 34 | |
|---|
| 35 | import java.io.File; |
|---|
| 36 | import java.io.FileFilter; |
|---|
| 37 | import java.io.IOException; |
|---|
| 38 | import java.io.PrintWriter; |
|---|
| 39 | import java.util.HashMap; |
|---|
| 40 | import java.util.Iterator; |
|---|
| 41 | import java.util.Map; |
|---|
| 42 | |
|---|
| 43 | import javax.servlet.ServletException; |
|---|
| 44 | import javax.servlet.http.HttpServlet; |
|---|
| 45 | import javax.servlet.http.HttpServletRequest; |
|---|
| 46 | import javax.servlet.http.HttpServletResponse; |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * The Template Generator Servlet. |
|---|
| 50 | * |
|---|
| 51 | * @web.servlet name="templategen" |
|---|
| 52 | * @web.servlet-mapping url-pattern="/templategen" |
|---|
| 53 | * |
|---|
| 54 | * @author Albrecht Messner |
|---|
| 55 | */ |
|---|
| 56 | public class TemplateGeneratorServlet |
|---|
| 57 | extends HttpServlet |
|---|
| 58 | { |
|---|
| 59 | private static final long serialVersionUID = 1017987995368589070L; |
|---|
| 60 | private static final String TEMPLATE_DIR = "templates"; |
|---|
| 61 | |
|---|
| 62 | /** {@inheritDoc} */ |
|---|
| 63 | protected void doPost ( |
|---|
| 64 | HttpServletRequest request, |
|---|
| 65 | HttpServletResponse response) |
|---|
| 66 | throws ServletException, IOException |
|---|
| 67 | { |
|---|
| 68 | doService(request, response); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** {@inheritDoc} */ |
|---|
| 72 | protected void doGet ( |
|---|
| 73 | HttpServletRequest request, |
|---|
| 74 | HttpServletResponse response) |
|---|
| 75 | throws ServletException, IOException |
|---|
| 76 | { |
|---|
| 77 | doService(request, response); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** {@inheritDoc} */ |
|---|
| 81 | protected void doService ( |
|---|
| 82 | HttpServletRequest request, |
|---|
| 83 | HttpServletResponse response) |
|---|
| 84 | throws ServletException, IOException |
|---|
| 85 | { |
|---|
| 86 | final String action = request.getParameter("action"); |
|---|
| 87 | if (action == null) |
|---|
| 88 | { |
|---|
| 89 | // give a selection of all registered templates |
|---|
| 90 | listTemplates(response); |
|---|
| 91 | } |
|---|
| 92 | else if (action.equals("showform")) |
|---|
| 93 | { |
|---|
| 94 | // show the form for one template |
|---|
| 95 | showTemplateForm(request, response); |
|---|
| 96 | } |
|---|
| 97 | else if (action.equals("parametrize")) |
|---|
| 98 | { |
|---|
| 99 | // parametrize the current template |
|---|
| 100 | parametrizeTemplate(request, response); |
|---|
| 101 | } |
|---|
| 102 | else |
|---|
| 103 | { |
|---|
| 104 | // hey! unknown action! |
|---|
| 105 | writeError(response, |
|---|
| 106 | "Parameter 'action' has unknown value '" + action + "'"); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | private void writeError (HttpServletResponse response, String errorText) |
|---|
| 111 | throws IOException |
|---|
| 112 | { |
|---|
| 113 | response.setContentType("text/html"); |
|---|
| 114 | |
|---|
| 115 | final PrintWriter pw = new PrintWriter(response.getOutputStream()); |
|---|
| 116 | pw.println("<HTML><HEAD><TITLE>Error</TITLE></HEAD>"); |
|---|
| 117 | pw.println("<BODY><H1>Error:</H1>"); |
|---|
| 118 | pw.println(errorText); |
|---|
| 119 | pw.println("</BODY></HTML>"); |
|---|
| 120 | pw.flush(); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | private void parametrizeTemplate ( |
|---|
| 124 | HttpServletRequest request, |
|---|
| 125 | HttpServletResponse response) throws ServletException, IOException |
|---|
| 126 | { |
|---|
| 127 | final TemplateGenerator templateGen = getTemplateGenerator(request); |
|---|
| 128 | final String template = request.getParameter("template"); |
|---|
| 129 | |
|---|
| 130 | final PrintWriter pw = new PrintWriter(response.getOutputStream()); |
|---|
| 131 | pw.println("Parametrizing template " + template); |
|---|
| 132 | |
|---|
| 133 | final Map paramMap = new HashMap(); |
|---|
| 134 | for (final Iterator it = templateGen.getParameterList().iterator(); |
|---|
| 135 | it.hasNext(); ) |
|---|
| 136 | { |
|---|
| 137 | final Parameter param = (Parameter) it.next(); |
|---|
| 138 | final String paramValue = request.getParameter(param.getName()); |
|---|
| 139 | if (paramValue == null || paramValue.length() == 0) |
|---|
| 140 | { |
|---|
| 141 | writeError(response, "ERROR: value for parameter " |
|---|
| 142 | + param.getName() + " missing"); |
|---|
| 143 | return; |
|---|
| 144 | } |
|---|
| 145 | else |
|---|
| 146 | { |
|---|
| 147 | paramMap.put(param.getName(), paramValue); |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | try |
|---|
| 152 | { |
|---|
| 153 | final byte[] templateZip = templateGen.parametrizeTemplates(paramMap); |
|---|
| 154 | response.setContentType("application/zip"); |
|---|
| 155 | response.setHeader("Content-Disposition", |
|---|
| 156 | "attachment; filename=\"" + template + ".zip\""); |
|---|
| 157 | response.setContentLength(templateZip.length); |
|---|
| 158 | response.getOutputStream().write(templateZip); |
|---|
| 159 | response.getOutputStream().flush(); |
|---|
| 160 | } |
|---|
| 161 | catch (TemplateGeneratorException e) |
|---|
| 162 | { |
|---|
| 163 | throw new ServletException("Template Generator Error", e); |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | private void showTemplateForm ( |
|---|
| 168 | HttpServletRequest request, |
|---|
| 169 | HttpServletResponse response) throws IOException, ServletException |
|---|
| 170 | { |
|---|
| 171 | |
|---|
| 172 | final TemplateGenerator templateGen = getTemplateGenerator(request); |
|---|
| 173 | final String template = request.getParameter("template"); |
|---|
| 174 | |
|---|
| 175 | response.setContentType("text/html"); |
|---|
| 176 | final PrintWriter pw = new PrintWriter(response.getOutputStream()); |
|---|
| 177 | pw.println("<HTML><HEAD><TITLE>" + template + " Template Form" |
|---|
| 178 | + "</TITLE></HEAD>"); |
|---|
| 179 | pw.println("<BODY>"); |
|---|
| 180 | pw.println("<H1>Template " + template + "</H1>"); |
|---|
| 181 | pw.println("<P>" + templateGen.getTemplateDescription() + "</P>"); |
|---|
| 182 | |
|---|
| 183 | pw.println("<FORM ACTION='templategen?action=parametrize&template=" |
|---|
| 184 | + template + "' " |
|---|
| 185 | + "METHOD='POST'>"); |
|---|
| 186 | pw.println("<table border='0'>"); |
|---|
| 187 | pw.println("<tr><th>Parameter</th><th>Value</th>" |
|---|
| 188 | + "<th>Description</th></tr>"); |
|---|
| 189 | |
|---|
| 190 | for (final Iterator it = templateGen.getParameterList().iterator(); |
|---|
| 191 | it.hasNext(); ) |
|---|
| 192 | { |
|---|
| 193 | final Parameter param = (Parameter) it.next(); |
|---|
| 194 | |
|---|
| 195 | pw.println("<tr>"); |
|---|
| 196 | pw.println("<td>" + param.getName() + "</td>"); |
|---|
| 197 | pw.println("<td>"); |
|---|
| 198 | |
|---|
| 199 | if (param.isMultiLine()) |
|---|
| 200 | { |
|---|
| 201 | pw.print("<textarea name='" + param.getName() + "'"); |
|---|
| 202 | pw.print(" rows='5' cols='30'>"); |
|---|
| 203 | if (param.getDefaultValue() != null) |
|---|
| 204 | { |
|---|
| 205 | pw.print(param.getDefaultValue()); |
|---|
| 206 | } |
|---|
| 207 | pw.print("</textarea>"); |
|---|
| 208 | } |
|---|
| 209 | else |
|---|
| 210 | { |
|---|
| 211 | pw.print("<input name='" + param.getName() + "'"); |
|---|
| 212 | pw.print(" size='30'"); |
|---|
| 213 | if (param.getDefaultValue() != null) |
|---|
| 214 | { |
|---|
| 215 | pw.print(" value='" + param.getDefaultValue() + "'"); |
|---|
| 216 | } |
|---|
| 217 | pw.print(" maxlength='" + param.getMaxLength() + "'>"); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | pw.println("</td>"); |
|---|
| 221 | pw.println("<td>" + param.getDescription() + "</td>"); |
|---|
| 222 | pw.println("<tr>"); |
|---|
| 223 | } |
|---|
| 224 | pw.println("</table>"); |
|---|
| 225 | pw.println("<INPUT TYPE='submit' VALUE='Do it'>"); |
|---|
| 226 | pw.println("</FORM>"); |
|---|
| 227 | |
|---|
| 228 | pw.println("</BODY>"); |
|---|
| 229 | pw.println("</HTML>"); |
|---|
| 230 | pw.flush(); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | private void listTemplates (HttpServletResponse response) |
|---|
| 234 | throws IOException, ServletException |
|---|
| 235 | { |
|---|
| 236 | response.setContentType("text/html"); |
|---|
| 237 | |
|---|
| 238 | final File templateDir = getTemplateDirectory(); |
|---|
| 239 | |
|---|
| 240 | final FileFilter templateFilter = new FileFilter() |
|---|
| 241 | { |
|---|
| 242 | public boolean accept (File file) |
|---|
| 243 | { |
|---|
| 244 | return file.getName().endsWith(".zip"); |
|---|
| 245 | } |
|---|
| 246 | }; |
|---|
| 247 | final File[] templates = templateDir.listFiles(templateFilter); |
|---|
| 248 | |
|---|
| 249 | final PrintWriter pw = new PrintWriter(response.getOutputStream()); |
|---|
| 250 | pw.println("<HTML><HEAD><TITLE>Template List</TITLE></HEAD>"); |
|---|
| 251 | pw.println("<BODY>"); |
|---|
| 252 | if (templates != null && templates.length > 0) |
|---|
| 253 | { |
|---|
| 254 | pw.println("<Table width='60%'><tr><th>Template Name</th>" |
|---|
| 255 | + "<th>Description</th></tr>"); |
|---|
| 256 | for (int i = 0; i < templates.length; i++) |
|---|
| 257 | { |
|---|
| 258 | pw.println("<tr>"); |
|---|
| 259 | final String template = templates[i].getName(); |
|---|
| 260 | final String baseName |
|---|
| 261 | = template.substring( |
|---|
| 262 | 0, template.length() - ".zip".length()); |
|---|
| 263 | |
|---|
| 264 | pw.print("<td><a href='templategen?action=showform&template=" |
|---|
| 265 | + baseName + "'>"); |
|---|
| 266 | pw.print(baseName); |
|---|
| 267 | pw.println("</a></td>"); |
|---|
| 268 | |
|---|
| 269 | final TemplateZip tz |
|---|
| 270 | = new TemplateZip(templates[i].getAbsolutePath()); |
|---|
| 271 | try |
|---|
| 272 | { |
|---|
| 273 | tz.readTemplateFile(); |
|---|
| 274 | } |
|---|
| 275 | catch (Exception e) |
|---|
| 276 | { |
|---|
| 277 | throw new ServletException("Failed to read template file " |
|---|
| 278 | + templates[i], e); |
|---|
| 279 | } |
|---|
| 280 | pw.println("<td>"); |
|---|
| 281 | pw.println(tz.getDescription().getDescription()); |
|---|
| 282 | pw.println("<td>"); |
|---|
| 283 | pw.println("</tr>"); |
|---|
| 284 | } |
|---|
| 285 | pw.println("</table>"); |
|---|
| 286 | } |
|---|
| 287 | else |
|---|
| 288 | { |
|---|
| 289 | pw.println("No templates found."); |
|---|
| 290 | } |
|---|
| 291 | pw.println("</BODY></HTML>"); |
|---|
| 292 | pw.flush(); |
|---|
| 293 | |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | private File getTemplateDirectory () |
|---|
| 297 | { |
|---|
| 298 | final String realPath = getServletContext().getRealPath("/"); |
|---|
| 299 | final File baseDir = new File(realPath); |
|---|
| 300 | final File templateDir = new File(baseDir, TEMPLATE_DIR); |
|---|
| 301 | return templateDir; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | private TemplateGenerator getTemplateGenerator (HttpServletRequest request) |
|---|
| 305 | throws ServletException |
|---|
| 306 | { |
|---|
| 307 | final String template = request.getParameter("template"); |
|---|
| 308 | if (template == null) |
|---|
| 309 | { |
|---|
| 310 | throw new ServletException("Parameter 'template' missing"); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | final String templateFileName = template + ".zip"; |
|---|
| 314 | final File templateFile |
|---|
| 315 | = new File(getTemplateDirectory(), templateFileName); |
|---|
| 316 | |
|---|
| 317 | if (! templateFile.exists()) |
|---|
| 318 | { |
|---|
| 319 | throw new ServletException("Template file " + templateFileName |
|---|
| 320 | + " does not exist"); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | TemplateGenerator templateGen; |
|---|
| 324 | try |
|---|
| 325 | { |
|---|
| 326 | templateGen = new TemplateGenerator(templateFile.getAbsolutePath()); |
|---|
| 327 | } |
|---|
| 328 | catch (Exception e) |
|---|
| 329 | { |
|---|
| 330 | throw new ServletException("Failed to open template file", e); |
|---|
| 331 | } |
|---|
| 332 | return templateGen; |
|---|
| 333 | } |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|