Skip to content

Commit 94580c0

Browse files
hansonrhansonr
authored andcommitted
missing POST option using OutputStream
1 parent 774dc74 commit 94580c0

File tree

3 files changed

+282
-255
lines changed

3 files changed

+282
-255
lines changed

sources/net.sf.j2s.java.core/src/javajs/util/AjaxURLConnection.java

Lines changed: 100 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,59 @@
11
package javajs.util;
22

33
import java.io.BufferedInputStream;
4+
import java.io.ByteArrayOutputStream;
45
import java.io.FileNotFoundException;
56
import java.io.IOException;
67
import java.io.InputStream;
8+
import java.io.OutputStream;
79
import java.net.HttpURLConnection;
810
import java.net.URL;
911
import java.net.URLConnection;
12+
import java.net.UnknownServiceException;
1013
import java.util.Hashtable;
1114
import java.util.Map;
1215

1316
import javajs.api.js.J2SObjectInterface;
1417

1518
/**
1619
*
17-
* A method to allow a JavaScript Ajax
20+
* A method to allow a JavaScript Ajax
1821
*
1922
*/
2023
public class AjaxURLConnection extends HttpURLConnection {
21-
22-
public static class AjaxHttpsURLConnection extends AjaxURLConnection {
2324

24-
static {
25-
/**
26-
* @j2sNative C$.implementz = [Clazz.load("javax.net.ssl.HttpsURLConnection")];
27-
*
28-
*/
29-
}
30-
protected AjaxHttpsURLConnection(URL url) {
31-
super(url);
25+
public static class AjaxHttpsURLConnection extends AjaxURLConnection {
26+
27+
static {
28+
/**
29+
* @j2sNative C$.implementz = [Clazz.load("javax.net.ssl.HttpsURLConnection")];
30+
*
31+
*/
32+
}
33+
34+
protected AjaxHttpsURLConnection(URL url) {
35+
super(url);
36+
}
37+
3238
}
33-
34-
}
3539

36-
public static URLConnection newConnection(URL url) {
40+
public static URLConnection newConnection(URL url) {
3741
return (url.getProtocol() == "https" ? new AjaxHttpsURLConnection(url) : new AjaxURLConnection(url));
3842
}
3943

40-
protected AjaxURLConnection(URL url) {
41-
super(url);
42-
ajax = /** @j2sNative url.ajax || */ null;
43-
}
44+
protected AjaxURLConnection(URL url) {
45+
super(url);
46+
ajax = /** @j2sNative url.ajax || */
47+
null;
48+
}
4449

45-
byte[] bytesOut;
46-
String postOut = "";
50+
byte[] bytesOut;
51+
String postOut = "";
52+
ByteArrayOutputStream streamOut;
4753

48-
private Object ajax;
54+
private Object ajax;
4955

50-
/**
56+
/**
5157
*
5258
* doAjax() is where the synchronous call to AJAX is to happen. or at least
5359
* where we wait for the asynchronous call to return. This method should fill
@@ -69,6 +75,10 @@ protected AjaxURLConnection(URL url) {
6975
*/
7076
@SuppressWarnings("null")
7177
private Object doAjax(boolean isBinary) {
78+
if (streamOut != null) {
79+
bytesOut = streamOut.toByteArray();
80+
streamOut = null;
81+
}
7282
J2SObjectInterface J2S = /** @j2sNative self.J2S || */ null;
7383
Object info = null;
7484
/** @j2sNative
@@ -95,20 +105,25 @@ private Object doAjax(boolean isBinary) {
95105
return result;
96106
}
97107

98-
@Override
99-
public void connect() throws IOException {
100-
// not expected to be used.
101-
}
108+
@Override
109+
public void connect() throws IOException {
110+
// not expected to be used.
111+
}
102112

103-
public void outputBytes(byte[] bytes) {
104-
// type = "application/octet-stream;";
105-
bytesOut = bytes;
106-
}
113+
public void outputBytes(byte[] bytes) {
114+
// type = "application/octet-stream;";
115+
bytesOut = bytes;
116+
}
107117

108-
public void outputString(String post) {
109-
postOut = post;
110-
// type = "application/x-www-form-urlencoded";
111-
}
118+
public void outputString(String post) {
119+
postOut = post;
120+
// type = "application/x-www-form-urlencoded";
121+
}
122+
123+
@Override
124+
public OutputStream getOutputStream() throws IOException {
125+
return streamOut = new ByteArrayOutputStream();
126+
}
112127

113128
@Override
114129
public InputStream getInputStream() throws FileNotFoundException {
@@ -135,12 +150,13 @@ private InputStream getInputStreamAndResponse(URL url, boolean allowNWError) {
135150
}
136151

137152
static Map<String, Object> urlCache = new Hashtable<String, Object>();
138-
153+
139154
private BufferedInputStream getCachedStream(URL url, boolean allowNWError) {
140155
Object data = urlCache.get(url.toString());
141156
if (data == null)
142157
return null;
143-
boolean isAjax = /** @j2sNative url.ajax || */false;
158+
boolean isAjax = /** @j2sNative url.ajax || */
159+
false;
144160
BufferedInputStream bis = getBIS(data, isAjax);
145161
return (!isNetworkError(bis) || allowNWError ? bis : null);
146162
}
@@ -161,11 +177,12 @@ private static BufferedInputStream getBIS(Object data, boolean isJSON) {
161177

162178
@SuppressWarnings("unused")
163179
private void setCachedStream(URL url) {
164-
Object data = /** @j2sNative url._streamData || */null;
180+
Object data = /** @j2sNative url._streamData || */
181+
null;
165182
if (data != null) {
166183
int code = this.responseCode;
167184
/**
168-
* @j2sNative data._responseCode = code;
185+
* @j2sNative data._responseCode = code;
169186
*/
170187
urlCache.put(url.toString(), data);
171188
}
@@ -194,26 +211,24 @@ private boolean isNetworkError(BufferedInputStream is) {
194211
}
195212

196213
final private static int[] NETWORK_ERROR = new int[] { 78, 101, 116, 119, 111, 114, 107, 69, 114, 114, 111, 114 };
197-
214+
198215
/**
199-
* J2S will attach the data (String, SB, or byte[]) to any URL that is
200-
* retrieved using a ClassLoader. This improves performance by
201-
* not going back to the server every time a second time, since
202-
* the first time in Java is usually just to see if it exists.
216+
* J2S will attach the data (String, SB, or byte[]) to any URL that is retrieved
217+
* using a ClassLoader. This improves performance by not going back to the
218+
* server every time a second time, since the first time in Java is usually just
219+
* to see if it exists.
203220
*
204221
* @param url
205222
* @return String, SB, or byte[], or JSON dat
206223
*/
207224
@SuppressWarnings("unused")
208225
public static BufferedInputStream getAttachedStreamData(URL url, boolean andDelete) {
209-
226+
210227
Object data = null;
211228
boolean isJSON = false;
212229
/**
213-
* @j2sNative
214-
* data = url._streamData;
215-
* if (andDelete) url._streamData = null;
216-
* isJSON = (data && url.ajax && url.ajax.dataType == "json")
230+
* @j2sNative data = url._streamData; if (andDelete) url._streamData = null;
231+
* isJSON = (data && url.ajax && url.ajax.dataType == "json")
217232
*/
218233
return getBIS(data, isJSON);
219234
}
@@ -222,24 +237,26 @@ public static BufferedInputStream getAttachedStreamData(URL url, boolean andDele
222237
*
223238
* @param url
224239
* @param o
225-
* @return InputStream or possibly a wrapper for an empty string, but also with JSON data.
240+
* @return InputStream or possibly a wrapper for an empty string, but also with
241+
* JSON data.
226242
*/
227-
public static BufferedInputStream attachStreamData(URL url, Object o) {
228-
/**
229-
* @j2sNative
230-
*
231-
* url._streamData = o;
232-
*/
233-
234-
return getBIS(o, /** @j2sNative url.ajax || */false);
235-
}
243+
public static BufferedInputStream attachStreamData(URL url, Object o) {
244+
/**
245+
* @j2sNative
246+
*
247+
* url._streamData = o;
248+
*/
236249

237-
/**
238-
* @return javajs.util.SB or byte[], depending upon the file type
239-
*/
240-
public Object getContents() {
241-
return doAjax(false);
242-
}
250+
return getBIS(o, /** @j2sNative url.ajax || */
251+
false);
252+
}
253+
254+
/**
255+
* @return javajs.util.SB or byte[], depending upon the file type
256+
*/
257+
public Object getContents() {
258+
return doAjax(false);
259+
}
243260

244261
@Override
245262
public int getResponseCode() throws IOException {
@@ -258,26 +275,27 @@ public int getResponseCode() throws IOException {
258275
}
259276
return responseCode;
260277
}
261-
@Override
262-
public void disconnect() {
263-
// TODO Auto-generated method stub
264-
265-
}
266278

267-
@Override
268-
public boolean usingProxy() {
269-
// TODO Auto-generated method stub
270-
return false;
271-
}
279+
@Override
280+
public void disconnect() {
281+
// TODO Auto-generated method stub
272282

273-
@Override
274-
public int getContentLength() {
275-
try {
276-
InputStream is = getInputStream();
277-
return is.available();
278-
} catch (IOException e) {
279-
return -1;
280283
}
281-
}
284+
285+
@Override
286+
public boolean usingProxy() {
287+
// TODO Auto-generated method stub
288+
return false;
289+
}
290+
291+
@Override
292+
public int getContentLength() {
293+
try {
294+
InputStream is = getInputStream();
295+
return is.available();
296+
} catch (IOException e) {
297+
return -1;
298+
}
299+
}
282300

283301
}

0 commit comments

Comments
 (0)