Search in sources :

Example 1 with InterceptUrlRequestCallback

use of com.teamdev.jxbrowser.net.callback.InterceptUrlRequestCallback in project JxBrowser-Examples by TeamDev-IP.

the class LoadHtmlThroughInterceptRequest method main.

public static void main(String[] args) {
    InterceptUrlRequestCallback interceptCallback = params -> {
        if (params.urlRequest().url().endsWith("?load-html")) {
            byte[] bytes = "<html><body>Hello!</body></html>".getBytes();
            UrlRequestJob job = params.newUrlRequestJob(UrlRequestJob.Options.newBuilder(HttpStatus.OK).addHttpHeader(HttpHeader.of("Content-Type", "text/html")).build());
            job.write(bytes);
            job.complete();
            return InterceptUrlRequestCallback.Response.intercept(job);
        }
        return InterceptUrlRequestCallback.Response.proceed();
    };
    Engine engine = Engine.newInstance(EngineOptions.newBuilder(HARDWARE_ACCELERATED).addScheme(Scheme.HTTP, interceptCallback).build());
    Browser browser = engine.newBrowser();
    SwingUtilities.invokeLater(() -> {
        BrowserView view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("Load Local Files");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.navigation().loadUrl("http://localhost?load-html");
}
Also used : InterceptUrlRequestCallback(com.teamdev.jxbrowser.net.callback.InterceptUrlRequestCallback) UrlRequestJob(com.teamdev.jxbrowser.net.UrlRequestJob) HttpHeader(com.teamdev.jxbrowser.net.HttpHeader) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) EngineOptions(com.teamdev.jxbrowser.engine.EngineOptions) HttpStatus(com.teamdev.jxbrowser.net.HttpStatus) Scheme(com.teamdev.jxbrowser.net.Scheme) SwingUtilities(javax.swing.SwingUtilities) Browser(com.teamdev.jxbrowser.browser.Browser) HARDWARE_ACCELERATED(com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) Engine(com.teamdev.jxbrowser.engine.Engine) WindowConstants(javax.swing.WindowConstants) InterceptUrlRequestCallback(com.teamdev.jxbrowser.net.callback.InterceptUrlRequestCallback) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) UrlRequestJob(com.teamdev.jxbrowser.net.UrlRequestJob) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Aggregations

Browser (com.teamdev.jxbrowser.browser.Browser)1 Engine (com.teamdev.jxbrowser.engine.Engine)1 EngineOptions (com.teamdev.jxbrowser.engine.EngineOptions)1 HARDWARE_ACCELERATED (com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED)1 HttpHeader (com.teamdev.jxbrowser.net.HttpHeader)1 HttpStatus (com.teamdev.jxbrowser.net.HttpStatus)1 Scheme (com.teamdev.jxbrowser.net.Scheme)1 UrlRequestJob (com.teamdev.jxbrowser.net.UrlRequestJob)1 InterceptUrlRequestCallback (com.teamdev.jxbrowser.net.callback.InterceptUrlRequestCallback)1 BrowserView (com.teamdev.jxbrowser.view.swing.BrowserView)1 BorderLayout (java.awt.BorderLayout)1 JFrame (javax.swing.JFrame)1 SwingUtilities (javax.swing.SwingUtilities)1 WindowConstants (javax.swing.WindowConstants)1