掲示板

How to get custom user attributes with v6.2

10年前 に donino donino によって更新されました。

How to get custom user attributes with v6.2

Junior Member 投稿: 81 参加年月日: 11/07/02 最新の投稿
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
10年前 に James Falkner によって更新されました。

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

Liferay Legend 投稿: 1399 参加年月日: 10/09/17 最新の投稿
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.
10年前 に donino donino によって更新されました。

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

Junior Member 投稿: 81 参加年月日: 11/07/02 最新の投稿
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
9年前 に Traolly Xiong によって更新されました。

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

Regular Member 投稿: 195 参加年月日: 11/12/30 最新の投稿
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
9年前 に Olaf Kock によって更新されました。

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

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
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.
9年前 に Traolly Xiong によって更新されました。

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

Regular Member 投稿: 195 参加年月日: 11/12/30 最新の投稿
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
9年前 に Olaf Kock によって更新されました。

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

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
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?
9年前 に Traolly Xiong によって更新されました。

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

Regular Member 投稿: 195 参加年月日: 11/12/30 最新の投稿
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.
9年前 に Traolly Xiong によって更新されました。

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

Regular Member 投稿: 195 参加年月日: 11/12/30 最新の投稿
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
9年前 に Olaf Kock によって更新されました。

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

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
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.
9年前 に Traolly Xiong によって更新されました。

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

Regular Member 投稿: 195 参加年月日: 11/12/30 最新の投稿
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
9年前 に Balaji Murugesan によって更新されました。

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

Junior Member 投稿: 74 参加年月日: 10/11/10 最新の投稿
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
9年前 に Robin Nagpal によって更新されました。

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

Junior Member 投稿: 44 参加年月日: 14/11/18 最新の投稿
Use


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