| 1 21 24 package org.lobobrowser.html.test; 25 26 import java.awt.Image ; 27 import java.awt.Toolkit ; 28 import java.io.ByteArrayInputStream ; 29 import java.io.IOException ; 30 import java.io.UnsupportedEncodingException ; 31 import java.net.HttpURLConnection ; 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 import java.net.URLConnection ; 35 import java.util.EventObject ; 36 import java.util.Map ; 37 import java.util.logging.*; 38 39 import javax.xml.parsers.DocumentBuilderFactory ; 40 41 import org.lobobrowser.html.*; 42 import org.lobobrowser.util.*; 43 import org.lobobrowser.util.io.IORoutines; 44 import org.w3c.dom.Document ; 45 46 54 public class SimpleHttpRequest implements HttpRequest { 55 private static final Logger logger = Logger.getLogger(SimpleHttpRequest.class.getName()); 56 private int readyState; 57 private int status; 58 private String statusText; 59 private byte[] responseBytes; 60 private java.util.Map responseHeadersMap; 61 private String responseHeaders; 62 private final UserAgentContext context; 63 64 public SimpleHttpRequest(UserAgentContext context) { 65 super(); 66 this.context = context; 67 } 68 69 public synchronized int getReadyState() { 70 return this.readyState; 71 } 72 73 public synchronized String getResponseText() { 74 byte[] bytes = this.responseBytes; 75 try { 77 return bytes == null ? null : new String (bytes, "ISO-8859-1"); 78 } catch(UnsupportedEncodingException uee) { 79 return null; 80 } 81 } 82 83 public synchronized Document getResponseXML() { 84 byte[] bytes = this.responseBytes; 85 if(bytes == null) { 86 return null; 87 } 88 java.io.InputStream in = new ByteArrayInputStream (bytes); 89 try { 90 return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); 91 } catch(Exception err) { 92 logger.log(Level.WARNING, "Unable to parse response as XML.", err); 93 return null; 94 } 95 } 96 97 public synchronized byte[] getResponseBytes() { 98 return this.responseBytes; 99 } 100 101 104 public synchronized Image getResponseImage() { 105 byte[] bytes = this.responseBytes; 106 if(bytes == null) { 107 return null; 108 } 109 return Toolkit.getDefaultToolkit().createImage(bytes); 110 } 111 112 public synchronized int getStatus() { 113 return this.status; 114 } 115 116 public synchronized String getStatusText() { 117 return this.statusText; 118 } 119 120 public void abort() { 121 URLConnection c; 122 synchronized(this) { 123 c = this.connection; 124 } 125 if(c instanceof HttpURLConnection ) { 126 ((HttpURLConnection ) c).disconnect(); 127 } 128 else if(c != null) { 129 try { 130 c.getInputStream().close(); 131 } catch(IOException ioe) { 132 ioe.printStackTrace(); 133 } 134 } 135 } 136 137 public synchronized String getAllResponseHeaders() { 138 return this.responseHeaders; 139 } 140 141 public synchronized String getResponseHeader(String headerName) { 142 Map headers = this.responseHeadersMap; 143 return headers == null ? null : (String ) headers.get(headerName); 144 } 145 146 public void open(String method, String url) { 147 this.open(method, url, true); 148 } 149 150 public void open(String method, URL url) { 151 this.open(method, url, true, null, null); 152 } 153 154 public void open(String method, URL url, boolean asyncFlag) { 155 this.open(method, url, asyncFlag, null, null); 156 } 157 158 public void open(String method, String url, boolean asyncFlag) { 159 this.open(method, url, asyncFlag, null); 160 } 161 162 public void open(String method, String url, boolean asyncFlag, 163 String userName) { 164 this.open(method, url, asyncFlag, userName, null); 165 } 166 167 public void open(String method, String url, boolean asyncFlag, 168 String userName, String password) { 169 try { 170 URL urlObj = new URL (url); 171 this.open(method, urlObj, asyncFlag, userName, password); 172 } catch(MalformedURLException mfu) { 173 logger.log(Level.WARNING,"Bad request URL:" + url, mfu); 174 this.changeState(HttpRequest.STATE_COMPLETE, 400, "Malformed URI", null); 175 } 176 } 177 178 public void open(final String method, final java.net.URL url, boolean asyncFlag, 179 final String userName, final String password) { 180 if(asyncFlag) { 181 new Thread ("Request") { 183 public void run() { 184 openSync(method, url, userName, password); 185 } 186 }.start(); 187 } 188 else { 189 this.openSync(method, url, userName, password); 190 } 191 } 192 193 private void changeState(int readyState, int status, String statusMessage, byte[] bytes) { 194 synchronized(this) { 195 this.readyState = readyState; 196 this.status = status; 197 this.statusText = statusMessage; 198 this.responseBytes = bytes; 199 } 200 this.readyEvent.fireEvent(null); 201 } 202 203 private String getAllResponseHeaders(URLConnection c) { 204 int idx = 0; 205 String value; 206 StringBuffer buf = new StringBuffer (); 207 while((value = c.getHeaderField(idx)) != null) { 208 String key = c.getHeaderFieldKey(idx); 209 buf.append(key); buf.append(": "); buf.append(value); 210 idx++; 211 } 212 return buf.toString(); 213 } 214 215 private java.net.URLConnection connection; 216 217 protected void openSync(String method, java.net.URL url, 218 String userName, String password) { 219 try { 220 this.abort(); 221 URLConnection c = url.openConnection(); 222 synchronized(this) { 223 this.connection = c; 224 } 225 try { 226 c.setRequestProperty("User-Agent", this.context.getUserAgent()); 227 this.changeState(HttpRequest.STATE_LOADING, 0, "", null); 228 java.io.InputStream in = c.getInputStream(); 229 int contentLength = c.getContentLength(); 230 byte[] bytes = IORoutines.load(in, contentLength == -1 ? 4096 : contentLength); 231 int status = 0; 232 String statusText = ""; 233 if(c instanceof HttpURLConnection ) { 234 HttpURLConnection hc = (HttpURLConnection ) c; 235 status = hc.getResponseCode(); 236 statusText = hc.getResponseMessage(); 237 } 238 synchronized(this) { 239 this.responseHeaders = this.getAllResponseHeaders(c); 240 this.responseHeadersMap = c.getHeaderFields(); 241 } 242 this.changeState(HttpRequest.STATE_COMPLETE, status, statusText, bytes); 243 } finally { 244 synchronized(this) { 245 this.connection = null; 246 } 247 } 248 } catch(Exception err) { 249 this.changeState(HttpRequest.STATE_COMPLETE, err instanceof java.io.FileNotFoundException ? 404 : 400, err.getMessage(), null); 250 logger.log(Level.WARNING, "Request failed on url=" + url, err); 251 } 252 } 253 254 private final EventDispatch readyEvent = new EventDispatch(); 255 256 public void addReadyStateChangeListener(final ReadyStateChangeListener listener) { 257 readyEvent.addListener(new GenericEventListener() { 258 public void processEvent(EventObject event) { 259 listener.readyStateChanged(); 260 } 261 }); 262 } 263 } 264 | Popular Tags |