1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.util.HtmlUtil;
20  import com.liferay.portal.kernel.util.JavaConstants;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  
24  import javax.portlet.RenderRequest;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.jsp.JspException;
28  import javax.servlet.jsp.tagext.TagSupport;
29  
30  /**
31   * <a href="ErrorTag.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class ErrorTag extends TagSupport {
36  
37      public int doEndTag() throws JspException {
38          try {
39              HttpServletRequest request =
40                  (HttpServletRequest)pageContext.getRequest();
41  
42              RenderRequest renderRequest = (RenderRequest)request.getAttribute(
43                  JavaConstants.JAVAX_PORTLET_REQUEST);
44  
45              boolean includeEndPage = false;
46  
47              if (_key == null) {
48                  if (!SessionErrors.isEmpty(renderRequest)) {
49                      includeEndPage = true;
50                  }
51              }
52              else {
53                  if (SessionErrors.contains(renderRequest, _key)) {
54                      includeEndPage = true;
55                  }
56              }
57  
58              if (includeEndPage) {
59                  PortalIncludeUtil.include(pageContext, getEndPage());
60              }
61  
62              return EVAL_PAGE;
63          }
64          catch (Exception e) {
65              throw new JspException(e);
66          }
67      }
68  
69      public int doStartTag() throws JspException {
70          try {
71              HttpServletRequest request =
72                  (HttpServletRequest)pageContext.getRequest();
73  
74              RenderRequest renderRequest = (RenderRequest)request.getAttribute(
75                  JavaConstants.JAVAX_PORTLET_REQUEST);
76  
77              request.setAttribute("liferay-ui:error:key", _key);
78              request.setAttribute("liferay-ui:error:message", _message);
79              request.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
80              request.setAttribute(
81                  "liferay-ui:error:translateMessage",
82                  String.valueOf(_translateMessage));
83  
84              if ((_exception != null) && (Validator.isNull(_message)) &&
85                  (SessionErrors.contains(renderRequest, _exception.getName()))) {
86  
87                  PortalIncludeUtil.include(pageContext, getStartPage());
88  
89                  pageContext.setAttribute(
90                      "errorException",
91                      SessionErrors.get(renderRequest, _exception.getName()));
92  
93                  return EVAL_BODY_INCLUDE;
94              }
95              else {
96                  return SKIP_BODY;
97              }
98          }
99          catch (Exception e) {
100             throw new JspException(e);
101         }
102     }
103 
104     public String getEndPage() {
105         if (Validator.isNull(_endPage)) {
106             return _END_PAGE;
107         }
108         else {
109             return _endPage;
110         }
111     }
112 
113     public String getStartPage() {
114         if (Validator.isNull(_startPage)) {
115             return _START_PAGE;
116         }
117         else {
118             return _startPage;
119         }
120     }
121 
122     public void setEndPage(String endPage) {
123         _endPage = endPage;
124     }
125 
126     public void setException(Class<?> exception) {
127         _exception = exception;
128 
129         if (_exception != null) {
130             _key = _exception.getName();
131         }
132     }
133 
134     public void setKey(String key) {
135         _key = key;
136     }
137 
138     public void setMessage(String message) {
139         _message = message;
140     }
141 
142     public void setRowBreak(String rowBreak) {
143         _rowBreak = HtmlUtil.unescape(rowBreak);
144     }
145 
146     public void setStartPage(String startPage) {
147         _startPage = startPage;
148     }
149 
150     public void setTranslateMessage(boolean translateMessage) {
151         _translateMessage = translateMessage;
152     }
153 
154     private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
155 
156     private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
157 
158     private String _endPage;
159     private Class<?> _exception;
160     private String _key;
161     private String _message;
162     private String _rowBreak = StringPool.BLANK;
163     private String _startPage;
164     private boolean _translateMessage = true;
165 
166 }