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 inventorylist.inventory(sock);
96 // System.printString("T > Error - Unknown transaction\n");
98 //Invoke close operations
103 //Send the http header for web browser display
104 public void httpresponse(){
105 StringBuffer header = new StringBuffer("HTTP/1.0 200 OK\n");
106 header.append("Content-type: text/html\n");
107 header.append("\n\n");
108 String temp_str = new String(header);
109 sock.write(temp_str.getBytes());
114 // Send the html file , read from file one byte at a time
115 public void sendfile() {
116 StringBuffer req_file = new StringBuffer("./htmlfiles/");
117 req_file.append(filename);
118 String filepath = new String(req_file);
119 FileInputStream def_file = new FileInputStream(filepath);
120 int status = def_file.getfd();//Checks if the file is present in
124 StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if
126 response.append(filename);
127 String buffer = new String(response);
128 sock.write(buffer.getBytes());
132 byte buf[] = new byte[16];
135 while ((ret = def_file.read(buf)) > 0) {// Read from file and write
136 // one byte at a time into the socket
137 byte tosend[] = new byte[ret];
138 for (int i = 0; i < ret; i++) {
142 //String str = new String(tosend);
147 //Read the client request and extract the filename from it
148 public boolean clientrequest(){
149 byte b1[] = new byte[1024];
150 int numbytes=sock.read(b1);//Read client request from web server socket
151 String curr=(new String(b1)).subString(0, numbytes);
153 StringBuffer sb=new StringBuffer(prefix);
158 if(prefix.indexOf("\r\n\r\n")>=0) {
160 int index = prefix.indexOf('/');//Parse the GET client request to find filename
161 int end = prefix.indexOf('H');
162 filename = prefix.subString((index+1), (end-1));
163 // System.printString("\n");
169 // Parse for the prefix in the client request
170 // This is helpful to find if the prefix is a special transaction
171 public boolean checktrans(){
172 if (filename.startsWith("trans") == true) {
179 //Parse for the substrings in the filename and use it to obtain the
180 //kind of operation, name of item, quantity of item, price of item
181 //e.g. trans_add_car_2_10000 is the filename
182 //store in the parsed[] string , add,car,2,1000
183 public int parseTransaction(){
184 int start = filename.indexOf('_');
185 String s = filename.subString(start+1);
187 if (s.startsWith("add")==true){
188 // System.printString("DEBUG > ADD\n");
189 int i1 = s.indexOf('_');
190 parsed[0] = new String(s.subString(0,i1));
192 int i2 = s.indexOf('_',i1+1);
193 parsed[1] = new String(s.subString(i1+1,i2));
195 int i3 = s.indexOf('_',i2+1);
196 parsed[2] = new String(s.subString(i2+1,i3));
198 String s3 = s.subString(i3+1);
204 if (s.startsWith("buy")==true){
205 // System.printString("DEBUG > BUY\n");
206 int i1 = s.indexOf('_');
207 parsed[0] = s.subString(0,i1);
209 int i2 = s.indexOf('_', i1+1);
210 parsed[1] = s.subString(i1+1,i2);
212 String s2 = s.subString(i2+1);
219 if (s.startsWith("inventory")==true){
220 // System.printString("DEBUG > INVENTORY\n");