/** * This will prepare <code>jcrPath</code> to have a storage node for the unique id. Call this * method on initialization time when no concurrent access will hapen. */ public void initializePath(String jcrPath) throws IDException { jcrPath = removeJCRPrefix(jcrPath); Session session; try { session = login(); Item item = session.getItem(jcrPath); if (!item.isNode()) { throw new IDException("Path '" + jcrPath + "' is a property (should be a node)"); } else { // check if it has a subnode containing a unique id Node parent = (Node) item; if (!parent.hasNode(ID_NODE)) { // create the id node if it does not exist yet parent.addNode(ID_NODE, ID_NODE_TYPE); session.save(); } } session.logout(); } catch (LoginException e) { throw new IDException("Login to repository failed.", e); } catch (PathNotFoundException e) { throw new IDException("Repository path does not exist: " + jcrPath, e); } catch (RepositoryException e) { throw new IDException("Cannot lookup repository path: " + jcrPath, e); } }
/** * Clears {@link #next} and tries to fetch the next Node instance. When this method returns {@link * #next} refers to the next available node instance in this iterator. If {@link #next} is null * when this method returns, then there are no more valid element in this iterator. */ private void fetchNext() { // reset next = null; nextScore = 0; while (next == null && rows.hasNext()) { try { QueryResultRow row = (QueryResultRow) rows.next(); nextId = row.getNodeId(null); Item tmp = itemMgr.getItem(hierarchyMgr.getNodeEntry(nextId)); if (tmp.isNode()) { next = (Node) tmp; nextScore = row.getScore(null); } else { log.warn("Item with Id is not a Node: " + nextId); // try next invalid++; pos++; } } catch (Exception e) { log.warn("Exception retrieving Node with Id: " + nextId); // try next invalid++; pos++; } } pos++; }
/** * @param item * @return */ private boolean isFilteredNamespace(Item item) { try { return isFilteredNamespace(item.getName(), item.getSession()); } catch (RepositoryException e) { log.warn(e.getMessage()); } return false; }
@Override public void removeMessageAfterSent() throws Exception { final boolean stats = NotificationContextFactory.getInstance().getStatistics().isStatisticsEnabled(); boolean created = NotificationSessionManager.createSystemProvider(); SessionProvider sProvider = NotificationSessionManager.getSessionProvider(); try { Node notificationHome = getNotificationHomeNode(sProvider, workspace); Session session = notificationHome.getSession(); // remove all Set<String> listPaths = removeByCallBack.get(REMOVE_ALL); removeByCallBack.remove(REMOVE_ALL); if (listPaths != null && listPaths.size() > 0) { for (String nodePath : listPaths) { try { session.getItem(nodePath).remove(); // record entity delete here if (stats) { NotificationContextFactory.getInstance() .getStatisticsCollector() .deleteEntity(NTF_MESSAGE); } LOG.debug("Remove NotificationMessage " + nodePath); } catch (Exception e) { LOG.warn( "Failed to remove node of NotificationMessage " + nodePath + "\n" + e.getMessage()); LOG.debug("Remove NotificationMessage " + nodePath, e); } } session.save(); } listPaths = removeByCallBack.get(REMOVE_DAILY); if (listPaths != null && listPaths.size() > 0) { for (String nodePath : listPaths) { try { Item item = session.getItem(nodePath); if (item.isNode()) { Node node = (Node) item; node.setProperty(NTF_SEND_TO_DAILY, new String[] {""}); } LOG.debug("Remove SendToDaily property " + nodePath); } catch (Exception e) { LOG.warn( "Failed to remove SendToDaily property of " + nodePath + "\n" + e.getMessage()); LOG.debug("Remove SendToDaily property " + nodePath, e); } } session.save(); } } catch (Exception e) { LOG.warn("Failed to remove message after sent email notification", e); } finally { NotificationSessionManager.closeSessionProvider(created); } }
/** * Generates a document view export using a {@link * org.apache.jackrabbit.commons.xml.DocumentViewExporter} instance. * * @param path of the node to be exported * @param handler handler for the SAX events of the export * @param skipBinary whether binary values should be skipped * @param noRecurse whether to export just the identified node * @throws PathNotFoundException if a node at the given path does not exist * @throws SAXException if the SAX event handler failed * @throws RepositoryException if another error occurs */ public void exportDocumentView( String path, ContentHandler handler, boolean skipBinary, boolean noRecurse) throws PathNotFoundException, SAXException, RepositoryException { DocumentViewExporter exporter = new DocumentViewExporter(this, handler, skipBinary, noRecurse); Item item = getItem(path); if (item.isNode()) { exporter.export((JCRNodeWrapper) item); } else { throw new PathNotFoundException("XML export is not defined for properties: " + path); } }
/** * Generates a system view export using a {@link * org.apache.jackrabbit.commons.xml.SystemViewExporter} instance. * * @param path of the node to be exported * @param handler handler for the SAX events of the export * @param skipBinary whether binary values should be skipped * @param noRecurse whether to export just the identified node * @throws PathNotFoundException if a node at the given path does not exist * @throws SAXException if the SAX event handler failed * @throws RepositoryException if another error occurs */ public void exportSystemView( String path, ContentHandler handler, boolean skipBinary, boolean noRecurse) throws PathNotFoundException, SAXException, RepositoryException { // todo implement our own system view .. ? SystemViewExporter exporter = new SystemViewExporter(this, handler, !noRecurse, !skipBinary); Item item = getItem(path); if (item.isNode()) { exporter.export((JCRNodeWrapper) item); } else { throw new PathNotFoundException("XML export is not defined for properties: " + path); } }
/** * Gets the subnode for <code>jcrPath</code> that contains the current value of the highest id. * This is "thread-safe". */ private Node getLockableIDNode(Session session, String jcrPath) throws IDException { try { Item item = session.getItem(jcrPath); if (!item.isNode()) { throw new IDException("Path '" + jcrPath + "' is a property (should be a node)"); } else { return ((Node) item).getNode(ID_NODE); } } catch (PathNotFoundException e) { throw new IDException("Repository path does not exist: " + jcrPath, e); } catch (RepositoryException e) { throw new IDException("Cannot lookup repository path: " + jcrPath, e); } }
@Override public void removeModel(ModelId modelId) { try { ModelResource modelResource = getById(modelId); if (!modelResource.getReferencedBy().isEmpty()) { throw new ModelReferentialIntegrityException( "Cannot remove model because it is referenced by other model(s)", modelResource.getReferencedBy()); } Item item = session.getItem(modelId.getFullPath()); item.remove(); session.save(); } catch (RepositoryException e) { throw new FatalModelRepositoryException("Problem occured removing the model", e); } }
public Node getEditNode(String nodeType) throws Exception { Node uploadNode = getUploadedNode(); try { Item primaryItem = uploadNode.getPrimaryItem(); if (primaryItem == null || !primaryItem.isNode()) return uploadNode; if (primaryItem != null && primaryItem.isNode()) { Node primaryNode = (Node) primaryItem; if (primaryNode.isNodeType(nodeType)) return primaryNode; } } catch (Exception e) { if (LOG.isWarnEnabled()) { LOG.warn(e.getMessage()); } } return uploadNode; }
/** * Get an ItemLocation object by an item * * @param item the item * @return a ItemLocation object */ public static final ItemLocation getItemLocationByItem(final Item item) { Session session = null; try { session = item.getSession(); String repository = ((ManageableRepository) session.getRepository()).getConfiguration().getName(); String workspace = session.getWorkspace().getName(); String path = item.getPath(); String uuid = null; try { if (item instanceof Node) uuid = ((Node) item).getUUID(); } catch (Exception e) { // Do nothing} } boolean isSystemSession = IdentityConstants.SYSTEM.equals(session.getUserID()); return new ItemLocation(repository, workspace, path, uuid, isSystemSession); } catch (Exception e) { return null; } }
@Override public boolean hasPrivileges(final String absPath, final Privilege[] privileges) throws PathNotFoundException, RepositoryException { if (supportPrivileges) { // if the node is created in the same session, return true for (Item item : session.getNewItems()) { if (item.getPath().equals(absPath)) { return true; } } // check privilege names return hasPrivilegesLegacy(absPath, privileges); } else { // check ACLs Set<String> privs = new HashSet<>(); for (Privilege privilege : privileges) { privs.add(privilege.getName()); } String mountPoint = session.getRepository().getStoreProvider().getMountPoint(); Session securitySession = JCRSessionFactory.getInstance() .getCurrentSystemSession(session.getWorkspace().getName(), null, null); PathWrapper pathWrapper = new ExternalPathWrapperImpl( StringUtils.equals(absPath, "/") ? mountPoint : mountPoint + absPath, securitySession); return AccessManagerUtils.isGranted( pathWrapper, privs, securitySession, jahiaPrincipal, workspaceName, false, pathPermissionCache, compiledAcls, registry); } }
/** * @param item * @return */ private boolean isFilteredNodeType(Item item) { // shortcut if (nodetypeFilter.isEmpty()) { return false; } try { String ntName; if (item.isNode()) { // ntName = ((Node) item).getDefinition().getDeclaringNodeType().getName(); ntName = ((Node) item).getPrimaryNodeType().getName(); } else { ntName = ((Property) item).getDefinition().getDeclaringNodeType().getName(); } return nodetypeFilter.contains(ntName); } catch (RepositoryException e) { log.warn(e.getMessage()); } // nodetype info could not be retrieved return false; }
public JCRItemWrapper getItem(String path, final boolean checkVersion) throws PathNotFoundException, RepositoryException { if (sessionCacheByPath.containsKey(path)) { return sessionCacheByPath.get(path); } if (path.contains(DEREF_SEPARATOR)) { JCRNodeWrapper parent = (JCRNodeWrapper) getItem(StringUtils.substringBeforeLast(path, DEREF_SEPARATOR), checkVersion); return dereference(parent, StringUtils.substringAfterLast(path, DEREF_SEPARATOR)); } Map<String, JCRStoreProvider> dynamicMountPoints = sessionFactory.getDynamicMountPoints(); for (Map.Entry<String, JCRStoreProvider> mp : dynamicMountPoints.entrySet()) { if (path.startsWith(mp.getKey() + "/")) { String localPath = path.substring(mp.getKey().length()); JCRStoreProvider provider = mp.getValue(); Item item = getProviderSession(provider).getItem(provider.getRelativeRoot() + localPath); if (item.isNode()) { return provider.getNodeWrapper((Node) item, localPath, null, this); } else { return provider.getPropertyWrapper((Property) item, this); } } } Map<String, JCRStoreProvider> mountPoints = sessionFactory.getMountPoints(); for (Map.Entry<String, JCRStoreProvider> mp : mountPoints.entrySet()) { String key = mp.getKey(); if (key.equals("/") || path.equals(key) || path.startsWith(key + "/")) { String localPath = path; if (!key.equals("/")) { localPath = localPath.substring(key.length()); } JCRStoreProvider provider = mp.getValue(); if (localPath.equals("")) { localPath = "/"; } // Item item = getProviderSession(provider).getItem(localPath); Session session = getProviderSession(provider); if (session instanceof JahiaSessionImpl && getUser() != null && sessionFactory.getCurrentAliasedUser() != null && sessionFactory.getCurrentAliasedUser().equals(getUser())) { ((JahiaSessionImpl) session).toggleThisSessionAsAliased(); } Item item = session.getItem(provider.getRelativeRoot() + localPath); if (item.isNode()) { final Node node = (Node) item; JCRNodeWrapper wrapper = provider.getNodeWrapper(node, localPath, null, this); if (getUser() != null && sessionFactory.getCurrentAliasedUser() != null && !sessionFactory.getCurrentAliasedUser().equals(getUser())) { final JCRNodeWrapper finalWrapper = wrapper; JCRTemplate.getInstance() .doExecuteWithUserSession( sessionFactory.getCurrentAliasedUser().getUsername(), getWorkspace().getName(), getLocale(), new JCRCallback<Object>() { public Object doInJCR(JCRSessionWrapper session) throws RepositoryException { return session.getNodeByUUID(finalWrapper.getIdentifier(), checkVersion); } }); } if (checkVersion && (versionDate != null || versionLabel != null) && node.isNodeType("mix:versionable")) { wrapper = getFrozenVersionAsRegular(node, provider); } sessionCacheByPath.put(path, wrapper); sessionCacheByIdentifier.put(wrapper.getIdentifier(), wrapper); return wrapper; } else { return provider.getPropertyWrapper((Property) item, this); } } } throw new PathNotFoundException(path); }