/
PageError.java
175 lines (142 loc) · 5.17 KB
/
PageError.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.web.page.error;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.application.PageDescriptor;
import com.evolveum.midpoint.web.component.AjaxButton;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.http.WebResponse;
import org.springframework.http.HttpStatus;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Base class for error web pages.
*
* @author lazyman
*/
@PageDescriptor(url = "/error", permitAll = true)
public class PageError extends PageBase {
private static final String ID_CODE = "code";
private static final String ID_LABEL = "label";
private static final String ID_MESSAGE = "message";
private static final String ID_ERROR_MESSAGE = "errorMessage";
private static final String ID_BACK = "back";
private static final String ID_HOME = "home";
private static final Trace LOGGER = TraceManager.getTrace(PageError.class);
private Integer code;
private String exClass;
private String exMessage;
protected String errorMessageKey;
public PageError() {
this(500);
}
public PageError(Integer code) {
this(code, null);
}
public PageError(Exception ex) {
this(500, ex);
}
public PageError(Integer code, Exception ex) {
this.code = code;
if (ex == null) {
// Log this on debug level, this is normal during application initialization
LOGGER.debug("Creating error page for code {}, no exception", code);
} else {
LOGGER.warn("Creating error page for code {}, exception {}: {}", ex.getClass().getName(), ex.getMessage(), ex);
}
Label codeLabel = new Label(ID_CODE, code);
add(codeLabel);
Label errorMessage = new Label(ID_ERROR_MESSAGE, createStringResource(getErrorMessageKey()));
add(errorMessage);
String errorLabel = "Unexpected error";
if (code != null) {
HttpStatus httpStatus = HttpStatus.valueOf(code);
if (httpStatus != null) {
errorLabel = httpStatus.getReasonPhrase();
}
}
Label labelLabel = new Label(ID_LABEL, errorLabel);
add(labelLabel);
if (ex != null) {
exClass = ex.getClass().getName();
exMessage = ex.getMessage();
}
final IModel<String> message = new AbstractReadOnlyModel<String>() {
@Override
public String getObject() {
if (exClass == null) {
return null;
}
SimpleDateFormat df = new SimpleDateFormat();
return df.format(new Date()) + "\t" + exClass + ": " + exMessage;
}
};
Label label = new Label(ID_MESSAGE, message);
label.add(new VisibleEnableBehaviour() {
@Override
public boolean isVisible() {
return StringUtils.isNotEmpty(message.getObject());
}
});
add(label);
AjaxButton back = new AjaxButton(ID_BACK, createStringResource("PageError.button.back")) {
@Override
public void onClick(AjaxRequestTarget target) {
backPerformed(target);
}
};
add(back);
AjaxButton home = new AjaxButton(ID_HOME, createStringResource("PageError.button.home")) {
@Override
public void onClick(AjaxRequestTarget target) {
homePerformed(target);
}
};
add(home);
}
private int getCode() {
return code != null ? code : 500;
}
@Override
protected void configureResponse(WebResponse response) {
super.configureResponse(response);
response.setStatus(getCode());
}
@Override
public boolean isVersioned() {
return false;
}
@Override
public boolean isErrorPage() {
return true;
}
private void homePerformed(AjaxRequestTarget target) {
setResponsePage(getMidpointApplication().getHomePage());
}
private void backPerformed(AjaxRequestTarget target) {
redirectBack();
}
protected String getErrorMessageKey() {
return "PageError.message";
}
}