2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.shale.examples.test.view;
18  
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.faces.component.UIViewRoot;
23  import javax.faces.context.FacesContext;
24  
25  import org.apache.shale.view.AbstractViewController;
26  import org.apache.shale.view.faces.FacesConstants;
27  
28  public class Error extends AbstractViewController {
29  
30      private String httpErrorCode = null;
31  
32      private String exceptionType = null;
33  
34      private String message = null;
35  
36      private String requestUri = null;
37  
38      private String servletName = null;
39      
40      private Recorder recorder = null;
41  
42      public void prerender() {
43          System.out.println("Error - Removing exceptions stack");
44  
45          Map requestMap = getRequestMap();
46          
47          Exception exception = (Exception) requestMap
48                  .get("javax.servlet.error.exception");
49  
50          FacesContext facesContext = getFacesContext();
51          if (facesContext == null) return;
52          
53          UIViewRoot root = facesContext.getViewRoot();
54          if (root == null) return;
55          
56          List exceptions = (List) requestMap.get(FacesConstants.EXCEPTIONS_LIST);
57          if (exception != null && exceptions != null) {
58              // save on the view root
59              getFacesContext().getViewRoot().getAttributes().put("errors", exceptions);
60              requestMap.remove(FacesConstants.EXCEPTIONS_LIST);
61  
62              
63              Integer errorCode = (Integer) requestMap.get("javax.servlet.error.status_code");
64              if (errorCode != null) {
65              setHttpErrorCode(errorCode.toString());
66              }
67              
68              Object clazz = requestMap.get("javax.servlet.error.exception_type");
69              if (clazz != null) {
70                 setExceptionType(clazz.toString());
71              }
72              setMessage((String) requestMap.get("javax.servlet.error.message"));
73              setRequestUri((String) requestMap
74                      .get("javax.servlet.error.request_uri"));
75              setServletName((String) requestMap
76                      .get("javax.servlet.error.servlet_name"));
77  
78              setRecorder((Recorder) getBean("recorder"));
79          }
80          
81      }
82  
83      public String getExceptionType() {
84          return exceptionType;
85      }
86  
87      public void setExceptionType(String exceptionType) {
88          this.exceptionType = exceptionType;
89      }
90  
91      public String getHttpErrorCode() {
92          return httpErrorCode;
93      }
94  
95      public void setHttpErrorCode(String httpErrorCode) {
96          this.httpErrorCode = httpErrorCode;
97      }
98  
99      public String getMessage() {
100         return message;
101     }
102 
103     public void setMessage(String message) {
104         this.message = message;
105     }
106 
107     public String getRequestUri() {
108         return requestUri;
109     }
110 
111     public void setRequestUri(String requestUri) {
112         this.requestUri = requestUri;
113     }
114 
115     public String getServletName() {
116         return servletName;
117     }
118 
119     public void setServletName(String servletName) {
120         this.servletName = servletName;
121     }
122 
123     public Recorder getRecorder() {
124         return recorder;
125     }
126 
127     public void setRecorder(Recorder recorder) {
128         this.recorder = recorder;
129     }
130     
131     public String getStackTrace() {
132        StringBuffer buff = new StringBuffer();
133        Exception e = (Exception) getBean("e");
134        for (int i = 0; i < e.getStackTrace().length; i++) {
135           buff.append(e.getStackTrace()[i].toString())
136               .append("\n");
137        }
138        return buff.toString();
139     }
140 }