Foren

WSRP UserContext -- Accessing Extensions[]

thumbnail
Andew Jardine, geändert vor 12 Jahren.

WSRP UserContext -- Accessing Extensions[]

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
I'm trying to get something to go with a WSRP implementation. What I have is a UserContext object (as defined by the OASIS standard). In addition to a handful of the default list of settings for a user, I have an extension that is defined. My extensions are all using the suggested String format meaning that I should be able to loop through the Extensions[] array that is on the UserContext object to try to find my attribute. Here is what I have --


for( Extension e : userContext.getExtensions()) 
{
    Deserializer ds = e.getDeserializer( mechType
                                   String.class, 
                                    new QName("urn:oasis:names:tc:wsrp:v2:types", "NamedString") );
    ...
    ...
}


The problem I have is that I don't understand what the first parameter is supposed to be. "mechType" which is of type String. Not the most descriptive. I have been scouring the web but haven't found anything to explain yet -- all the javadoc provides no explanation of the parameters.

Can anyone help?
thumbnail
Andew Jardine, geändert vor 12 Jahren.

RE: WSRP UserContext -- Accessing Extensions[]

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
I've managed to figure it out (I think -- just need to figure out how to test it emoticon ).

Liferay actually provides (THANKS LIFERAY!) a ExntesionUtil class under the com.liferay.wsrp.util package. I use that to retrieve an array of MessageElement object (you need axis.jar to resolve this class) and then loop through the elements to try to find the one I am looking for. No doubt there is a more elegant way to do this, but here is my code -- One of the parameters I pass to my method is the PortletRequest object.


	    HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(request);
        HttpServletRequest original = PortalUtil.getOriginalServletRequest(httpRequest);
        
        // retrieve the object from the header (where the UserCotnext is supposed to be stored)
        Object o = original.getHeader( USER_CONTEXT_OBJECT_ATTRIBUTE_NAME );
        
        // make sure what we pulled back is infact a User Context object.
        if ( o instanceof UserContext )
        {
        	logger.debug("User context object found in HTTP HEADERS.");
        	
        	// cast the geneeric to the WSRPv2 User Context Object
        	UserContext userContext = (UserContext) o;
        	
        	// set the real user id. We don't set the "user id" because impersonation
        	// does not occur when we are working with remote users.
        	mdexQueryLog.setRealUserId( userContext.getUserContextKey() );        	
        	logger.debug( "Found Real User ID in User Context object with value: " + userContext.getUserContextKey() );
        	
        	// get the message elements that make up the extensions.
        	MessageElement[] messageElements = ExtensionUtil.getMessageElements( userContext.getExtensions() );
        	logger.debug("Found " + messageElements.length + " extensions attached to user context.");
        	
        	// loop through the message elements and try to find the organization id.
        	for( MessageElement m : messageElements )
        	{
        		// if we matched the attribute we're looking for
        		if (m.getName().equalsIgnoreCase( CURRENT_ORGANIZATION_ID_ATTRIBUTE_NAME ))
        		{
        			logger.debug( "Found element with name " + CURRENT_ORGANIZATION_ID_ATTRIBUTE_NAME );
        			logger.debug( "Element value: " + m.getValue() );
        			
        			mdexQueryLog.setOrganizationId( m.getValue() );
        		}
        	}
                	
        }
        else
        {
        	// no object was found, or we found an object with an unexpected type.
        	if ( o == null )
        		logger.error("Expected to find User Context object in headers at key: " + USER_CONTEXT_OBJECT_ATTRIBUTE_NAME + " - but found no object." );
        	else
        		logger.warn("Expected to find User Context object in headers but found object of type " + o.getClass() + " instead.");
        }


Andew Jardine:
I'm trying to get something to go with a WSRP implementation. What I have is a UserContext object (as defined by the OASIS standard). In addition to a handful of the default list of settings for a user, I have an extension that is defined. My extensions are all using the suggested String format meaning that I should be able to loop through the Extensions[] array that is on the UserContext object to try to find my attribute. Here is what I have --


for( Extension e : userContext.getExtensions()) 
{
    Deserializer ds = e.getDeserializer( mechType
                                   String.class, 
                                    new QName("urn:oasis:names:tc:wsrp:v2:types", "NamedString") );
    ...
    ...
}


The problem I have is that I don't understand what the first parameter is supposed to be. "mechType" which is of type String. Not the most descriptive. I have been scouring the web but haven't found anything to explain yet -- all the javadoc provides no explanation of the parameters.

Can anyone help?