Skip to content

Commit

Permalink
Refactor UriAwareIDentifierConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 6, 2014
1 parent e4f09d0 commit 076c689
Showing 1 changed file with 86 additions and 58 deletions.
Expand Up @@ -43,6 +43,7 @@
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static java.util.Collections.singletonList;
Expand All @@ -56,6 +57,9 @@
import static org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext;

/**
* Convert between Jena Resources and JCR Nodes using a JAX-RS UriBuilder to mediate the
* URI translation.
*
* @author cabeer
* @since 10/5/14
*/
Expand All @@ -69,7 +73,7 @@ public class UriAwareIdentifierConverter extends IdentifierConverter<Resource,No
private final UriBuilder uriBuilder;

protected Converter<String, String> forward = identity();
protected Converter<String, String> reverse = identity();
protected Converter<String, String> reverse = identity();

private final UriTemplate uriTemplate;

Expand All @@ -88,25 +92,72 @@ public UriAwareIdentifierConverter(final Session session,
resetTranslationChain();
}

private UriBuilder uriBuilder() {
return UriBuilder.fromUri(uriBuilder.toTemplate());
}

@Override
public boolean inDomain(final Resource resource) {
protected Node doForward(final Resource resource) {
try {
final HashMap<String, String> values = new HashMap<>();
return uriTemplate.match(resource.getURI(), values) && values.containsKey("path");
} catch (final RuntimeException e) {
return false;

final String path = asString(resource, values);

if (path != null) {
final Node node = getNode(path);

final boolean metadata = values.containsKey("path")
&& values.get("path").endsWith("/" + FCR_METADATA);

if (!metadata && DatastreamImpl.hasMixin(node)) {
return node.getNode(JCR_CONTENT);
} else {
return node;
}
} else {
throw new RuntimeException("Asked to translate a resource " + resource
+ " that doesn't match the URI template");
}
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}

@Override
protected Resource doBackward(final Node node) {
try {
return toDomain(doBackwardPathOnly(node));
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}

@Override
public boolean inDomain(final Resource resource) {
final HashMap<String, String> values = new HashMap<>();
return uriTemplate.match(resource.getURI(), values) && values.containsKey("path");
}

@Override
public Resource toDomain(final String path) {
return createResource(uriBuilder().resolveTemplate("path", path.substring(1), false).build().toString());
}

@Override
public String asString(final Resource resource) {

final HashMap<String, String> values = new HashMap<>();

return asString(resource, values);
}

/**
* Convert the incoming Resource to a JCR path (but don't attempt to load the node).
*
* @param resource Jena Resource to convert
* @param values a map that will receive the matching URI template variables for future use.
* @return
*/
private String asString(final Resource resource, final Map<String, String> values) {
if (uriTemplate.match(resource.getURI(), values) && values.containsKey("path")) {
String path = "/" + values.get("path");

Expand All @@ -116,43 +167,25 @@ public String asString(final Resource resource) {
path = replaceOnce(path, "/" + FCR_METADATA, EMPTY);
}

return forward.convert(path);
} else {
return null;
}
}
path = forward.convert(path);

@Override
protected Node doForward(final Resource resource) {
try {
final HashMap<String, String> values = new HashMap<>();
if (uriTemplate.match(resource.getURI(), values) && values.containsKey("path")) {
String path = "/" + values.get("path");

final boolean metadata = path.endsWith("/" + FCR_METADATA);

if (metadata) {
path = replaceOnce(path, "/" + FCR_METADATA, EMPTY);
}

final Node node = getNode(URLDecoder.decode(forward.convert(path), "UTF-8"));
if (path == null) {
return null;
}

if (!metadata && DatastreamImpl.hasMixin(node)) {
return node.getNode(JCR_CONTENT);
} else {
return node;
}
} else {
throw new RuntimeException("Asked to translate a resource " + resource
+ " that doesn't match the URI template");
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException e) {
LOGGER.debug("Unable to URL-decode path " + e + " as UTF-8", e);
}
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RepositoryRuntimeException(e);

return path;
} else {
return null;
}
}


private Node getNode(final String path) throws RepositoryException {
if (path.contains(FCR_VERSIONS)) {
final String[] split = path.split("/" + FCR_VERSIONS, 2);
Expand Down Expand Up @@ -263,15 +296,12 @@ private static String getRelativePath(final Node node, final Node ancestor) {
}
}

@Override
protected Resource doBackward(final Node node) {
try {
return toDomain(doBackwardPathOnly(node));
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}

/**
* Get only the resource path to this node, before embedding it in a full URI
* @param node
* @return
* @throws RepositoryException
*/
private String doBackwardPathOnly(final Node node) throws RepositoryException {
String path = reverse.convert(getPath(node));
if (path != null) {
Expand All @@ -289,10 +319,6 @@ private String doBackwardPathOnly(final Node node) throws RepositoryException {
}
}

private UriBuilder uriBuilder() {
return UriBuilder.fromUri(uriBuilder.toTemplate());
}


protected void resetTranslationChain() {
if (translationChain == null) {
Expand All @@ -302,13 +328,10 @@ protected void resetTranslationChain() {
final Converter<String,String> transactionIdentifierConverter = new TransactionIdentifierConverter(session);

setTranslationChain(ImmutableList.copyOf(
Iterables.concat(Lists.newArrayList(workspaceIdentifierConverter, transactionIdentifierConverter),
translationChain)));
if (getTranslationChain() != null) {
setTranslationChain(getTranslationChain());
} else {
setTranslationChain(minimalTranslationChain);
}
Iterables.concat(Lists.newArrayList(
workspaceIdentifierConverter,
transactionIdentifierConverter),
translationChain)));
}
}

Expand Down Expand Up @@ -343,7 +366,9 @@ protected ApplicationContext getApplicationContext() {
return getCurrentWebApplicationContext();
}


/**
* Translate the current workspace into the identifier
*/
class WorkspaceIdentifierConverter extends Converter<String, String> {
public static final String WORKSPACE_PREFIX = "workspace:";

Expand Down Expand Up @@ -380,6 +405,9 @@ private String workspaceSegment() {
}
}

/**
* Translate the current transaction into the identifier
*/
class TransactionIdentifierConverter extends Converter<String, String> {
public static final String TX_PREFIX = "tx:";

Expand Down

0 comments on commit 076c689

Please sign in to comment.