package com.yvan.springmvc;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.view.AbstractView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Slf4j
public class StringView extends AbstractView {

    protected String jsonString;
    private String contentType;

    public StringView(String jsonString) {
        this(jsonString, "text/html;charset=utf-8");
    }

    public StringView(String jsonString, String contentType) {
        this.jsonString = jsonString;
        this.contentType = contentType;
    }

    @Override
    protected void renderMergedOutputModel(@SuppressWarnings("rawtypes") Map model, HttpServletRequest request,
                                           HttpServletResponse response) throws Exception {
        boolean useGzip = ResponseUtils.isSupportGzip(request);
        try {
            ResponseUtils.writeJSONToResponse(response, ResponseUtils.DEFAULT_ENCODING, jsonString, useGzip, false,
                    this.contentType);
        } catch (IOException e) {
            try {
                log.error("error", e);
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            } catch (IOException e1) {
                throw new RuntimeException("system error.", e1);
            }
        }
    }

}
