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 doEndTag() 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              boolean includeEndPage = false;
54  
55              if (_key == null) {
56                  if (!SessionErrors.isEmpty(renderRequest)) {
57                      includeEndPage = true;
58                  }
59              }
60              else {
61                  if (SessionErrors.contains(renderRequest, _key)) {
62                      includeEndPage = true;
63                  }
64              }
65  
66              if (includeEndPage) {
67                  PortalIncludeUtil.include(pageContext, getEndPage());
68  
69                  String errorMarkerKey = (String)request.getAttribute(
70                      "liferay-ui:error-marker:key");
71                  String errorMarkerValue = (String)request.getAttribute(
72                      "liferay-ui:error-marker:value");
73  
74                  if (Validator.isNotNull(errorMarkerKey) &&
75                      Validator.isNotNull(errorMarkerValue)) {
76  
77                      request.setAttribute(errorMarkerKey, errorMarkerValue);
78                  }
79              }
80  
81              return EVAL_PAGE;
82          }
83          catch (Exception e) {
84              throw new JspException(e);
85          }
86      }
87  
88      public int doStartTag() throws JspException {
89          try {
90              HttpServletRequest request =
91                  (HttpServletRequest)pageContext.getRequest();
92  
93              RenderRequest renderRequest = (RenderRequest)request.getAttribute(
94                  JavaConstants.JAVAX_PORTLET_REQUEST);
95  
96              request.setAttribute("liferay-ui:error:key", _key);
97              request.setAttribute("liferay-ui:error:message", _message);
98              request.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
99              request.setAttribute(
100                 "liferay-ui:error:translateMessage",
101                 String.valueOf(_translateMessage));
102 
103             if ((_exception != null) && (Validator.isNull(_message)) &&
104                 (SessionErrors.contains(renderRequest, _exception.getName()))) {
105 
106                 PortalIncludeUtil.include(pageContext, getStartPage());
107 
108                 pageContext.setAttribute(
109                     "errorException",
110                     SessionErrors.get(renderRequest, _exception.getName()));
111 
112                 return EVAL_BODY_INCLUDE;
113             }
114             else {
115                 return SKIP_BODY;
116             }
117         }
118         catch (Exception e) {
119             throw new JspException(e);
120         }
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 String getStartPage() {
133         if (Validator.isNull(_startPage)) {
134             return _START_PAGE;
135         }
136         else {
137             return _startPage;
138         }
139     }
140 
141     public void setEndPage(String endPage) {
142         _endPage = endPage;
143     }
144 
145     public void setException(Class<?> exception) {
146         _exception = exception;
147 
148         if (_exception != null) {
149             _key = _exception.getName();
150         }
151     }
152 
153     public void setKey(String key) {
154         _key = key;
155     }
156 
157     public void setMessage(String message) {
158         _message = message;
159     }
160 
161     public void setRowBreak(String rowBreak) {
162         _rowBreak = HtmlUtil.unescape(rowBreak);
163     }
164 
165     public void setStartPage(String startPage) {
166         _startPage = startPage;
167     }
168 
169     public void setTranslateMessage(boolean translateMessage) {
170         _translateMessage = translateMessage;
171     }
172 
173     private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
174 
175     private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
176 
177     private String _endPage;
178     private Class<?> _exception;
179     private String _key;
180     private String _message;
181     private String _rowBreak = StringPool.BLANK;
182     private String _startPage;
183     private boolean _translateMessage = true;
184 
185 }