Search in sources :

Example 1 with ParseException

use of net.sourceforge.htmlunit.corejs.javascript.json.JsonParser.ParseException 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 "";
}
Also used : Blob(com.gargoylesoftware.htmlunit.javascript.host.file.Blob) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) NativeArrayBuffer(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer) IOException(java.io.IOException) ParseException(net.sourceforge.htmlunit.corejs.javascript.json.JsonParser.ParseException) HttpHint(com.gargoylesoftware.htmlunit.WebRequest.HttpHint) JsonParser(net.sourceforge.htmlunit.corejs.javascript.json.JsonParser) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Aggregations

HttpHint (com.gargoylesoftware.htmlunit.WebRequest.HttpHint)1 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)1 Blob (com.gargoylesoftware.htmlunit.javascript.host.file.Blob)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Charset (java.nio.charset.Charset)1 JsonParser (net.sourceforge.htmlunit.corejs.javascript.json.JsonParser)1 ParseException (net.sourceforge.htmlunit.corejs.javascript.json.JsonParser.ParseException)1 NativeArrayBuffer (net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer)1