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

📄 httpresponse.java

📁 短信开发用于文件交换处理转发的类模块
💻 JAVA
字号:
package com.pub.servlet;

import java.io.*;
import javax.servlet.ServletResponse;
import javax.servlet.ServletOutputStream;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;

public class HttpResponse implements HttpServletResponse {

    private HttpHeaders responseHeaders;

    private boolean headersSent;

    private OutputStream output;

    private InternetOutputStream stream;

    private ServletOutputStream servletOutput;

    private HttpRequest request;

    private boolean keepConnectionOpen;

    public int SEND_BUFFER_SIZE = 4096;

    public HttpResponse(OutputStream output, HttpRequest request) {
        this.output = output;
        stream = new InternetOutputStream(output);
        this.headersSent = false;
        this.responseHeaders = new HttpHeaders();
        this.request = request;
    }

    public boolean isKeepAlive() {
        return (keepConnectionOpen && request.isKeepAlive());
    }

    public void addHeader(String key, String value) {
        responseHeaders.put(key, value);
    }

    public void sendError(int statusCode, String errorMessage) {
        keepConnectionOpen = false;
        if (headersSent) {
            return;
        }

        String body = "<html>\n<head>\n"
                      + "<title>Error: " + statusCode + "</title>\n"
                      + "<body>\n<h1>" + statusCode + " <b>"
                      + Http.getStatusPhrase(statusCode)
                      + "</b></h1><br>\nThe requested URL <b>"
                      +
                      ((request.getUrl() == null) ? "<i>unknown URL</i>" :
                       Http.encodeHtml(request.getUrl()))
                      + "</b>\n " + Http.encodeHtml(errorMessage)
                      + "\n<hr>"
                      + "</body>\n</html>";

        try {
            sendResponse(statusCode, "text/html", body);
        } catch (IOException e) {
        }
    }

    public void sendResponse(int b) throws IOException {
      stream.write(b);
    }

    public void sendResponse(int code,int b) throws IOException {
      sendHttpReply(code);
      stream.write(b);
    }

    public void sendResponse(byte [] b) throws IOException {
      stream.write(b);
    }

    public void sendResponse(int code,byte [] b) throws IOException {
      sendHttpReply(code);
      stream.write(b);
    }

    public void sendResponse(String body) throws IOException {
        try {
            stream.write(body.getBytes());
        } finally {
            stream.flush();
        }
    }

    public void sendResponse(int code,String body) throws IOException {
        try {
          sendHttpReply(code);
//          sendHeaders(null, 200000);
          stream.print(body);
        } finally {
            stream.flush();
        }
    }

    public void sendResponse(int code, String mimeType, String body) throws
            IOException {
        sendResponse(code, mimeType, body.getBytes());
    }

    public void sendResponse(int code, String mimeType, byte[] body) throws
            IOException {
        try {
            sendHttpReply(code);
            sendHeaders(mimeType, body.length);
            if (!isHeadMethod()) {
                stream.write(body);
            }
        } finally {
            stream.flush();
        }
    }



    private void sendHttpReply(int code) throws IOException {
        StringBuffer buffer = new StringBuffer(request.getProtocol());
        buffer.append(" ");
        buffer.append(code);
        buffer.append(" ");
        buffer.append(Http.getStatusPhrase(code));
        buffer.append(Http.CRLF);
        buffer.append(Http.CRLF);
        stream.write(buffer.toString().getBytes());
    }

    private void sendHeaders(String mimeType, int contentLength) throws
            IOException {
        responseHeaders.put("Date", Http.getCurrentTime());
        responseHeaders.put("Server", "Pygmy");
        String str = request.isKeepAlive() ? "Keep-Alive" : "close";
        responseHeaders.put(request.getConnectionHeader(), str);
        if (contentLength >= 0) {
            responseHeaders.put("Content-Length",
                                Integer.toString(contentLength));
        } else if (!request.isProtocolVersionLessThan(1, 1)) {
            responseHeaders.put("Transfer-Encoding", "chunked");
        }

        if (mimeType != null) {
            responseHeaders.put("Content-Type", mimeType);
        }
        responseHeaders.print(stream);
    }

    private boolean isHeadMethod() {
        return "HEAD".equalsIgnoreCase(request.getMethod());
    }

//    public class BlockCloseOutputStream extends FilterOutputStream {
//        public BlockCloseOutputStream(OutputStream out) {
//            super(out);
//        }
//
//        public void close() throws IOException {
//        }
//    }


    public ServletOutputStream getOutputStream() throws IOException {
        if (servletOutput == null) {
            servletOutput = new ServletOutputStream() {
                public void write(int b) throws IOException {
                    stream.write(b);
                }
            };
        }
        return servletOutput;
    }

    public String getCharacterEncoding() {
        return "";
    }

    public PrintWriter getWriter() throws IOException {
        PrintWriter out=new PrintWriter(output);
        StringBuffer buffer = new StringBuffer(request.getProtocol());
        buffer.append(" ");
        buffer.append(200);
        buffer.append(" ");
        buffer.append(Http.getStatusPhrase(200));
        buffer.append(Http.CRLF);
        buffer.append(Http.CRLF);
        out.print(buffer.toString());
        return out;
    }

    public void setContentLength(int _int) {
    }

    public void setContentType(String string) {
    }

    public void setBufferSize(int _int) {
        SEND_BUFFER_SIZE = _int;
    }

    public int getBufferSize() {
        return SEND_BUFFER_SIZE;
    }

    public void flushBuffer() throws IOException {
        stream.flush();
    }

    public void resetBuffer() {
    }

    public boolean isCommitted() {
        return false;
    }

    public void reset() {
    }

    public void setLocale(Locale locale) {
    }

    public Locale getLocale() {
        return null;
    }

    public void addCookie(Cookie cookie) {
    }

    public boolean containsHeader(String string) {
        return false;
    }

    public String encodeURL(String string) {
        return "";
    }

    public String encodeRedirectURL(String string) {
        return "";
    }

    public String encodeUrl(String string) {
        return "";
    }

    public String encodeRedirectUrl(String string) {
        return "";
    }

    public void sendError(int _int) throws IOException {
    }

    public void sendRedirect(String string) throws IOException {
    }

    public void setDateHeader(String string, long _long) {
    }

    public void addDateHeader(String string, long _long) {
    }

    public void setHeader(String string, String string1) {
    }

    public void setIntHeader(String string, int _int) {
    }

    public void addIntHeader(String string, int _int) {
    }

    public void setStatus(int _int) {
    }

    public void setStatus(int _int, String string) {
    }

    public String getContentType() {
        return "";
    }

    public void setCharacterEncoding(String charset) {
    }


}

⌨️ 快捷键说明

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