Wednesday, March 10, 2010

HTTPResponseHeader

/** HTTP Response Header Class
This class takes a byte array and parses it into a HTTP Response Header

Author:  Daniel J. Tanner
Date:    March 9th, 2010
CMPT 352
*/

import java.io.*;
import java.net.*;
import java.lang.*;

public class HTTPResponseHeader
{
    //Fields
    protected String httpVersion_;
    protected String code_;
    protected String message_;
    protected String date_;
    protected String serverName_;
    protected String contentType_;
    protected long contentLength_;
    protected static final String DEFAULT_CODE = "200";
    protected static final String DEFAULT_MESSAGE = "Ok";
    protected static final String DEFAULT_HTTPVERSION = "HTTP/1.0";
    protected static final String DEFAULT_DATE = "January 1st, 1970";
    protected static final String DEFAULT_SERVERNAME = "So Long, And Thanks For All The Fish!";
    protected static final String DEFAULT_CONTENTTYPE = "matter/dark";
    protected static final long DEFAULT_CONTENTLENGTH = 42;
   
    //Constructors
    public HTTPResponseHeader()
    {
        code_ = DEFAULT_CODE;
        message_ = DEFAULT_MESSAGE;
        httpVersion_ = DEFAULT_HTTPVERSION;
        date_ = DEFAULT_DATE;
        serverName_ = DEFAULT_SERVERNAME;
        contentType_ = DEFAULT_CONTENTTYPE;
        contentLength_ = DEFAULT_CONTENTLENGTH;
    }
   
    public HTTPResponseHeader(String httpVersion, String code, String message, String date, String serverName, String contentType, long contentLength)
    {
        code_ = code;
        message_ = message;
        httpVersion_ = httpVersion;
        date_ = date;
        serverName_ = serverName;
        contentType_ = contentType;
        contentLength_ = contentLength;
    }
   
    //set functions
    public void setCode(String code) {code_ = code;}
    public void setMessage(String message) {message_ = message;}
    public void setHTTPVersion(String httpVersion) {httpVersion_ = httpVersion;}
    public void setDate(String date) {date_ = date;}
    public void setServerName(String serverName) {serverName_ = serverName;}
    public void setContentType(String contentType) {contentType_ = contentType;}
    public void setContentLength(long contentLength) {contentLength_ = contentLength;}
   
    //get functions
    public String getCode() {return code_;}
    public String getMessage() {return message_;}
    public String getHTTPVersion() {return httpVersion_;}
    public String getDate() {return date_;}
    public String getServerName() {return serverName_;}
    public String getContentType() {return contentType_;}
    public long getContentLength() {return contentLength_;}
   
    //print function
    public byte[] getBytes() throws IOException
    {
        String header = httpVersion_ + " " + code_ + " " + message_ + "\r\n"
            + "Date:  " + date_ + "\r\n"
            + "Server:  " + serverName_ + "\r\n"
            + "Content-Type:  " + contentType_ + "\r\n"
            + "Content-Length:  " + contentLength_ + "\r\n";
        return header.getBytes("US-ASCII");
    }
       
}

No comments:

Post a Comment