How to get HTML source code from a wicket page

View: New views
10 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

Re: How to get HTML source code from a wicket page

by Tremelune :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

There we go. I've pretty much got this, thanks to everyone. Here's a helper class I drummed up--It's not fully tested, doesn't handle both HTML & text, and doesn't take parameters yet. I'll try and post a new thread (maybe even on the wiki?) when I get those details hashed out. For now, this is a good proof of concept that can be used virtually anywhere (to reiterate: This requires 1.3 beta 2):





/**
 * Used with WicketTester to capture rendered HTML from a WicketPage. ex:<p/>
 *
 * <pre>
 * CaptureApplication app = new CaptureApplication();
 * WicketTester renderer = new WicketTester(app);
 * renderer.startPage(WicketPage.class);
 * String html = app.getRenderedText();
 * </pre>
 */
class CaptureApplication extends WebApplication {
  /** Holds HTML rendered from Wicket files after the request cycle is run through. */
  private String renderedText;



  /** Required for WebApplication interface--this won't be called in WicketTester. */
  public Class getHomePage() {
    return null;
  }



  /**
   * @throws IllegalStateException if renderedText is null (meaning the request has not yet been
   * cycled through).
   */
  String getRenderedText() {
    if (renderedText == null) {
      throw new IllegalStateException("Response text has not been rendered yet");
    }

    return renderedText;
  }



  /** This is automagically called during the Wicket request cycle. */
  protected IRequestCycleProcessor newRequestCycleProcessor() {
    return new InterceptingCycle();
  }



  /**
   * Intercepts request cycle and extracts HTML rendered from Wicket files before they are sent as a
   * response to the "browser".
   */
  private class InterceptingCycle extends WebRequestCycleProcessor {
    @Override
    /** Intercepts request cycle and stores the rendered HTML in a gettable String. */
    public void respond(RequestCycle requestCycle) {
      //We need a holder for the HTML that Wicket can work with.
      StringResponse emailResponse = new StringResponse();

      //Now we swap the holder in for the real response.
      WebResponse originalResponse = (WebResponse) RequestCycle.get().getResponse();
      RequestCycle.get().setResponse(emailResponse);

      //Do the real work...
      super.respond(requestCycle);

      //Snag the rendered HTML...
      renderedText = emailResponse.toString();

      //We need to flip back to the original response to avoid a ClassCastException on the tail end
      //of the request cycle.
      RequestCycle.get().setResponse(originalResponse);
    }
  }
}

Re: How to get HTML source code from a wicket page

by Eelco Hillenius :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On 7/11/07, Tremelune <tremelune100@...> wrote:
>
> There we go. I've pretty much got this, thanks to everyone. Here's a helper
> class I drummed up--It's not fully tested, doesn't handle both HTML & text,
> and doesn't take parameters yet. I'll try and post a new thread (maybe even
> on the wiki?) when I get those details hashed out. For now, this is a good
> proof of concept that can be used virtually anywhere (to reiterate: This
> requires 1.3 beta 2):

It seems to be such a recurring issue (and tbh, I thought we recently
saw some contributions/ improvements here) that it might be a good
idea to create a JIRA issue for it and ultimately get it back in the
core project.

Eelco

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
Wicket-user@...
https://lists.sourceforge.net/lists/listinfo/wicket-user

Re: How to get HTML source code from a wicket page

by Tremelune :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Okay, well. It works, but not if you try and instigate the rendering from a Wicket page. Because the RequestCycle is all kinds of threadlocal, and it's used by WicketTester, the rendering of page from which you make the rendering call blows up with a null error because RequestCycle.current.get() returns null after WicketTester does its muckering:

org.apache.wicket.WicketRuntimeException: No RequestCycle is currently set!
at org.apache.wicket.Component.getRequest(Component.java:1417)

I've been trying to find a way to save the old cycle and swap it into the current request cycle to no avail...Everything is fine until the calling Wicket page needs to render itself (presumably with a page that says "Email sent").



Tremelune wrote:
There we go. I've pretty much got this, thanks to everyone. Here's a helper class I drummed up--It's not fully tested, doesn't handle both HTML & text, and doesn't take parameters yet. I'll try and post a new thread (maybe even on the wiki?) when I get those details hashed out. For now, this is a good proof of concept that can be used virtually anywhere (to reiterate: This requires 1.3 beta 2):

Re: How to get HTML source code from a wicket page

by Tremelune :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

For some reason, after my custom WebRequestProcessorCycle.respond() fires, RquestCycle.get() returns null...I can't seem to escape that...

I'm not familiar with JIRA, where should I go to sign up or post a bug?



Eelco Hillenius wrote:
It seems to be such a recurring issue (and tbh, I thought we recently
saw some contributions/ improvements here) that it might be a good
idea to create a JIRA issue for it and ultimately get it back in the
core project.

Eelco

Re: How to get HTML source code from a wicket page

by Eelco Hillenius :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

> I'm not familiar with JIRA, where should I go to sign up or post a bug?

http://issues.apache.org/jira/browse/WICKET

then look for a way to register.

Eelco

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
IMPORTANT NOTICE:

This mailing list is shutting down. Please subscribe to the Apache Wicket user list. Send a message to: "users-subscribe at wicket.apache.org" and follow the instructions.
_______________________________________________
Wicket-user mailing list
Wicket-user@...
https://lists.sourceforge.net/lists/listinfo/wicket-user

Re: How to get HTML source code from a wicket page

by Tremelune :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Great, thanks. This is pretty much all set.

http://issues.apache.org/jira/browse/WICKET-795



Eelco Hillenius wrote:
http://issues.apache.org/jira/browse/WICKET

then look for a way to register.

Eelco

Re: How to get HTML source code from a wicket page

by oliver.henlich :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message


Jean-Baptiste Quenot-3 wrote:
It's not your fault.  I didn't mention that you need to upgrade to
the latest beta2 for this to work, as there is a nasty cast to
WebResponse in WebPage.java that I removed.

Hi Jean,

i just tried the shown snippet with 1.3 snapshot (checkout from trunk) and i'm still getting:

java.lang.ClassCastException: org.apache.wicket.response.StringResponse cannot be cast to org.apache.wicket.protocol.http.WebResponse

org.apache.wicket.protocol.http.WebRequestCycle.getWebResponse(WebRequestCycle.java:109)

or did i misunderstand?

cheers
Oliver

Re: How to get HTML source code from a wicket page

by Jean-Baptiste Quenot-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

* oliver.henlich:

>
> i just tried the shown snippet with 1.3 snapshot (checkout from trunk) and
> i'm still getting:
>
> java.lang.ClassCastException: org.apache.wicket.response.StringResponse
> cannot be cast to org.apache.wicket.protocol.http.WebResponse
>
> org.apache.wicket.protocol.http.WebRequestCycle.getWebResponse(WebRequestCycle.java:109)
>
> or did i misunderstand?

Can you paste the full stacktrace please?
--
     Jean-Baptiste Quenot
aka  John Banana   Qwerty
http://caraldi.com/jbq/

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
IMPORTANT NOTICE:

This mailing list is shutting down. Please subscribe to the Apache Wicket user list. Send a message to: "users-subscribe at wicket.apache.org" and follow the instructions.
_______________________________________________
Wicket-user mailing list
Wicket-user@...
https://lists.sourceforge.net/lists/listinfo/wicket-user

Re: How to get HTML source code from a wicket page

by oliver.henlich :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Jean-Baptiste Quenot-3 wrote:
Can you paste the full stacktrace please?

Sure here you go (sorry for the delay).
Note i went back to beta2.

Stacktrace:
-----------
11:45:49.263 WARN!! Exception for /app/?wicket:interface=:1:linkDumpHtml::ILinkListener::
org.apache.wicket.WicketRuntimeException: Internal Error: Could not render error page class org.apache.wicket.markup.html.pages.InternalErrorPage
  at org.apache.wicket.request.AbstractRequestCycleProcessor.respond(AbstractRequestCycleProcessor.java:165)
  at org.apache.wicket.RequestCycle.step(RequestCycle.java:1142)
  at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1176)
  at org.apache.wicket.RequestCycle.request(RequestCycle.java:499)
  at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:257)
  at org.apache.wicket.protocol.http.WicketServlet.doGet(WicketServlet.java:126)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:360)
  at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:294)
  at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:558)
  at org.mortbay.http.HttpContext.handle(HttpContext.java:1714)
  at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:507)
  at org.mortbay.http.HttpContext.handle(HttpContext.java:1664)
  at org.mortbay.http.HttpServer.service(HttpServer.java:863)
  at org.mortbay.http.HttpConnection.service(HttpConnection.java:775)
  at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:939)
  at org.mortbay.http.HttpConnection.handle(HttpConnection.java:792)
  at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:201)
  at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:289)
  at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:455)
Caused by: java.lang.ClassCastException: org.apache.wicket.response.StringResponse cannot be cast to org.apache.wicket.protocol.http.WebResponse
  at org.apache.wicket.protocol.http.WebRequestCycle.getWebResponse(WebRequestCycle.java:108)
  at org.apache.wicket.markup.html.pages.ExceptionErrorPage.configureResponse(ExceptionErrorPage.java:107)
  at org.apache.wicket.Page.onRender(Page.java:1419)
  at org.apache.wicket.Component.render(Component.java:1941)
  at org.apache.wicket.Page.renderPage(Page.java:927)
  at org.apache.wicket.request.target.component.PageRequestTarget.respond(PageRequestTarget.java:64)
  at org.apache.wicket.request.AbstractRequestCycleProcessor.respond(AbstractRequestCycleProcessor.java:103)
  at org.apache.wicket.RequestCycle.respond(RequestCycle.java:1046)
  at org.apache.wicket.RequestCycle.step(RequestCycle.java:1112)
  ... 19 more


Pom:
----
<dependency>
    <groupId>org.apache.wicket</groupId>
    <artifactId>wicket</artifactId>
    <version>1.3.0-beta2</version>
    <scope>compile</scope>
    <type>jar</type>
</dependency>


Code:
-----
add(new Link("linkDumpHtml")
{
    public void onClick()
    {
        WebResponse originalResponse = (WebResponse) RequestCycle.get().getResponse();

        StringResponse dumpResponse = new StringResponse();
        RequestCycle.get().setResponse(dumpResponse);
        RequestCycle.get().getRequestTarget().respond(RequestCycle.get());
        logger.info(dumpResponse.toString());

        RequestCycle.get().setResponse(originalResponse);
        RequestCycle.get().setRequestTarget(new BookmarkablePageRequestTarget(SummaryPage.class));
    }
});

Re: How to get HTML source code from a wicket page

by oliver.henlich :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Hi Jean-Baptiste, just wondering if you got a chance to look at this?
Cheers
Oliver

oliver.henlich wrote:
Jean-Baptiste Quenot-3 wrote:
Can you paste the full stacktrace please?

Sure here you go (sorry for the delay).
Note i went back to beta2.

Stacktrace:
-----------
11:45:49.263 WARN!! Exception for /app/?wicket:interface=:1:linkDumpHtml::ILinkListener::
org.apache.wicket.WicketRuntimeException: Internal Error: Could not render error page class org.apache.wicket.markup.html.pages.InternalErrorPage
  at org.apache.wicket.request.AbstractRequestCycleProcessor.respond(AbstractRequestCycleProcessor.java:165)
  at org.apache.wicket.RequestCycle.step(RequestCycle.java:1142)
  at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1176)
  at org.apache.wicket.RequestCycle.request(RequestCycle.java:499)
  at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:257)
  at org.apache.wicket.protocol.http.WicketServlet.doGet(WicketServlet.java:126)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:360)
  at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:294)
  at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:558)
  at org.mortbay.http.HttpContext.handle(HttpContext.java:1714)
  at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:507)
  at org.mortbay.http.HttpContext.handle(HttpContext.java:1664)
  at org.mortbay.http.HttpServer.service(HttpServer.java:863)
  at org.mortbay.http.HttpConnection.service(HttpConnection.java:775)
  at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:939)
  at org.mortbay.http.HttpConnection.handle(HttpConnection.java:792)
  at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:201)
  at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:289)
  at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:455)
Caused by: java.lang.ClassCastException: org.apache.wicket.response.StringResponse cannot be cast to org.apache.wicket.protocol.http.WebResponse
  at org.apache.wicket.protocol.http.WebRequestCycle.getWebResponse(WebRequestCycle.java:108)
  at org.apache.wicket.markup.html.pages.ExceptionErrorPage.configureResponse(ExceptionErrorPage.java:107)
  at org.apache.wicket.Page.onRender(Page.java:1419)
  at org.apache.wicket.Component.render(Component.java:1941)
  at org.apache.wicket.Page.renderPage(Page.java:927)
  at org.apache.wicket.request.target.component.PageRequestTarget.respond(PageRequestTarget.java:64)
  at org.apache.wicket.request.AbstractRequestCycleProcessor.respond(AbstractRequestCycleProcessor.java:103)
  at org.apache.wicket.RequestCycle.respond(RequestCycle.java:1046)
  at org.apache.wicket.RequestCycle.step(RequestCycle.java:1112)
  ... 19 more


Pom:
----
<dependency>
    <groupId>org.apache.wicket</groupId>
    <artifactId>wicket</artifactId>
    <version>1.3.0-beta2</version>
    <scope>compile</scope>
    <type>jar</type>
</dependency>


Code:
-----
add(new Link("linkDumpHtml")
{
    public void onClick()
    {
        WebResponse originalResponse = (WebResponse) RequestCycle.get().getResponse();

        StringResponse dumpResponse = new StringResponse();
        RequestCycle.get().setResponse(dumpResponse);
        RequestCycle.get().getRequestTarget().respond(RequestCycle.get());
        logger.info(dumpResponse.toString());

        RequestCycle.get().setResponse(originalResponse);
        RequestCycle.get().setRequestTarget(new BookmarkablePageRequestTarget(SummaryPage.class));
    }
});
< Prev | 1 - 2 | Next >