use of net.sourceforge.htmlunit.corejs.javascript.json.JsonParser in project htmlunit by HtmlUnit.
the class XMLHttpRequest method getResponse.
/**
* @return returns the response's body content as an ArrayBuffer, Blob, Document, JavaScript Object,
* or DOMString, depending on the value of the request's responseType property.
*/
@JsxGetter
public Object getResponse() {
if (RESPONSE_TYPE_DEFAULT.equals(responseType_) || RESPONSE_TYPE_TEXT.equals(responseType_)) {
return getResponseText();
}
if (state_ != DONE) {
return null;
}
if (webResponse_ instanceof NetworkErrorWebResponse) {
if (LOG.isDebugEnabled()) {
LOG.debug("XMLHttpRequest.responseXML returns of a network error (" + ((NetworkErrorWebResponse) webResponse_).getError() + ")");
}
return null;
}
if (RESPONSE_TYPE_ARRAYBUFFER.equals(responseType_)) {
final NativeArrayBuffer nativeArrayBuffer = new NativeArrayBuffer(webResponse_.getContentLength());
try {
final int bufferLength = 1;
final byte[] buffer = new byte[bufferLength];
try (InputStream inputStream = webResponse_.getContentAsStream()) {
int offset = 0;
int readLen;
while ((readLen = inputStream.read(buffer, 0, bufferLength)) != -1) {
System.arraycopy(buffer, 0, nativeArrayBuffer.getBuffer(), offset, readLen);
offset += readLen;
}
}
nativeArrayBuffer.setParentScope(getParentScope());
nativeArrayBuffer.setPrototype(ScriptableObject.getClassPrototype(getWindow(), nativeArrayBuffer.getClassName()));
return nativeArrayBuffer;
} catch (final IOException e) {
webResponse_ = new NetworkErrorWebResponse(webRequest_, e);
return null;
}
} else if (RESPONSE_TYPE_BLOB.equals(responseType_)) {
try {
if (webResponse_ != null) {
try (InputStream inputStream = webResponse_.getContentAsStream()) {
final Blob blob = new Blob(IOUtils.toByteArray(inputStream), webResponse_.getContentType());
blob.setParentScope(getParentScope());
blob.setPrototype(ScriptableObject.getClassPrototype(getWindow(), blob.getClassName()));
return blob;
}
}
} catch (final IOException e) {
webResponse_ = new NetworkErrorWebResponse(webRequest_, e);
return null;
}
} else if (RESPONSE_TYPE_DOCUMENT.equals(responseType_)) {
if (webResponse_ != null) {
try {
final Charset encoding = webResponse_.getContentCharset();
if (encoding == null) {
return "";
}
final String content = webResponse_.getContentAsString(encoding);
if (content == null) {
return "";
}
return DOMParser.parseFromString(this, content, webResponse_.getContentType());
} catch (final IOException e) {
webResponse_ = new NetworkErrorWebResponse(webRequest_, e);
return null;
}
}
} else if (RESPONSE_TYPE_JSON.equals(responseType_)) {
if (webResponse_ != null) {
final Charset encoding = webResponse_.getContentCharset();
if (encoding == null) {
return null;
}
final String content = webResponse_.getContentAsString(encoding);
if (content == null) {
return null;
}
try {
return new JsonParser(Context.getCurrentContext(), this).parseValue(content);
} catch (final ParseException e) {
webResponse_ = new NetworkErrorWebResponse(webRequest_, new IOException(e));
return null;
}
}
}
return "";
}
Aggregations