Search in sources :

Example 1 with AjaxController

use of com.gargoylesoftware.htmlunit.AjaxController in project htmlunit by HtmlUnit.

the class XMLHTTPRequest method send.

/**
 * Sends an HTTP request to the server and receives a response.
 * @param body the body of the message being sent with the request.
 */
@JsxFunction
public void send(final Object body) {
    if (webRequest_ == null) {
        setState(STATE_DONE, Context.getCurrentContext());
        return;
    }
    if (sent_) {
        throw Context.reportRuntimeError("Unspecified error (request already sent).");
    }
    sent_ = true;
    prepareRequest(body);
    // quite strange but IE seems to fire state loading twice
    setState(STATE_OPENED, Context.getCurrentContext());
    final Window w = getWindow();
    final WebClient client = w.getWebWindow().getWebClient();
    final AjaxController ajaxController = client.getAjaxController();
    final HtmlPage page = (HtmlPage) w.getWebWindow().getEnclosedPage();
    final boolean synchron = ajaxController.processSynchron(page, webRequest_, async_);
    if (synchron) {
        doSend(Context.getCurrentContext());
    } else {
        // Create and start a thread in which to execute the request.
        final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
        final ContextAction<Object> action = cx -> {
            // KEY_STARTING_SCOPE maintains a stack of scopes
            @SuppressWarnings("unchecked") Deque<Scriptable> stack = (Deque<Scriptable>) cx.getThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE);
            if (null == stack) {
                stack = new ArrayDeque<>();
                cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, stack);
            }
            stack.push(w);
            try {
                doSend(cx);
            } finally {
                stack.pop();
            }
            return null;
        };
        final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavascriptXMLHttpRequestJob(cf, action);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Starting XMLHTTPRequest thread for asynchronous request");
        }
        jobID_ = w.getWebWindow().getJobManager().addJob(job, page);
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) Arrays(java.util.Arrays) URL(java.net.URL) ArrayUtils(org.apache.commons.lang3.ArrayUtils) Deque(java.util.Deque) NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) StringUtils(org.apache.commons.lang3.StringUtils) FormEncodingType(com.gargoylesoftware.htmlunit.FormEncodingType) BackgroundJavaScriptFactory(com.gargoylesoftware.htmlunit.javascript.background.BackgroundJavaScriptFactory) Charset(java.nio.charset.Charset) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) Locale(java.util.Locale) FormData(com.gargoylesoftware.htmlunit.javascript.host.xml.FormData) HttpMethod(com.gargoylesoftware.htmlunit.HttpMethod) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) MimeType(com.gargoylesoftware.htmlunit.util.MimeType) Context(net.sourceforge.htmlunit.corejs.javascript.Context) XmlPage(com.gargoylesoftware.htmlunit.xml.XmlPage) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) ScriptRuntime(net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime) MalformedURLException(java.net.MalformedURLException) UTF_8(java.nio.charset.StandardCharsets.UTF_8) HttpHeader(com.gargoylesoftware.htmlunit.HttpHeader) Collection(java.util.Collection) IOException(java.io.IOException) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor) IE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE) Entry(java.util.Map.Entry) Log(org.apache.commons.logging.Log) WebClient(com.gargoylesoftware.htmlunit.WebClient) LogFactory(org.apache.commons.logging.LogFactory) AjaxController(com.gargoylesoftware.htmlunit.AjaxController) Undefined(net.sourceforge.htmlunit.corejs.javascript.Undefined) ArrayDeque(java.util.ArrayDeque) Function(net.sourceforge.htmlunit.corejs.javascript.Function) Collections(java.util.Collections) InputStream(java.io.InputStream) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) WebClient(com.gargoylesoftware.htmlunit.WebClient) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) ArrayDeque(java.util.ArrayDeque) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) AjaxController(com.gargoylesoftware.htmlunit.AjaxController) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 2 with AjaxController

use of com.gargoylesoftware.htmlunit.AjaxController in project htmlunit by HtmlUnit.

the class XMLHttpRequest method send.

/**
 * Sends the specified content to the server in an HTTP request and receives the response.
 * @param content the body of the message being sent with the request
 */
@JsxFunction
public void send(final Object content) {
    if (webRequest_ == null) {
        return;
    }
    if (!async_ && timeout_ > 0) {
        Context.throwAsScriptRuntimeEx(new RuntimeException("Synchronous requests must not set a timeout."));
        return;
    }
    prepareRequestContent(content);
    if (timeout_ > 0) {
        webRequest_.setTimeout(timeout_);
    }
    final Window w = getWindow();
    final WebWindow ww = w.getWebWindow();
    final WebClient client = ww.getWebClient();
    final AjaxController ajaxController = client.getAjaxController();
    final HtmlPage page = (HtmlPage) ww.getEnclosedPage();
    final boolean synchron = ajaxController.processSynchron(page, webRequest_, async_);
    if (synchron) {
        doSend();
    } else {
        // Create and start a thread in which to execute the request.
        final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
        final ContextAction<Object> action = new ContextAction<Object>() {

            @Override
            public Object run(final Context cx) {
                // KEY_STARTING_SCOPE maintains a stack of scopes
                @SuppressWarnings("unchecked") Deque<Scriptable> stack = (Deque<Scriptable>) cx.getThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE);
                if (null == stack) {
                    stack = new ArrayDeque<>();
                    cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, stack);
                }
                stack.push(w);
                try {
                    doSend();
                } finally {
                    stack.pop();
                }
                return null;
            }

            @Override
            public String toString() {
                return "XMLHttpRequest " + webRequest_.getHttpMethod() + " '" + webRequest_.getUrl() + "'";
            }
        };
        final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavascriptXMLHttpRequestJob(cf, action);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Starting XMLHttpRequest thread for asynchronous request");
        }
        jobID_ = ww.getJobManager().addJob(job, page);
        if (getBrowserVersion().hasFeature(XHR_FIRE_STATE_OPENED_AGAIN_IN_ASYNC_MODE)) {
            // quite strange but IE seems to fire state loading twice
            // in async mode (at least with HTML of the unit tests)
            fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
        }
        if (!getBrowserVersion().hasFeature(XHR_LOAD_START_ASYNC)) {
            fireJavascriptEvent(Event.TYPE_LOAD_START);
        }
    }
}
Also used : WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) Context(net.sourceforge.htmlunit.corejs.javascript.Context) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) WebClient(com.gargoylesoftware.htmlunit.WebClient) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) AjaxController(com.gargoylesoftware.htmlunit.AjaxController) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

AjaxController (com.gargoylesoftware.htmlunit.AjaxController)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)2 JavaScriptJob (com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob)2 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)2 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)2 ArrayDeque (java.util.ArrayDeque)2 Deque (java.util.Deque)2 Context (net.sourceforge.htmlunit.corejs.javascript.Context)2 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)2 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)2 FormEncodingType (com.gargoylesoftware.htmlunit.FormEncodingType)1 HttpHeader (com.gargoylesoftware.htmlunit.HttpHeader)1 HttpMethod (com.gargoylesoftware.htmlunit.HttpMethod)1 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)1 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 BackgroundJavaScriptFactory (com.gargoylesoftware.htmlunit.javascript.background.BackgroundJavaScriptFactory)1 JsxClass (com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass)1