org.unique.web.render.impl.ErrorRender Maven / Gradle / Ivy
/**
* Copyright (c) 2014-2015, biezhi 王爵 ([email protected])
*
* 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 org.unique.web.render.impl;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.unique.Const;
import org.unique.commons.io.IOUtil;
import org.unique.web.render.Render;
import org.unique.web.render.RenderFactory;
/**
* ErrorRender.
*/
public class ErrorRender implements Render {
protected static final String contentType = "text/html;charset=" + Const.ENCODING;
protected static final String html404 = "404 Not Found 404 Not Found
Unique Web ";
protected static final String html500 = "500 Internal Server Error 500 Internal Server Error
Unique Web ";
protected static final String html401 = "401 Unauthorized 401 Unauthorized
Unique Web ";
protected static final String html403 = "403 Forbidden 403 Forbidden
Unique Web ";
protected int errorCode;
private String view;
public ErrorRender(int errorCode, String view) {
this.errorCode = errorCode;
this.view = view;
}
public void render(HttpServletRequest request, HttpServletResponse response, String viewPath) {
response.setStatus(getErrorCode());
// render with view
if (view != null) {
RenderFactory.single().getDefaultRender().render(request, response, view);
return;
}
// render with html content
PrintWriter writer = null;
try {
response.setContentType(contentType);
writer = response.getWriter();
writer.write(getErrorHtml());
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
finally {
IOUtil.closeQuietly(writer);
}
}
public String getErrorHtml() {
int errorCode = getErrorCode();
if (errorCode == 404)
return html404;
if (errorCode == 500)
return html500;
if (errorCode == 401)
return html401;
if (errorCode == 403)
return html403;
return "" + errorCode + " Error " + errorCode + " Error
Unique Web ";
}
public int getErrorCode() {
return errorCode;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy