1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.ui;
24  
25  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.HtmlUtil;
28  import com.liferay.portal.kernel.util.JavaConstants;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  
32  import javax.portlet.RenderRequest;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.jsp.JspException;
36  import javax.servlet.jsp.tagext.TagSupport;
37  
38  /**
39   * <a href="ErrorTag.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class ErrorTag extends TagSupport {
45  
46      public int doStartTag() throws JspException {
47          try {
48              HttpServletRequest request =
49                  (HttpServletRequest)pageContext.getRequest();
50  
51              RenderRequest renderRequest = (RenderRequest)request.getAttribute(
52                  JavaConstants.JAVAX_PORTLET_REQUEST);
53  
54              request.setAttribute("liferay-ui:error:key", _key);
55              request.setAttribute("liferay-ui:error:message", _message);
56              request.setAttribute(
57                  "liferay-ui:error:translateMessage",
58                  String.valueOf(_translateMessage));
59              request.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
60  
61              if ((_exception != null) && (Validator.isNull(_message)) &&
62                  (SessionErrors.contains(renderRequest, _exception.getName()))) {
63  
64                  PortalIncludeUtil.include(pageContext, getStartPage());
65  
66                  pageContext.setAttribute(
67                      "errorException",
68                      SessionErrors.get(renderRequest, _exception.getName()));
69  
70                  return EVAL_BODY_INCLUDE;
71              }
72              else {
73                  return SKIP_BODY;
74              }
75          }
76          catch (Exception e) {
77              throw new JspException(e);
78          }
79      }
80  
81      public int doEndTag() throws JspException {
82          try {
83              HttpServletRequest request =
84                  (HttpServletRequest)pageContext.getRequest();
85  
86              RenderRequest renderRequest = (RenderRequest)request.getAttribute(
87                  JavaConstants.JAVAX_PORTLET_REQUEST);
88  
89              boolean includeEndPage = false;
90  
91              if (_key == null) {
92                  if (!SessionErrors.isEmpty(renderRequest)) {
93                      includeEndPage = true;
94                  }
95              }
96              else {
97                  if (SessionErrors.contains(renderRequest, _key)) {
98                      includeEndPage = true;
99                  }
100             }
101 
102             if (includeEndPage) {
103                 PortalIncludeUtil.include(pageContext, getEndPage());
104             }
105 
106             return EVAL_PAGE;
107         }
108         catch (Exception e) {
109             throw new JspException(e);
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 setStartPage(String startPage) {
123         _startPage = startPage;
124     }
125 
126     public String getEndPage() {
127         if (Validator.isNull(_endPage)) {
128             return _END_PAGE;
129         }
130         else {
131             return _endPage;
132         }
133     }
134 
135     public void setEndPage(String endPage) {
136         _endPage = endPage;
137     }
138 
139     public void setException(Class<?> exception) {
140         _exception = exception;
141 
142         if (_exception != null) {
143             _key = _exception.getName();
144         }
145     }
146 
147     public void setKey(String key) {
148         _key = key;
149     }
150 
151     public void setMessage(String message) {
152         _message = message;
153     }
154 
155     public void setTranslateMessage(boolean translateMessage) {
156         _translateMessage = translateMessage;
157     }
158 
159     public void setRowBreak(String rowBreak) {
160         _rowBreak = HtmlUtil.unescape(rowBreak);
161     }
162 
163     private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
164 
165     private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
166 
167     private String _startPage;
168     private String _endPage;
169     private Class<?> _exception;
170     private String _key;
171     private String _message;
172     private boolean _translateMessage = true;
173     private String _rowBreak = StringPool.BLANK;
174 
175 }