Response.java :  » Scripting » tinyappserver » cn » aprilsoft » TinyAppServer » reqNrep » Java Open Source

Java Open Source » Scripting » tinyappserver 
tinyappserver » cn » aprilsoft » TinyAppServer » reqNrep » Response.java
package cn.aprilsoft.TinyAppServer.reqNrep;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Date;

import cn.aprilsoft.conf4j.Conf4j;
import cn.aprilsoft.http.HttpDateFormat;
import cn.aprilsoft.lang.StringUtil;

public class Response {

    private OutputStream outputStream;
    private boolean      httpHeaderWriten = false;
    private String       status           = "200";
    private Date         modified         = null;
    private long         contentSize      = -1;
    private String       contentType      = "text/html";
    private boolean      cache            = true;
    private String       setCookie        = null;
    private String       charset          = Conf4j.getTrimProperty(Response.class, "charset");
    private String       location         = null;

    Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    protected void writeHttpHeader() {
        if (httpHeaderWriten) {
            return;
        }
        httpHeaderWriten = true;
        if (modified == null) {
            modified = new Date();
        }

        if (location != null) {
            status = "302";
        }

        PrintWriter pw = new PrintWriter(outputStream);
        pw.write("HTTP/1.1 " + status + "\r\n");
        pw.write("Server:TinyAppServer/1.0 JSSP/1.0\r\n");
        if (StringUtil.isNotEmpty(setCookie)) {
            pw.write("Set-Cookie:" + setCookie + "\r\n");
        }
        if (!cache) {
            pw.write("Pragma:No-cache\r\n");
            pw.write("Cache-Control:no-cache\r\n");
            pw.write("Expires:" + HttpDateFormat.formatHttpDate(new Date(0)) + "\r\n");
        }
        pw.write("Date: " + HttpDateFormat.formatHttpDate(new Date()) + "\r\n");
        pw.write("Last-Modified: " + HttpDateFormat.formatHttpDate(modified) + "\r\n");
        if (contentSize > 0) {
            pw.write("Content-Length: " + contentSize + "\r\n");
        }
        String contentCharset = "";
        if (StringUtil.isNotEmpty(charset)) {
            contentCharset = ";charset=" + charset;
        }
        if (location != null) {
            pw.write("Location: " + location + "\r\n");
        } else {
            pw.write("Content-Type: " + contentType + contentCharset + "\r\n");
        }
        pw.write("\r\n");
        pw.flush();
    }

    public OutputStream getOutputStream() {
        writeHttpHeader();
        return outputStream;
    }

    public Writer getWriter() {
        try {
            return new PrintWriter(new BufferedWriter(new OutputStreamWriter(getOutputStream(), getCharset())));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public void flush() throws IOException {
        writeHttpHeader();
        outputStream.flush();
    }

    public String getStatus() {
        return status;
    }

    public long getContentSize() {
        return contentSize;
    }

    public String getContentType() {
        return contentType;
    }

    public boolean getCache() {
        return cache;
    }

    public String getSetCookie() {
        return setCookie;
    }

    public String getLocation() {
        return location;
    }

    public boolean isHttpHeaderWritten() {
        return httpHeaderWriten;
    }

    public String getCharset() {
        return charset;
    }

    public void setStatus(String status) {
        needNotHeaded();
        this.status = status;
    }

    public void setContentSize(long contentSize) {
        needNotHeaded();
        this.contentSize = contentSize;
    }

    public void setContentType(String contentType) {
        needNotHeaded();
        this.contentType = contentType;
    }

    public void setCharset(String charset) {
        needNotHeaded();
        this.charset = charset;
    }

    public void setCache(boolean cache) {
        this.cache = cache;
    }

    public void setSetCookie(String cookie) {
        this.setCookie = cookie;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public void redirect(String url) {
        setLocation(url);
    }

    private void needNotHeaded() {
        if (httpHeaderWriten) {
            throw new IllegalStateException("Cannot set header after writen.");
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.