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                 String errorMarkerKey = (String)request.getAttribute(
105                     "liferay-ui:error-marker:key");
106                 String errorMarkerValue = (String)request.getAttribute(
107                     "liferay-ui:error-marker:value");
108 
109                 if (Validator.isNotNull(errorMarkerKey) &&
110                     Validator.isNotNull(errorMarkerValue)) {
111 
112                     request.setAttribute(errorMarkerKey, errorMarkerValue);
113                 }
114             }
115 
116             return EVAL_PAGE;
117         }
118         catch (Exception e) {
119             throw new JspException(e);
120         }
121     }
122 
123     public String getStartPage() {
124         if (Validator.isNull(_startPage)) {
125             return _START_PAGE;
126         }
127         else {
128             return _startPage;
129         }
130     }
131 
132     public void setStartPage(String startPage) {
133         _startPage = startPage;
134     }
135 
136     public String getEndPage() {
137         if (Validator.isNull(_endPage)) {
138             return _END_PAGE;
139         }
140         else {
141             return _endPage;
142         }
143     }
144 
145     public void setEndPage(String endPage) {
146         _endPage = endPage;
147     }
148 
149     public void setException(Class<?> exception) {
150         _exception = exception;
151 
152         if (_exception != null) {
153             _key = _exception.getName();
154         }
155     }
156 
157     public void setKey(String key) {
158         _key = key;
159     }
160 
161     public void setMessage(String message) {
162         _message = message;
163     }
164 
165     public void setTranslateMessage(boolean translateMessage) {
166         _translateMessage = translateMessage;
167     }
168 
169     public void setRowBreak(String rowBreak) {
170         _rowBreak = HtmlUtil.unescape(rowBreak);
171     }
172 
173     private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
174 
175     private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
176 
177     private String _startPage;
178     private String _endPage;
179     private Class<?> _exception;
180     private String _key;
181     private String _message;
182     private boolean _translateMessage = true;
183     private String _rowBreak = StringPool.BLANK;
184 
185 }