Source code: com/tripi/asp/FileNotFoundExceptionHandler.java
1 /**
2 * ArrowHead ASP Server
3 * This is a source file for the ArrowHead ASP Server - an 100% Java
4 * VBScript interpreter and ASP server.
5 *
6 * For more information, see http://www.tripi.com/arrowhead
7 *
8 * Copyright (C) 2002 Terence Haddock
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 package com.tripi.asp;
26
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.util.Properties;
30 import javax.servlet.http.HttpServletResponse;
31 import org.apache.log4j.Category;
32
33 /**
34 * This exception only handles a "FileNotFound" exception by sending
35 * a file not found error report.
36 *
37 * @author Terence Haddock
38 * @version 0.9
39 */
40 public class FileNotFoundExceptionHandler implements AspExceptionHandler
41 {
42 /** Debugging class */
43 static Category DBG = Category.getInstance(FileNotFoundExceptionHandler.class);
44
45 /**
46 * This function configures any properties
47 */
48 public void configureExceptionHandler(Properties prop)
49 {
50 }
51
52 /**
53 * This function is called when an exception occurs
54 * @param ctx ASP context
55 * @param ex Exception which occured
56 * @return <b>true</b> on non-file not found exceptions, <b>false</b> on
57 * File not found exceptions.
58 */
59 public boolean onExceptionOccured(AspContext ctx, AspException ex)
60 throws AspException
61 {
62 if (ex instanceof AspNestedException)
63 {
64 AspNestedException nex = (AspNestedException)ex;
65 /* This is a little bit of a hack, but oh well. */
66 if ((nex.Line() == 0) &&
67 (nex.nestedEx instanceof FileNotFoundException))
68 {
69 /* Grab the filename */
70 final IdentNode filenameIdent = new IdentNode("!filename");
71 String filename = (String)ctx.getValue(filenameIdent);
72 /* Grab the response object */
73 Response rsp = ctx.getAspResponse();
74 HttpServletResponse servRest = rsp.getHttpServletResponse();
75 try {
76 servRest.sendError(404, filename);
77 } catch (IOException ioex) {
78 throw new AspNestedException(ioex);
79 }
80 return false;
81 }
82 }
83 return true;
84 }
85 }
86