Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Persist jcr/xml in hierarchy path structure.
  • Loading branch information
lsitu authored and Andrew Woods committed Aug 15, 2014
1 parent bce0d67 commit 9815690
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
Expand Up @@ -15,6 +15,7 @@
*/
package org.fcrepo.indexer.persistence;

import static org.apache.commons.lang.StringUtils.substringAfterLast;
import static org.fcrepo.indexer.Indexer.IndexerType.JCRXML_PERSISTENCE;
import static org.slf4j.LoggerFactory.getLogger;

Expand All @@ -25,10 +26,12 @@
import java.net.URLEncoder;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Callable;

import org.apache.commons.lang3.StringUtils;
import org.fcrepo.indexer.SynchIndexer;
import org.slf4j.Logger;

Expand Down Expand Up @@ -74,12 +77,37 @@ public Callable<File> updateSynch(final String id, final InputStream content) {

@Override
public File call() throws IOException {
// strip the http protocol and replace column(:) in front of the port number
String fullPath = id.substring(id.indexOf("//") + 2);
fullPath = StringUtils.substringBefore(fullPath, "/").replace(":", "/") +
"/" + StringUtils.substringAfter(fullPath, "/");
// URL encode the id
final String idPath = URLEncoder.encode(substringAfterLast(fullPath, "/"), "UTF-8");

// URL encode and build the file path
final String[] pathTokens = StringUtils.substringBeforeLast(fullPath, "/").split("/");
final StringBuilder pathBuilder = new StringBuilder();
for (final String token : pathTokens) {
if (StringUtils.isNotBlank(token)) {
pathBuilder.append(URLEncoder.encode(token, "UTF-8") + "/");
}
}

fullPath = pathBuilder.substring(0, pathBuilder.length() - 1).toString();

final Path dir = Paths.get(getPath(), fullPath);
if (Files.notExists(dir, LinkOption.NOFOLLOW_LINKS)) {
Files.createDirectories(Paths.get(getPath(), fullPath));
}

// file name with object identifier
final String fileName = URLEncoder.encode(id, "UTF-8") + "-jcr.xml";
final String fileName = idPath + ".jcr.xml";

LOGGER.debug("Updating {} to file: {}", id, getPath() + File.pathSeparatorChar + fileName);
// write content to disk
final Path p = Paths.get(getPath(), fileName);
final Path p = Paths.get(dir.toString(), fileName);

LOGGER.debug("Updating {} to file: {}", fullPath, p.toAbsolutePath().toString());

Files.copy(content, p, new CopyOption[]{});
return p.toFile();
}
Expand All @@ -104,4 +132,4 @@ public Callable<File> removeSynch(final String id) {
public IndexerType getIndexerType() {
return JCRXML_PERSISTENCE;
}
}
}
Expand Up @@ -26,6 +26,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.concurrent.ExecutionException;

import org.junit.Before;
Expand Down Expand Up @@ -68,7 +69,7 @@ public void updateTest() throws IOException, InterruptedException, ExecutionExce
final String testId = "updateTest" + randomUUID();
final InputStream input = new ByteArrayInputStream (testContent.getBytes());

final File f = indexer.update(testId, input).get();
final File f = indexer.update("http://localhost:8080/" + testId, input).get();

// file should exist
LOGGER.debug("Got filename: {}", f.getName());
Expand All @@ -80,15 +81,62 @@ public void updateTest() throws IOException, InterruptedException, ExecutionExce
}

@Test
public void removeTest() throws IOException, InterruptedException, ExecutionException {
final String testId = "removeTest" + randomUUID();
public void updateWithHierarchyPathTest()
throws IOException, InterruptedException, ExecutionException {
final String path1 = "updateHier" + randomUUID();
final String path2 = "" + randomUUID();
final String testId = "http://localhost:8080/" + path1 + "/" + path2;
final InputStream input = new ByteArrayInputStream (testContent.getBytes());

final File f = indexer.update(testId, input).get();

// file should exist
LOGGER.debug("Got filename: {}", f.getName());
assertTrue("Filename doesn't match", f.getName().startsWith(path2));
assertTrue("Path Path1 doesn't match", f.getParentFile().getName().equals(path1));
assertTrue("Path port number doesn't match",
f.getParentFile().getParentFile().getName().equals("8080"));
assertTrue("Path hostname doesn't match",
f.getParentFile().getParentFile().getParentFile().getName().equals("localhost"));

// content should be 'test content'
final String content = new String(readAllBytes(f.toPath()));
assertTrue("Content doesn't contain our property!", content.equals(testContent));
}

@Test
public void updateWithSpecialCharacterPathTest()
throws IOException, InterruptedException, ExecutionException {
final String path = "updateHier : \\'\" < >" + randomUUID();
final String testId = "http://localhost:8080/" + path;
final InputStream input = new ByteArrayInputStream (testContent.getBytes());

final File f = indexer.update(testId, input).get();

// file should exist
LOGGER.debug("Got filename: {}", f.getName());
assertTrue("Filename doesn't match", f.getName().startsWith(URLEncoder.encode(path, "UTF-8")));
assertTrue("Path port number doesn't match",
f.getParentFile().getName().equals("8080"));
assertTrue("Path hostname doesn't match",
f.getParentFile().getParentFile().getName().equals("localhost"));

// content should be 'test content'
final String content = new String(readAllBytes(f.toPath()));
assertTrue("Content doesn't contain our property!", content.equals(testContent));
}

@Test
public void removeTest() throws IOException, InterruptedException, ExecutionException {
final String path1 = "removeTest" + randomUUID();
final String path2 = "" + randomUUID();
final String testId = path1 + "/" + path2;
// should write empty file to disk
final File f = indexer.remove(testId).get();
final File f = indexer.remove("http://localhost:8080/" + testId).get();

// file should exist
LOGGER.debug("Got filename: {}", f.getName());
assertTrue("Filename doesn't match", f.getName().startsWith(testId));
assertTrue("Filename doesn't match", f.getName().startsWith(path2));

// content should be empty
final String content = new String(readAllBytes(f.toPath()));
Expand Down

0 comments on commit 9815690

Please sign in to comment.