001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.JavaConstants;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import javax.portlet.PortletRequest;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.jsp.JspException;
028    import javax.servlet.jsp.tagext.TagSupport;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class ErrorTag extends TagSupport {
034    
035            public int doEndTag() throws JspException {
036                    try {
037                            HttpServletRequest request =
038                                    (HttpServletRequest)pageContext.getRequest();
039    
040                            PortletRequest portletRequest =
041                                    (PortletRequest)request.getAttribute(
042                                            JavaConstants.JAVAX_PORTLET_REQUEST);
043    
044                            boolean includeEndPage = false;
045    
046                            if (Validator.isNull(_key)) {
047                                    if (!SessionErrors.isEmpty(portletRequest)) {
048                                            includeEndPage = true;
049                                    }
050                            }
051                            else {
052                                    if (SessionErrors.contains(portletRequest, _key)) {
053                                            includeEndPage = true;
054                                    }
055                            }
056    
057                            if (includeEndPage) {
058                                    PortalIncludeUtil.include(pageContext, getEndPage());
059    
060                                    String errorMarkerKey = (String)request.getAttribute(
061                                            "liferay-ui:error-marker:key");
062                                    String errorMarkerValue = (String)request.getAttribute(
063                                            "liferay-ui:error-marker:value");
064    
065                                    if (Validator.isNotNull(errorMarkerKey) &&
066                                            Validator.isNotNull(errorMarkerValue)) {
067    
068                                            request.setAttribute(errorMarkerKey, errorMarkerValue);
069                                    }
070                            }
071    
072                            return EVAL_PAGE;
073                    }
074                    catch (Exception e) {
075                            throw new JspException(e);
076                    }
077            }
078    
079            public int doStartTag() throws JspException {
080                    try {
081                            HttpServletRequest request =
082                                    (HttpServletRequest)pageContext.getRequest();
083    
084                            PortletRequest portletRequest =
085                                    (PortletRequest)request.getAttribute(
086                                            JavaConstants.JAVAX_PORTLET_REQUEST);
087    
088                            request.setAttribute("liferay-ui:error:key", _key);
089                            request.setAttribute("liferay-ui:error:message", _message);
090                            request.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
091                            request.setAttribute(
092                                    "liferay-ui:error:translateMessage",
093                                    String.valueOf(_translateMessage));
094    
095                            if (Validator.isNotNull(_message)) {
096                                    return SKIP_BODY;
097                            }
098    
099                            if (SessionErrors.contains(portletRequest, _key)) {
100                                    Object value = null;
101    
102                                    if (_exception != null) {
103                                            value = SessionErrors.get(
104                                                    portletRequest, _exception.getName());
105                                    }
106                                    else {
107                                            value = SessionErrors.get(portletRequest, _key);
108                                    }
109    
110                                    PortalIncludeUtil.include(pageContext, getStartPage());
111    
112                                    if (value != null) {
113                                            pageContext.setAttribute("errorException", value);
114                                    }
115    
116                                    return EVAL_BODY_INCLUDE;
117                            }
118    
119                            return SKIP_BODY;
120                    }
121                    catch (Exception e) {
122                            throw new JspException(e);
123                    }
124            }
125    
126            protected String getEndPage() {
127                    if (Validator.isNull(_endPage)) {
128                            return _END_PAGE;
129                    }
130                    else {
131                            return _endPage;
132                    }
133            }
134    
135            protected String getStartPage() {
136                    if (Validator.isNull(_startPage)) {
137                            return _START_PAGE;
138                    }
139                    else {
140                            return _startPage;
141                    }
142            }
143    
144            public void setEndPage(String endPage) {
145                    _endPage = endPage;
146            }
147    
148            public void setException(Class<?> exception) {
149                    _exception = exception;
150    
151                    if (_exception != null) {
152                            _key = _exception.getName();
153                    }
154            }
155    
156            public void setKey(String key) {
157                    _key = key;
158            }
159    
160            public void setMessage(String message) {
161                    _message = message;
162            }
163    
164            public void setRowBreak(String rowBreak) {
165                    _rowBreak = HtmlUtil.unescape(rowBreak);
166            }
167    
168            public void setStartPage(String startPage) {
169                    _startPage = startPage;
170            }
171    
172            public void setTranslateMessage(boolean translateMessage) {
173                    _translateMessage = translateMessage;
174            }
175    
176            private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
177    
178            private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
179    
180            private String _endPage;
181            private Class<?> _exception;
182            private String _key;
183            private String _message;
184            private String _rowBreak = StringPool.BLANK;
185            private String _startPage;
186            private boolean _translateMessage = true;
187    
188    }