001    /**
002     * Copyright (c) 2000-2013 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            @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                                            request.setAttribute(
071                                                    "liferay-ui:error:focusField", _focusField);
072                                    }
073                            }
074    
075                            return EVAL_PAGE;
076                    }
077                    catch (Exception e) {
078                            throw new JspException(e);
079                    }
080            }
081    
082            @Override
083            public int doStartTag() throws JspException {
084                    try {
085                            HttpServletRequest request =
086                                    (HttpServletRequest)pageContext.getRequest();
087    
088                            PortletRequest portletRequest =
089                                    (PortletRequest)request.getAttribute(
090                                            JavaConstants.JAVAX_PORTLET_REQUEST);
091    
092                            request.setAttribute("liferay-ui:error:key", _key);
093                            request.setAttribute("liferay-ui:error:message", _message);
094                            request.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
095                            request.setAttribute(
096                                    "liferay-ui:error:translateMessage",
097                                    String.valueOf(_translateMessage));
098    
099                            if (Validator.isNotNull(_message)) {
100                                    return SKIP_BODY;
101                            }
102    
103                            if (SessionErrors.contains(portletRequest, _key)) {
104                                    Object value = null;
105    
106                                    if (_exception != null) {
107                                            value = SessionErrors.get(
108                                                    portletRequest, _exception.getName());
109                                    }
110                                    else {
111                                            value = SessionErrors.get(portletRequest, _key);
112                                    }
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 String getStartPage() {
176                    if (Validator.isNull(_startPage)) {
177                            return _START_PAGE;
178                    }
179                    else {
180                            return _startPage;
181                    }
182            }
183    
184            private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
185    
186            private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
187    
188            private String _endPage;
189            private Class<?> _exception;
190            private String _focusField;
191            private String _key;
192            private String _message;
193            private String _rowBreak = StringPool.BLANK;
194            private String _startPage;
195            private boolean _translateMessage = true;
196    
197    }