1
votes

If a portlet is a web application, then why portlet preferences are taken from ServletRequest (at request scope)?

http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/util/PortalUtil.html#getPreferences(javax.servlet.http.HttpServletRequest)

How is it possible to take preferences object at application scope?

UPDATE 1

I am obtaining portlet preferences in the following way. Is it correct? prefs member below is at application scope.

private synchronized void setupPreferences(HttpServletRequest request) {
    // creating preferences
    if( prefs == null ) {

        prefs = PortalUtil.getPreferences(request);
        if( prefs != null ) {
            System.out.println("Preferences were taken from request");
        }

        String portletResource = ParamUtil.getString(request, "portletResource");
        if (Validator.isNotNull(portletResource)) {
            try {
                System.out.println("Replacing preferences from 'portletRecource' parameter");
                prefs = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
            } catch (SystemException e) {
                e.printStackTrace();
            } catch (PortalException e) {
                e.printStackTrace();
            }
        }

        if( prefs == null ) {
            System.out.println("Preferences were NOT obtained yet");
        }
        else {
            System.out.println("Preferences are OK");
        }

    }
}

UPDATE 2

I am using also servlets and static objects in my application. Static objects are in application scope as one can know. I wonder can I keep portlet preferences object in some static object or I need to reobtain it from request each time?

Actually I already assumed this and it works but I wish to know is it correct?

2

2 Answers

6
votes
com.liferay.portal.util.PortalUtil.getPreferences(HttpServletRequest) 

is just a utility method.

If you take a look at implementation you'll see what is going on

public PortletPreferences getPreferences(HttpServletRequest request) {
    RenderRequest renderRequest = (RenderRequest)request.getAttribute(
        JavaConstants.JAVAX_PORTLET_REQUEST);

    PortletPreferences preferences = null;

    if (renderRequest != null) {
        PortletPreferencesWrapper preferencesWrapper =
            (PortletPreferencesWrapper)renderRequest.getPreferences();

        preferences = preferencesWrapper.getPreferencesImpl();
    }

    return preferences;
}

In your handlers you can/should use request.getPreferences(). "request" being implementation of PortletRequest, RenderRequest, ActionRequest, ResourceRequest.

UPDATW:

If you need to access specific portlet preferneces from servlet, you could try with

com.liferay.portlet.PortletPreferencesFactoryUtil.getPortletSetup(HttpServletRequest p_request, String p_portletId)

It is static method, and returns PortletPreferences, so knowing your portlet's id and having request object you should be able to get preferences.

1
votes

I'm guessing you're not subclassing Liferay's MVCPortlet class?

Anyway is the request object that you're passing to setupPreferences originally a PortletRequest object that you've gotten the HttpServletRequest out of?

If so you can just call PortletRequest.getPreferences();