KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > webapp > BarcodeErrorServlet


1 /*
2  * $Id: BarcodeErrorServlet.java,v 1.1 2003/06/15 16:37:07 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.webapp;
59
60 import java.io.ByteArrayOutputStream JavaDoc;
61 import java.io.IOException JavaDoc;
62
63 import javax.servlet.ServletException JavaDoc;
64 import javax.servlet.http.HttpServlet JavaDoc;
65 import javax.servlet.http.HttpServletRequest JavaDoc;
66 import javax.servlet.http.HttpServletResponse JavaDoc;
67 import javax.xml.transform.Result JavaDoc;
68 import javax.xml.transform.Source JavaDoc;
69 import javax.xml.transform.TransformerFactory JavaDoc;
70 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
71 import javax.xml.transform.sax.TransformerHandler JavaDoc;
72 import javax.xml.transform.stream.StreamSource JavaDoc;
73
74 import org.xml.sax.ContentHandler JavaDoc;
75 import org.xml.sax.SAXException JavaDoc;
76 import org.xml.sax.helpers.AttributesImpl JavaDoc;
77
78 import org.apache.avalon.framework.CascadingException;
79 import org.apache.avalon.framework.logger.ConsoleLogger;
80 import org.apache.avalon.framework.logger.Logger;
81
82 /**
83  * Error handler servlet for Barcode exceptions.
84  *
85  * @author Jeremias Maerki
86  */

87 public class BarcodeErrorServlet extends HttpServlet JavaDoc {
88
89     private Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
90
91     /**
92      * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
93      */

94     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
95                 throws ServletException JavaDoc, IOException JavaDoc {
96
97         Throwable JavaDoc t = (Throwable JavaDoc)request.getAttribute("javax.servlet.error.exception");
98         try {
99             SAXTransformerFactory JavaDoc factory = (SAXTransformerFactory JavaDoc)TransformerFactory.newInstance();
100             java.net.URL JavaDoc xslt = getServletContext().getResource("/WEB-INF/exception2svg.xslt");
101             TransformerHandler JavaDoc thandler;
102             if (xslt != null) {
103                 log.debug(xslt.toExternalForm());
104                 Source JavaDoc xsltSource = new StreamSource JavaDoc(xslt.toExternalForm());
105                 thandler = factory.newTransformerHandler(xsltSource);
106                 response.setContentType("image/svg+xml");
107             } else {
108                 log.error("Exception stylesheet not found, sending back raw XML");
109                 thandler = factory.newTransformerHandler();
110                 response.setContentType("application/xml");
111             }
112
113             ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc(4096);
114             try {
115                 Result JavaDoc res = new javax.xml.transform.stream.StreamResult JavaDoc(bout);
116                 thandler.setResult(res);
117                 generateSAX(t, thandler);
118             } finally {
119                 bout.close();
120             }
121     
122             response.setContentLength(bout.size());
123             response.getOutputStream().write(bout.toByteArray());
124             response.getOutputStream().flush();
125         } catch (Exception JavaDoc e) {
126             log.error("Error in error servlet", e);
127             throw new ServletException JavaDoc(e);
128         }
129     }
130
131     private void generateSAX(Throwable JavaDoc t, ContentHandler JavaDoc handler) throws SAXException JavaDoc {
132         if (t == null) {
133             throw new NullPointerException JavaDoc("Throwable must not be null");
134         }
135         if (handler == null) {
136             throw new NullPointerException JavaDoc("ContentHandler not set");
137         }
138     
139         handler.startDocument();
140         generateSAXForException(t, handler, "exception");
141         handler.endDocument();
142     }
143     
144     private void generateSAXForException(Throwable JavaDoc t,
145                 ContentHandler JavaDoc handler, String JavaDoc elName) throws SAXException JavaDoc {
146         AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
147         attr.addAttribute(null, "classname", "classname", "CDATA", t.getClass().getName());
148         handler.startElement(null, elName, elName, attr);
149         attr.clear();
150         handler.startElement(null, "msg", "msg", attr);
151         char[] chars = t.getMessage().toCharArray();
152         handler.characters(chars, 0, chars.length);
153         handler.endElement(null, "msg", "msg");
154         
155         if (t instanceof CascadingException) {
156             Throwable JavaDoc nested = ((CascadingException)t).getCause();
157             generateSAXForException(nested, handler, "nested");
158         }
159         
160         handler.endElement(null, elName, elName);
161     }
162 }
163
Popular Tags