1 23 24 package org.dbforms.servlets.reports; 25 26 27 import org.apache.commons.lang.StringUtils; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.dbforms.config.DbFormsConfig; 32 import org.dbforms.config.DbFormsConfigRegistry; 33 import org.dbforms.config.ResultSetVector; 34 import org.dbforms.config.Table; 35 36 import org.dbforms.event.WebEvent; 37 38 import org.dbforms.taglib.DbFormTag; 39 40 import org.dbforms.util.MessageResourcesInternal; 41 import org.dbforms.util.PageContextBuffer; 42 import org.dbforms.util.ParseUtil; 43 import org.dbforms.util.Util; 44 import org.dbforms.util.external.FileUtil; 45 46 import java.io.IOException ; 47 import java.io.PrintWriter ; 48 49 import java.util.Collection ; 50 import java.util.Iterator ; 51 import java.util.Vector ; 52 53 import javax.servlet.ServletContext ; 54 import javax.servlet.ServletException ; 55 import javax.servlet.ServletOutputStream ; 56 import javax.servlet.http.HttpServlet ; 57 import javax.servlet.http.HttpServletRequest ; 58 import javax.servlet.http.HttpServletResponse ; 59 import javax.servlet.jsp.PageContext ; 60 61 66 public abstract class ReportServletAbstract extends HttpServlet { 67 68 private static Log logCat = LogFactory.getLog(ReportServletAbstract.class 69 .getName()); 70 71 private static final String REPORTCONFIGDIR = "reportdirs"; 72 73 private String [] reportdirs; 74 75 88 public final void doGet(HttpServletRequest request, 89 HttpServletResponse response) throws ServletException , IOException { 90 process(request, response); 91 } 92 93 106 public final void doPost(HttpServletRequest request, 107 HttpServletResponse response) throws ServletException , IOException { 108 process(request, response); 109 } 110 111 117 public void init() throws ServletException { 118 String value = getServletConfig().getInitParameter(REPORTCONFIGDIR); 119 if (value == null) { 120 value = "WEB-INF/reports/"; 121 } 122 reportdirs = StringUtils.split(value, ','); 123 } 124 125 142 protected abstract ReportWriter processReport(String reportFileFullName, 143 JRDataSourceAbstract dataSource, ServletContext context, 144 HttpServletRequest request, HttpServletResponse response); 145 146 protected abstract String getReportFileExtension(); 147 148 protected final void handleException(HttpServletRequest request, 149 HttpServletResponse response, Exception e) { 150 sendErrorMessage(request, response, MessageResourcesInternal 151 .getMessage("dbforms.reports.exception", request.getLocale(), 152 new String [] { e.getMessage() })); 153 } 154 155 private final void handleNoData(HttpServletRequest request, 156 HttpServletResponse response) { 157 sendErrorMessage(request, response, MessageResourcesInternal 158 .getMessage("dbforms.reports.nodata", request.getLocale())); 159 } 160 161 private final void sendErrorMessage(HttpServletRequest request, 162 HttpServletResponse response, String message) { 163 try { 164 Vector errors = (Vector) request.getAttribute("errors"); 165 errors.add(new Exception (message)); 166 167 String fue = ParseUtil.getParameter(request, "source"); 168 String contextPath = request.getContextPath(); 169 170 if (!Util.isNull(fue)) { 171 fue = fue.substring(contextPath.length()); 172 } 173 174 if (Util.isNull(fue)) { 175 sendErrorMessageText(response, message); 176 } else { 177 request.getRequestDispatcher(fue).forward(request, response); 178 } 179 } catch (Exception ex) { 180 logCat.error("sendErrorMessage", ex); 181 sendErrorMessageText(response, message); 182 } 183 } 184 185 protected void compileReport(ServletContext context, 186 String reportFile) throws Exception { 187 }; 188 189 197 protected void process(HttpServletRequest request, 198 HttpServletResponse response) { 199 try { 201 String reportFile = getReportFileFullName(request.getPathInfo(), 202 getServletContext(), request, response); 203 204 if (!Util.isNull(reportFile)) { 205 compileReport(getServletContext(), reportFile); 206 JRDataSourceAbstract dataSource = getDataForReport(request, response); 207 if (!response.isCommitted()) { 208 if (dataSource == null) { 209 handleNoData(request, response); 210 } else { 211 ReportWriter res = processReport(reportFile, 212 dataSource, getServletContext(), request, 213 response); 214 if (!response.isCommitted()) { 215 if (res == null) { 216 handleNoOutput(request, response); 217 } else { 218 processOutput(res, request, response); 219 } 220 } 221 } 222 } 223 } 224 } catch (Exception e) { 225 logCat.error("process", e); 226 handleException(request, response, e); 227 } 228 } 229 230 private void processOutput(ReportWriter out, HttpServletRequest request, 231 HttpServletResponse response) throws Exception { 232 if ((out.data == null) && (out.data.size() == 0)) { 233 handleNoOutput(request, response); 234 return; 235 } 236 response.setHeader("Expires", "0"); 237 response.setHeader("Cache-Control", 238 "must-revalidate, post-check=0, pre-check=0"); 239 response.setHeader("Pragma", "public"); 240 if (!Util.isNull(out.fileName)) { 241 response.setHeader("Content-disposition", "inline; filename=" 242 + out.fileName); 243 } 244 if (!Util.isNull(out.mimeType)) { 245 response.setContentType(out.mimeType); 246 } 247 response.setContentLength(out.data.size()); 249 ServletOutputStream outputStream = response.getOutputStream(); 250 try { 251 out.data.writeTo(outputStream); 252 outputStream.flush(); 253 } finally { 254 outputStream.close(); 255 } 256 } 257 258 private String getReportFileFullName(String reportFileName, 259 ServletContext context, HttpServletRequest request, 260 HttpServletResponse response) { 261 String reportFile = null; 262 263 try { 264 boolean found = false; 265 for (int i = 0; i < reportdirs.length; i++) { 266 reportFile = context 267 .getRealPath(reportdirs[i] + reportFileName) ; 268 String testName = reportFile + getReportFileExtension(); 269 if (FileUtil.fileExists(testName)) { 270 found = true; 271 break; 272 } 273 } 274 if (!found) { 275 handleNoReport(request, response); 276 reportFile = null; 277 } 278 } catch (Exception e) { 279 handleException(request, response, e); 280 } 281 282 return reportFile; 283 } 284 285 private void handleNoReport(HttpServletRequest request, 286 HttpServletResponse response) { 287 sendErrorMessage(request, response, MessageResourcesInternal 288 .getMessage("dbforms.reports.noreport", request.getLocale(), 289 new String [] { request.getPathInfo() })); 290 } 291 292 305 private JRDataSourceAbstract getDataForReport(HttpServletRequest request, HttpServletResponse response) { 306 JRDataSourceAbstract dataSource = null; 307 Table table = null; 308 ResultSetVector rsv = null; 309 310 try { 311 Object input = request.getAttribute("jasper.input"); 313 if (input == null) 314 input = request.getSession().getAttribute("jasper.input"); 315 if ((input != null) && (input instanceof Collection )) { 316 Iterator iter = ((Collection ) input).iterator(); 317 dataSource = new JRDataSourceIter(null, iter); 318 return dataSource; 319 } 320 321 rsv = (ResultSetVector) request.getAttribute("jasper.rsv"); 323 if (rsv == null) 324 rsv = (ResultSetVector) request.getSession().getAttribute("jasper.rsv"); 325 if (rsv != null) { 326 logCat.info("get resultsetvector rsv= " + rsv.size()); 327 if (rsv.size() == 0) { 328 handleNoData(request, response); 329 } else { 330 dataSource = new JRDataSourceRSV(rsv.getAttributes(), rsv); 331 } 332 return dataSource; 333 } 334 335 PageContext pageContext = new PageContextBuffer(); 336 pageContext 337 .initialize(this, request, response, null, true, 0, true); 338 339 WebEvent webEvent = (WebEvent) request.getAttribute("webEvent"); 341 table = webEvent.getTable(); 342 343 if (table == null) { 344 logCat.error("table==null"); 345 } 346 347 if ((webEvent != null) && (table != null) && (table.getId() != -1)) { 348 DbFormsConfig config = null; 350 351 try { 352 config = DbFormsConfigRegistry.instance().lookup(); 353 } catch (Exception e) { 354 logCat.error(e); 355 throw new ServletException (e); 356 } 357 358 String tableName = config.getTable(webEvent.getTable().getId()) 359 .getName(); 360 361 DbFormTag form = new DbFormTag(); 363 form.setPageContext(pageContext); 364 form.setTableName(tableName); 365 366 String maxRows = ParseUtil 367 .getParameter(request, "MaxRows", "*"); 368 form.setMaxRows(maxRows); 369 form.setFollowUp(""); 370 form.setAutoUpdate("false"); 371 372 String saveSource = (String ) request.getAttribute("source"); 378 String refSource = request.getRequestURI(); 379 380 if (request.getQueryString() != null) { 381 refSource += ("?" + request.getQueryString()); 382 } 383 384 request.setAttribute("source", refSource); 385 form.doStartTag(); 386 request.setAttribute("source", saveSource); 387 388 rsv = form.getResultSetVector(); 389 logCat.info("get resultsetvector rsv= " + rsv.size()); 390 391 if (rsv.size() == 0) { 392 handleNoData(request, response); 393 } else { 394 dataSource = new JRDataSourceRSV(rsv.getAttributes(), rsv); 395 } 396 397 form.doFinally(); 398 } 399 } catch (Exception e) { 400 logCat.error(e); 401 } 402 403 return dataSource; 404 } 405 406 private void sendErrorMessageText(HttpServletResponse response, 407 String message) { 408 try { 409 PrintWriter out = response.getWriter(); 410 response.setContentType("text/html"); 411 out.println("<html><body><h1>ERROR</h1><p>"); 412 out.println(message); 413 out.println("</p></body></html>"); 414 out.flush(); 415 out.close(); 416 } catch (IOException ioe2) { 417 logCat.error("sendErrorMessageText", ioe2); 418 } 419 } 420 421 private void handleNoOutput(HttpServletRequest request, 422 HttpServletResponse response) { 423 sendErrorMessage(request, response, MessageResourcesInternal 424 .getMessage("dbforms.reports.nooutput", request.getLocale())); 425 } 426 427 } 428 | Popular Tags |