1 public class WebServerThread extends Thread {
2 //Filename requested by the client
7 Inventory inventorylist;
11 public WebServerThread(Socket s, Logger log, Inventory inventory){
12 parsed = new String[4];
15 this.inventorylist=inventory;
19 // Process the incoming http request
20 while (!clientrequest()) {
22 if(checktrans()==false) {
23 // Not special transaction , do normal filesending
27 // Invoke special inventory transaction
33 //Do the WriteIO on server socket and send the requested file to Client
34 public void SendFile() {
39 // Log the Client request
40 public void LogRequest() {
41 log.logrequest(filename);
44 //Transaction on Inventory
45 public void Transaction() {
47 int op = parseTransaction();
48 // Check for the kind of operation
49 if (op == 0 ) { /* Add */
50 // System.printString("DEBUG > Calling add transaction\n");
51 Integer qty = new Integer(parsed[2]);
52 Integer price = new Integer(parsed[3]);
53 int ret = inventorylist.additem(parsed[1], qty.intValue(), price.intValue());
56 StringBuffer s = new StringBuffer("Added Item ");
58 s.append(" Quantity ");
63 String towrite = new String(s);
64 sock.write(towrite.getBytes());
67 String s = new String("Error encountered");
68 sock.write(s.getBytes());
70 } else if (op == 1) { /* Buy */
71 // System.printString("DEBUG > Calling buy transaction\n");
72 Integer qty = new Integer(parsed[2]);
73 int ret = inventorylist.buyitem(parsed[1], qty.intValue());
76 StringBuffer s = new StringBuffer("Bought item ");
78 s.append(" Quantity ");
81 Integer cost = new Integer(ret*qty.intValue());
82 String c = cost.toString();
84 String towrite = new String(s);
85 sock.write(towrite.getBytes());
88 String s = new String("Error encountered");
89 sock.write(s.getBytes());
91 } else if (op == 2) { /* Inventory */
92 // System.printString("DEBUG > Calling inventory transaction\n");
94 String towrite = inventorylist.inventory();
95 sock.write(towrite.getBytes());
97 // System.printString("T > Error - Unknown transaction\n");
99 //Invoke close operations
104 //Send the http header for web browser display
105 public void httpresponse(){
106 StringBuffer header = new StringBuffer("HTTP/1.0 200 OK\n");
107 header.append("Content-type: text/html\n");
108 header.append("\n\n");
109 String temp_str = new String(header);
110 sock.write(temp_str.getBytes());
115 // Send the html file , read from file one byte at a time
116 public void sendfile() {
117 StringBuffer req_file = new StringBuffer("./htmlfiles/");
118 req_file.append(filename);
119 String filepath = new String(req_file);
120 FileInputStream def_file = new FileInputStream(filepath);
121 int status = def_file.getfd();//Checks if the file is present in
125 StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if
127 response.append(filename);
128 String buffer = new String(response);
129 sock.write(buffer.getBytes());
133 byte buf[] = new byte[16];
136 while ((ret = def_file.read(buf)) > 0) {// Read from file and write
137 // one byte at a time into the socket
138 byte tosend[] = new byte[ret];
139 for (int i = 0; i < ret; i++) {
143 //String str = new String(tosend);
148 //Read the client request and extract the filename from it
149 public boolean clientrequest(){
150 byte b1[] = new byte[1024];
151 int numbytes=sock.read(b1);//Read client request from web server socket
152 String curr=(new String(b1)).subString(0, numbytes);
154 StringBuffer sb=new StringBuffer(prefix);
159 if(prefix.indexOf("\r\n\r\n")>=0) {
161 int index = prefix.indexOf('/');//Parse the GET client request to find filename
162 int end = prefix.indexOf('H');
163 filename = prefix.subString((index+1), (end-1));
164 System.printString("\n");
170 // Parse for the prefix in the client request
171 // This is helpful to find if the prefix is a special transaction
172 public boolean checktrans(){
173 if (filename.startsWith("trans") == true) {
180 //Parse for the substrings in the filename and use it to obtain the
181 //kind of operation, name of item, quantity of item, price of item
182 //e.g. trans_add_car_2_10000 is the filename
183 //store in the parsed[] string , add,car,2,1000
184 public int parseTransaction(){
185 int start = filename.indexOf('_');
186 String s = filename.subString(start+1);
188 if (s.startsWith("add")==true){
189 // System.printString("DEBUG > ADD\n");
190 int i1 = s.indexOf('_');
191 parsed[0] = new String(s.subString(0,i1));
193 String s1 = s.subString(i1+1);
194 int i2 = s1.indexOf('_');
195 parsed[1] = new String(s1.subString(0,i2));
197 String s2 = s1.subString(i2+1);
198 int i3 = s2.indexOf('_');
199 parsed[2] = new String(s2.subString(0,i3));
201 String s3 = s2.subString(i3+1);
207 if (s.startsWith("buy")==true){
208 // System.printString("DEBUG > BUY\n");
209 int i1 = s.indexOf('_');
210 parsed[0] = s.subString(0,i1);
212 String s1 = s.subString(i1+1);
213 int i2 = s1.indexOf('_');
214 parsed[1] = s1.subString(0,i2);
216 String s2 = s1.subString(i2+1);
223 if (s.startsWith("inventory")==true){
224 // System.printString("DEBUG > INVENTORY\n");