in ko-ws-tyrus/src/main/java/org/netbeans/html/wstyrus/LoadJSON.java [76:156]
public void run() {
final String url;
Throwable error = null;
Object json = null;
if (call.isJSONP()) {
url = call.composeURL("dummy");
} else {
url = call.composeURL(null);
}
try {
final URL u = new URL(base, url.replace(" ", "%20"));
URLConnection conn = u.openConnection();
if (call.isDoOutput()) {
conn.setDoOutput(true);
}
String h = call.getHeaders();
if (h != null) {
int pos = 0;
while (pos < h.length()) {
int tagEnd = h.indexOf(':', pos);
if (tagEnd == -1) {
break;
}
int r = h.indexOf('\r', tagEnd);
int n = h.indexOf('\n', tagEnd);
if (r == -1) {
r = h.length();
}
if (n == -1) {
n = h.length();
}
String key = h.substring(pos, tagEnd).trim();
String val = h.substring(tagEnd + 1, Math.min(r, n)).trim();
conn.setRequestProperty(key, val);;
pos = Math.max(r, n);
}
}
if (call.getMethod() != null && conn instanceof HttpURLConnection) {
((HttpURLConnection) conn).setRequestMethod(call.getMethod());
}
if (call.isDoOutput()) {
final OutputStream os = conn.getOutputStream();
call.writeData(os);
os.flush();
}
final PushbackInputStream is = new PushbackInputStream(
conn.getInputStream(), 1
);
boolean[] arrayOrString = { false, false };
detectJSONType(call.isJSONP(), is, arrayOrString);
try {
if (arrayOrString[1]) {
throw new JSONException("");
}
JSONTokener tok = createTokener(is);
Object obj;
obj = arrayOrString[0] ? new JSONArray(tok) : new JSONObject(tok);
json = convertToArray(obj);
} catch (JSONException ex) {
Reader r = new InputStreamReader(is, "UTF-8");
StringBuilder sb = new StringBuilder();
for (;;) {
int ch = r.read();
if (ch == -1) {
break;
}
sb.append((char)ch);
}
json = sb.toString();
}
} catch (IOException ex) {
error = ex;
} finally {
if (error != null) {
call.notifyError(error);
} else {
call.notifySuccess(json);
}
}
}