KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > app > qed > XmlDumpServlet


1 package com.quadcap.app.qed;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42 import java.io.OutputStream JavaDoc;
43 import java.io.OutputStreamWriter JavaDoc;
44
45 import java.util.zip.GZIPOutputStream JavaDoc;
46
47 import java.sql.Connection JavaDoc;
48
49 import javax.servlet.ServletConfig JavaDoc;
50 import javax.servlet.ServletException JavaDoc;
51
52 import javax.servlet.http.HttpServlet JavaDoc;
53 import javax.servlet.http.HttpServletRequest JavaDoc;
54 import javax.servlet.http.HttpServletResponse JavaDoc;
55 import javax.servlet.http.HttpSession JavaDoc;
56
57 import com.quadcap.sql.tools.XmlDump;
58
59 import com.quadcap.util.Debug;
60
61 /**
62  *
63  * @author Stan Bailes
64  */

65
66 public class XmlDumpServlet extends HttpServlet JavaDoc {
67     /**
68      * Initialize this servlet
69      *
70      * @param config the servlet configuration object, containing my
71      * initialization parameters
72      * @exception ServletException if I can't initialize for some reason.
73      */

74     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
75         super.init(config);
76     }
77     
78     /**
79      * Handle an incoming request.
80      *
81      * @param req HttpServletRequest that encapsulates the request to
82      * the servlet
83      * @param res HttpServletResponse that encapsulates the response
84      * from the servlet
85      *
86      * @exception IOException if detected when handling the request
87      * @exception ServletException if the request could not be handled
88      */

89     protected void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
90         throws ServletException JavaDoc, IOException JavaDoc
91     {
92         try {
93         HttpSession JavaDoc session = req.getSession(true);
94         if (session == null) throw new ServletException JavaDoc("No session");
95         AdminSession admin =
96         (AdminSession)session.getValue("admin");
97         if (admin == null)
98         throw new ServletException JavaDoc("no admin session");
99         res.setHeader("Content-Disposition",
100               "attachment; filename=\"dumpdb.xml.gz\"");
101         String JavaDoc dbName = req.getParameter("dbName");
102         if (dbName == null) {
103         throw new ServletException JavaDoc("no dbName");
104         }
105         OutputStream JavaDoc w = res.getOutputStream();
106         GZIPOutputStream JavaDoc gz = new GZIPOutputStream JavaDoc(w);
107             OutputStreamWriter JavaDoc ow = new OutputStreamWriter JavaDoc(gz);
108         Connection JavaDoc conn = admin.getConnection(dbName);
109         XmlDump dump = new XmlDump(conn);
110         dump.dumpTables(ow);
111         gz.finish();
112         ow.flush();
113         } catch (Exception JavaDoc e) {
114             Debug.print(e);
115             res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
116                            e.toString());
117         }
118     }
119 }
120
Popular Tags