public static void copy(ModifiableTraversableSource from, ModifiableTraversableSource to) throws IOException { if (!from.exists()) { throw new IOException("Cannot find source file/folder"); } if (from.isCollection()) { to.makeCollection(); Collection contents; try { contents = from.getChildren(); } catch (SourceException se) { throw new RuntimeException("Unable to list contents for collection " + from); } for (Iterator iter = contents.iterator(); iter.hasNext(); ) { ModifiableTraversableSource src = (ModifiableTraversableSource) iter.next(); SourceUtil.copy(src, resolve(to.getURI() + "/" + src.getName())); } } else { to = (ModifiableTraversableSource) resolve(to.getURI()); InputStream in = null; OutputStream out = null; try { in = from.getInputStream(); out = to.getOutputStream(); copy(in, out); } finally { if (out != null) out.close(); if (in != null) in.close(); } } }
/** * Initialization is only done, if the things to initialize are not yet existing, so it can be * called anytime to ensure the existence. */ private void initializeTalk(String teamspace, String teamspaceName) throws Exception { String talkDirPath = "/teamspaces/" + teamspace + "/talk/conversations"; // create sub directory ModifiableTraversableSource source; source = (ModifiableTraversableSource) this.sourceResolver.resolveURI(JCR_SCHEME + talkDirPath); if (!source.exists()) { source.makeCollection(); } // create id subnode this.uniqueIDGenerator.initializePath(talkDirPath); }
public String getUniqueTalkId(String path, String title) throws Exception { String teamspace = extractTeamspaceFromURI(path); initializeTalk(teamspace, teamspace); final String normalizedTitle = normalizeTitle(title); ModifiableTraversableSource source; source = (ModifiableTraversableSource) this.sourceResolver.resolveURI(path + normalizedTitle + "/meta.xml"); if (source.exists()) { long id = this.uniqueIDGenerator.getNextID(path); return normalizedTitle + "-" + id; } else { return normalizedTitle; } }
public static boolean remove(ModifiableTraversableSource resource) { boolean success = true; if (resource.isCollection()) { Collection contents; try { contents = resource.getChildren(); } catch (SourceException se) { throw new RuntimeException("Unable to list contents for collection " + resource); } for (Iterator iter = contents.iterator(); iter.hasNext(); ) { ModifiableTraversableSource element = (ModifiableTraversableSource) iter.next(); success = remove(element); } } try { resource.delete(); return success; } catch (SourceException e) { return false; } }
public void testTraverseDir() throws Exception { String text = "Look Ma, more data!"; ModifiableTraversableSource dir = (ModifiableTraversableSource) resolver.resolveURI("jcr://path/to/dir"); dir.makeCollection(); for (int i = 0; i < 10; i++) { ModifiableTraversableSource src = (ModifiableTraversableSource) dir.getChild("file" + i); write(src, text + i); } // Lookup dir again, and inspect children dir = (ModifiableTraversableSource) resolver.resolveURI("jcr://path/to/dir"); Collection children = dir.getChildren(); assertEquals(10, children.size()); for (int i = 0; i < 10; i++) { Source src = dir.getChild("file" + i); assertTrue(src.exists()); assertEquals(text + i, read(src)); } }
public static void save(Part part, ModifiableTraversableSource destination) throws Exception { InputStream in = null; OutputStream out = null; try { in = part.getInputStream(); out = destination.getOutputStream(); copy(in, out); } finally { if (out != null) { out.close(); } if (in != null) { in.close(); } } }
public void testDeleteDir() throws Exception { String text = "Wow, a lot of data going there"; ModifiableTraversableSource source = (ModifiableTraversableSource) resolver.resolveURI("jcr://and/again/a/deep/node"); assertFalse(source.exists()); write(source, text); // Lookup 'a' node source = (ModifiableTraversableSource) resolver.resolveURI("jcr://and/again/a/"); assertTrue(source.isCollection()); source.delete(); assertFalse(source.exists()); // Double check with a fresh source source = (ModifiableTraversableSource) resolver.resolveURI("jcr://and/again/a/"); assertFalse(source.exists()); // Check on children source = (ModifiableTraversableSource) resolver.resolveURI("jcr://and/again/a/deep/node"); assertFalse(source.exists()); }
public void testCrawlUp() throws Exception { String text = "Look Pa, some more!"; ModifiableTraversableSource src = (ModifiableTraversableSource) resolver.resolveURI("jcr://path/to/very/deep/content"); write(src, text); // Do a fresh lookup src = (ModifiableTraversableSource) resolver.resolveURI("jcr://path/to/very/deep/content"); ModifiableTraversableSource parent = (ModifiableTraversableSource) src.getParent(); assertTrue(parent.exists()); assertEquals("jcr://path/to/very/deep", parent.getURI()); parent = (ModifiableTraversableSource) parent.getParent(); assertTrue(parent.exists()); assertEquals("jcr://path/to/very", parent.getURI()); parent = (ModifiableTraversableSource) parent.getParent(); assertTrue(parent.exists()); assertEquals("jcr://path/to", parent.getURI()); parent = (ModifiableTraversableSource) parent.getParent(); assertTrue(parent.exists()); assertEquals("jcr://path", parent.getURI()); parent = (ModifiableTraversableSource) parent.getParent(); assertTrue(parent.exists()); assertEquals("jcr://", parent.getURI()); // Root node has no parent parent = (ModifiableTraversableSource) parent.getParent(); assertNull(parent); }