📄 wapmethodmanager.java
字号:
package org.lazybug.wap;
import java.util.*;
import net.sourceforge.jwap.wsp.pdu.CWSPHeaders;
import net.sourceforge.jwap.wsp.WSPDecoder;
import net.sourceforge.jwap.wsp.header.WAPCodePage;
import net.sourceforge.jwap.util.BitArrayOutputStream;
import org.lazybug.util.Tools;
/**
* <p>Title: </p>
*
* <p>Description: 实现WAP的事务管理</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: www.lazybug.com</p>
*
* @author David Lau
* @version 1.0
*/
public class WAPMethodManager extends HashMap
{
private static WAPMethodManager handle;
private static int transcation_id = 0;
public static WAPMethodManager getInstance()
{
if( handle == null ) handle = new WAPMethodManager();
return handle;
}
public WAPMethodManager()
{
transcation_id = 0;
}
public synchronized void abort()
{
Iterator it = this.values().iterator();
while(it.hasNext())
{
WAPRequest request = (WAPRequest)it.next();
request.getSession().s_abort_ind();
}
this.clear();
}
public synchronized static int generateTransactionId()
{
return transcation_id++;
}
public void addMethod(int tid, WAPRequest req)
{
req.setTid(tid);
req.getSession().setSid(tid);
this.put(req.ID(), req);
}
public void addMethod(WAPRequest req)
{
addMethod(generateTransactionId(), req);
}
public WAPRequest getMethod(int tid)
{
Object o = String.valueOf(tid);
return (WAPRequest)this.get(o);
}
/**
* 将数据解码到WAP头上
* @param payload
* @return
*/
public static CWSPHeaders decode2Headers(byte payload[])
{
// This is a class, that helps us with decoding bits
WSPDecoder bin = new WSPDecoder(payload);
// length of header + contenttype
//long headlength = payload.length + 1;//bin.getUintVar();
// decode contenttype
//int cpos = bin.seek(0);
String contentType = decod2ContentType(bin);
//System.out.println("decode content-type:"+contentType);
int headlength = payload.length - bin.seek(0);
//System.out.println("headlength="+headlength);
// decode headers
CWSPHeaders headers = new CWSPHeaders(bin, headlength, WAPCodePage.getInstance());
headers.addHeader("Content-Type", contentType);
// the balance is payload
/* payload = bin.getBytes(bin.getRemainingOctets());
CWSPReply pdu9 = new CWSPReply(status, payload, contenttype);
pdu9.setHeaders(headers);
end = pdu9;*/
return headers;
}
private static byte[] encode2ContentType(String ctype)
{
try
{
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
WAPCodePage.getInstance().encodeContentType(out, (short)0, ctype);
java.io.ByteArrayOutputStream out1 = new java.io.ByteArrayOutputStream();
out1.write(out.toByteArray(), 1, out.size() - 1);
return out1.toByteArray();
}
catch(Exception e)
{
}
return null;
}
public static byte[] encode2Header(CWSPHeaders hdr)
{
if( hdr != null )
{
String ctype = hdr.getHeader("Content-Type");
if (ctype != null && ctype.length() > 0)
{
BitArrayOutputStream out = new BitArrayOutputStream();
byte payload[] = hdr.getBytes();
return Tools.truncate(payload, 1, payload.length - 1);
}
}
return null;
}
/*public static byte[] encode2Header(String ctype, CWSPHeaders hdr)
{
if( ctype != null )
{
byte arrCtype[] = encode2ContentType(ctype);
byte buf[] = new byte[0];
if( arrCtype != null )
{
int len = arrCtype.length;
BitArrayOutputStream out = new BitArrayOutputStream();
if( hdr != null )
{
buf = hdr.getBytes();
}
len += buf.length;
out.writeUintVar(len);
out.write(arrCtype);
out.write(buf);
return out.toByteArray();
}
}
return null;
}*/
/**
* 从payload中解码出内容
* @param payload
* @return
*/
private static String decod2ContentType(byte payload[])
{
// This is a class, that helps us with decoding bits
WSPDecoder bin = new WSPDecoder(payload);
// length of header + contenttype
long headlength = bin.getUintVar();
// decode contenttype
int cpos = bin.seek(0);
return decod2ContentType(bin);
}
private static synchronized String decod2ContentType(WSPDecoder dc)
{
int hs = CWSPHeaders.getHeaderValueSize(dc);
byte[] header = dc.getBytes(hs);
return WAPCodePage.getInstance().decodeContentType(header);
}
public static void main( String[] args )
{
/* net.sourceforge.jwap.util.Logger.initLogSystem(true);
WAPCodePageTest test = new WAPCodePageTest("NEW");
try{
test.setUp();
test.testAcceptRanges();
}catch(Exception e){
e.printStackTrace();
}
byte buf[] = {0x03,0x88-256,0x81-256,0xEA-256,0x92-256,0x04,0x44,0x6C,0x90-256,0xE5-256};
CWSPHeaders h = decode2Headers(buf);
System.out.println(h); */
//byte buf[] = encode2ContentType("multipart/related;type=\"applicatoin/smil\";start=\"<sddd>\"");
//byte buf[] = encode2Header("multipart/related", null);
CWSPHeaders hdr1 = new CWSPHeaders();
hdr1.setHeader("Content-Type", "image/gif");
hdr1.setHeader("Location", "http://wap.sina.com");
hdr1.setHeader("Cookie", "xid=FTfhc3c1!833735161!2069560405; path=/");
hdr1.setHeader("User-Agent", "Nokia6231/2.0 (03.15) Profile/MIDP-2.0 Configuration/CLDC-1.1 ");
hdr1.setHeader("Accept-Encoding", "deflate,gzip");
hdr1.setHeader("Accept-Encoding", "gzip");
byte buf[] = encode2Header(hdr1);
if( buf != null )
Tools.printb(buf);
else
System.out.println("Failed to encode content-type.");
CWSPHeaders hdr = decode2Headers(buf);
System.out.println(hdr.getHeader("Content-Type"));
System.out.println(hdr.getHeader("Accept"));
System.out.println(hdr.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -