?? wapptr.java~348~
字號:
package net.sourceforge.jwap;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import java.io.*;
import java.net.*;
import java.util.*;
import net.sourceforge.jwap.http.*;
import net.sourceforge.jwap.util.*;
import net.sourceforge.jwap.util.wbxml.*;
import net.sourceforge.jwap.wsp.*;
import net.sourceforge.jwap.wsp.pdu.*;
import net.sourceforge.jwap.wtp.*;
import org.apache.xml.serialize.*;
import org.w3c.dom.*;
/**
* This class represents a WSP "User-Agent" which can be used for executing
* WSP <code>GET</code> and <code>POST</code> methods.
* <p>
* <h3>Example</h3>
* <pre>
* WAPClient client = new WAPClient("localhost", 9201);
* Request request = new GetRequest("http://localhost/");
* client.connect();
* Response response = client.execute(request);
* client.disconnect();
* </pre>
* @author Michel Marti
*
*/
public class WapPtr {
/** Default connect/disconnect timeout in milliseconds: 30000 */
public static final long DEFAULT_CONNECT_TIMEOUT = 10000;
/** Default execute timeout in milliseconds: 500000 */
public static final long DEFAULT_EXEC_TIMEOUT = 20000;
public static final long DEFAULT_HTTP_TIMEOUT = 120000;
private static final Logger log = Logger.getLogger(WapPtr.class);
static {
Logger.initLogSystem(true);
}
private static final String USAGE =
"Usage: WAPClient <WAP-Gateway-address[:port]> [GET/POST] [options] <URL>\n" +
" if method (GET/POST) is unspecified, GET is assumed\n\n" +
" Common options:\n" +
" -u <user-agent> The User-Agent (defaults to jWAP/1.x)\n" +
" -o <file> write response to file\n" +
" -l [addr]:port local port (and address) to bind to\n" +
" -tc <timeout> connection timeout (seconds, default=30)\n" +
" -tx <timeout> request timeout (seconds, default=180)\n" +
" -v show response-headers\n\n" +
" POST options:\n" +
" -c <content-type> The content-type of the response body\n" +
" -p <file> A file containing the post data, use '-' to read" +
" the post data from standard input";
private static final String DEFAULT_CONTENT_TYPE = "application/unknown";
private static final String CONNECTED = "CONNECTED";
private InetAddress gwAddr;
private InetAddress localAddr;
private int gwPort;
private int localPort;
private CWSPSession session;
private HttpSession http_session;
private Hurl http_url = null;
private long disconnectTimeout;
private int downloadflg = 0;
private byte[] sessionLock = new byte[0];
private IWSPUpperLayer2 upperLayerImpl;
private HttpUpperLayer upperlayerhttp;
private Hashtable pendingRequests;
public FileWork filework = null;
public String[] LogStr = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"};
public String FileType = "wml";
public String DownLoadFileName = "";
public String AgentName = "Nokio";
public int Sendflg = 0;
public int downmms = 0;
public int xhtml_wml = 0;
public int waitfor = 0;
public long downtime = 0;
public int linkgate = 1;
public WapPtr() {
}
/**
* Construct a new WAP Client
* @param wapGateway hostname of the WAP gateway to use
* @param port port-number
* @throws UnknownHostException if the hostname cannot be resolved
*/
public WapPtr(String wapGateway, int port) throws UnknownHostException {
this(InetAddress.getByName(wapGateway), port);
}
/**
* Construct a new WAP Client
* @param wapGateway the address of the WAP gateway to use
* @param port the WAP gateway port number
*/
public WapPtr(InetAddress wapGateway, int port) {
this(wapGateway, port, null, CWTPSocket.DEFAULT_PORT);
}
/**
* Construct a new WAP Client
* @param wapGateway the addresss of the WAP gateway to use
* @param wapPort the WAP gateway port number
* @param localAddress the local address to bind to
* @param localPort the local port to bind to (0 to let the OS pick a free port)
*/
public WapPtr(InetAddress wapGateway, int wapPort,
InetAddress localAddress,
int localPort) {
gwAddr = wapGateway;
gwPort = wapPort;
this.localAddr = localAddress;
this.localPort = localPort;
upperLayerImpl = new UpperLayerImpl();
pendingRequests = new Hashtable();
filework = new FileWork();
}
public WapPtr(InetAddress localAddress, int localPort, int type) {
if (!ReadJwapConf()) {
log.error("WapPtr 讀取配置文件失敗");
System.exit(1);
}
this.localAddr = localAddress;
this.localPort = localPort;
upperLayerImpl = new UpperLayerImpl();
pendingRequests = new Hashtable();
filework = new FileWork();
}
/**
* Execute a request. The client must be connected to the gateway.
* @param request the request to execute
* @return the response
* @throws SocketException if a timeout occurred
* @throws IllegalStateException if the client is not connected
*/
public Response execute(Request request) throws SocketException,
IllegalStateException {
return execute(request, DEFAULT_EXEC_TIMEOUT);
}
/**
* Execute a request. The client must be connected to the gateway.
* @param request the request to execute
* @param timeout timeout in milliseconds
* @return the response
* @throws SocketException if a timeout occurred
* @throws IllegalStateException if the client is not connected
*/
public int execute_http(Request request, long timeout) throws
SocketException,
IllegalStateException {
int code = -1;
String basheader = "";
synchronized (this) {
if (http_session == null) {
throw new IllegalStateException("Not yet connected");
}
basheader = "User-Agent:" + AgentName + "\r\n";
basheader = basheader + "Accept: www/source;text/html;" +
"application/vnd.wap.xhtml+xml;" +
"application/xhtml+xml;" +
"application/wml+xml; application/vnd.wap.wmlc;" +
"text/vnd.wap.wml; image/vnd.wap.wbmp; image/png; image/jpeg; image/gif; image/tiff; */*\r\n";
if (request instanceof GetRequest) {
System.out.println("Executing GET Request for URL " +
request.getURL());
if (log.isDebugEnabled()) {
log.debug("Executing GET Request for URL " + request.getURL());
}
try {
code = http_session.s_get(basheader, request.getURL());
} catch (Exception ex) {
}
} else if (request instanceof PostRequest) {
System.out.println("Executing POST Request for URL " +
request.getURL());
if (log.isDebugEnabled()) {
log.debug("Executing POST Request for URL " +
request.getURL());
}
PostRequest post = (PostRequest) request;
code = http_session.s_post(basheader, post.getRequestBody(),
post.getContentType(), post.getURL());
}
}
// Wait until the method shows up in our hashtable
/*
if (httpwaitForCompletion(DEFAULT_HTTP_TIMEOUT) == 1) {
code = -1;
} else {
code = 200;
}*/
int count = 0;
for (count = 0; count < 3; count++) {
if (httpwaitForCompletion(DEFAULT_HTTP_TIMEOUT) == 1) {
code = -1;
waitfor = 0;
//http_session.pThreadClose();
if (http_session.Sconnect() == 7) {
if (count >= 2) {
break;
}
} else {
if (count < 2) {
if (request instanceof GetRequest) {
System.out.println(count +
" Executing GET Request for URL " +
request.getURL());
http_session.s_get(basheader, request.getURL());
} else if (request instanceof PostRequest) {
System.out.println(count +
" Executing POST Request for URL " +
request.getURL());
PostRequest post = (PostRequest) request;
code = http_session.s_post(basheader,
post.getRequestBody(),
post.getContentType(), post.getURL());
}
} else {
code = -1;
break;
}
}
} else {
code = 200;
break;
}
}
if (log.isDebugEnabled()) {
log.debug("Waiting " + timeout + "ms for execute completion...");
}
return code;
}
/**
* Execute a request. The client must be connected to the gateway.
* @param request the request to execute
* @param timeout timeout in milliseconds
* @return the response
* @throws SocketException if a timeout occurred
* @throws IllegalStateException if the client is not connected
*/
public Response execute(Request request, long timeout) throws
SocketException,
IllegalStateException {
CWSPMethodManager mgr = null;
CWSPHeaders headers = request.getWSPHeaders();
synchronized (this) {
if (session == null) {
throw new IllegalStateException("Not yet connected");
}
if (headers.getHeader("Encoding-Version") == null) {
headers.setHeader("Encoding-Version", "2.0");
}
if (headers.getHeader("Accept") == null) {
headers.setHeader("Accept", "*/*");
headers.addHeader("Accept", "application/vnd.wap.wmlc");
headers.addHeader("Accept",
"application/vnd.wap.multipart.mixed");
headers.addHeader("Accept", "application/vnd.wap.wmlcscriptc");
headers.addHeader("Accept",
"application/vnd.wap.multipart.related");
headers.addHeader("Accept", "application/octet-stream");
headers.addHeader("Accept", "text/plain");
headers.addHeader("Accept", "text/css");
headers.addHeader("Accept", "image/bmp");
headers.addHeader("Accept", "image/gif");
headers.addHeader("Accept", "image/jpg");
headers.addHeader("Accept", "image/png");
headers.addHeader("Accept", "image/vnd.wap.wbmp");
headers.addHeader("Accept", "application/smil");
headers.addHeader("Accept", "application/vnd.wap.mms-message");
headers.addHeader("Accept", "text/vnd.sun.j2me.app-descriptor");
headers.addHeader("Accept", "application/java-archive");
headers.setHeader("Profile", "URIx");
headers.addHeader("Profile", "URIy");
}
String uh = headers.getHeader("User-Agent");
if (uh == null) {
headers.setHeader("User-Agent", AgentName);
} else if ("".equals(uh)) {
headers.setHeader("User-Agent", AgentName);
}
if (request instanceof GetRequest) {
System.out.println("Executing GET Request for URL " +
request.getURL());
if (log.isDebugEnabled()) {
log.debug("Executing GET Request for URL " + request.getURL());
}
mgr = session.s_get(headers, request.getURL());
} else if (request instanceof PostRequest) {
System.out.println("Executing POST Request for URL " +
request.getURL());
if (log.isDebugEnabled()) {
log.debug("Executing POST Request for URL " +
request.getURL());
}
PostRequest post = (PostRequest) request;
mgr = session.s_post(post.getWSPHeaders(), post.getRequestBody(),
post.getContentType(), post.getURL());
}
}
// Wait until the method shows up in our hashtable
if (log.isDebugEnabled()) {
log.debug("Waiting " + timeout + "ms for execute completion...");
}
int count = 0;
Response response = null;
for (count = 0; count < 3; count++) {
response = null;
response = (Response) waitForCompletion(mgr, timeout);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -