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;
}
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;
}
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());
}
}
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;
}
Aggregations