View Javadoc

1   package hu.elte.tribus.xsl;
2   
3   
4   import java.io.ByteArrayOutputStream;
5   import java.io.IOException;
6   
7   import javax.servlet.ServletException;
8   import javax.servlet.http.HttpServlet;
9   import javax.servlet.http.HttpServletRequest;
10  import javax.servlet.http.HttpServletResponse;
11  
12  import org.apache.fop.apps.Driver;
13  import org.apache.fop.apps.XSLTInputHandler;
14  import org.xml.sax.InputSource;
15  
16  
17  public class PDFOutput extends HttpServlet {
18  
19  	public static final String XML_REQUEST_PARAM = "xml";
20  	/***
21  	 * Constructor of the object.
22  	 */
23  	public PDFOutput() {
24  		super();
25  	}
26  
27  	/***
28  	 * Destruction of the servlet. <br>
29  	 */
30  	public void destroy() {
31  		super.destroy(); // Just puts "destroy" string in log
32  		// Put your code here
33  	}
34  
35  	public void doGet(HttpServletRequest request, HttpServletResponse response)
36  	throws ServletException, IOException {
37  		doPost(request,response);
38  	}
39  
40  	/***
41  	 * The doPost method of the servlet. <br>
42  	 *
43  	 * This method is called when a form has its tag value method equals to post.
44  	 * 
45  	 * @param request the request send by the client to the server
46  	 * @param response the response send by the server to the client
47  	 * @throws ServletException if an error occurred
48  	 * @throws IOException if an error occurred
49  	 */
50  	public void doPost(HttpServletRequest request, HttpServletResponse response)
51  			throws ServletException, IOException {
52  
53  
54  	     try {
55  	            String xmlParam = request.getParameter(XML_REQUEST_PARAM);
56  
57                /*  if (xmlParam != null) {*/
58  	                XSLTInputHandler input =
59  	                  new XSLTInputHandler(new InputSource(getClass().getClassLoader().getResourceAsStream("hu/elte/tribus/xsl/proba.xml")),
60  	                                       new InputSource(getClass().getClassLoader().getResourceAsStream("hu/elte/tribus/xsl/proba.xsl")));
61  	                renderXML(input, response);
62  	         /*   } else {
63  	                PrintWriter out = response.getWriter();
64  	                out.println("<html><head><title>Error</title></head>\n"+
65  	                            "<body><h1>FopServlet Error</h1><h3>No 'fo' "+
66  	                            "request param given.</body></html>");
67  	            }*/
68  	     } catch (Exception ex) {}
69  		
70  	}
71  
72  	
73  	   public void renderXML(XSLTInputHandler input,
74                 HttpServletResponse response) throws ServletException {
75  		try {
76  		 ByteArrayOutputStream out = new ByteArrayOutputStream();
77  		
78  		 response.setContentType("application/pdf");
79  		
80  		 Driver driver = new Driver();
81  		 /*driver.setLogger(log);*/
82  		 driver.setRenderer(Driver.RENDER_PDF);
83  		 driver.setOutputStream(out);
84  		 driver.render(input.getParser(), input.getInputSource());
85  		
86  		 byte[] content = out.toByteArray();
87  		 response.setContentLength(content.length);
88  		 response.getOutputStream().write(content);
89  		 response.getOutputStream().flush();
90  		} catch (Exception ex) {
91  		 throw new ServletException(ex);
92  		}
93  	   }
94  	
95  	/***
96  	 * Initialization of the servlet. <br>
97  	 *
98  	 * @throws ServletException if an error occure
99  	 */
100 	public void init() throws ServletException {
101 		// Put your code here
102 	}
103 
104 }