package com.dtflys.forest.backend.okhttp3.response;

import com.dtflys.forest.backend.ContentType;
import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.utils.StringUtils;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gongjun[jun.gong@thebeastshop.com]
 * @since 2018-02-27 15:05
 */
public class OkHttp3ForestResponse extends ForestResponse {

    private final Response okResponse;

    private final ResponseBody body;


    public OkHttp3ForestResponse(ForestRequest request, Response okResponse) {
        super(request);
        this.okResponse = okResponse;
        if (okResponse != null) {
            this.body = okResponse.body();
            this.statusCode = okResponse.code();
            String respEncodingFromHeader = okResponse.header("Content-Encoding");
            setupHeaders();
            if (body != null) {
                MediaType mediaType = body.contentType();
                if (mediaType != null) {
                    String type = mediaType.type();
                    String subType = mediaType.subtype();
                    this.contentType = new ContentType(type, subType);
                    Charset charset = mediaType.charset();
                    if (charset != null) {
                        this.contentEncoding = charset.name();
                    }
                }
                if (StringUtils.isEmpty(this.contentEncoding)) {
                    this.contentEncoding = respEncodingFromHeader;
                }
                if (contentType == null || contentType.isEmpty()) {
                    content = null;
                } else if (!request.isDownloadFile() && contentType.canReadAsString()) {
                    try {
                        this.content = body.string();
                    } catch (IOException e) {
                        throw new ForestRuntimeException(e);
                    }
                } else {
                    StringBuilder builder = new StringBuilder();
                    builder.append("[content-type: ")
                            .append(contentType.toString());
                    if (contentEncoding != null) {
                        builder.append("; encoding: ")
                                .append(contentEncoding);
                    }
                    builder.append("; length: ")
                            .append(contentLength)
                            .append("]");
                    this.content = builder.toString();
                }
            }
        } else {
            this.body = null;
            this.statusCode = 404;
        }
    }

    private void setupHeaders() {
        if (okResponse != null) {
            Headers hs = okResponse.headers();
            for (String name : hs.names()) {
                headers.addHeader(name, hs.get(name));
            }
        }
    }

    public boolean isText() {
        if (contentType == null) {
            return false;
        }
        return false;
    }

    @Override
    public boolean isReceivedResponseData() {
        return body != null;
    }

    @Override
    public byte[] getByteArray() throws Exception {
        return body.bytes();
    }

    @Override
    public InputStream getInputStream() throws Exception {
        return body.byteStream();
    }

}

@@ -407,27 +407,36 @@
 java.io.
-IOException
+ByteArrayInputStream
 ;%0Aimport
@@ -445,26 +445,26 @@
 ava.io.I
-nputStream
+OException
 ;%0Aimport
@@ -473,22 +473,22 @@
 ava.
-net.URLDecoder
+io.InputStream
 ;%0Aim
@@ -521,56 +521,8 @@
 set;
-%0Aimport java.util.HashMap;%0Aimport java.util.Map;
 %0A%0A/*
@@ -739,16 +739,73 @@
  body;%0A%0A
+    /**%0A     * %E5%86%85%E5%AE%B9%E5%AD%97%E8%8A%82%E6%95%B0%E7%BB%84%0A     */%0A    private byte%5B%5D bytes;%0A%0A
 %0A    pub
@@ -2061,16 +2061,62 @@
   try %7B%0A
+                        bytes = body.bytes();%0A
         
@@ -2150,20 +2150,24 @@
 t = 
-body.s
+new S
 tring(
+bytes
 );%0A 
@@ -3563,27 +3563,93 @@
     
-return body.bytes()
+if (bytes == null) %7B%0A            bytes = body.bytes();%0A        %7D%0A        return bytes
 ;%0A  
@@ -3745,24 +3745,47 @@
 urn 
-body.byteStream(
+new ByteArrayInputStream(getByteArray()
 );%0A 

修复 OkHttp3ForestResponse 无法多次调用 getByteArray()/getInputStream() 方法，修复第二次调用的时候无法获取内容问题