KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > view > FopPdfViewHandler


1 /*
2  * $Id: FopPdfViewHandler.java 6535 2006-01-22 03:53:59Z jaz $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.view;
26
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.StringWriter JavaDoc;
30 import java.io.Writer JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.GeneralException;
36
37 /**
38  * Uses XSL-FO formatted templates to generate PDF views
39  * This handler will use JPublish to generate the XSL-FO
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 6535 $
43  * @since 3.0
44  */

45 public class FopPdfViewHandler extends JPublishViewHandler {
46     
47     public static final String JavaDoc module = FopPdfViewHandler.class.getName();
48     
49     /**
50      * @see org.ofbiz.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
51      */

52     public void render(String JavaDoc name, String JavaDoc page, String JavaDoc info, String JavaDoc contentType, String JavaDoc encoding, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ViewHandlerException {
53         // render and obtain the XSL-FO
54
Writer JavaDoc writer = new StringWriter JavaDoc();
55         try {
56             wrapper.render(page, request, response, writer, null, false);
57         } catch (Throwable JavaDoc t) {
58             throw new ViewHandlerException("Problems with the response writer/output stream", t);
59         }
60         if (Debug.verboseOn()) {
61             Debug.logVerbose("XSL-FO : " + writer.toString(), module);
62         }
63         
64         // render the byte array
65
ByteArrayOutputStream JavaDoc out = null;
66         try {
67             out = FopRenderer.render(writer);
68         } catch (GeneralException e) {
69             throw new ViewHandlerException(e.getMessage(), e.getNested());
70         }
71                   
72         // set the content type and length
73
response.setContentType("application/pdf");
74         response.setContentLength(out.size());
75         
76         // write to the browser
77
try {
78             out.writeTo(response.getOutputStream());
79             response.getOutputStream().flush();
80         } catch (IOException JavaDoc e) {
81             throw new ViewHandlerException("Unable write to browser OutputStream", e);
82         }
83     }
84 }
85
Popular Tags