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

📄 wapsession.java

📁 封装了SQL、Socket、WAP、MIME等功能的通用组件
💻 JAVA
字号:
package org.lazybug.wap;

import net.sourceforge.jwap.wsp.pdu.CWSPHeaders;
import net.sourceforge.jwap.wsp.header.WAPCodePage;
import net.sourceforge.jwap.util.wbxml.WBXMLDecoder;
import net.sourceforge.jwap.util.wbxml.TokenRepository;
import org.lazybug.util.ConfigUtil;
import org.lazybug.util.Tools;

import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;

import org.w3c.dom.*;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: Kehaoinfo</p>
 *
 * @author David Lau
 * @version 1.0
 */
public abstract class WAPSession
{
    public int MAX_TRANSCATION_SIZE = 256;
    /*会话状态*/
    private int status = -1;
    /*会话端口*/
    private int port = -1;
    /*当前会话对应于请求的事务ID*/
    private int sid;
    /*内容类型*/
    private String contentType;
    /*请求的URL地址*/
    private String reqUri;
    /*会话请求的头信息*/
    private CWSPHeaders reqHdr;
    /*会话请求的方式*/
    private int method;
    /*会话请求的内容*/
    private byte reqBody[];
    /*事务响应的头信息*/
    private CWSPHeaders resHdr;
    /*会话响应的内容*/
    private byte resBody[];
    /*会话启动时间*/
    private long startTime;
    /*资源队列*/
    protected  ArrayList resArray = new ArrayList();

    public WAPSession()
    {
        contentType = "";
        this.startTime = System.currentTimeMillis();
    }

    /**
     * Use this method to construct a GET-MethodInvoke.req.
     *
     * @param uri The Unfied Resource Identifier of the resource to GET
     * @return WAPRequest
     */
    public WAPRequest s_get( int port, String uri )
    {
        this.port = port;
        this.reqUri = uri;
        this.method = WAPRequest.HTTP_METHOD_GET;
        return new WAPGetRequest(this);
    }

    /**
     * Use this method to construct a GET-MethodInvoke.req.
     * @return Implement this com.lazybug.wap.WAPRequest request
     */
    public WAPRequest s_get( int port, String uri, CWSPHeaders headers )
    {
        this.port = port;
        this.reqUri = uri;
        this.reqHdr = headers;
        this.method = WAPRequest.HTTP_METHOD_GET;
        return new WAPGetRequest(this);
    }

    /**
     * Use this method to construct a POST-MethodInvoke.req.
     * @return Implement this com.lazybug.wap.WAPRequest request
     */
    public WAPRequest s_post( int port, String uri, String contentType, byte[] data )
    {
        this.port = port;
        this.reqUri = uri;
        this.contentType = contentType;
        this.reqBody = data;
        this.method = WAPRequest.HTTP_METHOD_POST;
        return new WAPPostRequest(this);
    }

    /**
     * Use this method to construct a POST-MethodInvoke.req.
     */
    public WAPRequest s_post ( int port, String uri, String contentType, byte[] data, CWSPHeaders headers )
    {
        this.port = port;
        this.reqUri = uri;
        this.reqHdr = headers;
        this.reqBody = data;
        this.contentType = contentType;
        this.method = WAPRequest.HTTP_METHOD_POST;
        return new WAPPostRequest(this);
    }

    /**
     * 回调
     */
    public abstract void s_methodResult_ind( int status, byte[] headers, byte[] body);

    /**
     * 收到中止会话
     */
    public abstract void s_abort_ind();

    public void setMethod( int method )
    {
        this.method = method;
    }

    public int getMethod()
    {
        return method;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getPort()
    {
        return port;
    }

    public void setReqUri(String reqUri)
    {
        this.reqUri = reqUri;
    }

    public String getReqUri()
    {
        return this.reqUri;
    }

    /**
     * 得到当前链接的相对地址
     */
    public String getRelUri()
    {
        int i = reqUri.indexOf('/', 7);
        if( i == -1 )
        {
            return reqUri+"/";
        }
        else
        {
            i = reqUri.lastIndexOf('/');
            return reqUri.substring(0, i+1);
        }
    }
    public void setStatus(int result)
    {
        this.status = result;
        if( result < 0 ) this.status = 256 + result;
    }

    public int getStatus()
    {
        return status;
    }

    public void setSid( int sid )
    {
        this.sid = sid;
    }

    public Object getSid()
    {
        return String.valueOf(sid);
    }

    public void setReqHdr(CWSPHeaders reqHdr)
    {
        this.reqHdr = reqHdr;
    }

    public CWSPHeaders getReqHdr()
    {
        return this.reqHdr;
    }

    public byte[] getReqHdrBytes()
    {
        if( this.reqHdr != null )
        {
            return this.reqHdr.getBytes();
        }
        return new byte[0];
    }

    public void setResHdr(CWSPHeaders resHdr) {
        this.resHdr = resHdr;
    }

    public CWSPHeaders getResHdr()
    {
        return resHdr;
    }

    public void setReqBody(byte[] reqBody) {
        this.reqBody = reqBody;
    }

    public byte[] getReqBody() {
        return reqBody;
    }

    public void setResBody(byte[] resBody) {
        this.resBody = resBody;
    }

    public byte[] getResBody()
    {
        return resBody;
    }

    public int getResBodyLen()
    {
        return resBody == null ? 0 : resBody.length;
    }


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

    public String getResCt()
    {
        if( this.resHdr != null )
            return Tools.getValidStr(this.resHdr.getHeader("Content-Type"));
        else
            return "";
    }
    public String getContentType()
    {
        return contentType;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public synchronized WAPSession[] clear()
    {
        WAPSession s[] = new WAPSession[resArray.size()];
        int k = 0;
        for( int i = 0; i < resArray.size(); i++ )
        {
             WAPSession s1 = (WAPSession)WAPSessionManager.
                getInstance().remove(resArray.get(i).toString());
             if( s1 != null ) s[k++] = s1;
        }
        resArray.clear();
        return s;
    }

    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("Status:"+status);
        sb.append("\nPort:"+port);
        sb.append("\nContentType:"+contentType);
        sb.append("\nUri:"+reqUri);
        sb.append("\nMethod:"+method);
        sb.append("\nStartTime:"+Tools.getFormatTime("YYYY-MM-dd HH:mm:ss", startTime));
        return sb.toString();
    }

    public static void main(String args[])
    {
        CWSPHeaders hdr = new CWSPHeaders();
        //hdr.setHeader("Accept", "*/*");
        //hdr.setHeader("Cookie", "xid=FTfhc3c1!833735161!2069560405; path=/");
        hdr.setHeader("Content-Type", "multipart/related;type=\"applicatoin/smil\";start=\"<sddd>\"");
        hdr.setHeader("Accept-Encoding", "deflate,gzip");
        hdr.setHeader("Accept-Encoding", "gzip");
        hdr.setHeader("User-Agent", "Nokia6231/2.0 (03.15) Profile/MIDP-2.0 Configuration/CLDC-1.1  ");
        //System.out.println(hdr.getHeader("User-Agent"));
        Tools.printb(hdr.getBytes());
    }
}

⌨️ 快捷键说明

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