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

📄 tier4pro.java

📁 check and update program
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.net.ServerSocket;import java.net.Socket;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.OutputStream;import java.util.StringTokenizer;class Tier4Pro{         public static Object lockobject = new Object(); //lock	    public static String product; //product_name	    public static int portno; 	//port number for this product    public static String username;  //the user who has write permission	    public static String password;  //password for the above user	    	    public static String realm; //the protection realm name     public static String codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";    //a table to encode a base64 string		    	    //to encode a string in base64 	  	    static public String base64encode(String rawdata)	{	    int length=rawdata.length();		    //encdata sotres the encoded strng, the length is about 1/3 longer		    //Note that the passed in string is not empty		    char[] encdata = new char[((length + 2) / 3) * 4]; 	    int inpos=0;	    int outpos=0;		 					    while (inpos<length){		        boolean oneleft=true;		        boolean twoleft=true;		   		        int first8=0;		        int second8=0;		        int third8=0;					        int unit=0;			        int first6index=0;		        int second6index=0;                        int third6index=0;		 	int fourth6index=0;	   		        first8 = (0xFF & rawdata.charAt(inpos));		        		        if ((inpos+1) < length)		            second8 = (0xFF & rawdata.charAt(inpos+1));		        else 			    oneleft = false;						    		        if ((inpos+2) < length)		            third8 = (0xFF & rawdata.charAt(inpos+2));		        else 			    twoleft = false;			unit = (first8 <<16) | (second8<<8) | third8;						fourth6index = (twoleft? (unit & 0x3F):64);			third6index =  (oneleft? ((unit & 0XFC0)>>>6):64);			second6index = ((unit & 0X3F000) >>> 12);			first6index =  ((unit & 0XFC0000) >>> 18); 		        encdata[outpos+3] = codes.charAt(fourth6index);		        encdata[outpos+2] = codes.charAt(third6index);		        encdata[outpos+1] = codes.charAt(second6index);		        encdata[outpos] = codes.charAt(first6index);			inpos+=3;			outpos+=4;	    }	    return new String(encdata);	}    //Main method	    /* Tier4Pro listens to a particular port, and dispatches a thread to process a request once a request arrives.       Notes:        		1) Java security manager is intentionally not used here to reduce the complexity and to make the code rewritable in C.		2) The tool works for both http 1.0 and http 1.1. Although 'keep alive' can be supported by the tool, it is not supported for simplicity. Basically, the server in the tool is stateless.     */			    public static void main(String[] args) throws IOException {/*	    if (args.length==4) {		    product= args[0];		            portno = Integer.parseInt(args[1]);		    username= args[2];			    password= base64encode(args[2]+":"+args[3]);			    realm="\""+product+"CCE\"";		    }	    else {		System.out.println("Usage: java -Xms16M -Xmx512M Tier4Pro product_name port_number username password");		        return;	    }                  */		    product= "TUXEDO";		        portno = 11120;//		    username= args[2];	//		    password= base64encode(args[2]+":"+args[3]);	//		    realm="\""+product+"CCE\"";		    ServerSocket serversocket= new ServerSocket(portno);	    while (true) {	//System.out.println("ok");				Socket socket = serversocket.accept();			new Thread(new Session(socket)).start();	    }					    }   }class Session implements Runnable{       //the socket corresponding to a client request is stored here	    private Socket socket;       //the size of the header buffer to accomodate a http header	    private static int header_size = 4096;    //the size of a 'GET' request    private static int get_request_size = 32;        //constructor     public Session(Socket s) {	socket = s;	    }    //this class is runnable, and the run() method defined below just invokes a static method 'ProcessRequest'	    public void run(){	try{	    ProcessRequest(socket);  	}	catch (Exception e) {}    }    //To get the header of a request, assuming the header size < 4096.      //Note that the header terminates when there are two '\r\n' according     //to http 1.1 specification.(In some implementation, two '\n' are used for this purpose.)    //In the returned header, '\r' is removed.            private static byte[] getHeader(InputStream in) throws IOException {        byte[] buf = new byte[header_size];        int cur=0;        int tmp=-1;        while ((tmp=in.read()) != -1) {		if ((tmp=='\n')&&(cur>0))			if (buf[cur-1] ==(byte)tmp) {				           	            return buf;                	}		if (tmp!='\r'){			buf[cur++] = (byte)tmp;						//			System.out.println((char)buf[cur-1]);						    }	}        return null;    }             //To build a header for a reply. When a password challenge is required, statuscode is set to 401,    //and the authheader is part of the reply header.     //Note that content-lengeth is not contained in the reply header, which means the client(browser)     //has to determine the length of the reply using 'connection closed' method.	     private static String buildHeader(int statuscode, String reasonphrase){	String MIMEHeader = "Content-Type: text/html\r\n";	String AuthHeader = "WWW-Authenticate: Basic realm="+Tier4Pro.realm+"\r\n";	String header = "HTTP/1.0 " + statuscode + " " + reasonphrase + "\r\n" + MIMEHeader + "\r\n";	if (statuscode==401) header="HTTP/1.0 " + statuscode + " " + reasonphrase + "\r\n" + MIMEHeader +AuthHeader+"\r\n";	return header;    }    //The entry method to process a request    private static void ProcessRequest(Socket s) throws IOException {		InputStream in = s.getInputStream();		OutputStream out = s.getOutputStream();		byte[] nothing = getHeader(in);		String header=new String(nothing);//System.out.println("header=");		 //System.out.println(header);//System.out.println("header end");	   		String request=null;	try{		if (nothing[0]=='G') {                        //the client submitted a method using 'GET'			//positioning the meaningful information, 			//i=5 because the header looks like 'GET /...'			//the char after / is at position 5			int i=5;			byte casebuf[]=new byte[get_request_size]; 			//the 'GET' request terminates by '/'  			 			 			/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////要改的地方///////////////////////////////////////////////////////////////////////////////////			while ((nothing[i]!=' ') && (nothing[i]!='/')){				casebuf[i-5]=nothing[i];				i++;			}			request=new String(casebuf,0,i-5);			//System.out.println(request);						//A 'GET' request can be either to fetech a brand-new page			// or to update an existing entry. In the latter case, authorization			//is required. 			if ((casebuf[0]=='q')||(casebuf[0]=='u'))				doFetch(out,request);			else			    if (doAuth(out,header))		    		    					    doFetch(out, request);						//close the connection			in.close(); 			out.close();			return;		}				//The client submitted a request using 'POST' 		//The following piece of code is used to get the request body		int length = in.available();		byte[] buf = new byte[length];		for (int i=0;i<length;i++) 		    buf[i]= (byte) in.read();	    				request = new String(buf);///		//System.out.println("request=");	  //System.out.println(request);  //System.out.println("request end");			//Get the tag, i.e. the first property name, of the request 		//and then decide what action to perform.		//Currently, there are two kinds of actions: one is to update an escalation		//the other is to query escalations.		String tag = request.substring(0, request.indexOf("="));			if (tag.equals("update")) { //		    if (doAuth(out,header))		    		    			    doUpdate(out, request);		}			if (tag.equals("query"))			   doQuery(out,request);			//close the connection		in.close(); 		out.close();	}	catch (Exception e) { //send an Email to the maitainer when sth. is wrong.			try{				Socket s1=new Socket("liberty-corner.beasys.com",25);				BufferedReader ein = new BufferedReader(new InputStreamReader(s1.getInputStream()));				BufferedWriter eout = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));							eout.write("HELO WORLD\n");				eout.flush();				ein.readLine();				eout.write("RSET\n");				eout.flush();				ein.readLine();				eout.write("MAIL FROM:<tuxtier4@bea.com>\n");				eout.flush();				ein.readLine();				eout.write("RCPT TO: <someone@bea.com>\n");				eout.flush();				ein.readLine();				eout.write("DATA\n");				eout.flush();				ein.readLine();				eout.write("Subject: An Exception Happened in Session of"+Tier4Pro.realm+"\n");				eout.flush();				//Modify the Email address in future				eout.write("From: Tier4Pro Server <someone@bea.com>\n");				eout.flush();								eout.write("To: Your Name <someone@bea.com>\n"); 				eout.flush();				eout.write("\n\n"+"\nException: "+e+"\n\n\n");				eout.flush();				eout.write("\n\n"+"\nHeader: "+header+"\nRequest:  "+request+"\n.\n\n");								eout.flush();				eout.write("QUIT\n");				eout.flush();				ein.readLine();				s1.close();			}			catch (Exception e1){}	}    }    //To decode the hex coding for '!@#$%^&*' etc. 	    private static String decoding(String temps) throws IOException    {	int i=0;	int j=0;	byte[] tempb=temps.getBytes();	int length=tempb.length;//    int flag=0;	while (i<length) {	    byte temp = tempb[i++];	    if (temp == '%'){		byte[] tmpbuf = new byte[2];		tmpbuf[0]=tempb[i++];		tmpbuf[1]=tempb[i++];		temp=(byte)(Short.parseShort((new String(tmpbuf)),16));//////////////////////////////////////////		///////////////////////////////////////////		if (temp==0X0D)//		{//			flag=1;//			System.out.println("flag=1");//		}//		else{///			//         	if(flag==1)//           	{//	             	System.out.println("temp="+temp);

⌨️ 快捷键说明

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