Search in sources :

Example 1 with RespBody

use of com.rebuild.api.RespBody in project rebuild by getrebuild.

the class ControllerRespBodyAdvice method beforeBodyWrite.

@Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
    HttpServletRequest request = ((ServletServerHttpRequest) serverHttpRequest).getServletRequest();
    HttpServletResponse response = ((ServletServerHttpResponse) serverHttpResponse).getServletResponse();
    // Controller send status of error
    if (response.getStatus() != 200) {
        if (o instanceof Map) {
            String path404 = (String) ((Map<?, ?>) o).get("path");
            if (path404 != null && path404.endsWith(".map")) {
                return o;
            }
        }
        log.warn("Response Error Status : {}", o);
        return o;
    }
    // @ResponseBody 方法返回 `null` `String` 时 mediaType=TEXT_PLAIN
    if (MediaType.TEXT_PLAIN.equals(mediaType)) {
        // @Deprecated
        log.warn("Force conversion TEXT_PLAIN to APPLICATION_JSON : {}", request.getRequestURI());
        response.addHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
    }
    if (o instanceof RespBody) {
        return ((RespBody) o).toJSON();
    } else {
        return RespBody.ok(o).toJSON();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) Map(java.util.Map) RespBody(com.rebuild.api.RespBody)

Example 2 with RespBody

use of com.rebuild.api.RespBody in project rebuild by getrebuild.

the class GeneralOperatingController method save.

@PostMapping("record-save")
public JSONAware save(HttpServletRequest request) {
    final ID user = getRequestUser(request);
    final JSON formJson = ServletUtils.getRequestJson(request);
    final Object details = ((JSONObject) formJson).remove(GeneralEntityService.HAS_DETAILS);
    Record record;
    try {
        record = EntityHelper.parse((JSONObject) formJson, user);
    } catch (DataSpecificationException known) {
        log.warn(">>>>> {}", known.getLocalizedMessage());
        return RespBody.error(known.getLocalizedMessage());
    }
    // 非业务实体(兼容所有类型的实体)
    if (!MetadataHelper.isBusinessEntity(record.getEntity())) {
        return CommonOperatingController.saveRecord(record);
    }
    // 明细
    List<Record> detailsList = new ArrayList<>();
    if (details != null) {
        try {
            for (Object d : (JSONArray) details) {
                Record detail = EntityHelper.parse((JSONObject) d, user);
                detailsList.add(detail);
            }
        } catch (DataSpecificationException known) {
            log.warn(">>>>> {}", known.getLocalizedMessage());
            return RespBody.error(known.getLocalizedMessage());
        }
    }
    final EntityService ies = Application.getEntityService(record.getEntity().getEntityCode());
    // 检查重复值
    List<Record> repeated = ies.getAndCheckRepeated(record, 20);
    if (!repeated.isEmpty()) {
        return new RespBody(DefinedException.CODE_RECORDS_REPEATED, Language.L("存在重复记录"), buildRepeatedData(repeated));
    }
    if (!detailsList.isEmpty()) {
        record.setObjectValue(GeneralEntityService.HAS_DETAILS, detailsList);
        GeneralEntityServiceContextHolder.setRepeatedCheckMode(GeneralEntityServiceContextHolder.RCM_CHECK_DETAILS);
    }
    try {
        record = ies.createOrUpdate(record);
    } catch (RepeatedRecordsException know) {
        return new RespBody(DefinedException.CODE_RECORDS_REPEATED, Language.L("存在重复记录"), buildRepeatedData(know.getRepeatedRecords()));
    } catch (AccessDeniedException | DataSpecificationException known) {
        log.warn(">>>>> {}", known.getLocalizedMessage());
        return RespBody.error(known.getLocalizedMessage());
    } catch (GenericJdbcException ex) {
        String known = KnownExceptionConverter.convert2ErrorMsg(ex);
        if (known != null)
            return RespBody.error(known);
        log.error(null, ex);
        return RespBody.error(ex.getLocalizedMessage());
    } finally {
        // 确保清除
        GeneralEntityServiceContextHolder.getRepeatedCheckModeOnce();
    }
    JSONObject ret = new JSONObject();
    ret.put("id", record.getPrimary());
    // 单字段修改立即返回新值
    boolean viaSingle = getBoolParameter(request, "single");
    if (viaSingle) {
        for (String field : record.getAvailableFields()) {
            Field fieldMeta = record.getEntity().getField(field);
            if (MetadataHelper.isCommonsField(field) || fieldMeta.getType() == FieldType.PRIMARY) {
                continue;
            }
            Object newValue = FormsBuilder.instance.wrapFieldValue(record, EasyMetaFactory.valueOf(fieldMeta), null);
            ret.put(field, newValue);
        }
    }
    return ret;
}
Also used : AccessDeniedException(cn.devezhao.bizz.security.AccessDeniedException) JSONArray(com.alibaba.fastjson.JSONArray) JSON(com.alibaba.fastjson.JSON) DataSpecificationException(com.rebuild.core.service.DataSpecificationException) RespBody(com.rebuild.api.RespBody) Field(cn.devezhao.persist4j.Field) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) Record(cn.devezhao.persist4j.Record) ID(cn.devezhao.persist4j.engine.ID) GenericJdbcException(cn.devezhao.persist4j.exception.jdbc.GenericJdbcException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

RespBody (com.rebuild.api.RespBody)2 AccessDeniedException (cn.devezhao.bizz.security.AccessDeniedException)1 Field (cn.devezhao.persist4j.Field)1 Record (cn.devezhao.persist4j.Record)1 ID (cn.devezhao.persist4j.engine.ID)1 GenericJdbcException (cn.devezhao.persist4j.exception.jdbc.GenericJdbcException)1 JSON (com.alibaba.fastjson.JSON)1 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 DataSpecificationException (com.rebuild.core.service.DataSpecificationException)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)1 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1