1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ErrorTag extends TagSupport {
44  
45      public int doStartTag() throws JspException {
46          try {
47              HttpServletRequest request =
48                  (HttpServletRequest)pageContext.getRequest();
49  
50              RenderRequest renderRequest = (RenderRequest)request.getAttribute(
51                  JavaConstants.JAVAX_PORTLET_REQUEST);
52  
53              request.setAttribute("liferay-ui:error:key", _key);
54              request.setAttribute("liferay-ui:error:message", _message);
55              request.setAttribute(
56                  "liferay-ui:error:translateMessage",
57                  String.valueOf(_translateMessage));
58              request.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              HttpServletRequest request =
83                  (HttpServletRequest)pageContext.getRequest();
84  
85              RenderRequest renderRequest = (RenderRequest)request.getAttribute(
86                  JavaConstants.JAVAX_PORTLET_REQUEST);
87  
88              boolean includeEndPage = false;
89  
90              if (_key == null) {
91                  if (!SessionErrors.isEmpty(renderRequest)) {
92                      includeEndPage = true;
93                  }
94              }
95              else {
96                  if (SessionErrors.contains(renderRequest, _key)) {
97                      includeEndPage = true;
98                  }
99              }
100 
101             if (includeEndPage) {
102                 PortalIncludeUtil.include(pageContext, getEndPage());
103             }
104 
105             return EVAL_PAGE;
106         }
107         catch (Exception e) {
108             throw new JspException(e);
109         }
110     }
111 
112     public String getStartPage() {
113         if (Validator.isNull(_startPage)) {
114             return _START_PAGE;
115         }
116         else {
117             return _startPage;
118         }
119     }
120 
121     public void setStartPage(String startPage) {
122         _startPage = startPage;
123     }
124 
125     public String getEndPage() {
126         if (Validator.isNull(_endPage)) {
127             return _END_PAGE;
128         }
129         else {
130             return _endPage;
131         }
132     }
133 
134     public void setEndPage(String endPage) {
135         _endPage = endPage;
136     }
137 
138     public void setException(Class<?> exception) {
139         _exception = exception;
140 
141         if (_exception != null) {
142             _key = _exception.getName();
143         }
144     }
145 
146     public void setKey(String key) {
147         _key = key;
148     }
149 
150     public void setMessage(String message) {
151         _message = message;
152     }
153 
154     public void setTranslateMessage(boolean translateMessage) {
155         _translateMessage = translateMessage;
156     }
157 
158     public void setRowBreak(String rowBreak) {
159         _rowBreak = HtmlUtil.unescape(rowBreak);
160     }
161 
162     private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
163 
164     private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
165 
166     private String _startPage;
167     private String _endPage;
168     private Class<?> _exception;
169     private String _key;
170     private String _message;
171     private boolean _translateMessage = true;
172     private String _rowBreak = StringPool.BLANK;
173 
174 }