001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.components.valid; 017 018import com.jfinal.kit.JsonKit; 019import com.jfinal.kit.Ret; 020import com.jfinal.render.Render; 021import com.jfinal.render.RenderException; 022import io.jboot.utils.RequestUtil; 023 024/** 025 * 数据验证错误的渲染器,可以通过实现 JbootRenderFactory: {@link io.jboot.web.render.JbootRenderFactory} 来实现自定义渲染验证错误 026 */ 027public class ValidErrorRender extends Render { 028 029 protected static final String htmlContentType = "text/html;charset=" + getEncoding(); 030 protected static final String jsonContentType = "application/json;charset=" + getEncoding(); 031 protected static final String html_header = "<html><head><title>Invalid parameter</title></head>" + 032 "<body bgcolor='white'><center><h1>Invalid parameter</h1></center>" + 033 "<hr>"; 034 035 protected static final String poweredBy = "<center><a href='http://jboot.io' target='_blank'><b>Powered by Jboot</b></a></center>"; 036 protected static final String html_footer = "<hr>" + poweredBy + "</body></html>"; 037 038 protected int errorCode = ValidUtil.getErrorCode(); 039 protected final ValidException validException; 040 041 public ValidErrorRender(ValidException validException) { 042 this.validException = validException; 043 } 044 045 @Override 046 public void render() { 047 try { 048 if (RequestUtil.isJsonContentType(request) || RequestUtil.isAjaxRequest(request)) { 049 response.setStatus(200); 050 response.setContentType(jsonContentType); 051 response.getWriter().write(getErrorJson()); 052 } else { 053 response.setStatus(errorCode); 054 response.setContentType(htmlContentType); 055 response.getWriter().write(getErrorHtml()); 056 } 057 } catch (Exception ex) { 058 throw new RenderException(ex); 059 } 060 } 061 062 public String getErrorHtml() { 063 StringBuilder html = new StringBuilder(html_header); 064 html.append(validException.getFormName() == null ? "" : (validException.getFormName() + ": ")); 065 html.append(validException.getMessage()).append("<br />"); 066 return html.append(html_footer).toString(); 067 } 068 069 public String getErrorJson() { 070 Ret ret = Ret.fail(validException.getMessage()).set("errorCode", errorCode); 071 ret.set("throwable", validException.getClass().getName() + ": " + this.validException.getMessage()); 072 ret.set("errorMessage", validException.getReason()); 073 ret.set("formName", validException.getFormName()); 074 return JsonKit.toJson(ret); 075 } 076}