All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.afterturn.easypoi.wps.entity.resreq.WpsConvertResponse Maven / Gradle / Ivy

There is a newer version: 3.0.0-M4
Show newest version
package cn.afterturn.easypoi.wps.entity.resreq;


import java.io.Serializable;
import java.util.*;

/**
 * @author JueYue
 * @date 2021-05-21-5-25
 * @since 1.0
 */
public class WpsConvertResponse  extends WpsResponse implements Serializable {

    // keys
    private final static String MSG_KEY = "msg";
    private final static String STATUS_KEY = "status";
    private final static String CODE_KEY = "code";
    private final static String DATA_KEY = "data";

    // msg
    private final static String SUCCESS_MSG = "ok";

    // value
    private final static String SUCCESS_VALUE = "success";

    /**
     * 请求成功,并返回请求结果集
     *
     * @param data 返回到客户端的对象
     * @return Spring mvc ResponseEntity
     */
    public static ResponseEntity success(Map data, String msg) {
        return getObjectResponseEntity(data, msg);
    }

    public static ResponseEntity success(String msg) {
        Map result = new HashMap() {
            {
                put(STATUS_KEY, SUCCESS_VALUE);
                put(MSG_KEY, msg);
                put(CODE_KEY, 200);
            }
        };
        return getEntity(result);
    }

    public static ResponseEntity successOk() {
        Map result = new HashMap() {
            {
                put(STATUS_KEY, SUCCESS_VALUE);
                put(MSG_KEY, SUCCESS_MSG);
                put(CODE_KEY, 200);
            }
        };
        return getEntity(result);
    }

    /**
     * 请求成功,并返回请求结果集
     *
     * @param data 返回到客户端的对象
     * @return Spring mvc ResponseEntity
     */
    public static ResponseEntity success(Map data) {
        return getObjectResponseEntity(data, SUCCESS_MSG);
    }

    public static ResponseEntity success(Object data) {
        Map result = new HashMap() {
            {
                put(STATUS_KEY, SUCCESS_VALUE);
                put(MSG_KEY, SUCCESS_MSG);
                put(CODE_KEY, 200);
                put(DATA_KEY, data);
            }
        };
        return getEntity(result);
    }

    public static ResponseEntity success(byte[] data, String msg) {
        Map result = new HashMap() {
            {
                put(STATUS_KEY, SUCCESS_VALUE);
                put(MSG_KEY, msg);
                put(CODE_KEY, 200);
                put(DATA_KEY, data);
            }
        };
        return getEntity(result);
    }

    public static ResponseEntity success(boolean data, String msg) {
        Map result = new HashMap() {
            {
                put(STATUS_KEY, SUCCESS_VALUE);
                put(MSG_KEY, msg);
                put(CODE_KEY, 200);
                put(DATA_KEY, data);
            }
        };
        return getEntity(result);
    }

    public static ResponseEntity bad(String msg) {
        Map result = new HashMap() {
            {
                put(STATUS_KEY, SUCCESS_VALUE);
                put(MSG_KEY, msg);
                put(CODE_KEY, 400);
            }
        };
        return getEntity(result);
    }

    private static ResponseEntity getObjectResponseEntity(Map data, String msg) {
        Map result = new HashMap() {
            {
                put(STATUS_KEY, SUCCESS_VALUE);
                put(MSG_KEY, msg);
                put(CODE_KEY, 200);
                for (Entry entry : data.entrySet()) {
                    put(entry.getKey(), entry.getValue());
                }
            }
        };
        return getEntity(result);
    }

    private static ResponseEntity getEntity(Object body) {
        List contentType = new ArrayList() {
            {
                add("application/json;charset=utf-8");
            }
        };
        Map headers = new HashMap() {
            {
                put("Content-Type", contentType);
            }
        };
        return new ResponseEntity<>(body, headers, 200);
    }
}