-
1. Re: How do I fetch the HTML source of a page programatically
monkeyden Feb 7, 2008 5:03 PM (in response to jakec)Is this what you're looking for?
http://www.thescripts.com/forum/thread721450.html -
2. Re: How do I fetch the HTML source of a page programatically
jakec Feb 7, 2008 5:27 PM (in response to jakec)I'd rather not scrape the page. We are running behind SSL, and setting that up in HttpClient is a pain.
I just want to be able to trigger a JSF Render via Java code and get the resulting HTML back. -
3. Re: How do I fetch the HTML source of a page programatically
monkeyden Feb 7, 2008 5:40 PM (in response to jakec)Never tried it and not sure if it will work correctly but:
FacesContext.getCurrentInstance().getViewHandler().restoreView(ctx, viewId)
If it does work (fingers crossed), you'd have to ensure that the state is available for the particular viewId. -
4. Re: How do I fetch the HTML source of a page programatically
monkeyden Feb 7, 2008 5:41 PM (in response to jakec)...and call renderView on the ViewHandler
-
5. Re: How do I fetch the HTML source of a page programatically
jakec Feb 7, 2008 6:38 PM (in response to jakec)Hmm, I don't have getViewHander(), but getViewRoot().
This is in javax.faces.context.FacesContext, in jsf-api.jar in Seam 2.0.1.CR1.
org.jboss.seam.jsf.SeamApplication has getViewHandler(), and org.jboss.seam.jsf.SeamViewHandler() has restoreView(ctx, viewId), but I don't know how to get a handle on either of them. -
6. Re: How do I fetch the HTML source of a page programatically
jakec Feb 7, 2008 6:40 PM (in response to jakec)Ahh, I see FacesContext.getCurrentInstance().getApplication().getViewHandler(). Is that what you mean?
-
7. Re: How do I fetch the HTML source of a page programatically
jakec Feb 7, 2008 6:51 PM (in response to jakec)OK, I'm trying getApplication().getViewHandler(), but how do I get the rendered HTML afterward? Is that through FacesContext.getResponseStream()?
-
8. Re: How do I fetch the HTML source of a page programatically
monkeyden Feb 7, 2008 7:57 PM (in response to jakec)Ahh, I see FacesContext.getCurrentInstance().getApplication().getViewHandler(). Is that what you mean?
yep"JakeC" wrote:
OK, I'm trying getApplication().getViewHandler(), but how do I get the rendered HTML afterward? Is that through FacesContext.getResponseStream()?
As I said, I've never tried it but FacesContext.getResponseWriter() is a better bet. -
9. Re: How do I fetch the HTML source of a page programatically
matt.drees Feb 7, 2008 10:46 PM (in response to jakec)You could also read through Seam's code for doing facelets-based emails. It essentially renders a facelets xhtml template to a string.
-
10. Re: How do I fetch the HTML source of a page programatically
norman.richards Feb 8, 2008 12:19 PM (in response to jakec)Yeah - check through the Seam source. We also have similar code in places like s:cache and p:html. Those only render fragments, but they might be helpful to round out the ideas.
-
11. Re: How do I fetch the HTML source of a page programatically
jakec Feb 8, 2008 1:47 PM (in response to jakec)OK, I think I'm getting much closer. MailComponent.encode() has the process down, but from within a UIComponent, which renders differently. However, the info from Monkey shows how to render from outside a UIComponent, and org.jboss.seam.ui.facelet.FaceletsRenderer showed how to create a ResponseWriter (the one from FacesContext is null). Here is what I have so far:
try { FacesContext facesContext = FacesContext.getCurrentInstance(); ViewHandler viewHandler = facesContext.getApplication().getViewHandler(); UIViewRoot viewRoot = viewHandler.restoreView(facesContext, "/myDir/myPage.xhtml"); if(viewRoot == null) { log.info("restoreView returned null, calling createView"); viewRoot = viewHandler.createView(facesContext, "/myDir/myPage.xhtml"); } StringWriter stringWriter = new StringWriter(); ResponseWriter originalResponseWriter = facesContext.getResponseWriter(); if(originalResponseWriter == null) { log.info("responseWriter is null, creating new one"); facesContext.setResponseWriter(facesContext.getRenderKit().createResponseWriter(stringWriter, null, null)); } else { ResponseWriter cachingResponseWriter = originalResponseWriter.cloneWithWriter(stringWriter); facesContext.setResponseWriter(cachingResponseWriter); } viewHandler.renderView(facesContext, viewRoot); if(originalResponseWriter != null) facesContext.setResponseWriter(originalResponseWriter); String output = stringWriter.getBuffer().toString(); log.info("Rendered data: {0}", output); } catch (Throwable t) { log.error("Error rendering: {0}", t, t.getMessage()); }
Here is the output I got:restoreView returned null, calling createView responseWriter is null, creating new one Rendered data:
The call to restoreView() returns null, so I call createView(). I suspect that this may be my problem. I'm getting nothing rendered because I am creating a new view, not restoring my existing one. The viewId I'm using is "/myDir/myPage.xhtml", like I would refer to it in in pages.xml. Is this incorrect? If so, what should I use for viewId? -
12. Re: How do I fetch the HTML source of a page programatically
jakec Feb 8, 2008 5:47 PM (in response to jakec)OK, with the help of the SeamlessViewHandler at http://www.ilikespam.com/blog/faking-a-postback-with-jsf-facelets (with the addition of a call to initialize(context) at the beginning of buildView()), I have gotten much further. I believe I am actually getting to the page now, but I am getting lots of taglib exceptions.
14:14:30,578 ERROR [STDERR] Feb 8, 2008 2:14:30 PM com.sun.facelets.compiler.TagLibraryConfig loadImplicit SEVERE: Error Loading Library: jar:file:/C:/DevTools/jboss-4.2.2.GA/server/default/deploy/myApp.ear/lib/jsf-facelets.jar!/META-INF/jsf-core.taglib.xml java.io.IOException: Error parsing [jar:file:/C:/DevTools/jboss-4.2.2.GA/server/default/deploy/myApp.ear/lib/jsf-facelets.jar!/META-INF/jsf-core.taglib.xml]: at com.sun.facelets.compiler.TagLibraryConfig.create(TagLibraryConfig.java:410) at com.sun.facelets.compiler.TagLibraryConfig.loadImplicit(TagLibraryConfig.java:431) at com.sun.facelets.compiler.Compiler.initialize(Compiler.java:87) at com.sun.facelets.compiler.Compiler.compile(Compiler.java:104) at com.sun.facelets.impl.DefaultFaceletFactory.createFacelet(DefaultFaceletFactory.java:197) at com.sun.facelets.impl.DefaultFaceletFactory.getFacelet(DefaultFaceletFactory.java:144) at com.sun.facelets.impl.DefaultFaceletFactory.getFacelet(DefaultFaceletFactory.java:95) at com.sun.facelets.FaceletViewHandler.buildView(FaceletViewHandler.java:517) at com.medorder.myApp.ControllerBean$SeamlessViewHandler.buildView(ControllerBean.java:1172) at com.medorder.myApp.ControllerBean$SeamlessViewHandler.restoreView(ControllerBean.java:1153) at com.medorder.myApp.ControllerBean.getPageSource(ControllerBean.java:1086) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112) at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166) at org.jboss.seam.intercept.EJBInvocationContext.proceed(EJBInvocationContext.java:44) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56) at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:46) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.persistence.ManagedEntityIdentityInterceptor.aroundInvoke(ManagedEntityIdentityInterceptor.java:48) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.ConversationInterceptor.aroundInvoke(ConversationInterceptor.java:65) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.ConversationalInterceptor.aroundInvoke(ConversationalInterceptor.java:43) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.persistence.EntityManagerProxyInterceptor.aroundInvoke(EntityManagerProxyInterceptor.java:26) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.persistence.HibernateSessionProxyInterceptor.aroundInvoke(HibernateSessionProxyInterceptor.java:27) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107) at org.jboss.seam.intercept.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:50) at sun.reflect.GeneratedMethodAccessor416.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:118) at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor.invoke(ExtendedPersistenceContextPropagationInterceptor.java:57) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126) at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:83) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77) at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:206) at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:119) at $Proxy192.editSpecialist(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jboss.seam.util.Reflections.invoke(Reflections.java:21) at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31) at org.jboss.seam.intercept.ClientSideInterceptor$1.proceed(ClientSideInterceptor.java:76) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56) at org.jboss.seam.ejb.RemoveInterceptor.aroundInvoke(RemoveInterceptor.java:41) at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68) at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107) at org.jboss.seam.intercept.ClientSideInterceptor.invoke(ClientSideInterceptor.java:54) at org.javassist.tmp.java.lang.Object_$$_javassist_3.editSpecialist(Object_$$_javassist_3.java) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:329) at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:342) at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58) at org.jboss.el.parser.AstValue.invoke(AstValue.java:96) at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) at org.jboss.seam.core.Expressions$2.invoke(Expressions.java:173) at org.jboss.seam.navigation.Pages.callAction(Pages.java:636) 14:14:30,593 ERROR [STDERR] at org.jboss.seam.navigation.Pages.preRender(Pages.java:289) at org.jboss.seam.jsf.SeamPhaseListener.preRenderPage(SeamPhaseListener.java:549) at org.jboss.seam.jsf.SeamPhaseListener.beforeRenderResponse(SeamPhaseListener.java:460) at org.jboss.seam.jsf.SeamPhaseListener.beforeServletPhase(SeamPhaseListener.java:144) at org.jboss.seam.jsf.SeamPhaseListener.beforePhase(SeamPhaseListener.java:114) at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:222) at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83) at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69) at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69) at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69) at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141) at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281) at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69) at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69) at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68) at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69) at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446) at java.lang.Thread.run(Thread.java:619) Caused by: org.xml.sax.SAXException: Error Handling [jar:file:/C:/DevTools/jboss-4.2.2.GA/server/default/deploy/myApp.ear/lib/jsf-facelets.jar!/META-INF/jsf-core.taglib.xml@25,74] <library-class> at com.sun.facelets.compiler.TagLibraryConfig$LibraryHandler.endElement(TagLibraryConfig.java:271) at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source) at org.apache.xerces.impl.dtd.XMLDTDValidator.endNamespaceScope(Unknown Source) at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source) at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at javax.xml.parsers.SAXParser.parse(SAXParser.java:395) at javax.xml.parsers.SAXParser.parse(SAXParser.java:198) at com.sun.facelets.compiler.TagLibraryConfig.create(TagLibraryConfig.java:407) ... 134 more Caused by: java.lang.Exception: com.sun.facelets.tag.jsf.core.CoreLibrary must be an instance of com.sun.facelets.tag.TagLibrary at com.sun.facelets.compiler.TagLibraryConfig$LibraryHandler.createClass(TagLibraryConfig.java:293) at com.sun.facelets.compiler.TagLibraryConfig$LibraryHandler.processLibraryClass(TagLibraryConfig.java:339) at com.sun.facelets.compiler.TagLibraryConfig$LibraryHandler.endElement(TagLibraryConfig.java:171) ... 149 more
Then there was another one for the jsf-facelets.jar file in myApp.ear/myApp.war/WEB-INF/lib/jsf-facelets.jar.
Then there was a pair of those for jsf-html.taglib.xml, jsf-ui.taglib.xml, jstl-core.taglib.xml, and jstl-fn.taglib.xml.
seam-mail.taglib.xml and seam-pdf.taglib.xml loaded Ok from war/WEB-INF/lib/jboss-seam-mail.jar and war/WEB-INF/lib/jboss-seam-pdf.jar, respectively, but s.taglib.xml failed to load from jboss-seam-ui.jar because:Caused by: java.lang.Exception: org.jboss.seam.ui.handler.CommandButtonParameterComponentHandler must be an instance of com.sun.facelets.tag.TagHandler at com.sun.facelets.compiler.TagLibraryConfig$LibraryHandler.createClass(TagLibraryConfig.java:293) at com.sun.facelets.compiler.TagLibraryConfig$LibraryHandler.endElement(TagLibraryConfig.java:210) ... 149 more
The following also failed for similar "must be an instance of com.sun.facelets.tag.TagHandler" reasons:a4j.taglib.xml on org.ajax4jsf.taglib.html.facelets.AjaxListenerHandler ajax4jsf.taglib.xml on org.ajax4jsf.taglib.html.facelets.AjaxListenerHandler rich.taglib.xml on org.richfaces.taglib.DragSupportHandler richfaces.taglib.xml on org.richfaces.taglib.DragSupportHandler
Am I barking up the wrong tree? Shouldn't all this stuff already be initialized? When I first hit my web app, my log spits out a bunch of "loadImplicit" errors about each one of these taglibs, but no stackdump. Do I need to somehow change the classloader I'm working under? -
13. Re: How do I fetch the HTML source of a page programatically
dustismo Feb 8, 2008 6:19 PM (in response to jakec)Have you tried?:
String html = Renderer.instance().render(viewId);
I use it with great success, with one caveat -- it will not render custom tags that are not packaged. (see: http://jira.jboss.org/jira/browse/JBSEAM-1440). I don't think that is the taglib problem you are seeing.
Good luck, I'd be really interested if you come up with an improved solution.
-Dustin -
14. Re: How do I fetch the HTML source of a page programatically
jakec Feb 8, 2008 7:05 PM (in response to jakec)I KNEW it had to be something simple! It worked like a charm, thanks!