package com.jscape.inet.http.protocol;

import com.jscape.inet.http.protocol.messages.Headers;
import com.jscape.util.IOTools;

import java.io.InputStream;

import static com.jscape.util.Parameters.assertNotNull;
import static com.jscape.util.Parameters.assertStringValid;

/**
 * @author Alex Usun
 */
public class Request
        implements AutoCloseable {

    public final String protocolVersion;
    public final String uri;
    public final Headers headers;
    public final InputStream body;
    public final Headers trailerHeaders;

    public Request(String protocolVersion, String uri, Headers headers, InputStream body, Headers trailerHeaders) {
        assertNotNull(protocolVersion);
        this.protocolVersion = protocolVersion;

        assertStringValid(uri);
        this.uri = uri;

        assertNotNull(headers);
        this.headers = headers;

        assertNotNull(body);
        this.body = body;
        
        assertNotNull(trailerHeaders);
        this.trailerHeaders = trailerHeaders;
    }
    
    public Request(String protocolVersion, String uri, Headers headers, InputStream body) {
        this(protocolVersion, uri, headers, body, new Headers());
    }
    
    @Override
    public void close() {
        IOTools.close(this.body);
    }
    
    @Override
    public String toString() {
        return "Request {"
               + "protocolVersion='" + this.protocolVersion + '\''
               + ", uri='" + this.uri + '\''
               + ", headers=" + this.headers
               + ", body=" + this.body
               + ", trailerHeaders=" + this.trailerHeaders
               + '}';
    }
}
