package com.hamishrickerby.http_server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by rickerbh on 17/08/2016.
 */
public abstract class Response {
    Request request;

    public Response(Request request) {
        this.request = request;
    }

    public byte[] getBytes() {
        byte[] status = statusLine().getBytes();
        byte[] body = body();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            outputStream.write(status);
            outputStream.write(body);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return outputStream.toByteArray();
        }
    }

    private String statusLine() {
        StringBuilder b = new StringBuilder();
        b.append(request.getVersion())
                .append(" ")
                .append(code())
                .append(" ")
                .append(reason())
                .append("\r\n\r\n");
        return b.toString();
    }

    protected int code() {
        return 200;
    }

    protected String reason() {
        return "OK";
    }

    abstract protected byte[] body();

}

@@ -981,16 +981,100 @@
 nd(%22%5Cr%5Cn
+%22)%0A                .append(String.join(%22%5Cr%5Cn%22, headers()))%0A                .append(%22
 %5Cr%5Cn%22);%0A
@@ -1219,24 +1219,96 @@
 OK%22;%0A    %7D%0A%0A
+    protected String%5B%5D headers() %7B%0A        return new String%5B0%5D;%0A    %7D%0A%0A
     abstract

Integrate headers into repsonse