How to tell if you are in a Portal Server?


I am wondering what the best way (or at least a good way) of knowing when you are running in a portlet (Liferay in this case) vs just running as a servlet.

I am following some instructions from the Vaadin book about testing my portlet as a servlet first to make things easier… and it does. Now I want to add some portlet specific stuff but only if I am running in a portal.

In my searches I have seen that in Vaadin 6 you could go something like the following (and also found it in the Vaadin 6 book):

// Check that we are running as a portlet.
if (getContext() instanceof PortletApplicationContext2) {
     // We are in a portlet....

For Vaadin 7, possibly the book page is still to be written? Section 12.6. Portlet Context is still to be done and maybe this is where it would be covered?

Anyhow, I have code like the following:

if (request instanceof VaadinPortlet.VaadinLiferayRequest) {
    System.out.println("I am a Vaadin Liferay Request");
} else {
    System.out.println("I am not a Vaadin Liferay Request");

When running outside of a portlet I get a java.lang.NoClassDefFoundError: javax/portlet/filter/PortletRequestWrapper exception, which I guess I could catch, and then assume I am not running in a portlet…

Is there a better way to tell if I am running in a portlet?

Thanks for any help.

I came across some code that may be the solution… however it uses VaadinPortletSession which is depreciated, so still not sure on the recommended way… (I will also want to use porletSession.addPortletListener to handle changes between edit and view UI elements, so will need a way of doing this also…)

// Check that we are running as a portlet.
if (VaadinSession.getCurrent() instanceof VaadinPortletSession) {
    VaadinPortletSession portletSession = (VaadinPortletSession) VaadinSession.getCurrent();

    // Add a custom listener to handle action and render requests.
    portletSession.addPortletListener(this);"Adding portletListener" + VaadinSession.getCurrent());
    } else {"Was expecting VaadinSession to be VaadinPortletSession but was " + VaadinSession.getCurrent());