/** * <p>Encode the name to a valid JCR name. If the name is prefixed with a * namespace, the prefix is not encoded but returned as-is.</p> * <p>Calling encode multiple times on the same string will return * the same result as encoding the string once.</p> * <p>An IllegalArgumentException is thrown when the name is empty or null.</p> * @param name the name to encode * @return the encoded name */ public static final String encode(final String name) { return encode(name, false); }
private String getPropertyName(final String key) { return "hippolog:" + NodeNameCodec.encode(key); }
private static String newLinkName(final Node node, final String link) throws RepositoryException { if (!node.hasNode(link)) { return link; } int postfix = 1; while (true) { final String testLink = NodeNameCodec.encode(link + "_" + postfix, true); if (!node.hasNode(testLink)) { return testLink; } postfix++; } }
/** * Check for the existence of a node by name and return or create-and-return it, as necessary. * @param parent the parent node of the node we need * @param name the name of the node we need * @param type the JCR primary type of the node we need -- NOTE: existing nodes of any type will be accepted * @param encode iff true, encode the name of the node to make it safe for use as a JCR Name * @return the existing or new node of interest * @throws RepositoryException because everything in the JCR API throws this ... */ protected Node createNodeIfNecessary(Node parent, String name, String type, boolean encode) throws RepositoryException { if (encode) { name = NodeNameCodec.encode(name); } if (!parent.hasNode(name)) { parent.addNode(name, type); } return parent.getNode(name); }
public JcrTypeInfo(Session session, String type) throws RepositoryException { this.session = session; String prefix = "system"; if (type.indexOf(':') > 0) { prefix = type.substring(0, type.indexOf(':')); typeName = NodeNameCodec.encode(type.substring(type.indexOf(':') + 1)); } else { typeName = type; } nsInfo = new JcrNamespace(session, prefix); }
/** * Helper for building user path including the username itself. Takes care of the encoding * of the path AND the userId (the eventual node name) * @param rawUserId unencoded userId * @return the fully encoded normalized path */ private String buildUserPath(String rawUserId) { String userId = sanitizeId(rawUserId); if (dirLevels == 0) { return usersPath + "/" + NodeNameCodec.encode(userId, true); } int length = userId.length(); int pos = 0; StringBuilder path = new StringBuilder(usersPath); for (int i = 0; i < dirLevels; i++) { if (i < length) { pos = i; } path.append('/').append(NodeNameCodec.encode(Character.toLowerCase(userId.charAt(pos)))); } path.append('/').append(NodeNameCodec.encode(userId, true)); return path.toString(); }
private String getURLDecodedJcrEncodedName(final String name) { final String encoding = getEncoding(pageComposerContextService.getRequestContext()); try { final String urlDecodedName = URLDecoder.decode(name, encoding); return NodeNameCodec.encode(urlDecodedName); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(String.format("Could not ULR decode '%s'", name), e); } }
/** * Helper method for decoding a single uri part * @param part * @return the url decoded and jcr encoded part */ private static String decodePart(String part) { try { String name = URLDecoder.decode(part, "utf-8"); if (name.contains(SLASH_ENCODED)) { return NodeNameCodec.encode(name.replaceAll(SLASH_ENCODED, "/")); } else { return NodeNameCodec.encode(name); } } catch (UnsupportedEncodingException e) { Logger logger = HstServices.getLogger(LOGGER_CATEGORY_NAME); logger.error("Missing utf-8 codec?", e); return ""; } }
/** * Helper to get the JCR node within the baseline for a given module, with proper JCR path encoding. * @param baseline the root node of the baseline * @param module the module whose baseline node we want to find * @return the node representing the given module within the baseline * @throws RepositoryException because always with JCR ... */ private static Node getModuleNode(final Node baseline, final ModuleImpl module) throws RepositoryException { Node groupNode = baseline.getNode(NodeNameCodec.encode(module.getProject().getGroup().getName(), true)); Node projectNode = groupNode.getNode(NodeNameCodec.encode(module.getProject().getName(), true)); return projectNode.getNode(NodeNameCodec.encode(module.getName(), true)); }
protected Node getUserNode(Session session) throws RepositoryException { String userId = session.getUserID(); StringBuilder statement = new StringBuilder(); statement.append("//element"); statement.append("(*, ").append(HippoNodeType.NT_USER).append(")"); statement.append('[').append("fn:name() = ").append("'").append(NodeNameCodec.encode(userId, true)).append( "'").append(']'); Query q; q = session.getWorkspace().getQueryManager().createQuery(statement.toString(), Query.XPATH); QueryResult result = q.execute(); if (result.getNodes().hasNext()) { return result.getNodes().nextNode(); } return null; }
/** * Move the given node by appending it as the last child of the new parent and assigning it the give new node name. * * @param node the node to move * @param newNodeName the new name of the node * @param newParent the new parent of the node * @throws RepositoryException */ private void move(Node node, String newNodeName, Node newParent) throws RepositoryException { try { node.getSession().move(node.getPath(), newParent.getPath() + "/" + encode(newNodeName, true)); } catch (ItemExistsException e) { throw createClientException(newParent, newNodeName, e.getMessage()); } }
private Node createNode(String name, final String type) throws RepositoryException, WorkflowException { Node folder = rootSession.getNodeByIdentifier(subject.getIdentifier()); name = NodeNameCodec.encode(name); if (isSameNameSibling(name, folder)){ throw new WorkflowException(MessageFormat.format( "A node with name {0} already exists in folder {1}. Not allowed to create same-name siblings", name, folder.getPath())); } final Node handle = folder.addNode(name, NT_HANDLE); handle.addMixin(MIX_REFERENCEABLE); final Node document = handle.addNode(name, type); document.setProperty(HIPPO_AVAILABILITY, new String[] { "live", "preview" }); return document; }
/** * Create a new group * @throws RepositoryException */ public void create() throws RepositoryException { if (exists(getGroupname())) { throw new RepositoryException("Group already exists"); } // FIXME: should be delegated to a groupmanager StringBuilder relPath = new StringBuilder(); relPath.append(HippoNodeType.CONFIGURATION_PATH); relPath.append("/"); relPath.append(HippoNodeType.GROUPS_PATH); relPath.append("/"); relPath.append(NodeNameCodec.encode(getGroupname(), true)); node = ((UserSession) Session.get()).getRootNode().addNode(relPath.toString(), HippoNodeType.NT_GROUP); setOrRemoveStringProperty(node, PROP_DESCRIPTION, getDescription()); // save parent when adding a node node.getParent().getSession().save(); }
/** * Create a new user * @throws RepositoryException */ public void create() throws RepositoryException { if (userExists(getUsername())) { throw new RepositoryException("User already exists"); } // FIXME: should be delegated to a usermanager StringBuilder relPath = new StringBuilder(); relPath.append(HippoNodeType.CONFIGURATION_PATH); relPath.append("/"); relPath.append(HippoNodeType.USERS_PATH); relPath.append("/"); relPath.append(NodeNameCodec.encode(getUsername(), true)); node = ((UserSession) Session.get()).getRootNode().addNode(relPath.toString(), NT_USER); setOrRemoveStringProperty(node, PROP_EMAIL, getEmail()); setOrRemoveStringProperty(node, PROP_FIRSTNAME, getFirstName()); setOrRemoveStringProperty(node, PROP_LASTNAME, getLastName()); // save parent when adding a node node.getParent().getSession().save(); }
public Node create(Node parent, SiteMenuItemRepresentation newItem, Position position, String sibling) throws RepositoryException { lockHelper.acquireSimpleLock(getMenuAncestor(parent), 0); final String newItemName = newItem.getName(); try { final Node newChild = parent.addNode(encode(newItemName, true), NODETYPE_HST_SITEMENUITEM); repositionChildIfNecessary(newChild, position, sibling); update(newChild, newItem); return newChild; } catch (ItemExistsException e) { throw createClientException(parent, newItemName, e.getMessage()); } }
@Override public RichTextLink createLink(final Model<Node> targetModel) throws RichTextException { if (targetModel == null) { throw new IllegalArgumentException("Target is null"); } try { final Node targetNode = targetModel.get(); if (targetNode == null) { throw new RichTextException("Link target node does not exist"); } final String name = NodeNameCodec.encode(targetNode.getName()); final Node node = nodeModel.get(); FacetUtil.createFacet(node, name, targetNode); final String targetUuid = targetNode.getIdentifier(); return new RichTextLink(targetModel, targetUuid); } catch (final RepositoryException e) { throw new RichTextException("Could not create link", e); } }
private void convertLinkForRetrieval(final Tag tag, final FacetService facetService) { final String href = tag.getAttribute(ATTRIBUTE_HREF); if (StringUtils.isEmpty(href)) { return; } if (LinkUtil.isExternalLink(href)) { return; } final String name = NodeNameCodec.encode(href, true); final String uuid = facetService.getFacetId(name); if (uuid != null) { // set href to 'http://' so CKEditor handles it as a link instead of an anchor tag.addAttribute(ATTRIBUTE_HREF, LinkUtil.INTERNAL_LINK_DEFAULT_HREF); tag.addAttribute(ATTRIBUTE_DATA_UUID, uuid); facetService.markVisited(name); } }
public RichTextLink createLink(IDetachable targetId) throws RichTextException { JcrNodeModel targetModel = (JcrNodeModel) targetId; if (targetModel == null) { throw new IllegalArgumentException("Target is null"); } try { Node targetNode = targetModel.getNode(); if (targetNode == null) { throw new RichTextException("Node does not exist at " + targetModel.getItemModel().getPath()); } String name = NodeNameCodec.encode(targetNode.getName()); Node node = nodeModel.getNode(); name = RichTextFacetHelper.createFacet(node, name, targetNode); return new RichTextLink(targetModel, name); } catch (RepositoryException e) { throw new RichTextException("could not create link", e); } }
private void rename(Node node, String newName) throws RepositoryException { lockHelper.acquireSimpleLock(getMenuAncestor(node), 0); final Node parent = node.getParent(); // remember the next sibling name, to be able to restore the node's position final String nextSiblingName = getNextSiblingName(node, parent); // rename the node by moving it within the same parent move(node, newName, parent); // restore the position parent.orderBefore(encode(newName, true), nextSiblingName); }
/** * Move the source node into the parent at the given childIndex position. * * @param parent the target parent * @param source the target source * @param childIndex the index of child within parent * @throws RepositoryException */ public void move(Node parent, Node source, Integer childIndex) throws RepositoryException { lockHelper.acquireSimpleLock(getMenuAncestor(source), 0); final String sourceName = source.getName(); final String successorNodeName = getSuccessorOfSourceNodeName(parent, sourceName, childIndex); if (!source.getParent().isSame(parent)) { move(source, source.getName(), parent); } try { parent.orderBefore(encode(sourceName, true), successorNodeName); } catch (ItemExistsException e) { throw createClientException(parent, sourceName, e.getMessage()); } }