Search in sources :

Example 1 with PortletRequestDispatcher

use of javax.portlet.PortletRequestDispatcher in project acs-community-packaging by Alfresco.

the class AlfrescoFacesPortlet method handleError.

/**
 * Handles errors that occur during a render request
 */
private void handleError(RenderRequest request, RenderResponse response, Throwable error) throws PortletException, IOException {
    // get the error bean from the session and set the error that occurred.
    PortletSession session = request.getPortletSession();
    ErrorBean errorBean = (ErrorBean) session.getAttribute(ErrorBean.ERROR_BEAN_NAME, PortletSession.PORTLET_SCOPE);
    if (errorBean == null) {
        errorBean = new ErrorBean();
        session.setAttribute(ErrorBean.ERROR_BEAN_NAME, errorBean, PortletSession.PORTLET_SCOPE);
    }
    errorBean.setLastError(error);
    // if the faces context is available set the current view to the browse page
    // so that the error page goes back to the application (rather than going back
    // to the same page which just throws the error again meaning we can never leave
    // the error page)
    FacesContext context = FacesContext.getCurrentInstance();
    if (context != null) {
        ViewHandler viewHandler = context.getApplication().getViewHandler();
        // TODO: configure the portlet error return page
        UIViewRoot view = viewHandler.createView(context, FacesHelper.BROWSE_VIEW_ID);
        context.setViewRoot(view);
    }
    // get the error page and include that instead
    String errorPage = getErrorPage();
    if (logger.isDebugEnabled())
        logger.debug("An error has occurred, redirecting to error page: " + errorPage);
    response.setContentType("text/html");
    PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(errorPage);
    dispatcher.include(request, response);
}
Also used : PortletRequestDispatcher(javax.portlet.PortletRequestDispatcher) FacesContext(javax.faces.context.FacesContext) PortletSession(javax.portlet.PortletSession) ErrorBean(org.alfresco.web.bean.ErrorBean) ViewHandler(javax.faces.application.ViewHandler) UIViewRoot(javax.faces.component.UIViewRoot)

Example 2 with PortletRequestDispatcher

use of javax.portlet.PortletRequestDispatcher in project acs-community-packaging by Alfresco.

the class AlfrescoFacesPortlet method facesRender.

/**
 * @see org.apache.myfaces.portlet.MyFacesGenericPortlet#facesRender(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
 */
protected void facesRender(RenderRequest request, RenderResponse response) throws PortletException, IOException {
    Application.setInPortalServer(true);
    try {
        // Set the current locale
        I18NUtil.setLocale(getLanguage(request.getPortletSession()));
        if (request.getParameter(ERROR_OCCURRED) != null) {
            String errorPage = getErrorPage();
            if (logger.isDebugEnabled())
                logger.debug("An error has occurred, redirecting to error page: " + errorPage);
            response.setContentType("text/html");
            PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(errorPage);
            dispatcher.include(request, response);
        } else {
            WebApplicationContext ctx = (WebApplicationContext) getPortletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
            AuthenticationService auth = (AuthenticationService) ctx.getBean("AuthenticationService");
            // if we have no User object in the session then an HTTP Session timeout must have occured
            // use the viewId to check that we are not already on the login page
            PortletSession session = request.getPortletSession();
            String viewId = request.getParameter(VIEW_ID);
            // keep track of last view id so we can use it as return page from multi-part requests
            request.getPortletSession().setAttribute(SESSION_LAST_VIEW_ID, viewId);
            SessionUser sessionUser = (SessionUser) request.getPortletSession().getAttribute(AuthenticationHelper.AUTHENTICATION_USER, PortletSession.APPLICATION_SCOPE);
            User user = sessionUser instanceof User ? (User) sessionUser : null;
            if (user == null && (viewId == null || viewId.equals(getLoginPage()) == false)) {
                if (portalGuestAuthenticate(ctx, session, auth) != null) {
                    if (logger.isDebugEnabled())
                        logger.debug("Guest access successful.");
                    // perform the forward to the page processed by the Faces servlet
                    response.setContentType("text/html");
                    request.getPortletSession().setAttribute(PortletUtil.PORTLET_REQUEST_FLAG, "true");
                    // get the start location as configured by the web-client config
                    ConfigService configService = (ConfigService) ctx.getBean("webClientConfigService");
                    ClientConfigElement configElement = (ClientConfigElement) configService.getGlobalConfig().getConfigElement("client");
                    if (NavigationBean.LOCATION_MYALFRESCO.equals(configElement.getInitialLocation())) {
                        nonFacesRequest(request, response, "/jsp/dashboards/container.jsp");
                    } else {
                        nonFacesRequest(request, response, FacesHelper.BROWSE_VIEW_ID);
                    }
                } else {
                    if (logger.isDebugEnabled())
                        logger.debug("No valid User login, requesting login page. ViewId: " + viewId);
                    // set last used username as special session value used by the LoginBean
                    session.setAttribute(AuthenticationHelper.SESSION_USERNAME, request.getPreferences().getValue(PREF_ALF_USERNAME, null));
                    // login page is the default portal page
                    response.setContentType("text/html");
                    request.getPortletSession().setAttribute(PortletUtil.PORTLET_REQUEST_FLAG, "true");
                    nonFacesRequest(request, response);
                }
            } else {
                if (session.getAttribute(AuthenticationHelper.SESSION_INVALIDATED) != null) {
                    // remove the username preference value as explicit logout was requested by the user
                    if (request.getPreferences().isReadOnly(PREF_ALF_USERNAME) == false) {
                        request.getPreferences().reset(PREF_ALF_USERNAME);
                    }
                    session.removeAttribute(AuthenticationHelper.SESSION_INVALIDATED);
                }
                try {
                    if (user != null) {
                        if (logger.isDebugEnabled())
                            logger.debug("Validating ticket: " + user.getTicket());
                        // setup the authentication context
                        auth.validate(user.getTicket());
                    }
                    // do the normal JSF processing
                    super.facesRender(request, response);
                } catch (AuthenticationException authErr) {
                    // ticket is no longer valid!
                    if (logger.isDebugEnabled())
                        logger.debug("Invalid ticket, requesting login page.");
                    // remove User object as it's now useless
                    session.removeAttribute(AuthenticationHelper.AUTHENTICATION_USER, PortletSession.APPLICATION_SCOPE);
                    // login page is the default portal page
                    response.setContentType("text/html");
                    request.getPortletSession().setAttribute(PortletUtil.PORTLET_REQUEST_FLAG, "true");
                    nonFacesRequest(request, response);
                } catch (Throwable e) {
                    if (getErrorPage() != null) {
                        handleError(request, response, e);
                    } else {
                        logger.warn("No error page configured, re-throwing exception");
                        if (e instanceof PortletException) {
                            throw (PortletException) e;
                        } else if (e instanceof IOException) {
                            throw (IOException) e;
                        } else {
                            throw new PortletException(e);
                        }
                    }
                }
            }
        }
    } finally {
        Application.setInPortalServer(false);
    }
}
Also used : User(org.alfresco.web.bean.repository.User) SessionUser(org.alfresco.repo.SessionUser) AuthenticationException(org.alfresco.repo.security.authentication.AuthenticationException) PortletException(javax.portlet.PortletException) IOException(java.io.IOException) ClientConfigElement(org.alfresco.web.config.ClientConfigElement) WebApplicationContext(org.springframework.web.context.WebApplicationContext) PortletRequestDispatcher(javax.portlet.PortletRequestDispatcher) SessionUser(org.alfresco.repo.SessionUser) ConfigService(org.springframework.extensions.config.ConfigService) PortletSession(javax.portlet.PortletSession) AuthenticationService(org.alfresco.service.cmr.security.AuthenticationService)

Aggregations

PortletRequestDispatcher (javax.portlet.PortletRequestDispatcher)2 PortletSession (javax.portlet.PortletSession)2 IOException (java.io.IOException)1 ViewHandler (javax.faces.application.ViewHandler)1 UIViewRoot (javax.faces.component.UIViewRoot)1 FacesContext (javax.faces.context.FacesContext)1 PortletException (javax.portlet.PortletException)1 SessionUser (org.alfresco.repo.SessionUser)1 AuthenticationException (org.alfresco.repo.security.authentication.AuthenticationException)1 AuthenticationService (org.alfresco.service.cmr.security.AuthenticationService)1 ErrorBean (org.alfresco.web.bean.ErrorBean)1 User (org.alfresco.web.bean.repository.User)1 ClientConfigElement (org.alfresco.web.config.ClientConfigElement)1 ConfigService (org.springframework.extensions.config.ConfigService)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1