/**
 * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.web.render;

import com.jfinal.core.Action;
import com.jfinal.kit.JsonKit;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 * @Package io.jboot.web.render
 */
public class JbootReturnValueRender extends Render {

    private Action action;
    private Object value;

    public JbootReturnValueRender(Action action, Object returnValue) {
        this.action = action;
        this.value = returnValue;
    }

    @Override
    public void render() {
        if (isBaseType()) {
            renderText();
        } else {
            renderJson();
        }
    }


    private void renderText() {
        PrintWriter writer = null;
        try {
            response.setContentType("text/plain");
            writer = response.getWriter();
            writer.write(String.valueOf(value));
            // writer.flush();
        } catch (IOException e) {
            throw new RenderException(e);
        }
    }


    private void renderJson() {
        PrintWriter writer = null;
        try {
            response.setContentType("application/json; charset=utf-8");
            writer = response.getWriter();
            writer.write(JsonKit.toJson(value));
            // writer.flush();
        } catch (IOException e) {
            throw new RenderException(e);
        }
    }

    private boolean isBaseType() {
        if (value == null) {
            return true;
        }
        Class c = value.getClass();
        return c == String.class
                || c == Integer.class || c == int.class
                || c == Long.class || c == long.class
                || c == Double.class || c == double.class
                || c == Float.class || c == float.class
                || c == Boolean.class || c == boolean.class
                || c == BigDecimal.class || c == BigInteger.class;
    }
}

@@ -747,24 +747,28 @@
 inal.render.
+File
 Render;%0Aimpo
@@ -792,52 +792,182 @@
 der.
+Json
 Render
-Exception;%0A%0Aimport java.io.IOException
+;%0Aimport com.jfinal.render.Render;%0Aimport com.jfinal.render.TextRender;%0A%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse
 ;%0Aim
@@ -979,27 +979,20 @@
 java.io.
-PrintWriter
+File
 ;%0Aimport
@@ -1042,16 +1042,74 @@
 Integer;
+%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Date;
 %0A%0A/**%0A *
@@ -1314,16 +1314,43 @@
 t value;
+%0A    private Render render;
 %0A%0A    pu
@@ -1456,133 +1456,90 @@
     
-this.value = returnValue;%0A    %7D%0A%0A    @Override%0A    public void render() %7B%0A        if (isBaseType()) %7B%0A            renderText(
+if (isBaseType(returnValue)) %7B%0A            this.value = String.valueOf(returnValue
 );%0A 
@@ -1566,28 +1566,40 @@
         
-renderJson()
+this.value = returnValue
 ;%0A      
@@ -1606,302 +1606,387 @@
   %7D%0A
+%0A
     
-%7D%0A%0A%0A    private void renderText() %7B%0A        PrintWriter writer = null;%0A        try %7B%0A            response.setContentType(%22text/plain%22);%0A            writer = response.getWriter();%0A            writer.write(String.valueOf(value));%0A            // writer.flush();%0A        %7D catch (IOException e)
+    if (this.value instanceof File) %7B%0A            this.render = new FileRender((File) value);%0A        %7D else if (this.value instanceof String) %7B%0A            this.render = new TextRender((String) value);%0A        %7D else if (this.value instanceof Date) %7B%0A            this.render = new TextRender(new SimpleDateFormat(%22yyyy-MM-dd HH:mm:ss%22).format((Date) value));%0A        %7D else
  %7B%0A 
@@ -2002,33 +2002,56 @@
   th
-row
+is.render =
  new 
+Json
 Render
-Exception(e
+(JsonKit.toJson(value)
 );%0A 
@@ -2075,310 +2075,422 @@
     
-private void renderJson() %7B%0A        PrintWriter writer = null;%0A        try %7B
+@Override%0A    public Render setContext(HttpServletRequest request, HttpServletResponse response) %7B%0A        render.setContext(request, response);%0A        return this;
 %0A    
+%7D%0A%0A
     
-    response.setContentType(%22application/json; charset=utf-8%22);%0A            writer = response.getWriter();%0A            writer.write(JsonKit.toJson(value));%0A            // writer.flush()
+@Override%0A    public Render setContext(HttpServletRequest request, HttpServletResponse response, String viewPath) %7B%0A        render.setContext(request, response, viewPath);%0A        return this
 ;%0A    
+%7D%0A%0A
     
-%7D catch (IOException e
+@Override%0A    public void render(
 ) %7B%0A
@@ -2501,57 +2501,36 @@
     
-   
- 
 th
-row new RenderException(e);%0A        %7D%0A    %7D
+is.render.render();%0A    %7D%0A
 %0A%0A  
@@ -2558,16 +2558,28 @@
 aseType(
+Object value
 ) %7B%0A    
@@ -2698,32 +2698,51 @@
  == String.class
+ %7C%7C c == char.class
 %0A               
@@ -3006,24 +3006,80 @@
 olean.class%0A
+                %7C%7C c == Short.class %7C%7C c == short.class%0A
             

add restful controller support