Skip to content

Commit b5b1679

Browse files
author
zhourenjian
committed
Change modifier of class HttpRequest to be non-final class so that developers may extends it for more controls over XHR in Java level.
For example, inherited HttpRequest may override initializeReceivingMonitor for continuation, such as Comet applications.
1 parent d81ce81 commit b5b1679

File tree

1 file changed

+77
-16
lines changed

1 file changed

+77
-16
lines changed

sources/net.sf.j2s.ajax/ajaxcore/net/sf/j2s/ajax/HttpRequest.java

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*
4141
* 2006-2-11
4242
*/
43-
public final class HttpRequest {
43+
public class HttpRequest {
4444
/*
4545
* @(#)Base64.java 1.4 03/01/23
4646
*
@@ -115,6 +115,24 @@ static String byteArrayToBase64(byte[] a) {
115115
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
116116
};
117117
}
118+
119+
/**
120+
* This class is used to monitoring data-receiving process.
121+
*
122+
* Attention: Only be visible in Java.
123+
*/
124+
protected static interface IXHRReceiving {
125+
/**
126+
* Monitoring the received data along with the given output stream.
127+
*
128+
* @param baos an output stream
129+
* @param b buffer
130+
* @param off offset
131+
* @param len length
132+
* @return whether the data is dealt into the given output stream or not
133+
*/
134+
public boolean receiving(ByteArrayOutputStream baos, byte b[], int off, int len);
135+
}
118136

119137
private int status;
120138
private String statusText;
@@ -124,6 +142,7 @@ static String byteArrayToBase64(byte[] a) {
124142
private Document responseXML;
125143
private IXHRCallback onreadystatechange;
126144
//private boolean overrideMimeType;
145+
private IXHRReceiving receiving;
127146

128147
private boolean asynchronous;
129148
private HttpURLConnection connection;
@@ -184,15 +203,15 @@ public Document getResponseXML() {
184203
}
185204
}
186205
/**
187-
* Return respose code.
206+
* Return response code.
188207
* @return int response code. For more information please read about
189208
* HTTP protocol.
190209
*/
191210
public int getStatus() {
192211
return status;
193212
}
194213
/**
195-
* Return respose code related text.
214+
* Return response code related text.
196215
* @return int response code. For more information please read about
197216
* HTTP protocol.
198217
*/
@@ -207,6 +226,20 @@ public String getStatusText() {
207226
public void registerOnReadyStateChange(IXHRCallback onreadystatechange) {
208227
this.onreadystatechange = onreadystatechange;
209228
}
229+
/**
230+
* Register XMLHttpRequest receiving monitor.
231+
*
232+
* This method is to given inherited class a chance to set a monitor to
233+
* monitor the process that the data is receiving from the connection.
234+
*
235+
* Attention: Only be visible inside Java! There is no such JavaScript
236+
* methods.
237+
*
238+
* @param receiving IXHRReceiving monitor
239+
*/
240+
protected IXHRReceiving initializeReceivingMonitor() {
241+
return null;
242+
}
210243
/**
211244
* Set request header with given key and value.
212245
* @param key String request header keyword. For more information please
@@ -217,8 +250,8 @@ public void setRequestHeader(String key, String value) {
217250
headers.put(key, value);
218251
}
219252
/**
220-
* Get all response heades.
221-
* @return String the all reponse header value.
253+
* Get all response headers.
254+
* @return String the all response header value.
222255
*/
223256
public String getAllResponseHeaders() {
224257
StringBuffer buffer = new StringBuffer();
@@ -243,7 +276,7 @@ public String getAllResponseHeaders() {
243276
* Get response header with given key.
244277
* @param key String header keyword. For more information please
245278
* read about HTTP protocol.
246-
* @return String the reponse header value.
279+
* @return String the response header value.
247280
*/
248281
public String getResponseHeader(String key) {
249282
return connection.getHeaderField(key);
@@ -399,28 +432,56 @@ private void request() {
399432
activeOS = null;
400433
}
401434
if (checkAbort()) return; // just disconnect without receiving anything
402-
InputStream is = connection.getInputStream();
435+
InputStream is = null;
436+
try {
437+
is = connection.getInputStream();
438+
} catch (IOException e) {
439+
if (checkAbort()) return; // exception caused by abort action
440+
e.printStackTrace();
441+
readyState = 4;
442+
if (onreadystatechange != null) {
443+
onreadystatechange.onLoaded();
444+
}
445+
connection = null;
446+
readyState = 0;
447+
/*
448+
if (onreadystatechange != null) {
449+
onreadystatechange.onUninitialized();
450+
}
451+
*/
452+
return;
453+
}
403454
activeIS = is;
455+
456+
if (readyState < 2) {
457+
readyState = 2;
458+
status = connection.getResponseCode();
459+
statusText = connection.getResponseMessage();
460+
if (onreadystatechange != null) {
461+
onreadystatechange.onSent();
462+
}
463+
}
464+
465+
receiving = initializeReceivingMonitor();
466+
404467
ByteArrayOutputStream baos = new ByteArrayOutputStream();
405468
byte[] buffer = new byte[1024];
406469
int read;
407470
while (!toAbort && (read = is.read(buffer)) != -1) {
408471
if (checkAbort()) return; // stop receiving anything
409-
if (readyState < 2) {
410-
readyState = 2;
411-
status = connection.getResponseCode();
412-
statusText = connection.getResponseMessage();
413-
if (onreadystatechange != null) {
414-
onreadystatechange.onSent();
415-
}
416-
}
417-
baos.write(buffer, 0, read);
418472
if (readyState != 3) {
419473
readyState = 3;
420474
if (onreadystatechange != null) {
421475
onreadystatechange.onReceiving();
422476
}
423477
}
478+
boolean received = false;
479+
if (receiving != null) {
480+
received = receiving.receiving(baos, buffer, 0, read);
481+
}
482+
if (!received) {
483+
baos.write(buffer, 0, read);
484+
}
424485
}
425486
if (checkAbort()) return; // stop receiving anything
426487
is.close();

0 commit comments

Comments
 (0)