1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.taglib.ui;
21  
22  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
23  import com.liferay.portal.kernel.servlet.SessionErrors;
24  import com.liferay.portal.kernel.util.HtmlUtil;
25  import com.liferay.portal.kernel.util.JavaConstants;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  
29  import javax.portlet.RenderRequest;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.jsp.JspException;
33  import javax.servlet.jsp.tagext.TagSupport;
34  
35  /**
36   * <a href="ErrorTag.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class ErrorTag extends TagSupport {
42  
43      public int doStartTag() throws JspException {
44          try {
45              HttpServletRequest request =
46                  (HttpServletRequest)pageContext.getRequest();
47  
48              RenderRequest renderRequest = (RenderRequest)request.getAttribute(
49                  JavaConstants.JAVAX_PORTLET_REQUEST);
50  
51              request.setAttribute("liferay-ui:error:key", _key);
52              request.setAttribute("liferay-ui:error:message", _message);
53              request.setAttribute(
54                  "liferay-ui:error:translateMessage",
55                  String.valueOf(_translateMessage));
56              request.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
57  
58              if ((_exception != null) && (Validator.isNull(_message)) &&
59                  (SessionErrors.contains(renderRequest, _exception.getName()))) {
60  
61                  PortalIncludeUtil.include(pageContext, getStartPage());
62  
63                  pageContext.setAttribute(
64                      "errorException",
65                      SessionErrors.get(renderRequest, _exception.getName()));
66  
67                  return EVAL_BODY_INCLUDE;
68              }
69              else {
70                  return SKIP_BODY;
71              }
72          }
73          catch (Exception e) {
74              throw new JspException(e);
75          }
76      }
77  
78      public int doEndTag() throws JspException {
79          try {
80              HttpServletRequest request =
81                  (HttpServletRequest)pageContext.getRequest();
82  
83              RenderRequest renderRequest = (RenderRequest)request.getAttribute(
84                  JavaConstants.JAVAX_PORTLET_REQUEST);
85  
86              boolean includeEndPage = false;
87  
88              if (_key == null) {
89                  if (!SessionErrors.isEmpty(renderRequest)) {
90                      includeEndPage = true;
91                  }
92              }
93              else {
94                  if (SessionErrors.contains(renderRequest, _key)) {
95                      includeEndPage = true;
96                  }
97              }
98  
99              if (includeEndPage) {
100                 PortalIncludeUtil.include(pageContext, getEndPage());
101             }
102 
103             return EVAL_PAGE;
104         }
105         catch (Exception e) {
106             throw new JspException(e);
107         }
108     }
109 
110     public String getStartPage() {
111         if (Validator.isNull(_startPage)) {
112             return _START_PAGE;
113         }
114         else {
115             return _startPage;
116         }
117     }
118 
119     public void setStartPage(String startPage) {
120         _startPage = startPage;
121     }
122 
123     public String getEndPage() {
124         if (Validator.isNull(_endPage)) {
125             return _END_PAGE;
126         }
127         else {
128             return _endPage;
129         }
130     }
131 
132     public void setEndPage(String endPage) {
133         _endPage = endPage;
134     }
135 
136     public void setException(Class<?> exception) {
137         _exception = exception;
138 
139         if (_exception != null) {
140             _key = _exception.getName();
141         }
142     }
143 
144     public void setKey(String key) {
145         _key = key;
146     }
147 
148     public void setMessage(String message) {
149         _message = message;
150     }
151 
152     public void setTranslateMessage(boolean translateMessage) {
153         _translateMessage = translateMessage;
154     }
155 
156     public void setRowBreak(String rowBreak) {
157         _rowBreak = HtmlUtil.unescape(rowBreak);
158     }
159 
160     private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
161 
162     private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
163 
164     private String _startPage;
165     private String _endPage;
166     private Class<?> _exception;
167     private String _key;
168     private String _message;
169     private boolean _translateMessage = true;
170     private String _rowBreak = StringPool.BLANK;
171 
172 }