PortletRequest Vaadin 7 and Liferay - liferay

In Vaadin 6,
you could override onRequestStart to obtain the PortletRequest object like so
#Override
public void onRequestStart(PortletRequest request, PortletResponse response)
In Vaadin 7, due to the portlet class change to com.vaadin.server.VaadinPortlet, there is no more onRequestStart to get the PortletReqeust object, just their new VaadinRequest object.
#Override
protected void init(VaadinRequest request)
Issue is getting this to a PortletRequest to be used. Anyone found a way to retrieve PortletRequest from Vaadin 7 and liferay?

Once you find the information that a VaadinRequest is both VaadinPortletRequest and VaadinServletRequest, you can retrieve PortletRequest and HttpServletRequest as so:
VaadinPortletRequest vprRequest = (VaadinPortletRequest) request;
PortletRequest pRequest = vprRequest.getPortletRequest();
VaadinServletRequest vsRequest = (VaadinServletRequest)request;
HttpServletRequest hsRequest = vsRequest.getHttpServletRequest();

This will do as well
PortletRequest currentPortlet = VaadinPortletService.getCurrentPortletRequest();

Related

Catch specific URL pattern and do something in application scope bean

I have JSF web app project, I need to catch only incoming specific url pattern and do something in application scope bean. What is the best approach? I don't want to change existing functionality of webapp.
You can register a filter that applies to an specific url pattern. And from the doFilter method call your application scoped bean.
Something like this:
#WebFilter("/yourpattern")
public class MyFilter extends HttpFilter {
#Inject
MyApplicationScopedBean myApplicationScopedBean;
#Override
protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
myApplicationScopedBean.doSomeThing();
super.doFilter(request, response, chain);
}
}

Not able to Get Liferay logged in UserId

I am trying to get liferay userId (I am using primefaces 6.2 in Liferay 7).
What I have tried so far is:
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
ThemeDisplay td =(ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
long userId = td.getUserId();
I am getting the below error:
ERROR [stderr] (default task-49) java.lang.ClassCastException: com.liferay.faces.bridge.ext.filter.internal.ResourceRequestBridgeLiferayImpl cannot be cast to javax.servlet.http.HttpServletRequest
I have searched the problem, but not able to find out a working solution. Any help would be highly appreciated.
Thanks in advance.
I also tried using PortletRequest. Below is the code.
PortletRequest request = (PortletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
ThemeDisplay td =(ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
User user = td.getUser();
But now ThemeDisplacy class is not found
java.lang.NoClassDefFoundError: com/liferay/portal/theme/ThemeDisplay: javax.el.ELException: java.lang.NoClassDefFoundError: com/liferay/portal/theme/ThemeDisplay
I am not sure where i am missing.
Thanks
To fetch the Liferay User object, you could use the following snippet:
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
User u = PortalUtil.getUser(portletRequest);
Please also read this article about the return value of ExternalContext.getRequest() - this might be the reason of your cast error.
The article further explains that: "the ExternalContext.getRequest() method returns an Object instead of an javax.servlet.http.HttpServletRequest. When this method is used in a portal, the Object can be cast to a javax.portlet.PortletRequest."

Primefaces component not respond after the session expired

I have a filter which checks the session. My filter:
public class FilterLogin implements Filter{
FilterConfig fc;
#Override
public void init(FilterConfig filterConfig) throws ServletException {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
fc = filterConfig;
}
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
HttpSession session = req.getSession(true);
if (session.getAttribute("loginMB") == null) {
resp.sendRedirect("/home.xhtml");
} else {
chain.doFilter(request, response);
}
}
#Override
public void destroy() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
web.xml:
<filter>
<filter-name>filter</filter-name>
<filter-class>pl.ePrzychodnia.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/protected/*</url-pattern>
</filter-mapping>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/home.xhtml</location>
</error-page>
When the session is expired I should go to the site home.xhtml. But when the session is expired and I want click the navigation menu in the page, the component is not reacting to the click...
When I do not use Primefaces everything works correctly. When I use primefaces in my project I have this error. What could be the cause?
I try use a global exception handler but i have a little problem. I copy class from this site http://wmarkito.wordpress.com/2012/04/05/adding-global-exception-handling-using-jsf-2-x-exceptionhandler/
and edited in class CustomExceptionHandler:
try {
//log error ?
log.log(Level.SEVERE, "Critical Exception!", t);
//redirect error page
requestMap.put("exceptionMessage", t.getMessage());
nav.handleNavigation(fc, null, "/home");
fc.renderResponse();
// remove the comment below if you want to report the error in a jsf error message
//JsfUtil.addErrorMessage(t.getMessage());
}
I change: nav.handleNavigation(fc, null, "/error"); to nav.handleNavigation(fc, null, "/home");
But when session timeout i not reditect to home.xhtml page only go to the page when I clicked and i have a example error:
SEVERE: Critical Exception!
javax.faces.application.ViewExpiredException: viewId:/protected/admin/about.xhtml - View /protected/admin/about.xhtml could not be restored.
when i clicked reference to about page when my session expired. I see a incomplete about.xhtml page instead home.xhtml
Most possibly, you make an ajax call to the managed bean. That is why error navigation does not work in web xml. The error will be send to JavaScript callback onerror if the action is invoked as ajax request.
You can add a global exception handler to application if you want to handle errors on server side. Adding global exception handling using JSF tutorial is a good one to follow.
Furthermore, you can use FullAjaxExceptionHandler feature of OmniFaces

iceface 2.0: How to retain value in icepush?

I would like to integrate ice push into my web application.
I have a page(placeinfo.jsf) shows information of a place and ui:include weather.jsf for weather information of the place.
Users access to the page at http://xxx.com/xxx/placeinfo.jsf?place=california.
Simple example of code,
placeinfo.jsf
1) ice:output value="placeInfoBean.population"
2) ice:output value="placeInfoBean.language"
3) ui:include src="./weather.xhtml"
weather.jsf
1) ice:output value="weatherBean.humidity"
2) ice:output value="weatherBean.visibility"
PlaceInfoBean.java
#ManagedBean(name="placeInfoBean")
#RequestScoped
public class PlaceInfoBean
{
String population;
String language;
#PostConstruct
public void init()
{
HttpServletRequest request= (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String place = request.getParameter("place");
setPopulation(PlaceInfoDao.getPopulation(place));
setLanguage(PlaceInfoDao.getlanguage(place));
}
}
WeatherBean.java
#ManagedBean(name="weatherBean")
#RequestScoped
public class WeatherBean
{
String humidity;
String visibility;
#PostConstruct
public void init()
{
HttpServletRequest request= (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String place = request.getParameter("place");
setHumidity(WeatherDao.getHumidity(place));
setVisibility(WeatherDao.getVisibility(place));
}
public WeatherBean()
{
PushRenderer.addCurrentSession("weather");
}
}
I have another page to update the weather, and the method calls
PushRenderer.render("weather");
WeatherBean actually did a refresh, postconstrust method run again, but found there is no request param of "place" which suppose to be "california", and the page doesn't work properly then.
Question:
1) May I know besides session, how can the page remembers the value before PushRenderer did something?
2) Is it a proper way to get the request param for WeatherBean?
or request param should be passed by ui:param from placeInfo.jsf?
How to get the value of ui:param in WeatherBean?
Thank you!
Have you tried to set the managed beans as #SessionScoped? Or #ApplicationScoped? Did it work?

Accessing a JSF managedBean from a Servlet [duplicate]

This question already has answers here:
Get JSF managed bean by name in any Servlet related class
(6 answers)
Closed 7 years ago.
I need to know what's the best method for accessing a JSF managedBean (which is defined having application scope) from a servlet.
Currently I have something like this in my servlet:
MyApplicationScopeBean bean = null;
try {
FacesContext fContext = FacesUtil.getFacesContext(req, resp);
ServletContext sc = (ServletContext) fContext.getExternalContext().getContext();
bean = (MyApplicationScopeBean) sc.getAttribute("myManagedBean");
} catch (Exception e) {
e.printStackTrace();
}
FacesUtil.java (as described in http://balusc.blogspot.com/2006/06/communication-in-jsf.html):
import javax.faces.FactoryFinder;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FacesUtil {
// Getters -----------------------------------------------------------------------------------
public static FacesContext getFacesContext(
HttpServletRequest request, HttpServletResponse response)
{
// Get current FacesContext.
FacesContext facesContext = FacesContext.getCurrentInstance();
// Check current FacesContext.
if (facesContext == null) {
// Create new Lifecycle.
LifecycleFactory lifecycleFactory = (LifecycleFactory)
FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
// Create new FacesContext.
FacesContextFactory contextFactory = (FacesContextFactory)
FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
facesContext = contextFactory.getFacesContext(
request.getSession().getServletContext(), request, response, lifecycle);
// Create new View.
UIViewRoot view = facesContext.getApplication().getViewHandler().createView(
facesContext, "");
facesContext.setViewRoot(view);
// Set current FacesContext.
FacesContextWrapper.setCurrentInstance(facesContext);
}
return facesContext;
}
// Helpers -----------------------------------------------------------------------------------
// Wrap the protected FacesContext.setCurrentInstance() in a inner class.
private static abstract class FacesContextWrapper extends FacesContext {
protected static void setCurrentInstance(FacesContext facesContext) {
FacesContext.setCurrentInstance(facesContext);
}
}
}
I always get a null when trying to access the bean from the servlet. What are your suggestions?
I'm running JSF 1.2 on Tomcat 6
Thanks for your help.
JSF stores application scoped managed beans just in the ServletContext. In servlets, the ServletContext is just available by the inherited getServletContext() method. You don't need to manually create a whole FacesContext around it. That's only an unnecessarily expensive task for this purpose.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Bean bean = (Bean) getServletContext().getAttribute("bean");
// ...
}
If it returns null, then it simply means that JSF hasn't kicked in yet to auto-create the bean for you (i.e. the servlet is called too early). You would then need to create and store it yourself. It will be used by JSF if the managed bean name (the attribute key) is the same.
if (bean == null) {
bean = new Bean();
getServletContext().setAttribute("bean", bean);
}
That said, what's the purpose of this servlet? Aren't you trying to achieve some functional requirement the wrong way?

Resources