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.util.HtmlUtil;
27  import com.liferay.portal.kernel.util.JavaConstants;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.util.servlet.SessionErrors;
31  
32  import javax.portlet.RenderRequest;
33  
34  import javax.servlet.ServletRequest;
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              ServletRequest req = pageContext.getRequest();
49  
50              RenderRequest renderRequest = (RenderRequest)req.getAttribute(
51                  JavaConstants.JAVAX_PORTLET_REQUEST);
52  
53              req.setAttribute("liferay-ui:error:key", _key);
54              req.setAttribute("liferay-ui:error:message", _message);
55              req.setAttribute(
56                  "liferay-ui:error:translateMessage",
57                  String.valueOf(_translateMessage));
58              req.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
59  
60              if ((_exception != null) && (Validator.isNull(_message)) &&
61                  (SessionErrors.contains(renderRequest, _exception.getName()))) {
62  
63                  PortalIncludeUtil.include(pageContext, getStartPage());
64  
65                  pageContext.setAttribute(
66                      "errorException",
67                      SessionErrors.get(renderRequest, _exception.getName()));
68  
69                  return EVAL_BODY_INCLUDE;
70              }
71              else {
72                  return SKIP_BODY;
73              }
74          }
75          catch (Exception e) {
76              throw new JspException(e);
77          }
78      }
79  
80      public int doEndTag() throws JspException {
81          try {
82              ServletRequest req = pageContext.getRequest();
83  
84              RenderRequest renderRequest = (RenderRequest)req.getAttribute(
85                  JavaConstants.JAVAX_PORTLET_REQUEST);
86  
87              boolean includeEndPage = false;
88  
89              if (_key == null) {
90                  if (!SessionErrors.isEmpty(renderRequest)) {
91                      includeEndPage = true;
92                  }
93              }
94              else {
95                  if (SessionErrors.contains(renderRequest, _key)) {
96                      includeEndPage = true;
97                  }
98              }
99  
100             if (includeEndPage) {
101                 PortalIncludeUtil.include(pageContext, getEndPage());
102             }
103 
104             return EVAL_PAGE;
105         }
106         catch (Exception e) {
107             throw new JspException(e);
108         }
109     }
110 
111     public String getStartPage() {
112         if (Validator.isNull(_startPage)) {
113             return _START_PAGE;
114         }
115         else {
116             return _startPage;
117         }
118     }
119 
120     public void setStartPage(String startPage) {
121         _startPage = startPage;
122     }
123 
124     public String getEndPage() {
125         if (Validator.isNull(_endPage)) {
126             return _END_PAGE;
127         }
128         else {
129             return _endPage;
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 setTranslateMessage(boolean translateMessage) {
154         _translateMessage = translateMessage;
155     }
156 
157     public void setRowBreak(String rowBreak) {
158         _rowBreak = HtmlUtil.unescape(rowBreak);
159     }
160 
161     private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
162 
163     private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
164 
165     private String _startPage;
166     private String _endPage;
167     private Class<?> _exception;
168     private String _key;
169     private String _message;
170     private boolean _translateMessage = true;
171     private String _rowBreak = StringPool.BLANK;
172 
173 }