Java Code Examples for javax.portlet.PortletRequest#getUserPrincipal()

The following examples show how to use javax.portlet.PortletRequest#getUserPrincipal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Gets the user profile.
 *
 * @return the user profile
 * @throws Exception
 *             the exception
 */
public static IEngUserProfile getUserProfile() throws Exception {
	RequestContainer aRequestContainer = RequestContainer.getRequestContainer();
	SessionContainer aSessionContainer = aRequestContainer.getSessionContainer();
	SessionContainer permanentSession = aSessionContainer.getPermanentContainer();

	IEngUserProfile userProfile = (IEngUserProfile) permanentSession.getAttribute(IEngUserProfile.ENG_USER_PROFILE);

	if (userProfile == null) {

		String userId = null;
		PortletRequest portletRequest = PortletUtilities.getPortletRequest();
		Principal principal = portletRequest.getUserPrincipal();
		userId = principal.getName();
		logger.debug("got userId from Principal=" + userId);

		userProfile = UserUtilities.getUserProfile(userId);

		logger.debug("User profile created. User id: " + (String) ((UserProfile) userProfile).getUserId());
		logger.debug("Attributes name of the user profile: " + userProfile.getUserAttributeNames());
		logger.debug("Functionalities of the user profile: " + userProfile.getFunctionalities());
		logger.debug("Roles of the user profile: " + userProfile.getRoles());

		permanentSession.setAttribute(IEngUserProfile.ENG_USER_PROFILE, userProfile);

		// String username = (String) userProfile.getUserUniqueIdentifier();
		String username = ((UserProfile) userProfile).getUserId().toString();
		if (!UserUtilities.userFunctionalityRootExists(username)) {
			UserUtilities.createUserFunctionalityRoot(userProfile);
		}

	}

	return userProfile;
}
 
Example 2
Source File: FrameworkPortlet.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Determine the username for the given request.
 * <p>The default implementation first tries the UserPrincipal.
 * If that does not exist, then it checks the USER_INFO map.
 * Can be overridden in subclasses.
 * @param request current portlet request
 * @return the username, or {@code null} if none found
 * @see javax.portlet.PortletRequest#getUserPrincipal()
 * @see javax.portlet.PortletRequest#getRemoteUser()
 * @see javax.portlet.PortletRequest#USER_INFO
 * @see #setUserinfoUsernameAttributes
 */
protected String getUsernameForRequest(PortletRequest request) {
	// Try the principal.
	Principal userPrincipal = request.getUserPrincipal();
	if (userPrincipal != null) {
		return userPrincipal.getName();
	}

	// Try the remote user name.
	String userName = request.getRemoteUser();
	if (userName != null) {
		return userName;
	}

	// Try the Portlet USER_INFO map.
	Map<?, ?> userInfo = (Map<?, ?>) request.getAttribute(PortletRequest.USER_INFO);
	if (userInfo != null) {
		for (int i = 0, n = this.userinfoUsernameAttributes.length; i < n; i++) {
			userName = (String) userInfo.get(this.userinfoUsernameAttributes[i]);
			if (userName != null) {
				return userName;
			}
		}
	}

	// Nothing worked...
	return null;
}
 
Example 3
Source File: PortletLoginAction.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
    * Service.
    * 
    * @param request
    *                the request
    * @param response
    *                the response
    * 
    * @throws Exception
    *                 the exception
    * 
    * @see it.eng.spago.dispatching.action.AbstractHttpAction#service(it.eng.spago.base.SourceBean,
    *      it.eng.spago.base.SourceBean)
    */
   public void service(SourceBean request, SourceBean response) throws Exception {
logger.debug("IN");

try {

    RequestContainer reqCont = getRequestContainer();
    SessionContainer sessionCont = reqCont.getSessionContainer();
    SessionContainer permSession = sessionCont.getPermanentContainer();

    IEngUserProfile profile = (IEngUserProfile) permSession.getAttribute(IEngUserProfile.ENG_USER_PROFILE);

    if (profile == null) {

	String userId = null;
	PortletRequest portletRequest = PortletUtilities.getPortletRequest();
	Principal principal = portletRequest.getUserPrincipal();
	if(principal != null) {
		userId = principal.getName();
	} else {
		logger.debug("Principal not found on request. Looking for a default user configuration.... ");
		String defatulUserSB = SingletonConfig.getInstance().getConfigValue("SPAGOBI.SECURITY.DEFAULT_USER");
		if(defatulUserSB != null) {
			userId = defatulUserSB;
			logger.debug("Default user configuration found = [" + userId + "]");
		} else 	{
			logger.error("No default user configuration found");
			throw new Exception("Cannot identify user");
		}
	}		
	
	logger.debug("got userId from Principal=" + userId);
	
	profile=UserUtilities.getUserProfile(userId);
	logger.debug("userProfile created.UserUniqueIDentifier= " + (String) profile.getUserUniqueIdentifier());
	logger.debug("Attributes name of the user profile: " + profile.getUserAttributeNames());
	logger.debug("Functionalities of the user profile: " + profile.getFunctionalities());
	logger.debug("Roles of the user profile: " + profile.getRoles());

	permSession.setAttribute(IEngUserProfile.ENG_USER_PROFILE, profile);
	// updates locale information on permanent container for Spago messages mechanism
	Locale locale = PortletUtilities.getLocaleForMessage();
	if (locale != null) {
		permSession.setAttribute(Constants.USER_LANGUAGE, locale.getLanguage());
		permSession.setAttribute(Constants.USER_COUNTRY, locale.getCountry());
	}
	
	//String username = (String) profile.getUserUniqueIdentifier();
	String username = (String)((UserProfile)profile).getUserId();
	if (!UserUtilities.userFunctionalityRootExists(username)) {
	    UserUtilities.createUserFunctionalityRoot(profile);
	}

    }

} catch (Exception e) {
    logger.error("Exception");
    throw new SecurityException("Exception", e);
} finally {
    logger.debug("OUT");
}

   }
 
Example 4
Source File: AnnotationMethodHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves standard method arguments. The default implementation handles {@link NativeWebRequest},
 * {@link ServletRequest}, {@link ServletResponse}, {@link HttpSession}, {@link Principal},
 * {@link Locale}, request {@link InputStream}, request {@link Reader}, response {@link OutputStream},
 * response {@link Writer}, and the given {@code thrownException}.
 * @param parameterType the method parameter type
 * @param webRequest the request
 * @param thrownException the exception thrown
 * @return the argument value, or {@link org.springframework.web.bind.support.WebArgumentResolver#UNRESOLVED}
 */
protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest,
		Exception thrownException) throws Exception {

	if (parameterType.isInstance(thrownException)) {
		return thrownException;
	}
	else if (WebRequest.class.isAssignableFrom(parameterType)) {
		return webRequest;
	}

	PortletRequest request = webRequest.getNativeRequest(PortletRequest.class);
	PortletResponse response = webRequest.getNativeResponse(PortletResponse.class);

	if (PortletRequest.class.isAssignableFrom(parameterType)) {
		return request;
	}
	else if (PortletResponse.class.isAssignableFrom(parameterType)) {
		return response;
	}
	else if (PortletSession.class.isAssignableFrom(parameterType)) {
		return request.getPortletSession();
	}
	else if (PortletPreferences.class.isAssignableFrom(parameterType)) {
		return request.getPreferences();
	}
	else if (PortletMode.class.isAssignableFrom(parameterType)) {
		return request.getPortletMode();
	}
	else if (WindowState.class.isAssignableFrom(parameterType)) {
		return request.getWindowState();
	}
	else if (PortalContext.class.isAssignableFrom(parameterType)) {
		return request.getPortalContext();
	}
	else if (Principal.class.isAssignableFrom(parameterType)) {
		return request.getUserPrincipal();
	}
	else if (Locale.class == parameterType) {
		return request.getLocale();
	}
	else if (InputStream.class.isAssignableFrom(parameterType)) {
		if (!(request instanceof ClientDataRequest)) {
			throw new IllegalStateException("InputStream can only get obtained for Action/ResourceRequest");
		}
		return ((ClientDataRequest) request).getPortletInputStream();
	}
	else if (Reader.class.isAssignableFrom(parameterType)) {
		if (!(request instanceof ClientDataRequest)) {
			throw new IllegalStateException("Reader can only get obtained for Action/ResourceRequest");
		}
		return ((ClientDataRequest) request).getReader();
	}
	else if (OutputStream.class.isAssignableFrom(parameterType)) {
		if (!(response instanceof MimeResponse)) {
			throw new IllegalStateException("OutputStream can only get obtained for Render/ResourceResponse");
		}
		return ((MimeResponse) response).getPortletOutputStream();
	}
	else if (Writer.class.isAssignableFrom(parameterType)) {
		if (!(response instanceof MimeResponse)) {
			throw new IllegalStateException("Writer can only get obtained for Render/ResourceResponse");
		}
		return ((MimeResponse) response).getWriter();
	}
	else if (Event.class == parameterType) {
		if (!(request instanceof EventRequest)) {
			throw new IllegalStateException("Event can only get obtained from EventRequest");
		}
		return ((EventRequest) request).getEvent();
	}
	else {
		return WebArgumentResolver.UNRESOLVED;
	}
}
 
Example 5
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest)
		throws Exception {

	PortletRequest request = webRequest.getNativeRequest(PortletRequest.class);
	PortletResponse response = webRequest.getNativeResponse(PortletResponse.class);

	if (PortletRequest.class.isAssignableFrom(parameterType) ||
			MultipartRequest.class.isAssignableFrom(parameterType)) {
		Object nativeRequest = webRequest.getNativeRequest(parameterType);
		if (nativeRequest == null) {
			throw new IllegalStateException(
					"Current request is not of type [" + parameterType.getName() + "]: " + request);
		}
		return nativeRequest;
	}
	else if (PortletResponse.class.isAssignableFrom(parameterType)) {
		Object nativeResponse = webRequest.getNativeResponse(parameterType);
		if (nativeResponse == null) {
			throw new IllegalStateException(
					"Current response is not of type [" + parameterType.getName() + "]: " + response);
		}
		return nativeResponse;
	}
	else if (PortletSession.class.isAssignableFrom(parameterType)) {
		return request.getPortletSession();
	}
	else if (PortletPreferences.class.isAssignableFrom(parameterType)) {
		return request.getPreferences();
	}
	else if (PortletMode.class.isAssignableFrom(parameterType)) {
		return request.getPortletMode();
	}
	else if (WindowState.class.isAssignableFrom(parameterType)) {
		return request.getWindowState();
	}
	else if (PortalContext.class.isAssignableFrom(parameterType)) {
		return request.getPortalContext();
	}
	else if (Principal.class.isAssignableFrom(parameterType)) {
		return request.getUserPrincipal();
	}
	else if (Locale.class == parameterType) {
		return request.getLocale();
	}
	else if (InputStream.class.isAssignableFrom(parameterType)) {
		if (!(request instanceof ClientDataRequest)) {
			throw new IllegalStateException("InputStream can only get obtained for Action/ResourceRequest");
		}
		return ((ClientDataRequest) request).getPortletInputStream();
	}
	else if (Reader.class.isAssignableFrom(parameterType)) {
		if (!(request instanceof ClientDataRequest)) {
			throw new IllegalStateException("Reader can only get obtained for Action/ResourceRequest");
		}
		return ((ClientDataRequest) request).getReader();
	}
	else if (OutputStream.class.isAssignableFrom(parameterType)) {
		if (!(response instanceof MimeResponse)) {
			throw new IllegalStateException("OutputStream can only get obtained for Render/ResourceResponse");
		}
		return ((MimeResponse) response).getPortletOutputStream();
	}
	else if (Writer.class.isAssignableFrom(parameterType)) {
		if (!(response instanceof MimeResponse)) {
			throw new IllegalStateException("Writer can only get obtained for Render/ResourceResponse");
		}
		return ((MimeResponse) response).getWriter();
	}
	else if (Event.class == parameterType) {
		if (!(request instanceof EventRequest)) {
			throw new IllegalStateException("Event can only get obtained from EventRequest");
		}
		return ((EventRequest) request).getEvent();
	}
	return super.resolveStandardArgument(parameterType, webRequest);
}