Search in sources :

Example 1 with DefinedException

use of com.rebuild.core.DefinedException in project rebuild by getrebuild.

the class ChartsFactory method create.

/**
 * @param config
 * @param user
 * @return
 * @throws ChartsException
 */
public static ChartData create(JSONObject config, ID user) throws ChartsException {
    String entityName = config.getString("entity");
    if (!MetadataHelper.containsEntity(entityName)) {
        throw new ChartsException(Language.L("源实体 [%s] 已不存在", entityName.toUpperCase()));
    }
    Entity entity = MetadataHelper.getEntity(entityName);
    if (user == null || !Application.getPrivilegesManager().allowRead(user, entity.getEntityCode())) {
        throw new DefinedException(Language.L("没有读取 %s 的权限", EasyMetaFactory.getLabel(entity)));
    }
    String type = config.getString("type");
    if ("INDEX".equalsIgnoreCase(type)) {
        return (ChartData) new IndexChart(config).setUser(user);
    } else if ("TABLE".equalsIgnoreCase(type)) {
        return (ChartData) new TableChart(config).setUser(user);
    } else if ("LINE".equalsIgnoreCase(type)) {
        return (ChartData) new LineChart(config).setUser(user);
    } else if ("BAR".equalsIgnoreCase(type)) {
        return (ChartData) new BarChart(config).setUser(user);
    } else if ("PIE".equalsIgnoreCase(type)) {
        return (ChartData) new PieChart(config).setUser(user);
    } else if ("FUNNEL".equalsIgnoreCase(type)) {
        return (ChartData) new FunnelChart(config).setUser(user);
    } else if ("TREEMAP".equalsIgnoreCase(type)) {
        return (ChartData) new TreemapChart(config).setUser(user);
    } else if ("RADAR".equalsIgnoreCase(type)) {
        return (ChartData) new RadarChart(config).setUser(user);
    } else if ("SCATTER".equalsIgnoreCase(type)) {
        return (ChartData) new ScatterChart(config).setUser(user);
    } else {
        for (BuiltinChart ch : getBuiltinCharts()) {
            if (ch.getChartType().equalsIgnoreCase(type)) {
                return (ChartData) ((ChartData) ch).setUser(user);
            }
        }
    }
    throw new ChartsException("Unknown chart type : " + type);
}
Also used : Entity(cn.devezhao.persist4j.Entity) BuiltinChart(com.rebuild.core.service.dashboard.charts.builtin.BuiltinChart) DefinedException(com.rebuild.core.DefinedException)

Example 2 with DefinedException

use of com.rebuild.core.DefinedException in project rebuild by getrebuild.

the class RebuildWebConfigurer method createError.

/**
 * @see ControllerRespBodyAdvice
 */
private ModelAndView createError(HttpServletRequest request, Exception ex, HttpStatus status, Map<String, Object> model) {
    // IGNORED
    if (request.getRequestURI().contains("/assets/"))
        return null;
    ModelAndView error;
    if (ServletUtils.isAjaxRequest(request)) {
        error = new ModelAndView(new FastJsonJsonView());
    } else {
        error = new ModelAndView("/error/error");
        error.getModelMap().put(WebConstants.$BUNDLE, AppUtils.getReuqestBundle(request));
    }
    int errorCode = status.value();
    String errorMsg = ex == null ? null : KnownExceptionConverter.convert2ErrorMsg(ex);
    if (errorMsg == null)
        errorMsg = getErrorMessage(request, ex);
    String errorLog = "\n++ EXECUTE REQUEST ERROR(s) TRACE +++++++++++++++++++++++++++++++++++++++++++++" + "\nUser    : " + ObjectUtils.defaultIfNull(AppUtils.getRequestUser(request), "-") + "\nIP      : " + ServletUtils.getRemoteAddr(request) + "\nUA      : " + StringUtils.defaultIfEmpty(request.getHeader("user-agent"), "-") + "\nURL(s)  : " + getRequestUrls(request) + "\nMessage : " + errorMsg + (model != null ? (" " + model) : "") + "\n";
    if (ex instanceof DefinedException) {
        errorCode = ((DefinedException) ex).getErrorCode();
        log.warn(errorLog, Application.devMode() ? ex : null);
    } else {
        log.error(errorLog, ex);
        if (ex != null && ThrowableUtils.getRootCause(ex) instanceof TemplateInputException && errorMsg.contains("Error resolving template")) {
            errorMsg = Language.L("访问的页面/资源不存在");
        }
    }
    error.getModel().put("error_code", errorCode);
    error.getModel().put("error_msg", errorMsg);
    if (ex != null && Application.devMode()) {
        error.getModel().put("error_stack", ThrowableUtils.extractStackTrace(ex));
    }
    return error;
}
Also used : FastJsonJsonView(com.alibaba.fastjson.support.spring.FastJsonJsonView) ModelAndView(org.springframework.web.servlet.ModelAndView) DefinedException(com.rebuild.core.DefinedException) TemplateInputException(org.thymeleaf.exceptions.TemplateInputException)

Example 3 with DefinedException

use of com.rebuild.core.DefinedException in project rebuild by getrebuild.

the class RecycleRestore method conver2Record.

/**
 * 转换成 Record 对象,返回多条是可能存在明细
 *
 * @param content
 * @param recordId
 * @return
 */
private List<Record> conver2Record(JSONObject content, ID recordId) {
    if (!MetadataHelper.containsEntity(recordId.getEntityCode())) {
        throw new DefinedException(Language.L("所属实体已经不存在,无法恢复"));
    }
    JSONArray detailList = content.getJSONArray(RecycleBean.NAME_DETAILLIST);
    if (detailList != null) {
        content.remove(RecycleBean.NAME_DETAILLIST);
    }
    List<Record> records = new ArrayList<>();
    Entity entity = MetadataHelper.getEntity(recordId.getEntityCode());
    Record record = new RestoreRecordCreator(entity, content).create(true);
    records.add(record);
    Entity detailEntity = entity.getDetailEntity();
    if (detailList != null && detailEntity != null) {
        for (Object o : detailList) {
            Record detail = new RestoreRecordCreator(detailEntity, (JSONObject) o).create(true);
            records.add(detail);
        }
    }
    return records;
}
Also used : Entity(cn.devezhao.persist4j.Entity) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) ArrayList(java.util.ArrayList) Record(cn.devezhao.persist4j.Record) JSONObject(com.alibaba.fastjson.JSONObject) DefinedException(com.rebuild.core.DefinedException)

Example 4 with DefinedException

use of com.rebuild.core.DefinedException in project rebuild by getrebuild.

the class RebuildWebInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    response.addHeader("X-RB-Server", ServerStatus.STARTUP_ONCE + "/" + Application.BUILD);
    if (Application.isWaitLoad()) {
        throw new DefinedException(CODE_STARTING, "Please wait while REBUILD starting up ...");
    }
    if (SystemDiagnosis._DENIEDMSG != null) {
        throw new DefinedException(CODE_STARTING, SystemDiagnosis._DENIEDMSG);
    }
    final String ipAddr = ServletUtils.getRemoteAddr(request);
    UserContextHolder.setReqip(ipAddr);
    // Locale
    final String locale = detectLocale(request, response);
    UserContextHolder.setLocale(locale);
    final RequestEntry requestEntry = new RequestEntry(request, locale);
    REQUEST_ENTRY.set(requestEntry);
    // Lang
    request.setAttribute(WebConstants.LOCALE, requestEntry.getLocale());
    request.setAttribute(WebConstants.$BUNDLE, Application.getLanguage().getBundle(requestEntry.getLocale()));
    final String requestUri = requestEntry.getRequestUri();
    // 服务暂不可用
    if (!Application.isReady()) {
        final boolean isError = requestUri.endsWith("/error") || requestUri.contains("/error/");
        // 已安装
        if (checkInstalled()) {
            log.error("Server Unavailable : " + requestEntry);
            if (isError) {
                return true;
            } else {
                sendRedirect(response, "/error/server-status", null);
                return false;
            }
        } else // 未安装
        if (!(requestUri.contains("/setup/") || requestUri.contains("/commons/theme/") || isError)) {
            sendRedirect(response, "/setup/install", null);
            return false;
        } else {
            return true;
        }
    }
    final ID requestUser = requestEntry.getRequestUser();
    boolean skipCheckSafeUse;
    // 用户验证
    if (requestUser != null) {
        boolean adminVerified = AppUtils.isAdminVerified(request);
        // 管理中心
        if (requestUri.contains("/admin/") && !adminVerified) {
            if (isHtmlRequest(request)) {
                sendRedirect(response, "/user/admin-verify", requestUri);
            } else {
                ServletUtils.writeJson(response, RespBody.error(HttpStatus.FORBIDDEN.value()).toJSONString());
            }
            return false;
        }
        UserContextHolder.setUser(requestUser);
        // User
        request.setAttribute(WebConstants.$USER, Application.getUserStore().getUser(requestUser));
        request.setAttribute(ZeroEntry.AllowCustomNav.name(), Application.getPrivilegesManager().allow(requestUser, ZeroEntry.AllowCustomNav));
        if (isHtmlRequest(request)) {
            // Last active
            Application.getSessionStore().storeLastActive(request);
            // Nav collapsed
            String sidebarCollapsed = ServletUtils.readCookie(request, "rb.sidebarCollapsed");
            String sideCollapsedClazz = BooleanUtils.toBoolean(sidebarCollapsed) ? "rb-collapsible-sidebar-collapsed" : "";
            // Aside collapsed
            if (!(requestUri.contains("/admin/") || requestUri.contains("/setup/"))) {
                String asideCollapsed = ServletUtils.readCookie(request, "rb.asideCollapsed");
                if (BooleanUtils.toBoolean(asideCollapsed))
                    sideCollapsedClazz += " rb-aside-collapsed";
            }
            request.setAttribute("sideCollapsedClazz", sideCollapsedClazz);
        }
        // 超管可访问
        skipCheckSafeUse = UserHelper.isSuperAdmin(requestUser);
    } else if (!isIgnoreAuth(requestUri)) {
        // 外部表单特殊处理(媒体字段上传/预览)
        if (requestUri.contains("/filex/") && CsrfToken.verify(request, false)) {
            return true;
        }
        log.warn("Unauthorized access {}", RebuildWebConfigurer.getRequestUrls(request));
        if (isHtmlRequest(request)) {
            sendRedirect(response, "/user/login", requestUri);
        } else {
            ServletUtils.writeJson(response, RespBody.error(HttpStatus.UNAUTHORIZED.value()).toJSONString());
        }
        return false;
    } else {
        skipCheckSafeUse = true;
    }
    if (!skipCheckSafeUse)
        checkSafeUse(ipAddr, requestUri);
    return true;
}
Also used : DefinedException(com.rebuild.core.DefinedException) ID(cn.devezhao.persist4j.engine.ID)

Aggregations

DefinedException (com.rebuild.core.DefinedException)4 Entity (cn.devezhao.persist4j.Entity)2 Record (cn.devezhao.persist4j.Record)1 ID (cn.devezhao.persist4j.engine.ID)1 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 FastJsonJsonView (com.alibaba.fastjson.support.spring.FastJsonJsonView)1 BuiltinChart (com.rebuild.core.service.dashboard.charts.builtin.BuiltinChart)1 ArrayList (java.util.ArrayList)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 TemplateInputException (org.thymeleaf.exceptions.TemplateInputException)1