001
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.IncludeTag;
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.BodyTag;
029
030
033 public class ErrorTag extends IncludeTag implements BodyTag {
034
035 @Override
036 public int doStartTag() throws JspException {
037 setAttributeNamespace(_ATTRIBUTE_NAMESPACE);
038
039 PortletRequest portletRequest = (PortletRequest)request.getAttribute(
040 JavaConstants.JAVAX_PORTLET_REQUEST);
041
042 if (!SessionErrors.contains(portletRequest, _key)) {
043 return SKIP_BODY;
044 }
045
046 Object value = getException(portletRequest);
047
048 if (value == null) {
049 return SKIP_BODY;
050 }
051
052 pageContext.setAttribute("errorException", value);
053
054 return super.doStartTag();
055 }
056
057 public void setException(Class<?> exception) {
058 _exception = exception;
059
060 if (_exception != null) {
061 _key = _exception.getName();
062 }
063 }
064
065 public void setFocusField(String focusField) {
066 _focusField = focusField;
067 }
068
069 public void setKey(String key) {
070 _key = key;
071 }
072
073 public void setMessage(String message) {
074 _message = message;
075 }
076
077 public void setRowBreak(String rowBreak) {
078 _rowBreak = HtmlUtil.unescape(rowBreak);
079 }
080
081 public void setTranslateMessage(boolean translateMessage) {
082 _translateMessage = translateMessage;
083 }
084
085 @Override
086 protected void cleanUp() {
087 super.cleanUp();
088
089 _exception = null;
090 _focusField = null;
091 _key = null;
092 _message = null;
093 _rowBreak = StringPool.BLANK;
094 _translateMessage = true;
095 }
096
097 protected Object getException(PortletRequest portletRequest) {
098 Object value = null;
099
100 if (_exception != null) {
101 value = SessionErrors.get(portletRequest, _exception.getName());
102 }
103 else {
104 value = SessionErrors.get(portletRequest, _key);
105 }
106
107 return value;
108 }
109
110 @Override
111 protected String getPage() {
112 return _PAGE;
113 }
114
115 @Override
116 protected int processStartTag() throws Exception {
117 return EVAL_BODY_BUFFERED;
118 }
119
120 @Override
121 protected void setAttributes(HttpServletRequest request) {
122 PortletRequest portletRequest = (PortletRequest)request.getAttribute(
123 JavaConstants.JAVAX_PORTLET_REQUEST);
124
125 request.setAttribute("liferay-ui:error:key", _key);
126 request.setAttribute("liferay-ui:error:message", _message);
127 request.setAttribute("liferay-ui:error:rowBreak", _rowBreak);
128 request.setAttribute(
129 "liferay-ui:error:translateMessage",
130 String.valueOf(_translateMessage));
131
132 if (SessionErrors.contains(portletRequest, _key)) {
133 String errorMarkerKey = (String)request.getAttribute(
134 "liferay-ui:error-marker:key");
135 String errorMarkerValue = (String)request.getAttribute(
136 "liferay-ui:error-marker:value");
137
138 if (Validator.isNotNull(errorMarkerKey) &&
139 Validator.isNotNull(errorMarkerValue)) {
140
141 request.setAttribute(errorMarkerKey, errorMarkerValue);
142
143 Object exception = getException(portletRequest);
144
145 if (exception instanceof Exception) {
146 request.setAttribute(
147 "liferay-ui:error:exception", exception);
148 }
149
150 request.setAttribute(
151 "liferay-ui:error:focusField", _focusField);
152 }
153 }
154 }
155
156 private static final String _ATTRIBUTE_NAMESPACE = "liferay-ui:error:";
157
158 private static final String _PAGE = "/html/taglib/ui/error/page.jsp";
159
160 private Class<?> _exception;
161 private String _focusField;
162 private String _key;
163 private String _message;
164 private String _rowBreak = StringPool.BLANK;
165 private boolean _translateMessage = true;
166
167 }