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.PortletRequest;
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              PortletRequest portletRequest =
43                  (PortletRequest)request.getAttribute(
44                      JavaConstants.JAVAX_PORTLET_REQUEST);
45  
46              boolean includeEndPage = false;
47  
48              if (_key == null) {
49                  if (!SessionErrors.isEmpty(portletRequest)) {
50                      includeEndPage = true;
51                  }
52              }
53              else {
54                  if (SessionErrors.contains(portletRequest, _key)) {
55                      includeEndPage = true;
56                  }
57              }
58  
59              if (includeEndPage) {
60                  PortalIncludeUtil.include(pageContext, getEndPage());
61  
62                  String errorMarkerKey = (String)request.getAttribute(
63                      "liferay-ui:error-marker:key");
64                  String errorMarkerValue = (String)request.getAttribute(
65                      "liferay-ui:error-marker:value");
66  
67                  if (Validator.isNotNull(errorMarkerKey) &&
68                      Validator.isNotNull(errorMarkerValue)) {
69  
70                      request.setAttribute(errorMarkerKey, errorMarkerValue);
71                  }
72              }
73  
74              return EVAL_PAGE;
75          }
76          catch (Exception e) {
77              throw new JspException(e);
78          }
79      }
80  
81      public int doStartTag() throws JspException {
82          try {
83              HttpServletRequest request =
84                  (HttpServletRequest)pageContext.getRequest();
85  
86              PortletRequest portletRequest =
87                  (PortletRequest)request.getAttribute(
88                      JavaConstants.JAVAX_PORTLET_REQUEST);
89  
90              request.setAttribute("liferay-ui:error:key", _key);
91              request.setAttribute("liferay-ui:error:message", _message);
92              request.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
93              request.setAttribute(
94                  "liferay-ui:error:translateMessage",
95                  String.valueOf(_translateMessage));
96  
97              if (Validator.isNotNull(_message)) {
98                  return SKIP_BODY;
99              }
100 
101             if (SessionErrors.contains(portletRequest, _key)) {
102                 Object value = null;
103 
104                 if (_exception != null) {
105                     value = SessionErrors.get(
106                         portletRequest, _exception.getName());
107                 }
108                 else {
109                     value = SessionErrors.get(portletRequest, _key);
110                 }
111 
112                 PortalIncludeUtil.include(pageContext, getStartPage());
113 
114                 if (value != null) {
115                     pageContext.setAttribute("errorException", value);
116                 }
117 
118                 return EVAL_BODY_INCLUDE;
119             }
120 
121             return SKIP_BODY;
122         }
123         catch (Exception e) {
124             throw new JspException(e);
125         }
126     }
127 
128     public String getEndPage() {
129         if (Validator.isNull(_endPage)) {
130             return _END_PAGE;
131         }
132         else {
133             return _endPage;
134         }
135     }
136 
137     public String getStartPage() {
138         if (Validator.isNull(_startPage)) {
139             return _START_PAGE;
140         }
141         else {
142             return _startPage;
143         }
144     }
145 
146     public void setEndPage(String endPage) {
147         _endPage = endPage;
148     }
149 
150     public void setException(Class<?> exception) {
151         _exception = exception;
152 
153         if (_exception != null) {
154             _key = _exception.getName();
155         }
156     }
157 
158     public void setKey(String key) {
159         _key = key;
160     }
161 
162     public void setMessage(String message) {
163         _message = message;
164     }
165 
166     public void setRowBreak(String rowBreak) {
167         _rowBreak = HtmlUtil.unescape(rowBreak);
168     }
169 
170     public void setStartPage(String startPage) {
171         _startPage = startPage;
172     }
173 
174     public void setTranslateMessage(boolean translateMessage) {
175         _translateMessage = translateMessage;
176     }
177 
178     private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
179 
180     private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
181 
182     private String _endPage;
183     private Class<?> _exception;
184     private String _key;
185     private String _message;
186     private String _rowBreak = StringPool.BLANK;
187     private String _startPage;
188     private boolean _translateMessage = true;
189 
190 }