package org.cs.core.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.FileCopyUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * 
 * @author cosco
 *
 */
public class JsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> {

	public static final Charset UTF8 = Charset.forName("UTF-8");

	private Charset charset = UTF8;

	private SerializerFeature[] features = new SerializerFeature[0];

	public JsonHttpMessageConverter() {
		super(new MediaType[] { new MediaType("application", "json", UTF8),
				new MediaType("application", "*+json", UTF8) });
	}

	protected boolean supports(Class<?> clazz) {
		return true;
	}

	public Charset getCharset() {
		return this.charset;
	}

	public void setCharset(Charset charset) {
		this.charset = charset;
	}

	public SerializerFeature[] getFeatures() {
		return this.features;
	}

	public void setFeatures(SerializerFeature[] features) {
		this.features = features;
	}

	protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage)
			throws IOException, HttpMessageNotReadableException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		InputStream in = inputMessage.getBody();

		byte[] buf = new byte[1024];
		while (true) {
			int len = in.read(buf);
			if (len == -1) {
				break;
			}

			if (len > 0) {
				baos.write(buf, 0, len);
			}
		}

		byte[] bytes = baos.toByteArray();
		
		if(clazz.getName().equals(JSONObject.class.getName())){
			return JSON.parseObject(bytes, 0, bytes.length, this.charset.newDecoder(), clazz, new Feature[0]);
		}
		else{
			return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));
		}
		
	}

	protected void writeInternal(Object obj, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException {
		
		OutputStream out = outputMessage.getBody();
		
		if(obj instanceof JSONObject){
			String text = JSON.toJSONString(obj, this.features);
			byte[] bytes = text.getBytes(this.charset);
			out.write(bytes);
		}
		else{
			FileCopyUtils.copy(obj.toString(), new OutputStreamWriter(outputMessage.getBody(), charset));
		}
		
	}
	
	
}
