001    /**
002     * Copyright (c) 2000-present 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.SessionErrors;
018    import com.liferay.portal.kernel.util.HtmlUtil;
019    import com.liferay.portal.kernel.util.JavaConstants;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.taglib.util.PortalIncludeUtil;
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            @Override
036            public int doEndTag() throws JspException {
037                    try {
038                            HttpServletRequest request =
039                                    (HttpServletRequest)pageContext.getRequest();
040    
041                            PortletRequest portletRequest =
042                                    (PortletRequest)request.getAttribute(
043                                            JavaConstants.JAVAX_PORTLET_REQUEST);
044    
045                            boolean includeEndPage = false;
046    
047                            if (Validator.isNull(_key)) {
048                                    if (!SessionErrors.isEmpty(portletRequest)) {
049                                            includeEndPage = true;
050                                    }
051                            }
052                            else {
053                                    if (SessionErrors.contains(portletRequest, _key)) {
054                                            includeEndPage = true;
055                                    }
056                            }
057    
058                            if (includeEndPage) {
059                                    PortalIncludeUtil.include(pageContext, getEndPage());
060    
061                                    String errorMarkerKey = (String)request.getAttribute(
062                                            "liferay-ui:error-marker:key");
063                                    String errorMarkerValue = (String)request.getAttribute(
064                                            "liferay-ui:error-marker:value");
065    
066                                    if (Validator.isNotNull(errorMarkerKey) &&
067                                            Validator.isNotNull(errorMarkerValue)) {
068    
069                                            request.setAttribute(errorMarkerKey, errorMarkerValue);
070    
071                                            Object exception = getException(portletRequest);
072    
073                                            if (exception instanceof Exception) {
074                                                    request.setAttribute(
075                                                            "liferay-ui:error:exception", exception);
076                                            }
077    
078                                            request.setAttribute(
079                                                    "liferay-ui:error:focusField", _focusField);
080                                    }
081                            }
082    
083                            return EVAL_PAGE;
084                    }
085                    catch (Exception e) {
086                            throw new JspException(e);
087                    }
088            }
089    
090            @Override
091            public int doStartTag() throws JspException {
092                    try {
093                            HttpServletRequest request =
094                                    (HttpServletRequest)pageContext.getRequest();
095    
096                            PortletRequest portletRequest =
097                                    (PortletRequest)request.getAttribute(
098                                            JavaConstants.JAVAX_PORTLET_REQUEST);
099    
100                            request.setAttribute("liferay-ui:error:key", _key);
101                            request.setAttribute("liferay-ui:error:message", _message);
102                            request.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
103                            request.setAttribute(
104                                    "liferay-ui:error:translateMessage",
105                                    String.valueOf(_translateMessage));
106    
107                            if (Validator.isNotNull(_message)) {
108                                    return SKIP_BODY;
109                            }
110    
111                            if (SessionErrors.contains(portletRequest, _key)) {
112                                    Object value = getException(portletRequest);
113    
114                                    PortalIncludeUtil.include(pageContext, getStartPage());
115    
116                                    if (value != null) {
117                                            pageContext.setAttribute("errorException", value);
118                                    }
119    
120                                    return EVAL_BODY_INCLUDE;
121                            }
122    
123                            return SKIP_BODY;
124                    }
125                    catch (Exception e) {
126                            throw new JspException(e);
127                    }
128            }
129    
130            public void setEndPage(String endPage) {
131                    _endPage = endPage;
132            }
133    
134            public void setException(Class<?> exception) {
135                    _exception = exception;
136    
137                    if (_exception != null) {
138                            _key = _exception.getName();
139                    }
140            }
141    
142            public void setFocusField(String focusField) {
143                    _focusField = focusField;
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 setRowBreak(String rowBreak) {
155                    _rowBreak = HtmlUtil.unescape(rowBreak);
156            }
157    
158            public void setStartPage(String startPage) {
159                    _startPage = startPage;
160            }
161    
162            public void setTranslateMessage(boolean translateMessage) {
163                    _translateMessage = translateMessage;
164            }
165    
166            protected String getEndPage() {
167                    if (Validator.isNull(_endPage)) {
168                            return _END_PAGE;
169                    }
170                    else {
171                            return _endPage;
172                    }
173            }
174    
175            protected Object getException(PortletRequest portletRequest) {
176                    Object value = null;
177    
178                    if (_exception != null) {
179                            value = SessionErrors.get(portletRequest, _exception.getName());
180                    }
181                    else {
182                            value = SessionErrors.get(portletRequest, _key);
183                    }
184    
185                    return value;
186            }
187    
188            protected String getStartPage() {
189                    if (Validator.isNull(_startPage)) {
190                            return _START_PAGE;
191                    }
192                    else {
193                            return _startPage;
194                    }
195            }
196    
197            private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
198    
199            private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
200    
201            private String _endPage;
202            private Class<?> _exception;
203            private String _focusField;
204            private String _key;
205            private String _message;
206            private String _rowBreak = StringPool.BLANK;
207            private String _startPage;
208            private boolean _translateMessage = true;
209    
210    }