JSF lifecycle, how can response complete happen in the middle of the life cycle? - jsf

I am going through the article on JSF LifeCycles. I have the following doubt.
In many phases, the Response Complete is happening. How it is happening in
the middle of the life cycle. Can anyone explain it in detail?

Quoting from the JSF 2.0 spec:
The responseComplete() method, on the other hand, signals the JSF implementation that the HTTP response for this request has been completed by some means other than rendering the component tree, and that the request processing lifecycle for this request should be terminated when the current phase is complete. For example, an event listener that decided an HTTP redirect was required would perform the appropriate actions on the response object (i.e. calling ExternalContext.redirect()) and then call this method.
Calling ExternalContext.redirect(String) in a JSF servlet application causes a 302 response to be sent to the browser via the underlying API. In this case, it would be an error (or at least futile) to emit data in the response body, so ExternalContext.redirect(String) calls responseComplete().

If you are asking how it is happening I guess they do multi threading.
In case you are asking why it is happening, it is for the ajax requests or for repsonses that have to be submitted immediately

Related

is OAPageBean Class is act as a Controller?

Hi Every One.Now i started to learn OAF.I read some articles related
to OAF.In this articles they mention when user first sent a request
for OA.jsp page then that request received by OAPageBean class.As per
my knowledge if OAF follow MVC architecture then that request received
by Controller.So any one please clarify me whether OAPageBean is a
controller or bean and how it work when browser sent a request for jsp
page?
OAPageBean is not a controller. It is the main OA Framework page processing class.
The OAPageBean calls the processRequest method when a browser issues a GET request.
The OAPageBean then calls processFormData for pages that insert or update data.
The OAPageBean calls the processFormRequest method when a browser issues a POST request.

How to implement a (xmpp) listener in grails that renders a view

I am developing a grails application and am pretty new to grails at all. Right now I got stuck in implementing a listener that needs to render a view whenever it's called (Concrete: It is a listener for incoming xmpp messages of the Smack Library). I implemented this listener in a service which works fine so far, but now I need to render a view, when the listener gets called. So I tried to access the session or the controller on many different ways from inside of the listener, but all approaches lead to this error message:
No thread-bound request found: Are you referring to request attributes
outside of an actual web request , or processing a request outside the
originally receiving thread? If you are actually operating within a
web request and still receive this message, your code is probably
running outside of DispatcherServlet/DispatcherPortlet. In this case
use RequestContextListener or RequestContextFilter to expose the
current request.
In other code blocks of my service I was able to access the session, but not within my listener object:
def webRequest = RequestContextHolder.currentRequestAttributes()
webRequest.getSession()
Can anybody help me out here? Can I solve this using the RequestContextListener or RequestContextFilter like described in the error message and if yes, how do I do that? Or is there any other way to access the controller or the session from inside my listener?
Another approach would be to implemented this listener within the controller, but will this work and will I be able to access the session this way? Or will it also be a "no thread-bound request"?
Thank you very much for your answers in advance!

Post Form to a page in CQ5

I have a custom search component which searches for some parameter(s) from a dropdown [myParam] and displays the search results in another page. I currently use the default (GET) form
<form id="searchForm" action="/content/myWeb/searchResult.html" method="get" target="_blank">
In the result page, a component picks up the request params and processes the search.
I need to make it a POST submission so that the search parameters are NOT visible in the URL. But if I make it a method="Post" in the form above, I get this error:
Status
500
Message
javax.jcr.nodetype.ConstraintViolationException: no matching property definition found for {}myParam
Location /content/myWeb/searchResult
Parent Location /content/myWeb
Path
/path/to/search/page
That exception is the incidental way that Sling tells you that the servlet to which you are attempting to POST can not be found. What happens, in this case, is that Sling defaults to the SlingDefaultPostServlet, which attempts to to POST properties (represented by your form values) to the node /content/myWeb/searchResult. There's no way for Sling to say "I can't find a servlet that's registered to your request", so it just falls back to it's default behavior.
I'm assuming /content/myWeb/searchResult is a cq:Page node type. That node type is very restrictive, which is why it tells you that you cannot add properties that correspond to your form values.
This worked before, because your GET request to /content/myWeb/searchResult.html was able to resolve and execute. All GET requests to a page node can be served up by the system, inherently.
Now, since you are trying to do a POST, you need to create and register a new servlet that can handle this POST request. To do this, you'll need to create a SlingPostServlet and register it to your specific path (not recommended) or a specific selector/extension combination (recommended). That servlet should process the request parameters and respond with an HTML document.
A caveat...
What I just described will help you technically build what you are asking. That said, I don't agree with the premise that you should "make it a POST to hide the request parameters." The reason this is so much extra work, is because you are circumventing the principles of REST, which Sling is theoretically built to support. Your URL (via request path and parameters) should be communicating "I want the page at /content/myWeb/searchResult, given the criteria param1=x, param2=y, and so on". The GET with request params is an appropriately RESTful request.
I suggest you rethink what you're trying to do. Building a more complex solution around RESTful principles is not a good practice.
Just as a sidenote, you can always check if a given URL is bound to a servlet via the sling servlet resolver. Reachable via the OSGI-console or via URL:
http://localhost:4502/system/console/servletresolver
This can at least help you find closure on, if the servlet is registered to the given URL.
You can create a POST.jsp for your page, which could handle the POST request.
It is not restful to make get like request with POST, but sometimes it can be useful. Also With POST, dispatcher won't cache your request.

ServiceStack - OnEndRequest capturing Response body

I have a RequestLog feature completely decoupled from the application logic.
I capture the request/response in a pre request filter. To acomplish this, I instantiate a request scope object that keeps the request context. And before everything gets disposed (at AppHost's OnEndRequest handler), I write to the db. One line per http req.
I'm able to access the response code, the path, the method, request body, request headers, etc.
However the response stream is not available as it was already disposed. What's the logic behind this? Is it something like, IIS writes the stream content to the wire, and releases the resource imediately? Is there any way I can capture the Response body at the OnEndRequest handler?
Thanks
No, ServiceStack doesn't buffer the Response stream it gets written directly to the ASP.NET response.
You can add a Global Response Filter or Custom ServiceRunner to capture the Services response if the request reaches that far, but the request can be short-circuited at anytime throughout the request pipeline by closing the response.
I had the same issue with capturing the response in ServiceRequestLogger. Read this post - it is solved in 4.0.39+ (currently pre-release)
ServiceStack response filter "short circuiting" causing problems

CDI event triggered from MDB to update JSF page

Is it possible to fire a CDI event from an MDB? This MDB is monitoring a JMS queue and then a JSF page needs to be updated.
The issue that I'm experiencing is the JSF bean has a scope (was session, but trying request) and the MDB has no scope. The JSF bean contains the controller code (updating the page) and the #Observes annotation on a method parameter. Because the MDB is not in a 'context' and has no scope, the CDI event is never triggered on the JSF Bean.
How is it even possible then to update a JSF page based on a JMS (with MDB) event?
Disclaimer, I'm pretty sure of my what I'm claiming here but not 100%.
You can't do it with just MDB and CDI events. Typically a CDI event reuses the request thread and executes events synchronously after your method was invoked. That means that it's a potential bottleneck even if it worked. The second part is that it will find observers based on the request that spawned it and not find other "users" or whatever.
You can bridge this in several different ways and with several technologies but WebSockets / the Atmosphere framework is usually a solid way to get the message out there. Note that Primefaces Push exists and is based on Atmosphere.
I had a project recently (small) where I needed a plain tomcat and I could not use WebSockets so I brewed up a solution using CDI events.
It works like this:
Instead of using event.fire(new myEvent()) I went for something more transparent, ie I wanted it to show clearly that this event will be broadcasted to all sessions. So instead I created what I called SessionEventDelegator. It has a method called putEvent(Object object) (also other methods when you need Qualifiers). When you want to send events to all sessions you simply inject that and "put" your event. Maybe I should call it load since it will get fired later ;p
Later A JSF PhaseListener calls getEvents() and get all new events since the last time it checked. A timestamp functionality is in place so that you only get new events.
Now simply iterate over found events and use BeanManager.fire to get them out.
Since the PhaseListener will only trigger when the user makes a new request I use p:poll from primefaces ensuring that it polls for new events every third second.
A cleanup algorithm makes sure that events that everyone got is removed from the queue in SessionEventDelegator.
if you want the code let me know. To lazy to paste it if it doesn't sound right to you.
In case that CDI events won't work (I am not convinced about that because spec says that non-contextual objects can't observe events, there is nothing about firing them) but simplest workaround seems to use interceptor. MDBs can have interceptors so just intercept onMessage method and fire event from the interceptor.
Yes, you can fire CDI events from MDBs. The problem is that they're only going to be observed at the application level. If you're using push technology (e.g. Websockets) you can have this ApplicationScoped object receive the event, and then push to the appropriate client based on session id in websockets.
In RichFaces library there's a4j:push component that does exactly what you need. Check out the RichFaces Push section

Resources