package org.voovan.docker.network;

import org.voovan.http.message.Response;
import org.voovan.tools.json.JSON;

import java.nio.ByteBuffer;
import java.util.Map;

/**
 *
 * @author helyho
 * <p>
 * DockerFly Framework.
 * WebSite: https://git.oschina.net/helyho/JDocker
 * Licence: Apache v2 License
 */
public class Result {
    private String message;
    private Response response;

    public Result(Response response) {
        this.response = response;

        if(!response.header().get("Content-Type").contains("application/octet-stream")){
            message = response.body().getBodyString();
            Map<String, String> dockerErrMsg = null;
            try {
                dockerErrMsg = JSON.toObject(message, Map.class);
            } catch (Exception e) {
                dockerErrMsg = null;
            }
            if (dockerErrMsg != null && dockerErrMsg.containsKey("message")) {
                this.message = dockerErrMsg.get("message");
            } else {
                this.message = message;
            }
        }
    }

    public int getStatus() {
        return response.protocol().getStatus();
    }

    public String getStatusCode() {
        return  response.protocol().getStatusCode();
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public ByteBuffer getBufferData(){
        return ByteBuffer.wrap(response.body().getBodyBytes());
    }

    public static Result newInstance(Response response) {
        return new Result(response);
    }

    @Override
    public String toString(){
        return message;
    }

}

package org.voovan.docker.network;

import org.voovan.http.message.Response;
import org.voovan.tools.json.JSON;

import java.nio.ByteBuffer;
import java.util.Map;

/**
 *
 * @author helyho
 * <p>
 * DockerFly Framework.
 * WebSite: https://git.oschina.net/helyho/JDocker
 * Licence: Apache v2 License
 */
public class Result {
    private String message;
    private Response response;

    public Result(Response response) {
        this.response = response;

        if(response==null){
            this.response = new Response();
            response.protocol().setStatus(506);
            response.protocol().setStatusCode("NULLERROR");
            response.body().write("The HTTP response is null, may hava some error.");
        }

        if(!response.header().get("Content-Type").contains("application/octet-stream")){
            message = response.body().getBodyString();
            Map<String, String> dockerErrMsg = null;
            try {
                dockerErrMsg = JSON.toObject(message, Map.class);
            } catch (Exception e) {
                dockerErrMsg = null;
            }
            if (dockerErrMsg != null && dockerErrMsg.containsKey("message")) {
                this.message = dockerErrMsg.get("message");
            } else {
                this.message = message;
            }
        }
    }

    public int getStatus() {
        return response.protocol().getStatus();
    }

    public String getStatusCode() {
        return  response.protocol().getStatusCode();
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public ByteBuffer getBufferData(){
        return ByteBuffer.wrap(response.body().getBodyBytes());
    }

    public static Result newInstance(Response response) {
        return new Result(response);
    }

    @Override
    public String toString(){
        return message;
    }

}

修复 HTTP请求异常的时候返回的 null 导致的饿空指针问题