1 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 ; 23 import java.util.logging.*; 24 25 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 httpPost = null; 35 private StringBuffer buffer = null; 36 private int contentLength = 0; 37 38 public EchoWSData() { 39 httpHeader = new ArrayList(); 40 buffer = new StringBuffer (); 41 } 42 43 public void addInput(String command) throws IOException { 44 buffer.append(command); 45 int k = buffer.indexOf("\r\n"); 46 int s = 0; 47 String temp = null; 48 while(k!=-1) { 49 if(httpHeader.size() > MAX_HEADER_LENGTH) { 50 throw new IOException ("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) { buffer.delete(0, k); 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 command) { 70 logger.fine("Data: "+command); 71 httpPost.append(command); 72 } 73 74 public String getDataForOutput() { 75 StringBuffer sb = new StringBuffer (); 76 for(int j=0; j<httpHeader.size(); j++) { 77 sb.append((String )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 temp = (String ) 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 temp = (String ) httpHeader.get(0); 110 if(temp.toUpperCase().startsWith("POST")) { 111 contentLength = contentLength(); 112 httpPost = new StringBuffer (); 113 return true; 114 } else { 115 return false; 116 } 117 } 118 119 public int contentLength() { 122 String input, temp; 123 for(int i=0; i<httpHeader.size(); i++) { 124 temp = (String ) 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 length) { 134 StringTokenizer tok = new StringTokenizer(length); 135 tok.nextToken(); 136 try { 137 return Integer.parseInt(tok.nextToken()); 138 } catch(Exception e) { 139 return 0; 140 } 141 } 142 143 144 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 makeObject() { 159 return new EchoWSData(); 160 } 161 public void passivateObject(Object obj) { 162 EchoWSData ed = (EchoWSData)obj; 163 ed.clean(); 164 } 165 public void destroyObject(Object obj) { 166 if(obj==null) return; 167 passivateObject(obj); 168 obj = null; 169 } 170 public boolean validateObject(Object obj) { 171 if(obj==null) 172 return false; 173 else 174 return true; 175 } 176 }; 177 } 178 } 179 | Popular Tags |