Chapter 10. Accessing User Profile

The following code retrieves the details for a logged-in user:

// Alternative context: WebuiRequestContext context = WebuiRequestContext.getCurrentInstance() ;
PortalRequestContext context = PortalRequestContext.getCurrentInstance() ;
// Get the id of the user logged
String userId = context.getRemoteUser();

// Retrieve OrganizationService but it works only from WebUI code. See variants below in documentation 
OrganizationService orgService = getApplicationComponent(OrganizationService.class) ;

// Request the information from OrganizationService:
if (userId != null)
  {
  User user = orgService.getUserHandler().findUserByName(userId) ;
  if (user != null)
  {
    String firstName = user.getFirstName();
    String lastName = user.getLastName();
    String email = user.getEmail();
  }
}

Below are two alternatives for retrieving the Organization Service:
  1. 
     OrganizationService service = (OrganizationService)
       ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(OrganizationService.class);
    
    
  2. 
    OrganizationService service = (OrganizationService)
       PortalContainer.getInstance().getComponentInstanceOfType(OrganizationService.class);
    
    
Both alternatives are probably better than OrganizationService orgService = getApplicationComponent(OrganizationService.class) because you can use them from your own portlets or servlet/portlet filters. The variant with getApplicationComponent works only from WebUI.