diff --git a/source/java/org/alfresco/web/app/Application.java b/source/java/org/alfresco/web/app/Application.java index 0908c914a7..85ed0fe6c4 100644 --- a/source/java/org/alfresco/web/app/Application.java +++ b/source/java/org/alfresco/web/app/Application.java @@ -26,17 +26,12 @@ import java.util.Properties; import java.util.ResourceBundle; import javax.faces.context.FacesContext; -import javax.portlet.PortletContext; -import javax.portlet.PortletSession; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import org.springframework.extensions.config.Config; -import org.springframework.extensions.config.ConfigService; -import org.springframework.extensions.surf.util.I18NUtil; import org.alfresco.repo.importer.ImporterBootstrap; import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.web.app.servlet.AuthenticationHelper; @@ -53,6 +48,10 @@ import org.alfresco.web.bean.wizard.WizardManager; import org.alfresco.web.config.ClientConfigElement; import org.alfresco.web.config.LanguagesConfigElement; import org.apache.commons.logging.Log; +import org.springframework.context.ApplicationContext; +import org.springframework.extensions.config.Config; +import org.springframework.extensions.config.ConfigService; +import org.springframework.extensions.surf.util.I18NUtil; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.jsf.FacesContextUtils; @@ -239,30 +238,7 @@ public class Application { return getErrorPage(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)); } - - /** - * Retrieves the configured error page for the application - * - * @param portletContext The portlet context - * @return The error page - */ - public static String getErrorPage(PortletContext portletContext) - { - return getErrorPage((WebApplicationContext)portletContext.getAttribute( - WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); - } - - /** - * Retrieves the configured login page for the application - * - * @param facesContext The faces context - * @return The configured login page or null if the configuration is missing - */ - public static String getLoginPage(FacesContext facesContext) - { - return getLoginPage(FacesContextUtils.getRequiredWebApplicationContext(facesContext)); - } - + /** * Retrieves the configured login page for the application * @@ -273,19 +249,7 @@ public class Application { return getLoginPage(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)); } - - /** - * Retrieves the configured login page for the application - * - * @param portletContext The portlet context - * @return The login page - */ - public static String getLoginPage(PortletContext portletContext) - { - return getLoginPage((WebApplicationContext)portletContext.getAttribute( - WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); - } - + /** * @return Returns the User object representing the currently logged in user */ @@ -728,19 +692,7 @@ public class Application else { // else get from web-client config - the first item in the configured list of languages - Config config = Application.getConfigService(session.getServletContext()).getConfig("Languages"); - LanguagesConfigElement langConfig = (LanguagesConfigElement)config.getConfigElement( - LanguagesConfigElement.CONFIG_ELEMENT_ID); - List languages = langConfig.getLanguages(); - if (languages != null && languages.size() != 0) - { - locale = I18NUtil.parseLocale(languages.get(0)); - } - else - { - // failing that, use the server default locale - locale = Locale.getDefault(); - } + locale = getLanguage(WebApplicationContextUtils.getRequiredWebApplicationContext(session.getServletContext())); } // save in user session session.setAttribute(LOCALE, locale); @@ -749,37 +701,28 @@ public class Application } /** - * Return the language Locale for the current user PortletSession. - * - * @param session PortletSession for the current user + * Return the configured language Locale for the application context * + * @param ctx + * the application context * @return Current language Locale set or the VM default if none set - never null */ - public static Locale getLanguage(PortletSession session) + public static Locale getLanguage(ApplicationContext ctx) { - Locale locale = (Locale)session.getAttribute(LOCALE); - if (locale == null) + // get from web-client config - the first item in the configured list of languages + Config config = ((ConfigService) ctx.getBean(Application.BEAN_CONFIG_SERVICE)).getConfig("Languages"); + LanguagesConfigElement langConfig = (LanguagesConfigElement) config + .getConfigElement(LanguagesConfigElement.CONFIG_ELEMENT_ID); + List languages = langConfig.getLanguages(); + if (languages != null && languages.size() != 0) { - // get from web-client config - the first item in the configured list of languages - WebApplicationContext ctx = (WebApplicationContext)session.getPortletContext().getAttribute( - WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); - Config config = ((ConfigService)ctx.getBean(Application.BEAN_CONFIG_SERVICE)).getConfig("Languages"); - LanguagesConfigElement langConfig = (LanguagesConfigElement)config.getConfigElement( - LanguagesConfigElement.CONFIG_ELEMENT_ID); - List languages = langConfig.getLanguages(); - if (languages != null && languages.size() != 0) - { - locale = I18NUtil.parseLocale(languages.get(0)); - } - else - { - // failing that, use the server default locale - locale = Locale.getDefault(); - } - // save in user session - session.setAttribute(LOCALE, locale); + return I18NUtil.parseLocale(languages.get(0)); + } + else + { + // failing that, use the server default locale + return Locale.getDefault(); } - return locale; } /** @@ -1213,7 +1156,7 @@ public class Application * @param context The Spring context * @return The configured error page or null if the configuration is missing */ - private static String getErrorPage(WebApplicationContext context) + public static String getErrorPage(ApplicationContext context) { String errorPage = null; @@ -1235,7 +1178,7 @@ public class Application * @param context The Spring contexr * @return The configured login page or null if the configuration is missing */ - private static String getLoginPage(WebApplicationContext context) + public static String getLoginPage(ApplicationContext context) { String loginPage = null; diff --git a/source/java/org/alfresco/web/app/portlet/AlfrescoFacesPortlet.java b/source/java/org/alfresco/web/app/portlet/AlfrescoFacesPortlet.java index c88cf5bce0..9df8ffea61 100644 --- a/source/java/org/alfresco/web/app/portlet/AlfrescoFacesPortlet.java +++ b/source/java/org/alfresco/web/app/portlet/AlfrescoFacesPortlet.java @@ -22,6 +22,8 @@ import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; +import java.util.Locale; +import java.util.Map; import javax.faces.application.ViewHandler; import javax.faces.component.UIViewRoot; @@ -29,21 +31,21 @@ import javax.faces.context.FacesContext; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletException; +import javax.portlet.PortletRequest; import javax.portlet.PortletRequestDispatcher; import javax.portlet.PortletSession; +import javax.portlet.PortletURL; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import javax.portlet.UnavailableException; +import javax.servlet.ServletRequest; -import org.springframework.extensions.config.ConfigService; -import org.springframework.extensions.surf.util.I18NUtil; import org.alfresco.repo.SessionUser; import org.alfresco.repo.security.authentication.AuthenticationException; import org.alfresco.service.cmr.security.AuthenticationService; import org.alfresco.util.TempFileProvider; import org.alfresco.web.app.Application; import org.alfresco.web.app.servlet.AuthenticationHelper; -import org.alfresco.web.app.servlet.AuthenticationStatus; import org.alfresco.web.app.servlet.FacesHelper; import org.alfresco.web.bean.ErrorBean; import org.alfresco.web.bean.FileUploadBean; @@ -58,6 +60,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.myfaces.portlet.MyFacesGenericPortlet; import org.apache.myfaces.portlet.PortletUtil; +import org.springframework.context.ApplicationContext; +import org.springframework.extensions.config.ConfigService; +import org.springframework.extensions.surf.util.I18NUtil; import org.springframework.web.context.WebApplicationContext; /** @@ -69,10 +74,10 @@ import org.springframework.web.context.WebApplicationContext; */ public class AlfrescoFacesPortlet extends MyFacesGenericPortlet { + private static final String ATTRIBUTE_LOCALE = "locale"; private static final String PREF_ALF_USERNAME = "_alfUserName"; private static final String SESSION_LAST_VIEW_ID = "_alfLastViewId"; - private static final String ERROR_PAGE_PARAM = "error-page"; private static final String ERROR_OCCURRED = "error-occurred"; private static Log logger = LogFactory.getLog(AlfrescoFacesPortlet.class); @@ -88,9 +93,9 @@ public class AlfrescoFacesPortlet extends MyFacesGenericPortlet throws PortletException, IOException { Application.setInPortalServer(true); - + // Set the current locale - I18NUtil.setLocale(Application.getLanguage(request.getPortletSession())); + I18NUtil.setLocale(getLanguage(request.getPortletSession())); boolean isMultipart = PortletFileUpload.isMultipartContent(request); @@ -239,11 +244,11 @@ public class AlfrescoFacesPortlet extends MyFacesGenericPortlet Application.setInPortalServer(true); // Set the current locale - I18NUtil.setLocale(Application.getLanguage(request.getPortletSession())); + I18NUtil.setLocale(getLanguage(request.getPortletSession())); if (request.getParameter(ERROR_OCCURRED) != null) { - String errorPage = Application.getErrorPage(getPortletContext()); + String errorPage = getErrorPage(); if (logger.isDebugEnabled()) logger.debug("An error has occurred, redirecting to error page: " + errorPage); @@ -268,7 +273,7 @@ public class AlfrescoFacesPortlet extends MyFacesGenericPortlet User user = sessionUser instanceof User ? (User)sessionUser : null; if (user == null && (viewId == null || viewId.equals(getLoginPage()) == false)) { - if (AuthenticationHelper.portalGuestAuthenticate(ctx, session, auth) == AuthenticationStatus.Guest) + if (portalGuestAuthenticate(ctx, session, auth) != null) { if (logger.isDebugEnabled()) logger.debug("Guest access successful."); @@ -392,6 +397,65 @@ public class AlfrescoFacesPortlet extends MyFacesGenericPortlet response.setRenderParameter(ERROR_OCCURRED, "true"); } + /** + * Gets the error bean from a request + * + * @param request + * the request + * @return the error bean + */ + public static ErrorBean getErrorBean(ServletRequest request) + { + PortletRequest portletReq = (PortletRequest) request.getAttribute("javax.portlet.request"); + if (portletReq != null) + { + PortletSession session = portletReq.getPortletSession(); + return (ErrorBean)session.getAttribute(ErrorBean.ERROR_BEAN_NAME); + } + return null; + } + + /** + * Creates a render URL from the given request and parameters + * + * @param request + * the request + * @param parameters + * the parameters + * @return the render url + */ + public static String getRenderURL(ServletRequest request, Map parameters) + { + RenderResponse renderResp = (RenderResponse) request.getAttribute("javax.portlet.response"); + if (renderResp == null) + { + throw new IllegalStateException("RenderResponse object is null"); + } + + PortletURL url = renderResp.createRenderURL(); + url.setParameters(parameters); + return url.toString(); + } + + /** + * Creates an action url from the given request. + * + * @param request + * the request + * @return the action url + */ + public static String getActionURL(ServletRequest request) + { + RenderResponse renderResp = (RenderResponse) request.getAttribute("javax.portlet.response"); + if (renderResp == null) + { + throw new IllegalStateException( + "RenderResponse object is null. The web application is not executing within a portal server!"); + } + return renderResp.createActionURL().toString(); + + } + /** * Handles errors that occur during a render request */ @@ -445,6 +509,51 @@ public class AlfrescoFacesPortlet extends MyFacesGenericPortlet } } + /** + * For no previous authentication or forced Guest - attempt Guest access + * + * @param ctx WebApplicationContext + * @param auth AuthenticationService + */ + private static User portalGuestAuthenticate(WebApplicationContext ctx, PortletSession session, AuthenticationService auth) + { + User user = AuthenticationHelper.portalGuestAuthenticate(ctx, session.getId(), auth); + + if (user != null) + { + // store the User object in the Session - the authentication servlet will then proceed + session.setAttribute(AuthenticationHelper.AUTHENTICATION_USER, user); + + // Set the current locale + I18NUtil.setLocale(getLanguage(session)); + + // remove the session invalidated flag + session.removeAttribute(AuthenticationHelper.SESSION_INVALIDATED); + } + return user; + } + + /** + * Return the language Locale for the current user Session. + * + * @param session PortletSession for the current user + * + * @return Current language Locale set or the VM default if none set - never null + */ + private static Locale getLanguage(PortletSession session) + { + Locale locale = (Locale)session.getAttribute(ATTRIBUTE_LOCALE); + if (locale == null) + { + locale = Application.getLanguage((ApplicationContext)session.getPortletContext().getAttribute( + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); + + // save in user session + session.setAttribute(ATTRIBUTE_LOCALE, locale); + } + return locale; + } + /** * @return Retrieves the configured login page */ @@ -452,7 +561,8 @@ public class AlfrescoFacesPortlet extends MyFacesGenericPortlet { if (this.loginPage == null) { - this.loginPage = Application.getLoginPage(getPortletContext()); + this.loginPage = Application.getLoginPage((ApplicationContext)getPortletContext().getAttribute( + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); } return this.loginPage; @@ -465,7 +575,8 @@ public class AlfrescoFacesPortlet extends MyFacesGenericPortlet { if (this.errorPage == null) { - this.errorPage = Application.getErrorPage(getPortletContext()); + this.errorPage = Application.getErrorPage((ApplicationContext)getPortletContext().getAttribute( + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); } return this.errorPage; diff --git a/source/java/org/alfresco/web/app/servlet/AuthenticationHelper.java b/source/java/org/alfresco/web/app/servlet/AuthenticationHelper.java index 95504372d6..a4ac802310 100644 --- a/source/java/org/alfresco/web/app/servlet/AuthenticationHelper.java +++ b/source/java/org/alfresco/web/app/servlet/AuthenticationHelper.java @@ -23,7 +23,6 @@ import java.io.UnsupportedEncodingException; import java.util.Enumeration; import javax.faces.context.FacesContext; -import javax.portlet.PortletSession; import javax.servlet.ServletContext; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -391,28 +390,18 @@ public final class AuthenticationHelper /** * For no previous authentication or forced Guest - attempt Guest access * - * @param ctx WebApplicationContext - * @param auth AuthenticationService + * @param ctx + * WebApplicationContext + * @param auth + * AuthenticationService */ - public static AuthenticationStatus portalGuestAuthenticate(WebApplicationContext ctx, PortletSession session, AuthenticationService auth) + public static User portalGuestAuthenticate(WebApplicationContext ctx, String sessionId, AuthenticationService auth) { try { auth.authenticateAsGuest(); - - User user = createUser(ctx, AuthenticationUtil.getGuestUserName(), auth.getCurrentTicket(session.getId())); - - // store the User object in the Session - the authentication servlet will then proceed - session.setAttribute(AuthenticationHelper.AUTHENTICATION_USER, user); - - // Set the current locale - I18NUtil.setLocale(Application.getLanguage(session)); - - // remove the session invalidated flag - session.removeAttribute(AuthenticationHelper.SESSION_INVALIDATED); - - // it is the responsibilty of the caller to handle the Guest return status - return AuthenticationStatus.Guest; + + return createUser(ctx, AuthenticationUtil.getGuestUserName(), auth.getCurrentTicket(sessionId)); } catch (AuthenticationException guestError) { @@ -421,8 +410,7 @@ public final class AuthenticationHelper catch (AccessDeniedException accessError) { // Guest is unable to access either properties on Person - AuthenticationService unprotAuthService = (AuthenticationService)ctx.getBean(UNPROTECTED_AUTH_SERVICE); - String sessionId = session.getId(); + AuthenticationService unprotAuthService = (AuthenticationService) ctx.getBean(UNPROTECTED_AUTH_SERVICE); unprotAuthService.invalidateTicket(unprotAuthService.getCurrentTicket(sessionId), sessionId); unprotAuthService.clearCurrentSecurityContext(); logger.warn("Unable to login as Guest: " + accessError.getMessage()); @@ -430,14 +418,12 @@ public final class AuthenticationHelper catch (Throwable e) { // Some other kind of serious failure to report - AuthenticationService unprotAuthService = (AuthenticationService)ctx.getBean(UNPROTECTED_AUTH_SERVICE); - String sessionId = session.getId(); + AuthenticationService unprotAuthService = (AuthenticationService) ctx.getBean(UNPROTECTED_AUTH_SERVICE); unprotAuthService.invalidateTicket(unprotAuthService.getCurrentTicket(sessionId), sessionId); unprotAuthService.clearCurrentSecurityContext(); throw new AlfrescoRuntimeException("Failed to authenticate as Guest user.", e); } - - return AuthenticationStatus.Failure; + return null; } /** diff --git a/source/java/org/alfresco/web/ui/repo/tag/SystemErrorTag.java b/source/java/org/alfresco/web/ui/repo/tag/SystemErrorTag.java index 0177d9b154..f4ee622ca5 100644 --- a/source/java/org/alfresco/web/ui/repo/tag/SystemErrorTag.java +++ b/source/java/org/alfresco/web/ui/repo/tag/SystemErrorTag.java @@ -20,17 +20,15 @@ package org.alfresco.web.ui.repo.tag; import java.io.IOException; import java.io.Writer; +import java.util.Collections; import java.util.ResourceBundle; -import javax.portlet.PortletSession; -import javax.portlet.PortletURL; -import javax.portlet.RenderRequest; -import javax.portlet.RenderResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; import org.alfresco.web.app.Application; +import org.alfresco.web.app.portlet.AlfrescoFacesPortlet; import org.alfresco.web.app.servlet.ExternalAccessServlet; import org.alfresco.web.bean.ErrorBean; @@ -113,12 +111,9 @@ public class SystemErrorTag extends TagSupport // get the error details from the bean, this may be in a portlet // session or a normal servlet session. ErrorBean errorBean = null; - RenderRequest renderReq = (RenderRequest)pageContext.getRequest(). - getAttribute("javax.portlet.request"); - if (renderReq != null) + if (Application.inPortalServer()) { - PortletSession session = renderReq.getPortletSession(); - errorBean = (ErrorBean)session.getAttribute(ErrorBean.ERROR_BEAN_NAME); + errorBean = AlfrescoFacesPortlet.getErrorBean(pageContext.getRequest()); } else { @@ -222,17 +217,13 @@ public class SystemErrorTag extends TagSupport if (Application.inPortalServer()) { - RenderResponse renderResp = (RenderResponse)pageContext.getRequest().getAttribute( - "javax.portlet.response"); - if (renderResp == null) - { - throw new IllegalStateException("RenderResponse object is null"); - } - - PortletURL url = renderResp.createRenderURL(); // NOTE: we don't have to specify the page for the portlet, just the VIEW_ID parameter // being present will cause the current JSF view to be re-displayed - url.setParameter("org.apache.myfaces.portlet.MyFacesGenericPortlet.VIEW_ID", "current-view"); + String url = AlfrescoFacesPortlet.getRenderURL(pageContext.getRequest(), Collections.singletonMap( + "org.apache.myfaces.portlet.MyFacesGenericPortlet.VIEW_ID", new String[] + { + "current-view" + })); out.write(url.toString()); } else diff --git a/source/java/org/alfresco/web/ui/repo/tag/UploadFormTag.java b/source/java/org/alfresco/web/ui/repo/tag/UploadFormTag.java index 54b9f02050..5f029100be 100644 --- a/source/java/org/alfresco/web/ui/repo/tag/UploadFormTag.java +++ b/source/java/org/alfresco/web/ui/repo/tag/UploadFormTag.java @@ -21,12 +21,12 @@ package org.alfresco.web.ui.repo.tag; import java.io.IOException; import java.io.Writer; -import javax.portlet.RenderResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; import org.alfresco.web.app.Application; +import org.alfresco.web.app.portlet.AlfrescoFacesPortlet; import org.alfresco.web.app.servlet.BaseServlet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -56,14 +56,7 @@ public class UploadFormTag extends TagSupport if (Application.inPortalServer()) { - RenderResponse renderResp = (RenderResponse)pageContext.getRequest(). - getAttribute("javax.portlet.response"); - if (renderResp == null) - { - throw new IllegalStateException("RenderResponse object is null. The web application is not executing within a portal server!"); - } - - out.write(renderResp.createActionURL().toString()); + out.write(AlfrescoFacesPortlet.getActionURL(pageContext.getRequest())); out.write("'>"); } else