📄 wapbrowser.java~2~
字号:
{
String uri = null;
int k = wml.indexOf("onenterforward");
if( k == -1 ) k = wml.indexOf("ontimer=");
if( k == -1 ) return null;
int i = wml.indexOf("onenterforward=");
if( i > 0 )
{
int m, n;
i += "onenterforward=".length();
if( wml.charAt( i ) == '\'' || wml.charAt( i ) == '"' )
{
m = i + 1;
n = wml.indexOf(wml.charAt(i), m);
}
else
{
m = i;
n = wml.indexOf(' ', m);
}
if( n > 0 )
{
uri = wml.substring(m, n);
uri = Tools.replaceStr(uri, "&", "&");
uri = Tools.checkHttpUri(uri, this.getRelUri());
}
}
else
{
k = wml.indexOf("<go ");
if( k > 0 )
{
i = wml.indexOf("href=", k);
if( i > 0 )
{
i += "href=".length();
int m, n;
if( wml.charAt( i ) == '\'' || wml.charAt( i ) == '"' )
{
m = i + 1;
n = wml.indexOf(wml.charAt(i), m);
}
else
{
m = i;
n = wml.indexOf(' ', m);
}
if( n > 0 )
{
uri = wml.substring(m, n);
uri = Tools.replaceStr(uri, "&", "&");
uri = Tools.checkHttpUri(uri, this.getRelUri());
}
}
}
else
{
k = wml.indexOf("<card ");
if( k > 0 )
{
i = wml.indexOf("ontimer=", k);
if( i > 0 )
{
i += "ontimer=".length();
int m, n;
if( wml.charAt( i ) == '\'' || wml.charAt( i ) == '"' )
{
m = i + 1;
n = wml.indexOf(wml.charAt(i), m);
}
else
{
m = i;
n = wml.indexOf(' ', m);
}
if( n > 0 )
{
uri = wml.substring(m, n);
uri = Tools.replaceStr(uri, "&", "&");
uri = Tools.checkHttpUri(uri, this.getRelUri());
}
}
}
}
}
return uri;
}
public synchronized void print(PrintWriter writer) throws IOException
{
}
private String responseMessage(String title, String uri, String content)
{
StringBuffer sb = new StringBuffer();
sb.append(content);
if( uri != null )
{
sb.append("<BR><BR>-----------------------");
sb.append("<BR><BR><a href='#'>"+uri+"</a>");
}
sb.append("<BR><BR>-----------------------<BR>");
sb.append("Conent-Type: ");
sb.append(this.getResCt());
sb.append("<BR>--------------------------------------<BR>MSTE WAP Browser(c)<BR>");
sb.append("<img src='http://localhost/mste/images/mste.gif' width=186 height=60/>");
this.setTitle(title);
return sb.toString();
}
private void decodeWmlc() throws IOException
{
String wml = null;
if( getResBodyLen() > 0 )
{
ByteArrayInputStream in = new java.io.ByteArrayInputStream(getResBody());
Document doc = WBXMLDecoder.getInstance().decode(in);
ByteArrayOutputStream outer = new java.io.ByteArrayOutputStream(64*1024);
String charset = Tools.getValidStr(getResHdr().getHeader( "Charset" ), "UTF-8");
wml = new String(outer.toByteArray(), charset);
//wml = wml.toLowerCase();
String onterForward = checkOnenterforward(wml);
Log.logWarning(this, "Check the onterforward: "+onterForward);
if( onterForward != null )
{
wml = responseMessage("跳转页面", null,
"<a href='javascript:openi(\""+onterForward+"\")'>跳转到"+onterForward+"</a>");
}
else
{
decodeTitle(wml);
//System.out.println("Decode title:"+decodeTitle(wml));
if( this.title == null || this.title.length() == 0 )
setTitle("未知");
wml = decodeGoScript(wml);
wml = decodeImgResource( wml );
wml = decodeLinkResource( wml );
}
}
if( wml == null )
{
wml = responseMessage("错误页面", this.getReqUri(), "无法显示网页(failed to decode page)");
throw new IOException( "failed to decode page." );
}
this.setPreviewBuffer(wml.getBytes());
}
/**
*
* @throws IOException
*/
private void decodeWml( ) throws IOException
{
String wml = null;
if( getResBodyLen() > 0 )
{
String charset = Tools.getValidStr(getResHdr().getHeader( "Charset" ), "UTF-8");
wml = new String( getResBody(), charset);
//wml = wml.toLowerCase();
String onterForward = checkOnenterforward(wml);
if( onterForward != null )
{
wml = responseMessage("跳转页面", null,
"<a href='javascript:openi(\""+onterForward+"\")'>跳转到"+onterForward+"</a>");
}
else
{
decodeTitle(wml);
if( this.title == null || this.title.length() == 0 )
setTitle("未知");
wml = decodeGoScript(wml);
wml = decodeImgResource( wml );
wml = decodeLinkResource( wml );
}
}
if( wml == null )
{
wml = responseMessage("错误页面", this.getRelUri(), "无法显示网页(failed to decode page)");
throw new IOException( "failed to decode page." );
}
this.setPreviewBuffer(wml.getBytes());
}
/**
*
* @throws IOException
*/
private void decodeXhtml() throws IOException
{
String wml = null;
if( getResBodyLen() > 0 )
{
String charset = Tools.getValidStr(getResHdr().getHeader( "Charset" ), "UTF-8");
if( charset.length() > 0 )
wml = new String( getResBody(), charset);
else
wml = new String( getResBody());
//wml = wml.toLowerCase();
//Log.logError(this, wml);
decodeTitle(wml);
if( this.title == null || this.title.length() == 0 )
setTitle("未知");
wml = decodeImgResource( wml );
wml = decodeLinkResource( wml );
}
if( wml == null )
{
wml = responseMessage("错误页面", this.getRelUri(), "无法显示网页(failed to decode page)");
throw new IOException( "failed to decode page." );
}
this.setPreviewBuffer(wml.getBytes());;
}
/**
*
* @throws IOException
*/
private void decodeUnknown() throws IOException
{
String encoding = getResHdr().getHeader( "Content-Transfer-Encoding" );
Log.logMessage(this, "Decode<encoding="+encoding+">.");
byte buf[] = null;
int i = 0;
if( encoding != null )
{
ByteArrayInputStream in = new ByteArrayInputStream( getResBody() );
InputStream in1 = MimeUtility.decode( in, encoding );
buf = new byte[in1.available()];
i = in1.read( buf, 0, in1.available() );
in.close();
}
else
{
buf = getResBody();
i = buf.length;
}
//out.write( buf, 0, i );
this.previewBuffer = new byte[i];
Tools.copyByteArray(buf, this.previewBuffer);
}
public synchronized void decode() throws IOException
{
/*保存数据到本地文件缓存*/
saveToFile(getResCt() ,getResBody());
if( getStatus() != 0x20 )
{
throw new IOException(WAPConstainer.WAP_STATUS[getStatus()]);
}
Log.logMessage(this, "Decode data(Content-Type="+getResCt()+
";Content-Length="+getResBodyLen()+";Url="+getBrowserUrl()+";RelUrl="+getRelUri()+">.");
/*普通Text类型*/
if( getResCt().indexOf( "text" ) != -1 )
{
decodeWml();
}
/*WMLc类型*/
else if( getResCt().indexOf( "application/vnd.wap.wmlc" ) != -1 )
{
decodeWmlc();
}
/*XHTML*/
else if( getResCt().indexOf( "application/xhtml+xml" ) != -1 )
{
decodeXhtml();
}
/*其它类型*/
else
{
decodeUnknown();
}
}
public String getContext()
{
return context;
}
public void setContext(String context)
{
this.context = context;
}
public void saveToFile(String ct, byte[] payload)
{
File dir = new File(ConfigUtil.WEB_PATH + "wml\\");
if( !dir.exists() ) dir.mkdir();
String fn = Tools.encodeMD5(getReqUri());
//Log.logError(this,"Save "+getReqUri());
//Log.logError(this,"To "+fn);
if( ct.indexOf("text") != - 1 || ct.indexOf("application/xhtml+xml") != -1 )
{
fn = fn + ".txt";
}
else
{
fn = Tools.replaceStr(fn, "\\", ";");
fn = Tools.replaceStr(fn, "/", ";");
int k = ct.lastIndexOf('.');
if( k == -1 ) k = ct.lastIndexOf('/');
if( k != -1 ) fn = fn + "."+ct.substring(k+1);
}
setContext("wml/"+fn);
String f = ConfigUtil.WEB_PATH + "wml\\" + fn;
File file = new File(f);
try
{
FileOutputStream out = new FileOutputStream( file );
out.write( payload);
out.flush();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setLocalProxy( String InternetLinkPath )
{
this.LocalProxy = InternetLinkPath;
}
public boolean isReply()
{
return reply;
}
public boolean isAbort()
{
return abort;
}
public String getTitle()
{
return title;
}
/*
public void setFilter( boolean filter )
{
this.filter = filter;
}
public boolean isFilter()
{
return filter;
}*/
public String getBodyString()
{
try{
String s = new String(getResBody(), "UTF-8");
//System.out.println(s);
return s;
}
catch(Exception e){
//System.out.println(e.getMessage());
return "";
}
}
public void setReply( boolean reply )
{
this.reply = reply;
}
public void setAbort( boolean abort )
{
this.abort = abort;
}
public void setTitle( String title )
{
this.title = title;
}
public String getUserAgent()
{
return userAgent;
}
public void setUserAgent(String userAgent)
{
this.userAgent = userAgent;
}
public String getBrowserKey()
{
return this.getReqUri() + this.getUserAgent();
}
public String getBrowserUrl()
{
return this.getReqUri() + "<"+this.getUserAgent()+">";
}
public byte[] getPreviewBuffer()
{
return previewBuffer;
}
public void setPreviewBuffer(byte[] previewBuffer)
{
this.previewBuffer = previewBuffer;
}
public static void main( String[] args )
{
try
{
FileInputStream in = new FileInputStream(new File(
"D:\\work\\project\\mstar\\tomcat\\webapps\\wap\\wml\\aHR0cDovL215LmkxMzkuY24vaW5kZXgueGh0bWw=.xml"));
byte buf[] = new byte[in.available()];
in.read(buf);
String s = new String(buf, "UTF-8");
WAPBrowser b = new WAPBrowser();
System.out.println(b.decodeTitle(s));
}
catch(IOException e)
{
e.printStackTrace();
}
//System.out.println(Base64.encode("http://218.206.69.39/picto/i.php?http://my.i139.cn/i34338f6w230h50.gif".getBytes()));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -