001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package echowebserver;
016:
017: import org.quickserver.net.server.ClientData;
018: import org.quickserver.util.pool.PoolableObject;
019: import org.apache.commons.pool.BasePoolableObjectFactory;
020: import org.apache.commons.pool.PoolableObjectFactory;
021: import java.util.*;
022: import java.io.IOException;
023: import java.util.logging.*;
024:
025: /**
026: * EchoWSData
027: * @author Akshathkumar Shetty
028: */
029: public class EchoWSData implements ClientData, PoolableObject {
030: private static Logger logger = Logger.getLogger(EchoWSData.class
031: .getName());
032: private static final int MAX_HEADER_LENGTH = 50;
033:
034: private List httpHeader = null;
035: private StringBuffer httpPost = null;
036: private StringBuffer buffer = null;
037: private int contentLength = 0;
038:
039: public EchoWSData() {
040: httpHeader = new ArrayList();
041: buffer = new StringBuffer();
042: }
043:
044: public void addInput(String command) throws IOException {
045: buffer.append(command);
046: int k = buffer.indexOf("\r\n");
047: int s = 0;
048: String temp = null;
049: while (k != -1) {
050: if (httpHeader.size() > MAX_HEADER_LENGTH) {
051: throw new IOException("Max header length exceeded! ");
052: }
053:
054: temp = buffer.substring(s, k);
055: logger.fine("Header " + temp);
056: httpHeader.add(temp);
057: k = k + 2;
058: s = k;
059: if (temp.length() == 0) { //HeaderComplete
060: buffer.delete(0, k);//del header
061: if (isPost()) {
062: addPost(buffer.toString());
063: }
064: break;
065: }
066: k = buffer.indexOf("\r\n", k);
067: }
068: }
069:
070: public void addPost(String command) {
071: logger.fine("Data: " + command);
072: httpPost.append(command);
073: }
074:
075: public String getDataForOutput() {
076: StringBuffer sb = new StringBuffer();
077: for (int j = 0; j < httpHeader.size(); j++) {
078: sb.append((String) httpHeader.get(j));
079: sb.append("\r\n");
080: }
081: if (httpPost != null) {
082: sb.append(httpPost);
083: }
084: return sb.toString();
085: }
086:
087: public boolean isRequestComplete() {
088: if (isHeaderComplete()) {
089: logger.fine("Header complete!");
090: if (httpPost != null && httpPost.length() < contentLength) {
091: logger.fine("Waiting for httpPost!");
092: return false;
093: } else {
094: return true;
095: }
096: } else {
097: return false;
098: }
099: }
100:
101: public boolean isHeaderComplete() {
102: if (httpHeader.size() == 0)
103: return false;
104: String temp = (String) httpHeader.get(httpHeader.size() - 1);
105: return temp.length() == 0;
106: }
107:
108: public boolean isPost() {
109: if (httpHeader.size() == 0)
110: return false;
111:
112: String temp = (String) httpHeader.get(0);
113: if (temp.toUpperCase().startsWith("POST")) {
114: contentLength = contentLength();
115: httpPost = new StringBuffer();
116: return true;
117: } else {
118: return false;
119: }
120: }
121:
122: // Given a line that starts with Content-Length,
123: // this returns the integer value specified.
124: public int contentLength() {
125: String input, temp;
126: for (int i = 0; i < httpHeader.size(); i++) {
127: temp = (String) httpHeader.get(i);
128: if (temp.length() == 0)
129: break;
130: input = temp.toUpperCase();
131: if (input.startsWith("CONTENT-LENGTH"))
132: return (getLength(input));
133: }
134: return (0);
135: }
136:
137: private int getLength(String length) {
138: StringTokenizer tok = new StringTokenizer(length);
139: tok.nextToken();
140: try {
141: return Integer.parseInt(tok.nextToken());
142: } catch (Exception e) {
143: return 0;
144: }
145: }
146:
147: //---- PoolableObject ---
148: private void clean() {
149: httpHeader.clear();
150: buffer.setLength(0);
151: httpPost = null;
152: contentLength = 0;
153: }
154:
155: public boolean isPoolable() {
156: return true;
157: }
158:
159: public PoolableObjectFactory getPoolableObjectFactory() {
160: return new BasePoolableObjectFactory() {
161: public Object makeObject() {
162: return new EchoWSData();
163: }
164:
165: public void passivateObject(Object obj) {
166: EchoWSData ed = (EchoWSData) obj;
167: ed.clean();
168: }
169:
170: public void destroyObject(Object obj) {
171: if (obj == null)
172: return;
173: passivateObject(obj);
174: obj = null;
175: }
176:
177: public boolean validateObject(Object obj) {
178: if (obj == null)
179: return false;
180: else
181: return true;
182: }
183: };
184: }
185: }
|