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                                    }
071                            }
072    
073                            return EVAL_PAGE;
074                    }
075                    catch (Exception e) {
076                            throw new JspException(e);
077                    }
078            }
079    
080            @Override
081            public int doStartTag() throws JspException {
082                    try {
083                            HttpServletRequest request =
084                                    (HttpServletRequest)pageContext.getRequest();
085    
086                            PortletRequest portletRequest =
087                                    (PortletRequest)request.getAttribute(
088                                            JavaConstants.JAVAX_PORTLET_REQUEST);
089    
090                            request.setAttribute("liferay-ui:error:key", _key);
091                            request.setAttribute("liferay-ui:error:message", _message);
092                            request.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
093                            request.setAttribute(
094                                    "liferay-ui:error:translateMessage",
095                                    String.valueOf(_translateMessage));
096    
097                            if (Validator.isNotNull(_message)) {
098                                    return SKIP_BODY;
099                            }
100    
101                            if (SessionErrors.contains(portletRequest, _key)) {
102                                    Object value = null;
103    
104                                    if (_exception != null) {
105                                            value = SessionErrors.get(
106                                                    portletRequest, _exception.getName());
107                                    }
108                                    else {
109                                            value = SessionErrors.get(portletRequest, _key);
110                                    }
111    
112                                    PortalIncludeUtil.include(pageContext, getStartPage());
113    
114                                    if (value != null) {
115                                            pageContext.setAttribute("errorException", value);
116                                    }
117    
118                                    return EVAL_BODY_INCLUDE;
119                            }
120    
121                            return SKIP_BODY;
122                    }
123                    catch (Exception e) {
124                            throw new JspException(e);
125                    }
126            }
127    
128            public void setEndPage(String endPage) {
129                    _endPage = endPage;
130            }
131    
132            public void setException(Class<?> exception) {
133                    _exception = exception;
134    
135                    if (_exception != null) {
136                            _key = _exception.getName();
137                    }
138            }
139    
140            public void setKey(String key) {
141                    _key = key;
142            }
143    
144            public void setMessage(String message) {
145                    _message = message;
146            }
147    
148            public void setRowBreak(String rowBreak) {
149                    _rowBreak = HtmlUtil.unescape(rowBreak);
150            }
151    
152            public void setStartPage(String startPage) {
153                    _startPage = startPage;
154            }
155    
156            public void setTranslateMessage(boolean translateMessage) {
157                    _translateMessage = translateMessage;
158            }
159    
160            protected String getEndPage() {
161                    if (Validator.isNull(_endPage)) {
162                            return _END_PAGE;
163                    }
164                    else {
165                            return _endPage;
166                    }
167            }
168    
169            protected String getStartPage() {
170                    if (Validator.isNull(_startPage)) {
171                            return _START_PAGE;
172                    }
173                    else {
174                            return _startPage;
175                    }
176            }
177    
178            private static final String _END_PAGE = "/html/taglib/ui/error/end.jsp";
179    
180            private static final String _START_PAGE = "/html/taglib/ui/error/start.jsp";
181    
182            private String _endPage;
183            private Class<?> _exception;
184            private String _key;
185            private String _message;
186            private String _rowBreak = StringPool.BLANK;
187            private String _startPage;
188            private boolean _translateMessage = true;
189    
190    }