Gateway.java
上传用户:kyckim
上传日期:2007-12-11
资源大小:332k
文件大小:20k
- package Hiisi;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.util.Calendar;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import javax.microedition.io.Connector;
- import javax.microedition.io.HttpConnection;
- import javax.microedition.io.ServerSocketConnection;
- import javax.microedition.io.SocketConnection;
- import javax.microedition.io.StreamConnection;
- import javax.microedition.lcdui.Display;
- public class Gateway extends Thread {
-
- boolean running = true;
-
- private Calendar cal = null;
- private int year = 0;
- private String month = null;
- private int day_of_month = 0;
- private int hour = 0;
- private int minute = 0;
- private int second = 0;
-
- private ServerSocketConnection ssc = null;
-
- private SocketConnection sc = null;
- private DataInputStream input = null;
- private DataOutputStream output = null;
- private StreamConnection bc = null;
- private DataInputStream bluetoothInput = null;
- private DataOutputStream bluetoothOutput = null;
-
- private HttpConnection hc = null;
- private DataInputStream httpInput = null;
- private DataOutputStream httpOutput = null;
-
- private String requestMethod = null;
- private String requestURI = null;
- private String requestProtocol = null;
- private String requestHost = null;
- private int requestPort = 80;
- private Hashtable requestHeaderField = new Hashtable();
- private byte[] postData = null;
-
- private String responseProtocol = null;
- private String responseCode = null;
- private String responseMessage = null;
- private Hashtable responseHeaderField = new Hashtable();
- private int byteTranfered = 0;
-
- Gateway() {
- }
-
- private void closeBluetooth() {
- try{
- if(bluetoothInput != null) {
- bluetoothInput.close();
- bluetoothInput=null;
- }
- } catch(IOException e) {
- }
- try{
- if(bluetoothOutput != null) {
- bluetoothOutput.close();
- bluetoothOutput=null;
- }
- } catch(IOException e) {
- }
- try{
- if(bc != null){
- bc.close();
- bc = null;
- }
- } catch(IOException e){
- }
- }
-
- private void closeHttp() {
- try{
- if(httpInput != null) {
- httpInput.close();
- httpInput=null;
- }
- } catch(IOException e) {
- }
- try{
- if(httpOutput != null) {
- httpOutput.close();
- httpOutput=null;
- }
- } catch(IOException e) {
- }
- try{
- if(hc != null){
- hc.close();
- hc = null;
- }
- } catch(IOException e){
- }
- }
-
- private void closeInput() {
- try{
- if(input != null){
- input.close();
- input = null;
- }
- } catch(IOException e) {
- }
- }
-
- private void closeOutput() {
- try{
- if(output != null){
- output.close();
- output = null;
- }
- } catch(IOException e) {
- }
- }
-
- private void closeSocket() {
- try{
- if(sc != null){
- sc.close();
- sc = null;
- }
- } catch(IOException e) {
- }
- }
-
- private void closeServerSocket() {
- try {
- if(ssc != null) {
- ssc.close();
- ssc = null;
- }
- } catch(IOException e){
- }
- }
-
- void exitGateway() {
- closeInput();
- closeBluetooth();
- closeHttp();
- closeOutput();
- closeSocket();
- closeServerSocket();
- }
-
- public void run() {
- try {
- ssc = (ServerSocketConnection)Connector.open("socket://:1234");
- } catch(IOException e) {
- }
- while(running){
- HiisiMIDlet.mainForm.log("Hiisi Proxy " + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString() + " is idling...");
- connectingProcess();
- }
- exitGateway();
- }
-
- private void connectingProcess() {
- try{
- sc = (SocketConnection)ssc.acceptAndOpen();
- sc.setSocketOption(SocketConnection.DELAY, 0);
- sc.setSocketOption(SocketConnection.LINGER, 0);
- sc.setSocketOption(SocketConnection.KEEPALIVE, 0);
- sc.setSocketOption(SocketConnection.RCVBUF, 8192);
- sc.setSocketOption(SocketConnection.SNDBUF, 8192);
-
- HiisiMIDlet.mainForm.log("Hiisi Proxy " + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString() + " is running...");
-
- requestMethod = "";
- requestURI = "";
- requestProtocol = "";
- requestHeaderField.clear();
- requestHost = "";
- requestPort = 80;
-
- input = sc.openDataInputStream();
- readRequestHeader();
- closeInput();
- if(HiisiMIDlet.settingForm.isFilter() && (requestURI.indexOf(HiisiMIDlet.settingForm.getFilterURL()) == -1)) {
- requestURI = HiisiMIDlet.settingForm.getFilterURL() + "?_ucb_l=1&_ucb_k=1&_ucb_u=" + requestURI;
- output = sc.openDataOutputStream();
- output.write(("HTTP/1.1 302 Moved Temporarilyrn").getBytes());
- output.write(("Location: " + requestURI + "rnrn").getBytes());
- output.flush();
- closeOutput();
- closeSocket();
- return;
- }
- if(requestURI.indexOf("http://ime.nu/") != -1) {
- requestURI = "http://" + requestURI.substring(14);
- output = sc.openDataOutputStream();
- output.write(("HTTP/1.1 302 Moved Temporarilyrn").getBytes());
- output.write(("Location: " + requestURI + "rnrn").getBytes());
- output.flush();
- closeOutput();
- closeSocket();
- return;
- }
-
- if(HiisiMIDlet.settingForm.getGatewayMode() == HiisiMIDlet.settingForm.BLUETOOTH_MODE) {
- bc = (StreamConnection)Connector.open(HiisiMIDlet.bluetoothConnection.getUrl());
- bluetoothOutput = bc.openDataOutputStream();
- connectViaBluetooth();
- } else if (HiisiMIDlet.settingForm.getGatewayMode() == HiisiMIDlet.settingForm.WAP_MODE) {
- hc = (HttpConnection)Connector.open(requestURI, 1);
- ConnectViaWap();
- }
-
- responseProtocol = "";
- responseCode = "";
- responseMessage = "";
- responseHeaderField.clear();
- byteTranfered = 0;
-
- if(HiisiMIDlet.settingForm.getGatewayMode() == HiisiMIDlet.settingForm.BLUETOOTH_MODE) {
- bluetoothInput = bc.openDataInputStream();
- readResponseHeaderViaBluetooth();
- output = sc.openDataOutputStream();
- writeResponseFromBluetooth();
- closeBluetooth();
- } else if(HiisiMIDlet.settingForm.getGatewayMode() == HiisiMIDlet.settingForm.WAP_MODE) {
- httpInput = hc.openDataInputStream();
- readResponseHeaderViaWap();
- output = sc.openDataOutputStream();
- writeResponseFromWap();
- closeHttp();
- }
- HiisiMIDlet.logForm.totalLog(HiisiMIDlet.logForm.getSentByte(), HiisiMIDlet.logForm.getRecvdByte());
- HiisiMIDlet.logForm.log("localhost - - [" + getAccessTime()+ "] "" +
- requestMethod + " " + requestURI + " " + requestProtocol + "" " + responseCode + " " + byteTranfered + "n");
- closeOutput();
- closeSocket();
- } catch(IOException e) {
- // HiisiMIDlet.mainForm.log(e);
- } catch(InterruptedException e) {
- // HiisiMIDlet.mainForm.log(e);
- } finally{
- closeInput();
- closeBluetooth();
- closeHttp();
- closeOutput();
- closeSocket();
- }
- }
-
- private void readRequestHeader() throws IOException {
- StringBuffer sb = new StringBuffer();
- int c;
- int line = 0;
-
- while((c = input.read()) != -1){
- if((char)c == 'r') {
- if(sb.length() == 0) {
- break;
- } else {
- processRequestHeader(sb.toString(), line);
- sb.delete(0, sb.length());
- line++;
- }
- } else if((char)c == 'n') {
- } else {
- sb.append((char)c);
- }
- }
- sb = null;
- if(requestMethod.equals("POST")) {
- int dummy = input.read();
- if(requestHeaderField.get("content-length") != null) {
- int contentLength = Integer.valueOf((String)requestHeaderField.get("content-length")).intValue();
- postData = new byte[contentLength];
- input.read(postData, 0, contentLength);
- HiisiMIDlet.logForm.addSentByte(contentLength);
- }
- }
- }
-
- private void processRequestHeader(String str, int line) throws IOException {
- str = str.trim();
-
- if(line == 0){
- int delimitPos = str.indexOf(' ');
- requestMethod = (str.substring(0, delimitPos)).trim();
- str = (str.substring(delimitPos + 1)).trim();
- delimitPos = str.indexOf(' ');
- requestURI = (str.substring(0, delimitPos)).trim();
- requestProtocol = (str.substring(delimitPos + 1)).trim();
- } else {
- int delimitPos = str.indexOf(':');
- String key = ((str.substring(0, delimitPos)).trim()).toLowerCase();
- String value = (str.substring(delimitPos + 1)).trim();
- if(key.equals("host")) {
- delimitPos = value.indexOf(':');
- if(delimitPos == -1) {
- requestHost = value;
- } else {
- requestHost = value.substring(0, delimitPos);
- requestPort = Integer.valueOf(value.substring(delimitPos + 1)).intValue();
- }
- }
- requestHeaderField.put(key, value);
- }
- }
-
- private void connectViaBluetooth() throws IOException {
- String ua = "";
- String wap = "";
- if(HiisiMIDlet.settingForm.getUaId() == 0) {
- ua = HiisiMIDlet.hiisiMIDlet.getAppProperty("User-Agent");
- wap = HiisiMIDlet.hiisiMIDlet.getAppProperty("x-wap-profile");
- } else if(HiisiMIDlet.settingForm.getUaId() == 1) ua = HiisiMIDlet.hiisiMIDlet.getAppProperty("User-Agent2");
- else if(HiisiMIDlet.settingForm.getUaId() == 2) ua = HiisiMIDlet.hiisiMIDlet.getAppProperty("User-Agent3");
-
- bluetoothOutput.write((requestMethod + " " + requestURI + " " + requestProtocol + "rn").getBytes());
- HiisiMIDlet.logForm.addSentByte((requestMethod + " " + requestURI + " " + requestProtocol + "rn").length());
- Enumeration e = requestHeaderField.keys();
- String key;
- String value;
- while (e.hasMoreElements()){
- key = (String)e.nextElement();
- value = (String)requestHeaderField.get(key);
- if(!key.equals("user-agent") && !key.equals("x-wap-profile") && !key.equals("host") && !key.equals("connection")) {
- bluetoothOutput.write((key + ": " + value+"rn").getBytes());
- HiisiMIDlet.logForm.addSentByte((key + ": " + value+"rn").length());
- }
- }
- if(!ua.equals("")) {
- bluetoothOutput.write(("user-agent: " + ua +"rn").getBytes());
- HiisiMIDlet.logForm.addSentByte(("user-agent: " + ua +"rn").length());
- }
- if(!wap.equals("")) {
- bluetoothOutput.write(("x-wap-profile: " + wap +"rn").getBytes());
- HiisiMIDlet.logForm.addSentByte(("x-wap-profile: " + wap +"rn").length());
- }
- if(requestPort == 80) {
- bluetoothOutput.write(("host: " + requestHost + "rn").getBytes());
- HiisiMIDlet.logForm.addSentByte(("host: " + requestHost + "rn").length());
- } else {
- bluetoothOutput.write(("host: " + requestHost + ":" + requestPort + "rn").getBytes());
- HiisiMIDlet.logForm.addSentByte(("host: " + requestHost + ":" + requestPort + "rn").length());
- }
- bluetoothOutput.write("connection: closernrn".getBytes());
- HiisiMIDlet.logForm.addSentByte("connection: closernrn".length());
- if(requestMethod.equals("POST")) {
- bluetoothOutput.write(postData);
- postData = null;
- }
- bluetoothOutput.flush();
- }
-
- private void ConnectViaWap() throws IOException {
- String UA = HiisiMIDlet.hiisiMIDlet.getAppProperty("User-Agent") + "n";
- String WAP = HiisiMIDlet.hiisiMIDlet.getAppProperty("x-wap-profile");
-
- hc.setRequestMethod(requestMethod);
- HiisiMIDlet.logForm.addSentByte((requestMethod + " " + requestURI + " " + requestProtocol + "rn").length());
- Enumeration e = requestHeaderField.keys();
- String key;
- String value;
- while (e.hasMoreElements()){
- key = (String)e.nextElement();
- value = (String)requestHeaderField.get(key);
- if(!key.equals("user-agent") && !key.equals("x-wap-profile") && !key.equals("host") && !key.equals("connection")) {
- hc.setRequestProperty(key, value);
- HiisiMIDlet.logForm.addSentByte((key + ": " + value + "rn").length());
- }
- }
- if(!UA.equals("")) {
- hc.setRequestProperty("user-agent", UA);
- HiisiMIDlet.logForm.addSentByte(("user-agent: " + UA + "rn").length());
- }
- if(!WAP.equals("")) {
- hc.setRequestProperty("x-wap-profile", WAP);
- HiisiMIDlet.logForm.addSentByte(("x-wap-profile: " + WAP + "rn").length());
- }
- if(requestPort == 80) {
- hc.setRequestProperty("host", requestHost);
- HiisiMIDlet.logForm.addSentByte(("host: " + requestHost + "rn").length());
- } else {
- hc.setRequestProperty("host", requestHost + ":" + requestPort);
- HiisiMIDlet.logForm.addSentByte(("host: " + requestHost + ":" + requestPort + "rn").length());
- }
- hc.setRequestProperty("connection", "close");
- HiisiMIDlet.logForm.addSentByte("connection: closernrn".length());
- if(requestMethod.equals("POST")) {
- httpOutput = hc.openDataOutputStream();
- httpOutput.write(postData);
- HiisiMIDlet.logForm.addSentByte(postData.length);
- httpOutput.flush();
- postData = null;
- }
- }
-
- private void readResponseHeaderViaBluetooth() throws IOException, InterruptedException {
- StringBuffer sb = new StringBuffer();
- int c;
- int line = 0;
- while(true){
- if((c = readbuffer(bluetoothInput, 300)) == -1) {
- responseProtocol = "HTTP/1.1";
- responseCode = "504";
- responseMessage = "Gateway Time-out";
- break;
- } else {
- if((char)c == 'r') {
- if(sb.length() == 0) {
- break;
- } else {
- processResponseHeader(sb.toString(), line);
- sb.delete(0, sb.length());
- line++;
- }
- } else if((char)c == 'n') {
- } else {
- sb.append((char)c);
- }
- }
- }
- sb = null;
- }
-
- private void processResponseHeader(String str, int line) throws IOException {
- str = str.trim();
-
- if(line == 0){
- int delimitPos = str.indexOf(' ');
- responseProtocol = (str.substring(0, delimitPos)).trim();
- str = (str.substring(delimitPos + 1)).trim();
- delimitPos = str.indexOf(' ');
- responseCode = (str.substring(0, delimitPos)).trim();
- responseMessage = (str.substring(delimitPos + 1)).trim();
- } else {
- int delimitPos = str.indexOf(':');
- String key = ((str.substring(0, delimitPos)).trim()).toLowerCase();
- String value = (str.substring(delimitPos + 1)).trim();
- responseHeaderField.put(key, value);
- }
- }
-
- private void writeResponseFromBluetooth() throws IOException, InterruptedException {
- output.write((responseProtocol + " " + responseCode + " " + responseMessage + "rn").getBytes());
- HiisiMIDlet.logForm.addRecvdByte((responseProtocol + " " + responseCode + " " + responseMessage + "rn").length());
- Enumeration e = responseHeaderField.keys();
- String key;
- String value;
- while (e.hasMoreElements()){
- key = (String)e.nextElement();
- value = (String)responseHeaderField.get(key);
- if(!key.equals("connection")) {
- output.write((key + ": " + value+"rn").getBytes());
- HiisiMIDlet.logForm.addRecvdByte((key + ": " + value+"rn").length());
- }
- }
- output.write("connection: closernrn".getBytes());
- HiisiMIDlet.logForm.addRecvdByte("connection: closernrn".length());
-
- if(!requestMethod.equals("HEAD")) {
- int dummy;
- if((dummy = readbuffer(bluetoothInput,100)) == -1) return;
-
- int b;
- if(responseHeaderField.get("content-length") != null) {
- int contentLength = Integer.valueOf((String)responseHeaderField.get("content-length")).intValue();
- int temp = 0;
- int read = 0 ;
- byte[] buf = new byte[4096];
- while ((read < contentLength) && (temp != -1)) {
- temp = readbuffer(bluetoothInput, buf, 4096, 300);
- if(temp != -1) {
- output.write(buf, 0, temp);
- read += temp;
- byteTranfered += temp;
- HiisiMIDlet.logForm.addRecvdByte(temp);
- }
- }
- buf = null;
- } else {
- byte[] buf = new byte[4096];
- while(true) {
- if((b = readbuffer(bluetoothInput, buf, 4096, 5)) == -1) {
- break;
- } else {
- output.write(buf, 0, b);
- byteTranfered += b;
- HiisiMIDlet.logForm.addRecvdByte(b);
- }
- }
- buf = null;
- }
- }
- output.flush();
- }
-
- private void readResponseHeaderViaWap() throws IOException {
- responseProtocol = "HTTP/1.1";
- responseCode = Integer.toString(hc.getResponseCode());
- responseMessage = hc.getResponseMessage();
- String key = "";
- for(int i = 0; ((key = hc.getHeaderFieldKey(i)) != null); i++){
- responseHeaderField.put(key, hc.getHeaderField(i));
- }
- }
-
- private void writeResponseFromWap() throws IOException {
- output.write((responseProtocol + " " + responseCode + " " + responseMessage + "rn").getBytes());
- HiisiMIDlet.logForm.addRecvdByte((responseProtocol + " " + responseCode + " " + responseMessage + "rn").length());
- Enumeration e = responseHeaderField.keys();
- String key;
- String value;
- while (e.hasMoreElements()){
- key = (String)e.nextElement();
- value = (String)responseHeaderField.get(key);
- if(!key.equals("connection")) {
- output.write((key + ": " + value+"rn").getBytes());
- HiisiMIDlet.logForm.addRecvdByte((key + ": " + value+"rn").length());
- }
- }
- output.write("connection: closernrn".getBytes());
- HiisiMIDlet.logForm.addRecvdByte("connection: closernrn".length());
-
- int contentLength = (int)hc.getLength();
- int temp = 0;
- int read = 0 ;
- byte[] buf = new byte[4096];
- while ((read < contentLength) && (temp != -1)) {
- temp = httpInput.read(buf, 0, 4096);
- if(temp != -1) {
- output.write(buf, 0, temp);
- read += temp;
- byteTranfered += temp;
- HiisiMIDlet.logForm.addRecvdByte(temp);
- }
- }
- output.flush();
- buf = null;
- }
-
- private String getAccessTime() {
- cal = Calendar.getInstance();
- year = cal.get(Calendar.YEAR);
- switch(cal.get(Calendar.MONTH) + 1) {
- case 1: month = "Jan"; break;
- case 2: month = "Feb"; break;
- case 3: month = "Mar"; break;
- case 4: month = "Apr"; break;
- case 5: month = "May"; break;
- case 6: month = "Jun"; break;
- case 7: month = "Jul"; break;
- case 8: month = "Aug"; break;
- case 9: month = "Sep"; break;
- case 10: month = "Oct"; break;
- case 11: month = "Nov"; break;
- case 12: month = "Dec"; break;
- }
- day_of_month = cal.get(Calendar.DAY_OF_MONTH);
- hour = cal.get(Calendar.HOUR_OF_DAY);
- minute = cal.get(Calendar.MINUTE);
- second = cal.get(Calendar.SECOND);
- return formatNum(day_of_month) + "/" + month + "/" + year +
- ":" + formatNum(hour) + ":" + formatNum(minute) + ":" + formatNum(second) + " +0900";
- }
-
- private String formatNum(int num) {
- if(num < 10) return "0" + num;
- else return "" + num;
- }
-
- private static int readbuffer(DataInputStream stream, int reread) throws IOException, InterruptedException {
- int c = -1;
- for(int i = 0; i < reread; i++) {
- if(stream.available() != 0) {
- c = stream.read();
- break;
- }
- Thread.sleep(100);
- }
- return c;
- }
-
- private static int readbuffer(DataInputStream stream, byte[] buf, int len, int reread) throws IOException, InterruptedException {
- int c = -1;
- for(int i = 0; i < reread; i++) {
- if(stream.available() != 0) {
- c = stream.read(buf, 0, len);
- break;
- }
- Thread.sleep(100);
- }
- return c;
- }
-
- void checkConnection() {
- Thread check = new Thread() {
- public void run() {
- try {
- Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
- HiisiMIDlet.mainForm.log("Checking bluetooth connection...");
- bc = (StreamConnection)Connector.open(HiisiMIDlet.bluetoothConnection.getUrl());
- bluetoothOutput = bc.openDataOutputStream();
- bluetoothOutput.write("r".getBytes());
- HiisiMIDlet.mainForm.log("Bluetooth connection is available.nHiisi Proxy "
- + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString()
- + " is idling...");
- if(bc != null) {
- bc.close();
- bc = null;
- }
- if(bluetoothOutput != null) {
- bluetoothOutput.close();
- bluetoothOutput = null;
- }
- } catch(IOException e) {
- HiisiMIDlet.mainForm.log("Bluetooth connection is not available.nHiisi Proxy "
- + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString()
- + " is idling...");
- }
- }
- };
- check.start();
- }
- }