|
5 | 5 | import org.tinystruct.ApplicationException;
|
6 | 6 | import org.tinystruct.dom.Element;
|
7 | 7 | import org.tinystruct.handler.Reforward;
|
8 |
| - |
9 |
| -import javax.servlet.http.HttpServletRequest; |
10 |
| -import javax.servlet.http.HttpServletResponse; |
11 |
| -import javax.servlet.http.HttpSession; |
| 8 | +import org.tinystruct.http.Request; |
| 9 | +import org.tinystruct.http.Response; |
| 10 | +import org.tinystruct.http.Session; |
12 | 11 |
|
13 | 12 | public class error extends AbstractApplication {
|
14 | 13 |
|
15 |
| - private HttpServletRequest request; |
16 |
| - private HttpServletResponse response; |
17 |
| - private Reforward reforward; |
18 |
| - |
19 |
| - @Override |
20 |
| - public void init() { |
21 |
| - // TODO Auto-generated method stub |
22 |
| - this.setAction("error", "process"); |
23 |
| - this.setAction("info", "info"); |
24 |
| - } |
25 |
| - |
26 |
| - @Override |
27 |
| - public String version() { |
28 |
| - // TODO Auto-generated method stub |
29 |
| - return null; |
30 |
| - } |
31 |
| - |
32 |
| - public Object process() |
33 |
| - throws ApplicationException { |
34 |
| - this.request = (HttpServletRequest) this.context.getAttribute("HTTP_REQUEST"); |
35 |
| - this.response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE"); |
36 |
| - |
37 |
| - this.reforward=new Reforward(this.request,this.response); |
38 |
| - |
39 |
| - this.setVariable("from", this.reforward.getFromURL()); |
40 |
| - |
41 |
| - HttpSession session=this.request.getSession(); |
42 |
| - |
43 |
| - if(session.getAttribute("error")!=null) |
44 |
| - { |
45 |
| - ApplicationException exception=(ApplicationException)session.getAttribute("error"); |
46 |
| - |
47 |
| - String message=exception.getRootCause().getMessage(); |
48 |
| - if(message!=null) this.setVariable("exception.message", message); |
49 |
| - else this.setVariable("exception.message", "Unknown error"); |
50 |
| - |
51 |
| - this.setVariable("exception.details", this.getDetail(exception).toString()); |
52 |
| - return this.getVariable("exception.message").getValue().toString()+this.getVariable("exception.details").getValue(); |
53 |
| - } |
54 |
| - else |
55 |
| - { |
56 |
| - this.reforward.forward(); |
57 |
| - } |
58 |
| - |
59 |
| - return "This request is forbidden!"; |
60 |
| - } |
61 |
| - |
62 |
| - private Element getDetail(ApplicationException exception) |
63 |
| - { |
64 |
| - Element errors=new Element("ul"); |
65 |
| - int i=0; |
66 |
| - |
67 |
| - Throwable ex=exception.getRootCause(); |
68 |
| - |
69 |
| - StackTraceElement[] trace=ex.getStackTrace(); |
70 |
| - |
71 |
| - while(i<trace.length) |
72 |
| - { |
73 |
| - Element element=new Element("li"); |
74 |
| - element.setData(trace[i++].toString()); |
75 |
| - errors.addElement(element); |
76 |
| - } |
77 |
| - |
78 |
| - return errors; |
79 |
| - } |
80 |
| - |
81 |
| - public StringBuffer info() |
82 |
| - { |
83 |
| - StringBuffer buffer=new StringBuffer(); |
84 |
| - |
85 |
| - buffer.append("Protocol: " + this.request.getProtocol()+"\r\n"); |
86 |
| - buffer.append("Scheme: " + this.request.getScheme()+"\r\n"); |
87 |
| - buffer.append("Server Name: " + this.request.getServerName()+"\r\n"); |
88 |
| - buffer.append("Server Port: " + this.request.getServerPort()+"\r\n"); |
89 |
| - buffer.append("Protocol: " + this.request.getProtocol()+"\r\n"); |
90 |
| -// buffer.append("Server Info: " + getServletConfig().getServletContext().getServerInfo()+"\r\n"); |
91 |
| - buffer.append("Remote Addr: " + this.request.getRemoteAddr()+"\r\n"); |
92 |
| - buffer.append("Remote Host: " + this.request.getRemoteHost()+"\r\n"); |
93 |
| - buffer.append("Character Encoding: " + this.request.getCharacterEncoding()+"\r\n"); |
94 |
| - buffer.append("Content Length: " + this.request.getContentLength()+"\r\n"); |
95 |
| - buffer.append("Content Type: "+ this.request.getContentType()+"\r\n"); |
96 |
| - buffer.append("Auth Type: " + this.request.getAuthType()+"\r\n"); |
97 |
| - buffer.append("HTTP Method: " + this.request.getMethod()+"\r\n"); |
98 |
| - buffer.append("Path Info: " + this.request.getPathInfo()+"\r\n"); |
99 |
| - buffer.append("Path Trans: " + this.request.getPathTranslated()+"\r\n"); |
100 |
| - buffer.append("Query String: " + this.request.getQueryString()+"\r\n"); |
101 |
| - buffer.append("Remote User: " + this.request.getRemoteUser()+"\r\n"); |
102 |
| - buffer.append("Session Id: " + this.request.getRequestedSessionId()+"\r\n"); |
103 |
| - buffer.append("Request URI: " + this.request.getRequestURI()+"\r\n"); |
104 |
| - buffer.append("Servlet Path: " + this.request.getServletPath()+"\r\n"); |
105 |
| - buffer.append("Accept: " + this.request.getHeader("Accept")+"\r\n"); |
106 |
| - buffer.append("Host: " + this.request.getHeader("Host")+"\r\n"); |
107 |
| - buffer.append("Referer : " + this.request.getHeader("Referer")+"\r\n"); |
108 |
| - buffer.append("Accept-Language : " + this.request.getHeader("Accept-Language")+"\r\n"); |
109 |
| - buffer.append("Accept-Encoding : " + this.request.getHeader("Accept-Encoding")+"\r\n"); |
110 |
| - buffer.append("User-Agent : " + this.request.getHeader("User-Agent")+"\r\n"); |
111 |
| - buffer.append("Connection : " + this.request.getHeader("Connection")+"\r\n"); |
112 |
| - buffer.append("Cookie : " + this.request.getHeader("Cookie")+"\r\n"); |
113 |
| - |
114 |
| - return buffer; |
115 |
| - } |
| 14 | + private Request request; |
| 15 | + private Response response; |
| 16 | + private Reforward reforward; |
| 17 | + |
| 18 | + @Override |
| 19 | + public void init() { |
| 20 | + // TODO Auto-generated method stub |
| 21 | + this.setAction("error", "process"); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public String version() { |
| 26 | + // TODO Auto-generated method stub |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + public Object process() throws ApplicationException { |
| 31 | + this.request = (Request) this.context.getAttribute("HTTP_REQUEST"); |
| 32 | + this.response = (Response) this.context.getAttribute("HTTP_RESPONSE"); |
| 33 | + |
| 34 | + this.reforward = new Reforward(this.request, this.response); |
| 35 | + |
| 36 | + this.setVariable("from", this.reforward.getFromURL()); |
| 37 | + |
| 38 | + Session session = this.request.getSession(); |
| 39 | + |
| 40 | + if (session.getAttribute("error") != null) { |
| 41 | + ApplicationException exception = (ApplicationException) session.getAttribute("error"); |
| 42 | + |
| 43 | + String message = exception.getRootCause().getMessage(); |
| 44 | + if (message != null) this.setVariable("exception.message", message); |
| 45 | + else this.setVariable("exception.message", "Unknown error"); |
| 46 | + |
| 47 | + this.setVariable("exception.details", this.getDetail(exception).toString()); |
| 48 | + return this.getVariable("exception.message").getValue().toString() + this.getVariable("exception.details").getValue(); |
| 49 | + } else { |
| 50 | + this.reforward.forward(); |
| 51 | + } |
| 52 | + |
| 53 | + return "This request is forbidden!"; |
| 54 | + } |
| 55 | + |
| 56 | + private Element getDetail(ApplicationException exception) { |
| 57 | + Element errors = new Element("ul"); |
| 58 | + int i = 0; |
| 59 | + |
| 60 | + Throwable ex = exception.getRootCause(); |
| 61 | + |
| 62 | + StackTraceElement[] trace = ex.getStackTrace(); |
| 63 | + |
| 64 | + while (i < trace.length) { |
| 65 | + Element element = new Element("li"); |
| 66 | + element.setData(trace[i++].toString()); |
| 67 | + errors.addElement(element); |
| 68 | + } |
116 | 69 |
|
| 70 | + return errors; |
| 71 | + } |
117 | 72 |
|
118 | 73 | }
|
0 commit comments