1   /**
2    * Copyright (c) 2000-2009 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                 String errorMarkerKey = (String)request.getAttribute(
106                     "liferay-ui:error-marker:key");
107                 String errorMarkerValue = (String)request.getAttribute(
108                     "liferay-ui:error-marker:value");
109 
110                 if (Validator.isNotNull(errorMarkerKey) &&
111                     Validator.isNotNull(errorMarkerValue)) {
112 
113                     request.setAttribute(errorMarkerKey, errorMarkerValue);
114                 }
115             }
116 
117             return EVAL_PAGE;
118         }
119         catch (Exception e) {
120             throw new JspException(e);
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 setStartPage(String startPage) {
134         _startPage = startPage;
135     }
136 
137     public String getEndPage() {
138         if (Validator.isNull(_endPage)) {
139             return _END_PAGE;
140         }
141         else {
142             return _endPage;
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 setTranslateMessage(boolean translateMessage) {
167         _translateMessage = translateMessage;
168     }
169 
170     public void setRowBreak(String rowBreak) {
171         _rowBreak = HtmlUtil.unescape(rowBreak);
172     }
173 
174     private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
175 
176     private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
177 
178     private String _startPage;
179     private String _endPage;
180     private Class<?> _exception;
181     private String _key;
182     private String _message;
183     private boolean _translateMessage = true;
184     private String _rowBreak = StringPool.BLANK;
185 
186 }