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

📄 httprequest.java

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

import java.io.InputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.EOFException;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.net.URLDecoder;
import javax.servlet.ServletInputStream;


import java.net.URLDecoder;
import javax.servlet.ServletRequest;
import java.util.Enumeration;
import java.io.BufferedReader;
import java.util.Locale;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Cookie;
import java.security.Principal;
import javax.servlet.http.HttpSession;


public class HttpRequest implements HttpServletRequest {


    private InputStream input;

    private String method;

    private String protocol;

    private String url;

    private String query;

    private byte[] postData;

    private Map httpVariableMap;

    private int major;

    private int minor;

    private HttpHeaders headers;

    private String connectionHeader;

    public int RECEIVER_BUFFER_SIZE = 4096;

    public HttpRequest(InputStream input) {
        init();
        this.input = input;
    }

    private void init() {
        method = null;
        url = null;
        query = null;
        protocol = null;
        connectionHeader = null;
        postData = null;
        httpVariableMap = null;
//        timeStamp = System.currentTimeMillis();
        connectionHeader = "Connection";
//        requestId = new Integer( nextRequestId++ );
    }


    public void streamHandler() throws Exception {
        String startLine = null;

        InternetInputStream stream = new InternetInputStream(input,
                RECEIVER_BUFFER_SIZE);
        startLine = readHttpCommand(stream);
        if (startLine == null) {
            throw new EOFException();
        }
        if (protocol.equals("HTTP/1.0")) {
            major = 1;
            minor = 0;
        } else if (protocol.equals("HTTP/1.1")) {
            major = 1;
            minor = 1;
        } else {
            throw new Exception("Protocol " + protocol +
                                " not supported.");
        }
        //获得http头信息
        headers = new HttpHeaders(stream);
        //
        readPostData(stream);
    }

    private String readHttpCommand(InternetInputStream stream) throws
            IOException {
        String startLine = null;
        do {
            startLine = stream.readline();
            if (startLine == null)
                return null;
        } while (startLine.trim().length() == 0);

        StringTokenizer tokenizer = new StringTokenizer(startLine);
        method = tokenizer.nextToken();
        parseUrl(tokenizer.nextToken());
        protocol = tokenizer.nextToken();
        return startLine;
    }

    private void readPostData(InternetInputStream stream) throws IOException {
        String contenLength = getRequestHeader("Content-Length");
        if (contenLength == null)
            return;

        int postLength = Integer.parseInt(contenLength);
        postData = new byte[postLength];

        int length = -1;
        int offset = stream.read(postData);
        while (offset >= 0 && offset < postData.length) {
            length = stream.read(postData, offset, postData.length - offset);
            if (length == -1) {
                break;
            }
            offset += length;
        }
    }

    private void parseUrl(String aUrl) {
        int queryIndex = aUrl.indexOf('?');
        if (queryIndex < 0) {
            url = aUrl.substring(1);
        } else {
            url = aUrl.substring(1, queryIndex);
            if ((queryIndex + 1) < aUrl.length())
                query = aUrl.substring(queryIndex + 1);
        }
    }

    public String getRequestHeader(String key) {
        return headers.get(key);
    }

    public String getRequestHeader(String key, String defaultValue) {
        String val = getRequestHeader(key);
        return (val == null) ? defaultValue : val;
    }

    public String getMethod() {
        return method;
    }

    public String getUrl() {
        return url;
    }

    public String getQuery() {
        return query;
    }


    public String getQueryData(String key) {
        if (httpVariableMap == null) {
            httpVariableMap = createQueryMap(query);
            if (postData != null) {
                String contentType = headers.get("Content-Type");
                if ("application/x-www-form-urlencoded".equals(contentType)) {
                    httpVariableMap.putAll(createQueryMap(new String(postData)));
                }
            }
        }
        return (String) httpVariableMap.get(key);
    }

    private Map createQueryMap(String query) {
        Map queryMap = new TreeMap();
        if (query == null) {
            return queryMap;
        }

        query = query.replace('+', ' ');
        StringTokenizer st = new StringTokenizer(query, "&");
        try {
            while (st.hasMoreTokens()) {
                String field = st.nextToken();
                int index = field.indexOf('=');
//                if (index < 0) {
//                    queryMap.put(URLDecoder.decode(field, "UTF-8"), "");
//                } else {
//                    queryMap.put(URLDecoder.decode(field.substring(0, index),
//                            "UTF-8"),
//                                 URLDecoder.decode(field.substring(index + 1),
//                            "UTF-8"));
//                }
                if (index < 0) {
                  queryMap.put(URLDecoder.decode(field, "ISO-8859-1"), "");
                }
                else {
                  queryMap.put(URLDecoder.decode(field.substring(0, index),
                                                 "ISO-8859-1"),
                               URLDecoder.decode(field.substring(index + 1),
                                                 "ISO-8859-1"));
                }
            }
        } catch (UnsupportedEncodingException e) {}

        return queryMap;
    }

    public String getProtocol() {
        return protocol;
    }

    public byte[] getPostData() {
        return postData;
    }

    public boolean isKeepAlive() {
        if ("Keep-Alive".equalsIgnoreCase(getRequestHeader(connectionHeader))) {
            return true;
        } else if ("close".equalsIgnoreCase(getRequestHeader(connectionHeader))) {
            return false;
        } else if (major >= 1 && minor > 0) {
            return true;
        } else {
            return false;
        }
    }

    public int getMajorVersion() {
        return major;
    }

    public int getMinorVersion() {
        return minor;
    }

    public String getConnectionHeader() {
        return connectionHeader;
    }

//    public String getProperty(String key) {
//        return getProperty( key, null );
//    }

    public HttpHeaders getHeaders() {
        return headers;
    }

    public String toString() {
        return method + " " + url + ((query != null) ? "?" + query : "") + " " +
                protocol;
    }

//    public String serverUrl() {
//        return getProperty("url");
//    }

    public String createUrl(String absolutePath) throws IOException {
        return absolutePath;
    }

    public boolean isProtocolVersionLessThan(int aMajor, int aMinor) {
        return (major <= aMajor && minor < aMinor);
    }


    public Object getAttribute(String string) {
        return null;
    }

    public Enumeration getAttributeNames() {
        return null;
    }

    public String getCharacterEncoding() {
        return "";
    }

    public void setCharacterEncoding(String string) throws
            UnsupportedEncodingException {
    }

    public int getContentLength() {
        return 0;
    }

    public String getContentType() {
        return "";
    }

    public ServletInputStream getInputStream() throws IOException {
        return (ServletInputStream)input;
    }

    public String getParameter(String string) {
        return getQueryData(string);
    }

    public Enumeration getParameterNames() {
        return null;
    }

    public String[] getParameterValues(String string) {
        return null;
    }

    public Map getParameterMap() {
        return createQueryMap(query);
    }

    public String getScheme() {
        return "";
    }

    public String getServerName() {
        return "";
    }

    public int getServerPort() {
        return 0;
    }

    public BufferedReader getReader() throws IOException {
        return null;
    }

    public String getRemoteAddr() {
        return "";
    }

    public String getRemoteHost() {
        return "";
    }

    public void setAttribute(String string, Object object) {
    }

    public void removeAttribute(String string) {
    }

    public Locale getLocale() {
        return null;
    }

    public Enumeration getLocales() {
        return null;
    }

    public boolean isSecure() {
        return false;
    }

    public RequestDispatcher getRequestDispatcher(String string) {
        return null;
    }

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

    public String getAuthType() {
        return "";
    }

    public Cookie[] getCookies() {
        return null;
    }

    public long getDateHeader(String string) {
        return 0L;
    }

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

    public Enumeration getHeaders(String string) {
        return null;
    }

    public Enumeration getHeaderNames() {
        return null;
    }

    public int getIntHeader(String string) {
        return 0;
    }

    public String getPathInfo() {
        return "";
    }

    public String getPathTranslated() {
        return "";
    }

    public String getContextPath() {
        return "";
    }

    public String getQueryString() {
        return "";
    }

    public String getRemoteUser() {
        return "";
    }

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

    public Principal getUserPrincipal() {
        return null;
    }

    public String getRequestedSessionId() {
        return "";
    }

    public String getRequestURI() {
        return "";
    }

    public StringBuffer getRequestURL() {
        return null;
    }

    public String getServletPath() {
        return "";
    }

    public HttpSession getSession(boolean _boolean) {
        return null;
    }

    public HttpSession getSession() {
        return null;
    }

    public boolean isRequestedSessionIdValid() {
        return false;
    }

    public boolean isRequestedSessionIdFromCookie() {
        return false;
    }

    public boolean isRequestedSessionIdFromURL() {
        return false;
    }

    public boolean isRequestedSessionIdFromUrl() {
        return false;
    }

    public int getRemotePort() {
        return 0;
    }

    public String getLocalName() {
        return "";
    }

    public String getLocalAddr() {
        return "";
    }

    public int getLocalPort() {
        return 0;
    }

}


⌨️ 快捷键说明

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