CustomAssert.java 1.59 KB
package com.objecteye.utils;

import com.objecteye.exception.CustomXException;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

public class CustomAssert extends Assert {


    public static void checkHeader(String header) {
        if (!header.equals("application/json;charset=UTF-8")) {
            throw new CustomXException(CustomInfoCollections.REQUEST_HEADER_CONTENT_ERROR.getMsg(),
                    CustomInfoCollections.REQUEST_HEADER_CONTENT_ERROR.getCode());
        }
    }

    public static void checkField(Map<String, Object> map, String[] keys) {
        for (String key : keys) {
            if (map.get(key) == null) {
                throw new CustomXException(CustomInfoCollections.REQUEST_BODY_FIELD_INCOMPLETE.getMsg(),
                        CustomInfoCollections.REQUEST_BODY_FIELD_INCOMPLETE.getCode());
            }
        }
    }

    public static void checkSimpleMapParam(Map<String, Object> map, String[] keys) {
        for (String key : keys) {
            if (StringUtils.isEmpty(map.get(key))) {
                throw new CustomXException(CustomInfoCollections.REQUEST_BODY_IS_EMPTY.getMsg(),
                        CustomInfoCollections.REQUEST_BODY_IS_EMPTY.getCode());
            }
        }
    }

    public static void checkFileNull(MultipartFile file) {
        if (null == file) {
            throw new CustomXException(CustomInfoCollections.IMAGE_PARSE_EXCEPTION.getMsg(),
                    CustomInfoCollections.IMAGE_PARSE_EXCEPTION.getCode());
        }
    }
}