public void setUpRepositoryConfiguration() throws RepositoryException, IOException { final JcrTools jcrTools = new JcrTools(true); final Session internalSession = sessions.getInternalSession(); try { jcrTools.registerNodeTypes(internalSession, "ldpath.cnd"); jcrTools.findOrCreateNode(internalSession, "/fedora:system/fedora:transform", "fedora:Configuration", "fedora:NodeTypeConfiguration"); final Node node = jcrTools.findOrCreateNode(internalSession, CONFIGURATION_FOLDER + "default", NT_FOLDER, NT_FOLDER); LOGGER.debug("Transforming node: {}", node.getPath()); jcrTools.uploadFile(internalSession, baseConfig.getPath(), getClass().getResourceAsStream( "/ldpath/default/nt_base_ldpath_program.txt"));
protected void printSubgraph( Node node ) throws RepositoryException { if (print) { JcrTools tools = new JcrTools(); tools.printSubgraph(node); } }
String path, InputStream stream ) throws RepositoryException, IOException { isNotNull(session, "session"); isNotNull(path, "path"); isNotNull(stream, "stream"); Node fileNode = null; boolean error = false; try { fileNode = findOrCreateNode(session.getRootNode(), path, "nt:folder", "nt:file"); Node contentNode = findOrCreateChild(fileNode, "jcr:content", "nt:resource"); Binary binary = session.getValueFactory().createBinary(stream); contentNode.setProperty("jcr:data", binary);
/** * Upload the content at the supplied URL into the repository at the defined path, using the given session. This method will * create a 'nt:file' node at the supplied path, and any non-existant ancestors with nodes of type 'nt:folder'. As defined by * the JCR specification, the binary content (and other properties) will be placed on a child of the 'nt:file' node named * 'jcr:content' with a node type of 'nt:resource'. * * @param session the JCR session * @param path the path to the file * @param contentUrl the URL where the content can be found * @return the newly created 'nt:file' node * @throws RepositoryException if there is a problem uploading the file * @throws IOException if there is a problem using the stream * @throws IllegalArgumentException is any of the parameters are null */ public Node uploadFile( Session session, String path, URL contentUrl ) throws RepositoryException, IOException { isNotNull(session, "session"); isNotNull(path, "path"); isNotNull(contentUrl, "contentUrl"); // Open the URL's stream first ... InputStream stream = contentUrl.openStream(); return uploadFile(session, path, stream); }
private Supplier<Boolean> addFilesToFolder(JcrRepository repository, CyclicBarrier cyclicBarrier, String clusterNodeId, List<String> fileNames) throws RepositoryException { JcrTools tools = new JcrTools(); JcrSession session = repository.login(); return () -> { try { fileNames.forEach(fileName -> { try { tools.uploadFile(session, "/folder/" + fileName, new ByteArrayInputStream(clusterNodeId.getBytes())); } catch (RepositoryException | IOException e) { throw new RuntimeException(e); } }); cyclicBarrier.await(); session.save(); return true; } catch (ItemExistsException ies) { return false; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { if (session != null) { session.logout(); } } }; }
/** * Get or create a node at the specified path. * * @param session the JCR session. may not be null * @param path the path of the desired node to be found or created. may not be null * @return the existing or newly created node * @throws RepositoryException * @throws IllegalArgumentException if either the session or path argument is null */ public Node findOrCreateNode( Session session, String path ) throws RepositoryException { return findOrCreateNode(session, path, null, null); }
JcrTools tools = new JcrTools(); try { tools.uploadFile(session, "/files/caution.png", getFile("caution.png")); tools.uploadFile(session, "/files/sample1.mp3", getFile("sample1.mp3")); tools.uploadFile(session, "/files/fixedWidthFile.txt", getFile("fixedWidthFile.txt")); tools.uploadFile(session, "/files/MySource.java", getFile("MySource.java")); if (print) tools.printSubgraph(png); if (print) tools.printSubgraph(sampleMp3); if (print) tools.printSubgraph(javaClass); if (print) tools.printSubgraph(textFile);
public static <T extends Object> T findFirst(Session session, String query, Map<String, String> bindParams, Class<T> type) { JcrTools tools = new JcrTools(); try { QueryResult result = query(session, query, bindParams); List<T> list = queryResultToList(result, 1, type); if (list != null && list.size() > 0) { return list.get(0); } else { return null; } } catch (RepositoryException e) { throw new MetadataRepositoryException("Unable to findFirst for query : " + query, e); } }
private void uploadFile( String filepath ) throws RepositoryException, IOException, InterruptedException { // this will create jcr:content of type nt:resource with the jcr:data property jcrTools.uploadFile(session, "/" + filepath, getResource(filepath)); session.save(); // wait a bit to make sure the text extraction has happened Thread.sleep(500); }
@Test @FixFor( "MODE-1978" ) public void shouldAllowRemovingArtifactWithNoExistingTrash() throws Exception { JcrTools tools = new JcrTools(); Node artifacts = tools.findOrCreateNode(session, "/sramp/artifacts", "nt:folder"); session.save(); String parentTrashPath = parentSrcPath.replace("sramp", "sramp-trash"); Node artifactTrash = tools.findOrCreateNode(session, parentTrashPath, "nt:folder"); print(artifactTrash);
tools.registerNodeTypes(sessA, "cnd/brix.cnd"); sessA.save(); InputStream brixWorkspace = resourceStream("io/brixWorkspace.xml"); sessA.save(); tools.registerNodeTypes(sessB, "cnd/brix.cnd"); sessB.save(); sessB.save(); tools.registerNodeTypes(sessC, "cnd/brix.cnd"); sessC.save(); new JcrTools().printSubgraph(sessA.getNode(root));
/** * Get or Create a node relative to the Parent Node and return the Wrapper JcrObject */ public static <T extends JcrObject> T getOrCreateNode(Node parentNode, String name, String nodeType, Class<T> type, Object... constructorArgs) { T entity = null; try { JcrTools tools = new JcrTools(); //if versionable checkout // if(isVersionable(parentNode)){ // JcrVersionUtil.checkout(parentNode); // } Node n = tools.findOrCreateChild(parentNode, name, nodeType); entity = createJcrObject(n, type, constructorArgs); //save ?? // JcrVersionUtil.checkinRecursively(n); // if(isVersionable(parentNode)){ // JcrVersionUtil.checkin(parentNode); // } } catch (AccessDeniedException e) { log.debug("Access denied", e); throw new AccessControlException(e.getMessage()); } catch (RepositoryException e) { throw new MetadataRepositoryException("Failed to retrieve the Node named" + name, e); } return entity; }
/** * Load the subgraph below this node, and print it to System.out if printing is enabled. * * @param node the root of the subgraph * @throws RepositoryException */ public void printSubgraph( Node node ) throws RepositoryException { printSubgraph(node, Integer.MAX_VALUE); }
/** * Get or create a node with the specified node under the specified parent node. * * @param parent the parent node. may not be null * @param name the name of the child node. may not be null * @return the existing or newly created child node * @throws RepositoryException * @throws IllegalArgumentException if either the parent or name argument is null */ public Node findOrCreateChild( Node parent, String name ) throws RepositoryException { return findOrCreateChild(parent, name, null); }
if (currentDepth > maxDepthOfSubgraph) return; if (lead == null) lead = ""; String nodeLead = lead + createString(' ', (currentDepth - 1) * 2); sb.append(" jcr:uuid=" + node.getIdentifier()); print(sb); sb.append(value.getBinary()); } else { sb.append(getStringValue(value, type)); sb.append(value.getBinary()); } else { sb.append(getStringValue(value, type)); print(sb); for (NodeIterator iter = node.getNodes(); iter.hasNext();) { Node child = iter.nextNode(); printSubgraph(child, lead, depthOfSubgraph, maxDepthOfSubgraph);
@FixFor("MODE-1139") @Test(expected = ConstraintViolationException.class) public void shouldThrowCorrectExceptionWhenImportingSystemViewContainingPropertiesThatViolateConstraints() throws Exception { // Register the node types ... tools.registerNodeTypes(session, "cars.cnd"); // Set up the repository ... assertImport("io/full-workspace-system-view-with-constraint-violation.xml", "/", ImportBehavior.THROW); }
tools.setDebug(print); if (print) { tools.printSubgraph(artifactA); tools.printSubgraph(artifactB); tools.printSubgraph(artifactC);
public void printNode(Session session, String absPath) { try { Node node = session.getNode(absPath); super.printNode(node); out.flush(); } catch (RepositoryException e) { e.printStackTrace(this.out); } }
public void testShouldVerifyNtFileNodesHavePrimaryItem() throws Exception { Session session1 = getHelper().getSuperuserSession(); // Create node structure Node root1 = getTestRoot(session1); Node folder1 = root1.addNode("folder1", "nt:folder"); String fileName = "simple.json"; String filePath = folder1.getPath() + "/" + fileName; new JcrTools().uploadFile(session1, filePath, getClass().getResourceAsStream("/data/" + fileName)); session1.save(); // Find the primary item ... Node file1 = folder1.getNode(fileName); Node content = file1.getNode("jcr:content"); assertNotNull(file1); Item primary = file1.getPrimaryItem(); assertThat(primary, is(sameInstance((Item)content))); // Change the primary type of the "jcr:content" node to "nt:unstructured" ... content.setPrimaryType("nt:unstructured"); session1.save(); // Find the primary item (again) ... Node content2 = file1.getNode("jcr:content"); assertNotNull(file1); Item primary2 = file1.getPrimaryItem(); assertThat(primary2, is(sameInstance((Item)content2))); }