package org.qfox.jestful.server.render;

import org.qfox.jestful.core.*;
import org.qfox.jestful.core.formatting.ResponseSerializer;
import org.qfox.jestful.server.NoClosePrintWriter;

import java.io.OutputStream;
import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * </p>
 * <p>
 * <p>
 * Company: 广州市俏狐信息科技有限公司
 * </p>
 *
 * @author Payne 646742615@qq.com
 * @date 2016年4月9日 下午3:46:30
 * @since 1.0.0
 */
public class DefaultResultRender implements Actor, Initialable {
    private final Map<MediaType, ResponseSerializer> map = new HashMap<MediaType, ResponseSerializer>();

    public Object react(Action action) throws Exception {
        Restful restful = action.getRestful();
        Result result = action.getResult();
        // 忽略没有回应体和声明void返回值的方法
        if (restful.isReturnBody() == false || result.getKlass() == Void.TYPE) {
            return action.execute();
        }

        Object value = action.execute();

        if (result.isRendered()) {
            return value;
        }

        switch (action.getDispatcher()) {
            case INCLUDE: {
                Response response = action.getResponse();
                MediaType mediaType = (MediaType) action.getExtra().get(MediaType.class);
                ResponseSerializer serializer = map.get(mediaType);
                Writer writer = new NoClosePrintWriter(response.getResponseWriter(), true);
                serializer.serialize(action, mediaType, writer);
                writer.flush();
            }
            break;
            default: {
                Response response = action.getResponse();
                String charset = response.getResponseHeader("Content-Charset");
                String contentType = response.getContentType();
                MediaType mediaType = MediaType.valueOf(contentType);
                ResponseSerializer serializer = map.get(mediaType);
                OutputStream out = response.getResponseOutputStream();
                serializer.serialize(action, mediaType, charset, out);
                out.flush();
            }
            break;
        }

        result.setRendered(true);

        return value;
    }

    public void initialize(BeanContainer beanContainer) {
        Collection<ResponseSerializer> serializers = beanContainer.find(ResponseSerializer.class).values();
        for (ResponseSerializer serializer : serializers) {
            String contentType = serializer.getContentType();
            MediaType mediaType = MediaType.valueOf(contentType);
            map.put(mediaType, serializer);
        }
    }

}

@@ -176,16 +176,87 @@
 tWriter;
+%0Aimport org.qfox.jestful.server.exception.NotAcceptableStatusException;
 %0A%0Aimport
@@ -1398,32 +1398,78 @@
 get(mediaType);%0A
+                if (serializer != null) %7B%0A    
                 
@@ -1552,32 +1552,36 @@
                 
+    
 serializer.seria
@@ -1629,16 +1629,20 @@
         
+    
 writer.f
@@ -1641,32 +1641,708 @@
 writer.flush();%0A
+                %7D else %7B%0A                    Request request = action.getRequest();%0A                    String accept = request.getRequestHeader(%22Accept%22);%0A                    Accepts accepts = accept == null %7C%7C accept.length() == 0 ? new Accepts(map.keySet()) : Accepts.valueOf(accept);%0A                    Accepts produces = action.getProduces();%0A                    Accepts supports = new Accepts(map.keySet());%0A                    String URI = action.getURI();%0A                    String method = action.getRestful().getMethod();%0A                    throw new NotAcceptableStatusException(URI, method, accepts, produces.isEmpty() ? supports : produces);%0A                %7D%0A
             %7D%0A  
@@ -2713,32 +2713,78 @@
 get(mediaType);%0A
+                if (serializer != null) %7B%0A    
                 
@@ -2846,32 +2846,36 @@
                 
+    
 serializer.seria
@@ -2929,16 +2929,20 @@
         
+    
 out.flus
@@ -2938,32 +2938,708 @@
    out.flush();%0A
+                %7D else %7B%0A                    Request request = action.getRequest();%0A                    String accept = request.getRequestHeader(%22Accept%22);%0A                    Accepts accepts = accept == null %7C%7C accept.length() == 0 ? new Accepts(map.keySet()) : Accepts.valueOf(accept);%0A                    Accepts produces = action.getProduces();%0A                    Accepts supports = new Accepts(map.keySet());%0A                    String URI = action.getURI();%0A                    String method = action.getRestful().getMethod();%0A                    throw new NotAcceptableStatusException(URI, method, accepts, produces.isEmpty() ? supports : produces);%0A                %7D%0A
             %7D%0A  

Include problem fixed