Search in sources :

Example 1 with WebAppContext

use of org.jahia.data.applications.WebAppContext in project jahia by Jahia.

the class ServletContextManager method getApplicationContext.

// --------------------------------------------------------------------------
/**
 * Get an ApplicationContext for a given context
 *
 * @return the application context , null if not found
 * @param appBean the application bean against which we want the wepapp context
 * @throws org.jahia.exceptions.JahiaException on error
 */
public WebAppContext getApplicationContext(ApplicationBean appBean) throws JahiaException {
    if (appBean == null) {
        String errMsg = "Error getting application bean for application";
        logger.debug(errMsg);
        throw new JahiaException(errMsg, errMsg, JahiaException.ERROR_SEVERITY, JahiaException.APPLICATION_ERROR);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Requested for context : " + appBean.getContext());
    }
    if (appBean.getContext() == null) {
        return null;
    }
    WebAppContext appContext = mRegistry.get(appBean.getContext());
    if (appContext == null) {
        // try to load from disk
        appContext = loadContextInfoFromDisk(appBean.getID(), appBean.getContext());
        if (appContext == null) {
            // create a fake Application Context to avoid loading from disk the next time.
            appContext = new WebAppContext(appBean.getContext());
        }
        mRegistry.put(appBean.getContext(), appContext);
    }
    return appContext;
}
Also used : WebAppContext(org.jahia.data.applications.WebAppContext) JahiaException(org.jahia.exceptions.JahiaException)

Example 2 with WebAppContext

use of org.jahia.data.applications.WebAppContext in project jahia by Jahia.

the class ApplicationsManagerServiceImpl method addDefinition.

/**
 * Add a new Application Definition.
 * both in ApplicationsRegistry and in Persistance
 *
 * @param app the app Definition
 * @return false on error
 */
public boolean addDefinition(final ApplicationBean app) throws JahiaException {
    checkIsLoaded();
    if (app == null) {
        return false;
    }
    boolean ret = false;
    try {
        registerNamespace(getWebAppPrefix(app.getName()), getWebAppNamespaceURI(app.getName()));
        for (EntryPointDefinition entryPointDefinition : app.getEntryPointDefinitions()) {
            registerNamespace(getEntryPointPrefix(app.getName(), entryPointDefinition.getName()), getEntryPointNamespaceURI(app.getName(), entryPointDefinition.getName()));
        }
        ret = jcrTemplate.doExecuteWithSystemSession(new JCRCallback<Boolean>() {

            public Boolean doInJCR(JCRSessionWrapper session) throws RepositoryException {
                final JCRNodeWrapper parentNode = session.getNode("/portletdefinitions");
                final String name = Patterns.SLASH.matcher(app.getName()).replaceAll("___");
                session.checkout(parentNode);
                final JCRNodeWrapper wrapper = parentNode.addNode(name, "jnt:portletDefinition");
                wrapper.setProperty("j:context", app.getContext());
                wrapper.setProperty("j:name", app.getName());
                wrapper.setProperty("j:description", app.getDescription());
                wrapper.setProperty("j:type", app.getType());
                wrapper.setProperty("j:isVisible", app.isVisible());
                session.save();
                app.setID(wrapper.getIdentifier());
                // we must now create the corresponding permissions for web application roles as well as portlet
                // modes, and then assign them to default roles, possibly using some kind of configuration.
                String webappPath = WEBAPPS_PERMISSION_PATH + app.getName();
                // now let's add the web application roles as Jahia permissions, that we will map to Jahia roles
                // if a default mapping is provided in the service's configuration.
                String webappRolesPath = webappPath + "/" + getWebAppQualifiedNodeName(app.getName(), "roles");
                getOrCreatePermissionNode(webappRolesPath, session);
                session.save();
                try {
                    WebAppContext appContext = servletContextManager.getApplicationContext(app);
                    Iterator<String> updatedRoles = appContext.getRoles().iterator();
                    String webAppRoleName;
                    while (updatedRoles.hasNext()) {
                        webAppRoleName = updatedRoles.next();
                        JCRNodeWrapper permission = getOrCreatePermissionNode(webappRolesPath + "/" + getWebAppQualifiedNodeName(app.getName(), webAppRoleName), session);
                        session.save();
                        // if a mapping to a Jahia role is defined, let's add it.
                        if (defaultWebAppRoleMappings != null) {
                            String jahiaRolePath = defaultWebAppRoleMappings.get(webAppRoleName);
                            if (jahiaRolePath != null) {
                                grantPermissionToRole(permission, jahiaRolePath, session);
                            }
                        }
                    }
                } catch (JahiaException e) {
                    logger.error("Error while retrieving web application roles", e);
                }
                for (EntryPointDefinition entryPointDefinition : app.getEntryPointDefinitions()) {
                    String portletsPath = webappPath + "/" + getWebAppQualifiedNodeName(app.getName(), "portlets");
                    // let's add the default portlet permissions
                    String createInstancesPermPath = portletsPath + "/" + getPortletQualifiedNodeName(app.getName(), entryPointDefinition.getName(), "createInstance");
                    JCRNodeWrapper createInstancesPermNode = getOrCreatePermissionNode(createInstancesPermPath, session);
                    session.save();
                    if (defaultPortletPermissionMappings != null) {
                        String jahiaRolePath = defaultPortletPermissionMappings.get("createInstance");
                        grantPermissionToRole(createInstancesPermNode, jahiaRolePath, session);
                    }
                    String portletModePath = webappPath + "/" + getWebAppQualifiedNodeName(app.getName(), "portlets") + "/" + getPortletQualifiedNodeName(app.getName(), entryPointDefinition.getName(), "modes");
                    getOrCreatePermissionNode(portletModePath, session);
                    session.save();
                    for (PortletMode portletMode : entryPointDefinition.getPortletModes()) {
                        JCRNodeWrapper permission = getOrCreatePermissionNode(portletModePath + "/" + getPortletQualifiedNodeName(app.getName(), entryPointDefinition.getName(), portletMode.toString()), session);
                        session.save();
                        // if a mapping to a Jahia role is defined, let's add it.
                        if (defaultPortletModeMappings != null) {
                            String jahiaRolePath = defaultPortletModeMappings.get(portletMode.toString());
                            if (jahiaRolePath != null) {
                                grantPermissionToRole(permission, jahiaRolePath, session);
                            }
                        }
                    }
                }
                session.save();
                JahiaPrivilegeRegistry.init(session);
                return true;
            }
        });
    } catch (RepositoryException e) {
        logger.error(e.getMessage(), e);
    }
    putInApplicationCache(app);
    return ret;
}
Also used : EntryPointDefinition(org.jahia.data.applications.EntryPointDefinition) WebAppContext(org.jahia.data.applications.WebAppContext) JahiaException(org.jahia.exceptions.JahiaException) PortletMode(javax.portlet.PortletMode)

Example 3 with WebAppContext

use of org.jahia.data.applications.WebAppContext in project jahia by Jahia.

the class ApplicationsManagerServiceTest method testRetrievingApplicationDefinitions.

@Test
public void testRetrievingApplicationDefinitions() throws Exception {
    List<ApplicationBean> applicationBeans = applicationsManagerService.getApplications();
    assertFalse("Applications should not be empty", applicationBeans.isEmpty());
    for (ApplicationBean applicationBean : applicationBeans) {
        WebAppContext webAppContext = applicationsManagerService.getApplicationContext(applicationBean);
        ApplicationBean appFoundByContext = applicationsManagerService.getApplicationByContext(webAppContext.getContext());
        assertEquals(appFoundByContext.getID(), applicationBean.getID());
    }
}
Also used : WebAppContext(org.jahia.data.applications.WebAppContext) ApplicationBean(org.jahia.data.applications.ApplicationBean) Test(org.junit.Test)

Example 4 with WebAppContext

use of org.jahia.data.applications.WebAppContext in project jahia by Jahia.

the class ServletContextManager method loadContextInfoFromDisk.

// --------------------------------------------------------------------------
/**
 * Returns a ApplicationContext bean with the information loaded from the web.xml
 * file of a given context
 *
 * @param context , the context
 *
 * @return an ApplicationContext or null on error
 * @param applicationID  id of the application
 * @param filename the directory of the application to read from
 */
private WebAppContext loadContextInfoFromDisk(String applicationID, String context) {
    ServletContext dispatchedContext = mContext.getContext(context);
    if (dispatchedContext == null) {
        logger.error("Error getting dispatch context [" + context + "]");
        return null;
    }
    InputStream is = null;
    // extract data from the web.xml file
    WebAppContext appContext;
    Web_App_Xml webXmlDoc;
    try {
        is = dispatchedContext.getResourceAsStream(WEB_XML_FILE);
        webXmlDoc = Web_App_Xml.parse(is);
    } catch (Exception e) {
        logger.error("Error during loading of web.xml file for application " + context, e);
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }
    appContext = new WebAppContext(context, webXmlDoc.getDisplayName(), webXmlDoc.getdesc(), null, webXmlDoc.getServletMappings(), null, webXmlDoc.getWelcomeFiles());
    List<Servlet_Element> servlets = webXmlDoc.getServlets();
    Servlet_Element servlet;
    ServletBean servletBean;
    for (int i = 0; i < servlets.size(); i++) {
        servlet = servlets.get(i);
        servletBean = new ServletBean(applicationID, servlet.getType(), servlet.getDisplayName(), servlet.getName(), servlet.getSource(), context, servlet.getdesc());
        appContext.addServlet(servletBean);
    }
    List<Security_Role> roles = webXmlDoc.getRoles();
    Security_Role role;
    for (int i = 0; i < roles.size(); i++) {
        role = roles.get(i);
        appContext.addRole(role.getName());
    }
    return appContext;
}
Also used : WebAppContext(org.jahia.data.applications.WebAppContext) Web_App_Xml(org.jahia.data.webapps.Web_App_Xml) Servlet_Element(org.jahia.data.webapps.Servlet_Element) Security_Role(org.jahia.data.webapps.Security_Role) ServletBean(org.jahia.data.applications.ServletBean) InputStream(java.io.InputStream) ServletContext(javax.servlet.ServletContext) JahiaInitializationException(org.jahia.exceptions.JahiaInitializationException) JahiaException(org.jahia.exceptions.JahiaException)

Aggregations

WebAppContext (org.jahia.data.applications.WebAppContext)4 JahiaException (org.jahia.exceptions.JahiaException)3 InputStream (java.io.InputStream)1 PortletMode (javax.portlet.PortletMode)1 ServletContext (javax.servlet.ServletContext)1 ApplicationBean (org.jahia.data.applications.ApplicationBean)1 EntryPointDefinition (org.jahia.data.applications.EntryPointDefinition)1 ServletBean (org.jahia.data.applications.ServletBean)1 Security_Role (org.jahia.data.webapps.Security_Role)1 Servlet_Element (org.jahia.data.webapps.Servlet_Element)1 Web_App_Xml (org.jahia.data.webapps.Web_App_Xml)1 JahiaInitializationException (org.jahia.exceptions.JahiaInitializationException)1 Test (org.junit.Test)1