⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpresponse.java

📁 一个简单的visio程序。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

package servlet.http;

import java.io.*;
import java.util.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
import servlet.ServletConnection;


public class HttpResponse
    implements HttpServletResponse, Observer, Cloneable
{
    static class OldCookieExpiry extends HttpDate
    {

        void append(StringBuffer stringbuffer)
        {
            stringbuffer.append(days3[wday]);
            stringbuffer.append(", ");
            append2Digit(stringbuffer, mday);
            stringbuffer.append('-');
            stringbuffer.append(months3[mon]);
            stringbuffer.append('-');
            stringbuffer.append(1900 + year);
            stringbuffer.append(' ');
            append2Digit(stringbuffer, hour);
            stringbuffer.append(':');
            append2Digit(stringbuffer, min);
            stringbuffer.append(':');
            append2Digit(stringbuffer, sec);
            stringbuffer.append(" GMT");
        }

        private void append2Digit(StringBuffer stringbuffer, int i)
        {
            if(i < 10)
                stringbuffer.append("0");
            stringbuffer.append(i);
        }

        private static final String days3[] = {
            "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
        };
        private static final String months3[] = {
            "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
            "Nov", "Dec"
        };


        OldCookieExpiry(long l)
        {
            setTime(HttpDate.getCurrentTime() + l * 1000L);
        }
    }


    public HttpResponse()
    {
        out = new HttpOutputStream();
        outHeader = new HttpOutputStream();
        bufHeader = new ByteArrayOutputStream();
        printKeepAlive = false;
        contentLenSet = false;
        headersWritten = false;
    }

    private void maybeQuote(int i, StringBuffer stringbuffer, String s)
    {
        if(i == 0 || isToken(s))
        {
            stringbuffer.append(s);
            return;
        }
        else
        {
            stringbuffer.append('"');
            stringbuffer.append(s);
            stringbuffer.append('"');
            return;
        }
    }

    private boolean isToken(String s)
    {
        int i = s.length();
        for(int j = 0; j < i; j++)
        {
            char c = s.charAt(j);
            if(c < ' ' || c >= '\177' || "()<>@,;:\\\"/[]?={} \t".indexOf(c) != -1)
                return false;
        }

        return true;
    }

    public void addCookie(Cookie cookie)
    {
        StringBuffer stringbuffer = new StringBuffer();
        String s = cookie.getName();
        String s1 = cookie.getValue();
        String s2 = cookie.getDomain();
        String s3 = cookie.getPath();
        String s4 = cookie.getComment();
        int i = cookie.getMaxAge();
        int j = cookie.getVersion();
        boolean flag = cookie.getSecure();
        stringbuffer.append(s);
        stringbuffer.append("=");
        maybeQuote(j, stringbuffer, s1);
        if(j == 1)
        {
            stringbuffer.append(";Version=1");
            if(s4 != null)
            {
                stringbuffer.append(";Comment=");
                maybeQuote(j, stringbuffer, s4);
            }
        }
        if(s2 != null)
        {
            stringbuffer.append(";Domain=");
            maybeQuote(j, stringbuffer, s2);
        }
        if(i >= 0)
        {
            if(j == 0)
            {
                stringbuffer.append(";Expires=");
                new OldCookieExpiry(i).append(stringbuffer);
            }
            else
            {
                stringbuffer.append(";MaxAge=");
                stringbuffer.append(i);
            }
        }
        else
        if(j == 1)
            stringbuffer.append(";Discard");
        if(s3 != null)
        {
            stringbuffer.append(";Path=");
            maybeQuote(j, stringbuffer, s3);
        }
        if(flag)
            stringbuffer.append(";Secure");
        if(j == 1)
        {
            Cookie cookie1 = (Cookie)cookie.clone();
            appendHeader("Set-Cookie2", stringbuffer.toString());
            cookie1.setVersion(0);
            addCookie(cookie1);
        }
        else
        {
            appendHeader("Set-Cookie", stringbuffer.toString());
        }
        String s5 = headers.getHeader("Cache-Control");
        CacheControlHeader cachecontrolheader = new CacheControlHeader(s5);
        for(Enumeration enumeration = cachecontrolheader.getStringDirective("no-cache"); enumeration.hasMoreElements();)
        {
            String s6 = (String)enumeration.nextElement();
            if(s6.equals("set-cookie"))
                return;
        }

        cachecontrolheader.addStringDirective("no-cache", "set-cookie");
        cachecontrolheader.addStringDirective("no-cache", "set-cookie2");
        headers.putHeader("Cache-Control", cachecontrolheader.toString());
        if(headers.getHeader("Expires") == null)
            headers.putHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");
    }

    public void init(ServletConnection servletconnection)
        throws IOException
    {
        con = servletconnection;
        keepAlive = true;
        out.init(servletconnection.getOutputStream());
    }

    private void init()
    {
        headers.clear();
        mimeType = null;
        writer = null;
        charEncoding = null;
        gotOutputStream = false;
        gotWriter = false;
        session = null;
        headersWritten = false;
    }

    public void next()
    {
        init();
        printKeepAlive = false;
        contentLenSet = false;
        status = 200;
        reason = reason(200);
        protocol = null;
        headerBytes = 0;
        out.next();
    }

    public void finish()
        throws IOException
    {
        if(writer != null)
            writer.flush();
        out.close();
    }

    public void reset()
    {
        out.resets();
    }

    public int getTotalBytes()
    {
        return out.getTotal();
    }

    public int getEntityBytes()
    {
        return out.getTotal() - headerBytes;
    }

    public void setProtocol(String s)
    {
        protocol = s;
        out.setObserver(this);
    }

    public void setKeepAlive(boolean flag)
    {
        keepAlive = flag;
    }

    public boolean getKeepAlive()
    {
        return keepAlive;
    }

    public void printKeepAlive()
    {
        printKeepAlive = true;
    }

    public void setContentLength(int i)
    {
        contentLenSet = true;
        setIntHeader("Content-Length", i);
        out.setContentLength(i);
    }

    public void setContentType(String s)
    {
        if(s.equalsIgnoreCase(mimeType))
            return;
        if(mimeType != null)
        {
            throw new IllegalStateException("MIME type was already set");
        }
        else
        {
            mimeType = s;
            setHeader("Content-Type", s);
            return;
        }
    }

    public ServletOutputStream getOutputStream()
    {
        if(gotWriter)
        {
            throw new IllegalStateException("can't mix text and binary input");
        }
        else
        {
            gotOutputStream = true;
            return out;
        }
    }

    public void reinitStreamState()
    {
        if(gotWriter)
        {
            throw new IllegalStateException("can't reset stream state, writer's been gotten");
        }
        else
        {
            gotOutputStream = false;
            return;
        }
    }

    public static String getContentCharset(String s)
    {
        String s1 = "8859_1";
        for(int i = 0; (i = s.indexOf(59)) > 0;)
        {
            s = s.substring(i + 1);
            i = s.indexOf(61);
            String s2 = s.substring(0, i).trim().toLowerCase();
            if("charset".equals(s2))
            {
                s1 = s.substring(i + 1);
                if((i = s1.indexOf(59)) > 0)
                    s1 = s1.substring(0, i);
                s1 = s1.trim();
                if(s1.charAt(0) == '"')
                {
                    i = 1;
                    do
                        i = s1.indexOf(34, i);
                    while(i > 0 && s1.charAt(i - 1) == '\\');
                    if(i > 0)
                        s1 = s1.substring(1, i);
                    else
                        s1 = "8859_1";
                }
                else
                if((i = s1.indexOf(40)) > 0)
                    s1 = s1.substring(0, i).trim();
                s1 = s1.toLowerCase();
            }
        }

        if("us-ascii".equals(s1))
            s1 = "8859_1";
        return s1;
    }

    public String getCharacterEncoding()
    {
        if(charEncoding == null)
            if(mimeType == null)
                setContentType("text/plain");
            else
                charEncoding = getContentCharset(mimeType);
        if(charEncoding == null)
        {
            StringBuffer stringbuffer = new StringBuffer(mimeType);
            charEncoding = chooseCharacterEncoding();
            stringbuffer.append(";charset=\"");
            stringbuffer.append(charEncoding);
            stringbuffer.append("\"");
            mimeType = stringbuffer.toString();
            setHeader("Content-Type", mimeType);
        }
        return charEncoding;
    }

    protected String chooseCharacterEncoding()
    {
        return "8859_1";
    }

    public PrintWriter getWriter()
        throws UnsupportedEncodingException
    {
        if(gotOutputStream)
            throw new IllegalStateException("can't mix text and binary input");
        if(writer == null)
        {
            gotWriter = true;
            OutputStreamWriter outputstreamwriter = new OutputStreamWriter(out, getCharacterEncoding());
            writer = new PrintWriter(out);
        }
        return writer;
    }

    public void setStatus(int i, String s)
    {
        status = i;
        reason = s;
    }

    public void setStatus(int i)
    {
        setStatus(i, reason(i));
    }

    public int getStatus()
    {
        return status;
    }

    public void appendHeader(String s, String s1)
    {
        headers.appendHeader(s, s1);
    }

    public void setHeader(String s, String s1)
    {
        headers.putHeader(s, s1);
    }

    public void setIntHeader(String s, int i)
    {
        headers.putIntHeader(s, i);
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -