KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echowebserver > EchoWSData


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package echowebserver;
16
17 import org.quickserver.net.server.ClientData;
18 import org.quickserver.util.pool.PoolableObject;
19 import org.apache.commons.pool.BasePoolableObjectFactory;
20 import org.apache.commons.pool.PoolableObjectFactory;
21 import java.util.*;
22 import java.io.IOException JavaDoc;
23 import java.util.logging.*;
24
25 /**
26  * EchoWSData
27  * @author Akshathkumar Shetty
28  */

29 public class EchoWSData implements ClientData, PoolableObject {
30     private static Logger logger = Logger.getLogger(EchoWSData.class.getName());
31     private static final int MAX_HEADER_LENGTH = 50;
32
33     private List httpHeader = null;
34     private StringBuffer JavaDoc httpPost = null;
35     private StringBuffer JavaDoc buffer = null;
36     private int contentLength = 0;
37
38     public EchoWSData() {
39         httpHeader = new ArrayList();
40         buffer = new StringBuffer JavaDoc();
41     }
42
43     public void addInput(String JavaDoc command) throws IOException JavaDoc {
44         buffer.append(command);
45         int k = buffer.indexOf("\r\n");
46         int s = 0;
47         String JavaDoc temp = null;
48         while(k!=-1) {
49             if(httpHeader.size() > MAX_HEADER_LENGTH) {
50                 throw new IOException JavaDoc("Max header length exceeded! ");
51             }
52             
53             temp = buffer.substring(s, k);
54             logger.fine("Header "+temp);
55             httpHeader.add(temp);
56             k = k + 2;
57             s = k;
58             if(temp.length()==0) { //HeaderComplete
59
buffer.delete(0, k);//del header
60
if(isPost()) {
61                     addPost(buffer.toString());
62                 }
63                 break;
64             }
65             k = buffer.indexOf("\r\n", k);
66         }
67     }
68
69     public void addPost(String JavaDoc command) {
70         logger.fine("Data: "+command);
71         httpPost.append(command);
72     }
73
74     public String JavaDoc getDataForOutput() {
75         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
76         for(int j=0; j<httpHeader.size(); j++) {
77             sb.append((String JavaDoc)httpHeader.get(j));
78             sb.append("\r\n");
79         }
80         if(httpPost!=null) {
81             sb.append(httpPost);
82         }
83         return sb.toString();
84     }
85
86     public boolean isRequestComplete() {
87         if(isHeaderComplete()) {
88             logger.fine("Header complete!");
89             if(httpPost!=null && httpPost.length()<contentLength) {
90                 logger.fine("Waiting for httpPost!");
91                 return false;
92             } else {
93                 return true;
94             }
95         } else {
96             return false;
97         }
98     }
99
100     public boolean isHeaderComplete() {
101         if(httpHeader.size()==0) return false;
102         String JavaDoc temp = (String JavaDoc) httpHeader.get(httpHeader.size()-1);
103         return temp.length() == 0;
104     }
105
106     public boolean isPost() {
107         if(httpHeader.size()==0) return false;
108
109         String JavaDoc temp = (String JavaDoc) httpHeader.get(0);
110         if(temp.toUpperCase().startsWith("POST")) {
111             contentLength = contentLength();
112             httpPost = new StringBuffer JavaDoc();
113             return true;
114         } else {
115             return false;
116         }
117     }
118
119     // Given a line that starts with Content-Length,
120
// this returns the integer value specified.
121
public int contentLength() {
122         String JavaDoc input, temp;
123         for(int i=0; i<httpHeader.size(); i++) {
124             temp = (String JavaDoc) httpHeader.get(i);
125             if (temp.length() == 0)
126                 break;
127             input = temp.toUpperCase();
128             if(input.startsWith("CONTENT-LENGTH"))
129                 return(getLength(input));
130         }
131         return(0);
132     }
133     private int getLength(String JavaDoc length) {
134         StringTokenizer tok = new StringTokenizer(length);
135         tok.nextToken();
136         try {
137             return Integer.parseInt(tok.nextToken());
138         } catch(Exception JavaDoc e) {
139             return 0;
140         }
141     }
142
143
144     //---- PoolableObject ---
145
private void clean() {
146         httpHeader.clear();
147         buffer.setLength(0);
148         httpPost = null;
149         contentLength = 0;
150     }
151
152     public boolean isPoolable() {
153         return true;
154     }
155
156     public PoolableObjectFactory getPoolableObjectFactory() {
157         return new BasePoolableObjectFactory() {
158             public Object JavaDoc makeObject() {
159                 return new EchoWSData();
160             }
161             public void passivateObject(Object JavaDoc obj) {
162                 EchoWSData ed = (EchoWSData)obj;
163                 ed.clean();
164             }
165             public void destroyObject(Object JavaDoc obj) {
166                 if(obj==null) return;
167                 passivateObject(obj);
168                 obj = null;
169             }
170             public boolean validateObject(Object JavaDoc obj) {
171                 if(obj==null)
172                     return false;
173                 else
174                     return true;
175             }
176         };
177     }
178 }
179
Popular Tags