1 package org.faceless.report; 4 5 import java.util.*; 6 import java.io.*; 7 import java.net.*; 8 import javax.servlet.*; 9 import javax.servlet.http.*; 10 import org.faceless.pdf2.*; 11 import org.faceless.util.*; 12 import org.faceless.report.*; 13 import org.xml.sax.*; 14 15 101 public class PDFFilter implements Filter, ErrorHandler 102 { 103 private FilterConfig conf=null; 104 105 public void init(FilterConfig config) 106 throws ServletException 107 { 108 this.conf = config; 109 String license = conf.getInitParameter("license"); 110 if (license==null) license = conf.getInitParameter("License"); 111 if (license!=null) { 112 ReportParser.setLicenseKey(license); 113 } 114 115 String cache = conf.getInitParameter("cache-prefix"); 116 if (cache==null) cache = conf.getInitParameter("Cache-prefix"); 117 if (cache==null) cache = conf.getInitParameter("Cache-Prefix"); 118 119 String cachemin = conf.getInitParameter("cache-minsize"); 120 if (cachemin==null) cachemin = conf.getInitParameter("Cache-minsize"); 121 if (cachemin==null) cachemin = conf.getInitParameter("Cache-Minsize"); 122 if (cachemin==null) cachemin="8192"; 123 124 if (cache!=null) { 125 cache=cache.trim(); 126 if (cache.length()>0) { 127 PDF.setCache(new DiskCache(cache, Integer.parseInt(cachemin))); 128 } 129 } 130 } 131 132 public void destroy() 133 { 134 this.conf = null; 135 } 136 137 140 public FilterConfig getFilterConfig() 141 { 142 return conf; 143 } 144 145 148 public void setFilterConfig(FilterConfig conf) 149 { 150 this.conf=conf; 151 String license = conf.getInitParameter("license"); 152 if (license!=null) { 153 org.faceless.report.ReportParser.setLicenseKey(license); 154 } 155 } 156 157 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 158 throws ServletException, IOException 159 { 160 if (conf==null || !(request instanceof HttpServletRequest && response instanceof HttpServletResponse)) return; 161 boolean iehack = "contype".equals(((HttpServletRequest)request).getHeader("User-Agent")); 167 168 CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse)response); 169 170 chain.doFilter((HttpServletRequest)request, wrapper); 171 172 if (wrapper.getStatus()>=200 && wrapper.getStatus()<=299 && (wrapper.getContentType()!=null && wrapper.getContentType().startsWith("text/xml"))) { 173 if (iehack) { 174 response.setContentType("application/pdf"); 175 } else { 176 InputSource xmlin = new InputSource(); 177 xmlin.setCharacterStream(new StringReader(wrapper.getString().trim())); 178 String url = ((HttpServletRequest)request).getRequestURL().toString(); 179 if (url!=null && url.toLowerCase().startsWith("https:")) { 180 try { 181 URL temp = new URL(url); 182 } catch (Throwable e) { 183 System.err.println("WARNING: HTTPS protocol not recognized by webserver (error is "+e.toString()+"). Switching to HTTP"); 184 url = "http:"+url.substring(6); 185 } 186 } 187 xmlin.setSystemId(url); 188 189 PDF pdf=null; 190 try { 191 ReportParser parser = ReportParser.getInstance((String )conf.getInitParameter("org.xml.sax.driver")); 192 parser.setMetaHandler(new MetaCallback((HttpServletRequest)request, (HttpServletResponse)response, this)); 193 if ("false".equalsIgnoreCase(conf.getInitParameter("org.faceless.report.flag.WarningUnknownTag"))) { 194 parser.setFlag(ReportParser.WARNING_UNKNOWN_TAG, false); 195 } 196 if ("false".equalsIgnoreCase(conf.getInitParameter("org.faceless.report.flag.WarningUnknownAttribute"))) { 197 parser.setFlag(ReportParser.WARNING_UNKNOWN_ATTRIBUTE, false); 198 } 199 if ("false".equalsIgnoreCase(conf.getInitParameter("org.faceless.report.flag.WarningMisplacedText"))) { 200 parser.setFlag(ReportParser.WARNING_MISPLACED_TEXT, false); 201 } 202 if ("true".equalsIgnoreCase(conf.getInitParameter("org.faceless.report.flag.Debug"))) { 203 parser.setFlag(ReportParser.DEBUG_TO_STDOUT, true); 204 } 205 pdf = parser.parse(xmlin); 206 } catch (SAXException e) { 207 if (e.getException()!=null) { 208 throw new ServletException(e.getException()); 209 } else { 210 throw new ServletException(e); 211 } 212 } 213 214 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 215 pdf.render(bout); 216 217 response.setContentType("application/pdf"); 218 response.setContentLength(bout.size()); 219 bout.writeTo(response.getOutputStream()); 220 response.getOutputStream().close(); 221 } 222 } else { 223 if (wrapper.getContentType()!=null) { 224 response.setContentType(wrapper.getContentType()); 225 } 226 227 response.getWriter().print(wrapper.getString()); 228 } 229 } 230 231 private class CharResponseWrapper extends HttpServletResponseWrapper 232 { 233 private static final boolean debug=false; 234 private OutputStreamWrapper bout; 235 private PrintWriter pout; 236 private CharArrayWriter cout; 237 private int status=200; 238 private String ctype; 239 240 private class OutputStreamWrapper extends ServletOutputStream 241 { 242 ByteArrayOutputStream out; 243 244 public OutputStreamWrapper() 245 { 246 out = new ByteArrayOutputStream(); 247 } 248 249 public void write(int i) 250 { 251 out.write(i); 252 } 253 254 public String getString(String charset) 255 throws IOException 256 { 257 String s = new String (out.toByteArray(), charset); 258 return s; 259 } 260 } 261 262 public CharResponseWrapper(HttpServletResponse response) 263 { 264 super(response); 265 } 266 267 public String getString() 268 throws IOException 269 { 270 if (bout!=null) { 271 return bout.getString(getCharacterEncoding()); 272 } else if (cout!=null) { 273 return cout.toString(); 274 } else { 275 return ""; 276 } 277 } 278 279 public PrintWriter getWriter() 280 throws IOException 281 { 282 if (debug) System.out.println("--> getWriter()"); 283 if (bout!=null) throw new IllegalStateException ("Already called getOutputStream"); 284 if (cout==null) { 285 cout = new CharArrayWriter(); 286 pout = new PrintWriter(cout); 287 } 288 return pout; 289 } 290 291 public ServletOutputStream getOutputStream() 292 throws IOException 293 { 294 if (debug) System.out.println("--> getOutputStream()"); 295 if (cout!=null) throw new IllegalStateException ("Already called getWriter"); 296 if (bout==null) bout = new OutputStreamWrapper(); 297 return bout; 298 } 299 300 public void flushBuffer() 301 { 302 if (debug) System.out.println("--> flushBuffer()"); 303 } 304 305 public boolean isCommitted() 306 { 307 if (debug) System.out.println("--> isCommitted()"); 308 return false; 309 } 310 311 public void reset() 312 throws IllegalStateException 313 { 314 if (debug) System.out.println("--> reset()"); 315 cout=null; 316 bout=null; 317 super.reset(); 318 } 319 320 public void resetBuffer() 321 { 322 if (debug) System.out.println("--> resetBuffer()"); 323 cout=null; 324 bout=null; 325 } 326 327 public void sendError(int sc, String msg) 328 throws IOException 329 { 330 this.status=sc; 331 super.sendError(sc,msg); 332 } 333 334 public void sendError(int sc) 335 throws IOException 336 { 337 this.status=sc; 338 super.sendError(sc); 339 } 340 341 public void setStatus(int sc) 342 { 343 if (debug) System.out.println("--> setStatus("+sc+")"); 344 this.status=sc; 345 super.setStatus(sc); 346 } 347 348 public void sendRedirect(String loc) 349 throws IOException 350 { 351 if (debug) System.out.println("--> sendRedirect(\""+loc+"\")"); 352 this.status=302; 353 super.sendRedirect(loc); 354 } 355 356 public int getStatus() 357 { 358 return status; 359 } 360 361 public void setContentType(String type) 362 { 363 if (debug) System.out.println("--> setContentType(\""+type+"\")"); 364 365 372 this.ctype=type; 373 String server = getFilterConfig().getServletContext().getServerInfo(); 374 if (type!=null && !server.startsWith("Oracle Application Server")) { 375 int i = type.indexOf(';'); 376 if (i>0) type=type.substring(0,i); 377 } 378 super.setContentType(type); 379 } 380 381 public void setHeader(String name, String value) 382 { 383 if (debug) System.out.println("--> setHeader(\""+name+"\", \""+value+"\")"); 384 if ("Content-Type".equalsIgnoreCase(name)) { 385 setContentType(value); 386 } else { 387 super.setHeader(name, value); 388 } 389 } 390 391 public void setLocale(Locale locale) 392 { 393 if (debug) System.out.println("--> setLocale("+locale+")"); 394 super.setLocale(locale); 395 } 396 397 public String getCharacterEncoding() 398 { 399 String charset=""; 400 if (ctype!=null && ctype.indexOf(";")>0 && ctype.indexOf("charset")>0) { 401 int pos = ctype.indexOf("charset")+8; 402 charset = ctype.substring(pos).trim(); 403 if (charset.charAt(0)=='=') charset=charset.substring(1).trim(); 404 if (charset.indexOf(";")>=0) charset=charset.substring(0,charset.indexOf(";")).trim(); 405 } 406 if (debug) System.out.println("--> getCharacterEncoding() - this="+charset+" super="+super.getCharacterEncoding()); 407 if (charset.length()==0) charset=super.getCharacterEncoding(); 408 return charset; 409 } 410 411 public String getContentType() 412 { 413 if (debug) System.out.println("--> getContentType() --> \""+ctype+"\""); 414 return ctype; 415 } 416 } 417 418 private class MetaCallback implements MetaHandler 422 { 423 private HttpServletRequest reader; 424 private HttpServletResponse writer; 425 private PDFFilter prox; 426 427 public MetaCallback(HttpServletRequest reader, HttpServletResponse writer, PDFFilter prox) 428 { 429 this.reader=reader; 430 this.writer=writer; 431 this.prox=prox; 432 } 433 434 public void handleMetaTag(String key, String val) 438 throws SAXException 439 { 440 try { 441 prox.metaTag(reader, writer, key, val); 442 } catch (Exception e) { 443 throw new SAXException(e); 444 } 445 } 446 } 447 448 458 public void metaTag(HttpServletRequest reader, HttpServletResponse writer, String name, String value) 459 throws ServletException, IOException 460 { 461 if (name.startsWith("HTTP-")) { 462 writer.setHeader(name.substring(5), value); 463 } else if (name.equalsIgnoreCase("Servlet-FileName")) { 464 writer.setHeader("Content-Disposition", "attachment; filename=\""+value+"\""); 465 } 466 } 467 468 470 public void warning(SAXParseException exception) 471 throws SAXException 472 { 473 System.err.println("PDF WARNING"+(exception.getLineNumber()>=0 ? " AT "+exception.getSystemId()+" line "+exception.getLineNumber() : "")+": "+exception.getMessage()); 474 throw exception; 475 } 476 477 public void error(SAXParseException exception) 478 throws SAXException 479 { 480 System.err.println("PDF ERROR"+(exception.getLineNumber()>=0 ? " AT "+exception.getSystemId()+" line "+exception.getLineNumber() : "")+": "+exception.getMessage()); 481 throw exception; 482 } 483 484 public void fatalError(SAXParseException exception) 485 throws SAXException 486 { 487 System.err.println("PDF FATAL ERROR"+(exception.getLineNumber()>=0 ? " AT "+exception.getSystemId()+" line "+exception.getLineNumber() : "")+": "+exception.getMessage()); 488 throw exception; 489 } 490 } 491 | Popular Tags |