package org.qfox.jestful.server.render;

import org.qfox.jestful.core.Action;
import org.qfox.jestful.core.Actor;
import org.qfox.jestful.core.Response;
import org.qfox.jestful.core.Result;
import org.qfox.jestful.core.io.IOUtils;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * Description:
 * </p>
 * <p>
 * <p>
 * Company: 广州市俏狐信息科技有限公司
 * </p>
 *
 * @author Payne 646742615@qq.com
 * @date 2016年6月8日 下午12:30:55
 * @since 1.0.0
 */
public class TextResultRender implements Actor {
    private final Pattern pattern = Pattern.compile("@(\\(([^()]+?)\\))?\\:(.*)");

    public Object react(Action action) throws Exception {
        Object value = action.execute();
        Result result = action.getResult();
        if (result.isRendered()) {
            return value;
        }
        if (value instanceof String == false) {
            return value;
        }
        String text = (String) value;
        if (text.matches(pattern.pattern())) {
            Matcher matcher = pattern.matcher(text);
            matcher.find();
            String type = matcher.group(2) != null ? matcher.group(2) : "text/plain";
            String content = matcher.group(3) != null ? matcher.group(3) : "";
            Writer writer = null;
            try {
                Response response = action.getResponse();
                String charset = response.getResponseHeader("Content-Charset");
                charset = charset != null ? charset : response.getCharacterEncoding();
                response.setContentType(type + (charset != null ? "; charset=" + charset : ""));
                switch (action.getDispatcher()) {
                    case INCLUDE:
                        writer = response.getResponseWriter();
                        break;
                    default:
                        OutputStream out = response.getResponseOutputStream();
                        writer = new OutputStreamWriter(out, charset);
                        break;
                }
                writer.write(content);
                writer.flush();
            } catch (Exception e) {
                throw e;
            } finally {
                result.setRendered(true);
                IOUtils.close(writer);
            }
        }
        return value;
    }

}

@@ -1313,42 +1313,8 @@
 %22%22;%0A
-            Writer writer = null;%0A
     
@@ -1405,339 +1405,314 @@
     
-String charset = response.getResponseHeader(%22Content-Charset%22);%0A                charset = charset != null ? charset : response.getCharacterEncoding();%0A                response.setContentType(type + (charset != null ? %22; charset=%22 + charset : %22%22));%0A                switch (action.getDispatcher()) %7B%0A                    case INCLUDE:
+switch (action.getDispatcher()) %7B%0A                    case INCLUDE: %7B%0A                        Writer writer = response.getResponseWriter();%0A                        writer.write(content);%0A                        writer.flush();%0A                    %7D%0A                    break;%0A                    default: %7B
 %0A   
@@ -1732,22 +1732,30 @@
         
-writer
+String charset
  = respo
@@ -1761,39 +1761,56 @@
 onse.getResponse
-Writer(
+Header(%22Content-Charset%22
 );%0A             
@@ -1824,43 +1824,183 @@
     
-break;%0A                    default:
+charset = charset != null ? charset : response.getCharacterEncoding();%0A                        response.setContentType(type + (charset != null ? %22; charset=%22 + charset : %22%22));
 %0A   
@@ -2095,24 +2095,31 @@
             
+Writer 
 writer = new
@@ -2181,103 +2181,184 @@
     
-break;%0A                %7D%0A                writer.write(content);%0A                writer.flush();
+writer.write(content);%0A                        writer.flush();%0A                        IOUtils.close(writer);%0A                    %7D%0A                    break;%0A                %7D
 %0A   
@@ -2485,47 +2485,8 @@
 e);%0A
-                IOUtils.close(writer);%0A
     

Support include