public boolean isValidName(String name, String charset) { return name.equals(getValidatedName(name, charset)); }
private Node addWrite(String parentPath, String property, Node acls) throws PathNotFoundException, RepositoryException, AccessDeniedException { Node acl = acls.addNode(nodeNameHelper.getUniqueName(acls.getSession(), acls.getPath(), "0"), NodeTypes.ContentNode.NAME); acl.setProperty("path", parentPath + "/" + property); acl.setProperty("permissions", Permission.ALL); return acl; }
basename = createUniqueName(basename); fullName = basename + FilenameUtils.EXTENSION_SEPARATOR + extension; return getUniqueName(session, parentPath, name);
@Override public void execute(InstallContext installContext) throws TaskExecutionException { log.debug("Registering servlet {} in servlet filter.", servletDefinition.getName()); try { final Session session = installContext.getConfigJCRSession(); final Node servletNode = NodeUtil.createPath(session.getRootNode(), DEFAULT_SERVLET_FILTER_PATH + "/" + servletDefinition.getName(), NodeTypes.ContentNode.NAME); servletNode.setProperty("class", "info.magnolia.cms.filters.ServletDispatchingFilter"); servletNode.setProperty("enabled", true); servletNode.setProperty("servletClass", servletDefinition.getClassName()); servletNode.setProperty("servletName", servletDefinition.getName()); servletNode.setProperty("comment", servletDefinition.getComment()); final Node mappingsNode = servletNode.addNode("mappings", NodeTypes.ContentNode.NAME); for (String pattern : servletDefinition.getMappings()) { if (StringUtils.isBlank(pattern)) { installContext.warn("Empty mappings configuration is not supported and servlet was not installed."); continue; } String mappingNodeName = nodeNameHelper.getUniqueName(mappingsNode, nodeNameHelper.getValidatedName(pattern)); final Node mappingNode = mappingsNode.addNode(mappingNodeName, NodeTypes.ContentNode.NAME); mappingNode.setProperty("pattern", pattern); } final Node parametersNode = servletNode.addNode("parameters", NodeTypes.ContentNode.NAME); for (ServletParameterDefinition parameter : servletDefinition.getParams()) { parametersNode.setProperty(parameter.getName(), parameter.getValue()); } } catch (RepositoryException e) { log.error("Cannot create servlet node in servlet filter.", e); } }
@Before public void setUp() { // GIVEN nodeNameHelper = new NodeNameHelper(mock(MagnoliaConfigurationProperties.class)); session = new MockSession(RepositoryConstants.CONFIG); }
public String getUniqueName(Node parent, String name) throws RepositoryException { while (parent.hasNode(name) || parent.hasProperty(name)) { name = createUniqueName(name); } return name; }
public boolean isValidName(String name) { String charset = StringUtils.EMPTY; if (magnoliaConfigurationProperties.getBooleanProperty(MagnoliaConfigurationProperties.MAGNOLIA_UTF8_ENABLED)) { charset = StandardCharsets.UTF_8.name(); } return isValidName(name, charset); }
if (!isCharValid(ch, charset) || ch == 46) { newLabel.append("-"); } else { if (isCharValid(charCode, charset)) { newLabel.append(name.charAt(i)); } else {
@Before public void setUp() throws Exception { MagnoliaConfigurationProperties magnoliaConfigurationProperties = mock(MagnoliaConfigurationProperties.class); ComponentsTestUtil.setInstance(MagnoliaConfigurationProperties.class, magnoliaConfigurationProperties); nodeNameHelper = new NodeNameHelper(magnoliaConfigurationProperties); context = mock(InstallContext.class); ModuleDefinition moduleDefinition = mock(ModuleDefinition.class); session = new MockSession(RepositoryConstants.CONFIG); mappings = new HashMap<>(); mappings.put(NodeTypes.Folder.NAME, "expandNodeAction"); mappings.put("mgnl:customNodeType", "someAction"); task = new DefaultActionToDelegateActionTask("", "", "myApp", "mySubApp", mappings); when(context.getJCRSession(RepositoryConstants.CONFIG)).thenReturn(session); when(context.getCurrentModuleDefinition()).thenReturn(moduleDefinition); when(moduleDefinition.getName()).thenReturn("myModule"); }
/** * Checks if an item already exists with given name in JCR, and returns a unique name; * if necessary, an increment is appended to the name. * * <p>Given the following tree:</p> * <pre> * / * ├── a * ├── b * └── b0 * * NodeUtil.getUniqueName(session, "/", "a") = "a0" * NodeUtil.getUniqueName(session, "/", "b") = "b1" * NodeUtil.getUniqueName(session, "/", "b0") = "b1" * NodeUtil.getUniqueName(session, "/", "c") = "c" * </pre> */ public String getUniqueName(Session session, String parentPath, String name) throws RepositoryException { if (parentPath.equals("/")) { parentPath = StringUtils.EMPTY; } while (session.itemExists(parentPath + "/" + name)) { name = createUniqueName(name); } return name; }
/** * Check if the path *may be* a valid path before calling getItem, in order to avoid annoying logs. * * @param handle node handle * @return true if the path is invalid */ private boolean isJcrPathValid(String handle) { if (StringUtils.isBlank(handle) || StringUtils.equals(handle, "/")) { // empty path not allowed return false; } String[] jcrNames = handle.replaceFirst("/", "").split("/"); for (String jcrName : jcrNames) { if (!nodeNameHelper.isValidName(jcrName)) { return false; } } return true; }
/** * @deprecated since 5.5.3, use {@link NodeNameHelper#getUniqueName(Session, String, String, String)} */ public static String getUniqueLabel(Session session, String parentPath, String label, String extension) throws RepositoryException { return Components.getComponent(NodeNameHelper.class).getUniqueName(session, parentPath, label, extension); }
/** * Replace illegal characters based on magnolia property magnolia.ut8.enabled. * * @return validated label */ public String getValidatedName(String name) { String charset = StringUtils.EMPTY; if (magnoliaConfigurationProperties.getBooleanProperty(MagnoliaConfigurationProperties.MAGNOLIA_UTF8_ENABLED)) { charset = StandardCharsets.UTF_8.name(); } return getValidatedName(name, charset); }
@Before public void setUp() throws Exception { nodeNameHelper = new NodeNameHelper(mock(MagnoliaConfigurationProperties.class)); SecuritySupport securitySupport = mock(SecuritySupport.class); when(securitySupport.getGroupManager()).thenReturn(new MgnlGroupManager(nodeNameHelper)); when(securitySupport.getRoleManager()).thenReturn(new MgnlRoleManager(nodeNameHelper)); when(securitySupport.getUserManager()).thenReturn(userManager); ComponentsTestUtil.setInstance(SecuritySupport.class, securitySupport); userManager = createMgnlUserManager("test", RepositoryConstants.USERS); MockUtil.initMockContext(); MockUtil.createAndSetHierarchyManager(RepositoryConstants.USERS, getClass().getResourceAsStream("sample-users.properties")); MockUtil.createAndSetHierarchyManager(RepositoryConstants.USER_GROUPS, getClass().getResourceAsStream("sample-usergroups.properties")); MockUtil.createAndSetHierarchyManager(RepositoryConstants.USER_ROLES, getClass().getResourceAsStream("sample-userroles.properties")); final InstallContextImpl context = mock(InstallContextImpl.class); when(context.getStatus()).thenReturn(InstallStatus.installDone); ComponentsTestUtil.setInstance(InstallContextImpl.class, context); }
/** * @deprecated since 5.5.3, use {@link NodeNameHelper#getUniqueName(Session, String, String)} instead */ public static String getUniqueLabel(Session session, String parentPath, String label) throws RepositoryException { return Components.getComponent(NodeNameHelper.class).getUniqueName(session, parentPath, label); }
/** * @deprecated since 5.5.3, use {@link NodeNameHelper#getValidatedName(String, String)} */ public static String getValidatedLabel(String label, String charset) { return Components.getComponent(NodeNameHelper.class).getValidatedName(label, charset); }
@Before public void setUp() throws Exception { this.ctx = mock(SystemContext.class); nodeNameHelper = new NodeNameHelper(mock(MagnoliaConfigurationProperties.class)); SecuritySupport securitySupport = mock(SecuritySupport.class); Components.setComponentProvider(new MockComponentProvider()); ComponentsTestUtil.setInstance(SecuritySupport.class, securitySupport); this.userManager = new MgnlUserManager(nodeNameHelper); this.session = new MockSession(RepositoryConstants.USERS); when(ctx.getJCRSession(RepositoryConstants.USERS)).thenReturn(session); when(securitySupport.getUserManager()).thenReturn(userManager); ComponentsTestUtil.setInstance(SystemContext.class, ctx); MgnlContext.setInstance(ctx); final InstallContextImpl context = mock(InstallContextImpl.class); when(context.getStatus()).thenReturn(InstallStatus.installDone); ComponentsTestUtil.setInstance(InstallContextImpl.class, context); }
@Override public MgnlRole doExec(Session session) throws RepositoryException { String parentPath = StringUtils.defaultString(path, "/"); Node roleNode = session.getNode(parentPath).addNode(name, NodeTypes.Role.NAME); final Node acls = roleNode.addNode(NODE_ACLROLES, NodeTypes.ContentNode.NAME); // read only access to the role itself Node acl = acls.addNode(nodeNameHelper.getUniqueName(session, acls.getPath(), "0"), NodeTypes.ContentNode.NAME); acl.setProperty("path", roleNode.getPath()); acl.setProperty("permissions", Permission.READ); session.save(); return newRoleInstance(roleNode); }
/** * @deprecated since 5.5.3, use {@link NodeNameHelper#getValidatedName(String)} */ public static String getValidatedLabel(String label) { return Components.getComponent(NodeNameHelper.class).getValidatedName(label); }
@Before public void setUp() throws Exception { moduleDefinition = new ModuleDefinition(); moduleDefinition.setName("test"); final MockSession session = new MockSession(RepositoryConstants.CONFIG); servletsNode = NodeUtil.createPath(session.getRootNode(), "/server/filters/servlets", NodeTypes.Content.NAME); nodeNameHelper = new NodeNameHelper(new TestMagnoliaConfigurationProperties()); ComponentsTestUtil.setInstance(NodeNameHelper.class, nodeNameHelper); installContext = mock(InstallContext.class); when(installContext.getJCRSession(RepositoryConstants.CONFIG)).thenReturn(session); when(installContext.getConfigJCRSession()).thenReturn(session); when(installContext.getCurrentModuleDefinition()).thenReturn(moduleDefinition); }