Foros de discusión

How to get custom user attributes with v6.2

donino donino, modificado hace 10 años.

How to get custom user attributes with v6.2

Junior Member Mensajes: 81 Fecha de incorporación: 2/07/11 Mensajes recientes
Hi,

I have developed a portlet application which runs very well with 5.x, 6.0x and 6.1x Liferay versions. I tried it with v6.2.0-ce-m6 and i noticed user attributes cannot be accessed anymore.

Here is how i retrieve custom attributes of the current user:

        Map liferayCustomAttributesMap=null;
        
        Object oThemeDisplay=request.getAttribute(WebKeys.THEME_DISPLAY);
        if (oThemeDisplay!=null){
            logger.log( Level.FINER, "THEME_DISPLAY found in Liferay request attributes with key:{0}",WebKeys.THEME_DISPLAY);
            liferayCustomAttributesMap = ((ThemeDisplay) oThemeDisplay).getUser().getExpandoBridge().getAttributes();
        }else{
            logger.log( Level.WARNING, "THEME_DISPLAY cannot be found in Liferay request attributes with key:{0}",WebKeys.THEME_DISPLAY);                    
        }


But with liferay 6.2.0-ce-m6,
request.getAttribute(WebKeys.THEME_DISPLAY)
always return null.
Is there a new way to do this? Or is it just because 6.2 is not yet officially released, if so is it a known bug or should i log it ?

Thanks
thumbnail
James Falkner, modificado hace 10 años.

RE: How to get custom user attributes with v6.2

Liferay Legend Mensajes: 1399 Fecha de incorporación: 17/09/10 Mensajes recientes
donino donino:
Hi,

I have developed a portlet application which runs very well with 5.x, 6.0x and 6.1x Liferay versions. I tried it with v6.2.0-ce-m6 and i noticed user attributes cannot be accessed anymore.

Here is how i retrieve custom attributes of the current user:

        Map liferayCustomAttributesMap=null;
        
        Object oThemeDisplay=request.getAttribute(WebKeys.THEME_DISPLAY);
        if (oThemeDisplay!=null){
            logger.log( Level.FINER, "THEME_DISPLAY found in Liferay request attributes with key:{0}",WebKeys.THEME_DISPLAY);
            liferayCustomAttributesMap = ((ThemeDisplay) oThemeDisplay).getUser().getExpandoBridge().getAttributes();
        }else{
            logger.log( Level.WARNING, "THEME_DISPLAY cannot be found in Liferay request attributes with key:{0}",WebKeys.THEME_DISPLAY);                    
        }


But with liferay 6.2.0-ce-m6,
request.getAttribute(WebKeys.THEME_DISPLAY)
always return null.
Is there a new way to do this? Or is it just because 6.2 is not yet officially released, if so is it a known bug or should i log it ?

Thanks


Did you recompile your plugin against the 6.2 Plugins SDK? WebKeys.THEME_DISPLAY is a constant whose value has changed in 6.2, so if you don't recompile, then you'll be using an old value for that constant.
donino donino, modificado hace 10 años.

RE: How to get custom user attributes with v6.2

Junior Member Mensajes: 81 Fecha de incorporación: 2/07/11 Mensajes recientes
Thanks a million James, that was the reason of this error!

Sorry for a such late response, i missed the notification of your answer.

Best regards
Traolly Xiong, modificado hace 9 años.

RE: How to get custom user attributes with v6.2

Regular Member Mensajes: 195 Fecha de incorporación: 30/12/11 Mensajes recientes
I created my portlet from a fresh 6.2 SDK and deployed it to a 6.2 LR instance and this NPE is still being thrown when using the code snippet below.

ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
String userName = themeDisplay.getUser().getScreenName();


Error:

8:08:29,021 ERROR [http-bio-80-exec-55][render_portlet_jsp:132] null
java.lang.NullPointerException
at com.gsa.portal.portlet.controller.GSASearchController.homeGSASearch(GSASearchController.java:114)
at javax.portlet.GenericPortlet.render(GenericPortlet.java:233)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:103)
at com.liferay.portlet.ScriptDataPortletFilter.doFilter(ScriptDataPortletFilter.java:55)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:100)

Any other ideas anyone?

Thanks.
thumbnail
Olaf Kock, modificado hace 9 años.

RE: How to get custom user attributes with v6.2

Liferay Legend Mensajes: 6403 Fecha de incorporación: 23/09/08 Mensajes recientes
The message (including render_portlet_jsp) sounds like you're doing this from a jsp - on a jsp request is the HttpServletRequest and you might rather want to use renderRequest, which is a PortletRequest and that's what Liferay initializes for you.
Traolly Xiong, modificado hace 9 años.

RE: How to get custom user attributes with v6.2

Regular Member Mensajes: 195 Fecha de incorporación: 30/12/11 Mensajes recientes
An ajax call from the jsp triggers this java class method below and does use the "RenderRequest" and "RenderResponse" objects.

jsp:

............
jQ.ajax({
type : "GET",
url : "${searchGSAURL}",
dataType : "xml",
cache: true,
data : {
gsa : gsa,
query : query,
client : client,
output : output,
sort : sort,
num : num,
site : site,
getfields : getfields,
filter : filter,
entqr : entqr,
ulang : ulang,
entqrm : entqrm,
oe : oe,
ie : ie,
ud : ud,
access : access,
rc : rc,
exclude_apps : exclude_apps,
start : start,
requiredfields : requiredfields,
p_act_as_client : 'true',
p_mimetype : 'text/xml'
},
success : parseXML,
complete : function() {

............

java class file:
.....

@RenderMapping
public String homeGSASearch(RenderRequest request, RenderResponse response,
ModelMap modelMap) {
System.out.println("homeGSASearch(x,y,z);");

try {
....................

if (StringUtils.isEmpty(outputXML)) {
// Fetch logged in user name
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
String userName = themeDisplay.getUser().getScreenName();
logger.info("user name: " + userName);
System.out.println("user name: " + userName);
.............

Any other ideas on why the ThemeDisplay is returning null?

Thanks.
thumbnail
Olaf Kock, modificado hace 9 años.

RE: How to get custom user attributes with v6.2

Liferay Legend Mensajes: 6403 Fecha de incorporación: 23/09/08 Mensajes recientes
It depends, what ${searchGSAURL} points to. If it's a resourceURL (as you mention Ajax there's a possibility for this), you'd rather have to check the resourceRequest than the renderRequest, because typically Ajax calls - if they target the serveResource phase - don't trigger a full render, but rather serve the full request on their own. The portal context, including ThemeDisplay, will be in the PortletRequest for that phase, namely resourceRequest.

You post the Java code for the render phase as well - is this triggered by your Ajax call?
Traolly Xiong, modificado hace 9 años.

RE: How to get custom user attributes with v6.2

Regular Member Mensajes: 195 Fecha de incorporación: 30/12/11 Mensajes recientes
Quick recap:

NOTE: From what I understand. When using AJAX in a portlet we need to use the @ResourceMapping; we shouldn't use the @ActionMapping / @RenderMapping. ActionRequest will always call one of the render method. @ActionMapping / @RenderMapping both will always refresh the page. If you use @ResourceMapping, it will NOT refresh the page and it doesn't require to call any of the render method.

JSP portlet that makes the ajax calls to Class methods:

- Default page load render methods maps to:

Class method:
@RenderMapping
public String homeGSASearch(RenderRequest request, RenderResponse response, ModelMap modelMap) {
............
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); // This line throws NULL pointer exception
.....
}

- When a post is submitted to attempt to get a xml file via ajax and there is no page reload, the resourceURL is

JSP: <portlet:resourceURL var="searchGSAURL" id="searchGSA"></portlet:resourceURL>

Class method:

@ResourceMapping(value = "searchGSA")
public void searchGSA(ResourceRequest request, ResourceResponse response, ModelMap modelMap) {
............
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); // This line throws NULL pointer exception
.....
}

In the logs you'll notice in both methods throw the NULL pointer exception along with the code line numbers.

ex)

8:08:29,021 ERROR [http-bio-80-exec-55][render_portlet_jsp:132] null
java.lang.NullPointerException
at com.gsa.portal.portlet.controller.GSASearchController.homeGSASearch(GSASearchController.java:114)
at javax.portlet.GenericPortlet.render(GenericPortlet.java:233)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:103)
at com.liferay.portlet.ScriptDataPortletFilter.doFilter(ScriptDataPortletFilter.java:55)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:100)


I tried using the user object and it gave me a NPE as well along with some serialization error.

User user = (User) request.getAttribute(WebKeys.USER);

I don't want to confuse anyone with more logs, so it's probably focus on the themedisplay object returning null when trying to get the
screen name.

Hope this helps.

Thanks.
Traolly Xiong, modificado hace 9 años.

RE: How to get custom user attributes with v6.2

Regular Member Mensajes: 195 Fecha de incorporación: 30/12/11 Mensajes recientes
Robin I must of posted my comment after yours. See my comment for both methods RenderMapping and ResourceMapping methods.

Let me know your thoughts a well.

Thanks.
thumbnail
Olaf Kock, modificado hace 9 años.

RE: How to get custom user attributes with v6.2

Liferay Legend Mensajes: 6403 Fecha de incorporación: 23/09/08 Mensajes recientes
Somehow this looks different than what I assumed before
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);      // This line throws NULL pointer exception

The only way that this line can throw a NullpointerException is when request is null. There is no other option. Either this isn't the line or I'd like to see how either of your request handler is called.

User user = (User) request.getAttribute(WebKeys.USER);


same here: The only way this line can throw a NullpointerException is if request is null. If request was valid, then the worst thing that this line would do is assign null to user - not throw an exception.

Please clarify what's going on. E.g. sprinkle some logging of request's actual value into your code - or just inspect the value of request in the debugger.
Traolly Xiong, modificado hace 9 años.

RE: How to get custom user attributes with v6.2

Regular Member Mensajes: 195 Fecha de incorporación: 30/12/11 Mensajes recientes
Hello Olaf,
After doing some more debugging, I discovered when building a war from a different checked out subversion sdk (different machine), the war deployed fine and the portlet did NOT throw any NPE errors at run time (ergghh). I wonder the compiler or java version had anything to do with it. I will update this post
as soon as I find out.

Thanks.
thumbnail
Balaji Murugesan, modificado hace 9 años.

RE: How to get custom user attributes with v6.2

Junior Member Mensajes: 74 Fecha de incorporación: 10/11/10 Mensajes recientes
Hi,

I am facing the similar issue on the Render phase of my custom portlet when tried to upgrade from LR 6.1.1CD to LR 6.2.2 CE.
My issue is this NPE is coming up on the page load itself and I am not doing any Ajax request that the issue could be related to wrong request object.

Any help on this issue?
thumbnail
Robin Nagpal, modificado hace 9 años.

RE: How to get custom user attributes with v6.2

Junior Member Mensajes: 44 Fecha de incorporación: 18/11/14 Mensajes recientes
Use


    @ResourceMapping(value = "homeGSASearch")
    public void homeGSASearch(ResourceRequest resourceRequest, ResourceResponse resourceResponse, ModelMap modelMap)  {
     ThemeDisplay themeDisplay =   (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);        
    
     }