1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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                  String errorMarkerKey = (String)request.getAttribute(
62                      "liferay-ui:error-marker:key");
63                  String errorMarkerValue = (String)request.getAttribute(
64                      "liferay-ui:error-marker:value");
65  
66                  if (Validator.isNotNull(errorMarkerKey) &&
67                      Validator.isNotNull(errorMarkerValue)) {
68  
69                      request.setAttribute(errorMarkerKey, errorMarkerValue);
70                  }
71              }
72  
73              return EVAL_PAGE;
74          }
75          catch (Exception e) {
76              throw new JspException(e);
77          }
78      }
79  
80      public int doStartTag() throws JspException {
81          try {
82              HttpServletRequest request =
83                  (HttpServletRequest)pageContext.getRequest();
84  
85              RenderRequest renderRequest = (RenderRequest)request.getAttribute(
86                  JavaConstants.JAVAX_PORTLET_REQUEST);
87  
88              request.setAttribute("liferay-ui:error:key", _key);
89              request.setAttribute("liferay-ui:error:message", _message);
90              request.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
91              request.setAttribute(
92                  "liferay-ui:error:translateMessage",
93                  String.valueOf(_translateMessage));
94  
95              if ((_exception != null) && (Validator.isNull(_message)) &&
96                  (SessionErrors.contains(renderRequest, _exception.getName()))) {
97  
98                  PortalIncludeUtil.include(pageContext, getStartPage());
99  
100                 pageContext.setAttribute(
101                     "errorException",
102                     SessionErrors.get(renderRequest, _exception.getName()));
103 
104                 return EVAL_BODY_INCLUDE;
105             }
106             else {
107                 return SKIP_BODY;
108             }
109         }
110         catch (Exception e) {
111             throw new JspException(e);
112         }
113     }
114 
115     public String getEndPage() {
116         if (Validator.isNull(_endPage)) {
117             return _END_PAGE;
118         }
119         else {
120             return _endPage;
121         }
122     }
123 
124     public String getStartPage() {
125         if (Validator.isNull(_startPage)) {
126             return _START_PAGE;
127         }
128         else {
129             return _startPage;
130         }
131     }
132 
133     public void setEndPage(String endPage) {
134         _endPage = endPage;
135     }
136 
137     public void setException(Class<?> exception) {
138         _exception = exception;
139 
140         if (_exception != null) {
141             _key = _exception.getName();
142         }
143     }
144 
145     public void setKey(String key) {
146         _key = key;
147     }
148 
149     public void setMessage(String message) {
150         _message = message;
151     }
152 
153     public void setRowBreak(String rowBreak) {
154         _rowBreak = HtmlUtil.unescape(rowBreak);
155     }
156 
157     public void setStartPage(String startPage) {
158         _startPage = startPage;
159     }
160 
161     public void setTranslateMessage(boolean translateMessage) {
162         _translateMessage = translateMessage;
163     }
164 
165     private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
166 
167     private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
168 
169     private String _endPage;
170     private Class<?> _exception;
171     private String _key;
172     private String _message;
173     private String _rowBreak = StringPool.BLANK;
174     private String _startPage;
175     private boolean _translateMessage = true;
176 
177 }