Skip to content

Commit

Permalink
Adding reverse translation to HTTP API
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Apr 3, 2014
1 parent 1d25c09 commit 923060a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 11 deletions.
19 changes: 13 additions & 6 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java
Expand Up @@ -19,6 +19,7 @@
import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static com.hp.hpl.jena.graph.Triple.create;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static com.sun.jersey.api.Responses.clientError;
import static com.sun.jersey.api.Responses.notAcceptable;
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE;
Expand Down Expand Up @@ -406,16 +407,25 @@ public Response createObject(@PathParam("path")
final String newObjectPath;
final String path = toPath(pathList);

final HttpIdentifierTranslator idTranslator =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

if (nodeService.exists(session, path)) {
final String pid;
String pid;

if (slug != null) {
pid = slug;
} else {
} else {
pid = pidMinter.mintPid();
}

// reverse translate the proffered or created identifier
LOGGER.trace("Using external identifier {} to create new resource.", pid);
LOGGER.trace("Using prefixed external identifier {} to create new resource.", uriInfo.getBaseUri() + "/"
+ pid);
pid = idTranslator.getPathFromSubject(createResource(uriInfo.getBaseUri() + "/" + pid));
// remove leading slash left over from translation
pid = pid.substring(1, pid.length());
LOGGER.trace("Using internal identifier {} to create new resource.", pid);
newObjectPath = path + "/" + pid;
} else {
newObjectPath = path;
Expand All @@ -437,9 +447,6 @@ public Response createObject(@PathParam("path")
checksumURI = null;
}

final HttpIdentifierTranslator idTranslator =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

final String objectType;

if (mixin != null) {
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.fcrepo.http.api;

import static com.hp.hpl.jena.graph.NodeFactory.createAnon;
import static javax.jcr.PropertyType.PATH;
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE;
import static javax.ws.rs.core.Response.Status.CONFLICT;
import static javax.ws.rs.core.Response.Status.CREATED;
Expand Down Expand Up @@ -56,6 +57,7 @@
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeType;
Expand Down Expand Up @@ -145,6 +147,12 @@ public class FedoraNodesTest {

private UriInfo mockUriInfo;

@Mock
private Value mockValue;

@Mock
private ValueFactory mockValueFactory;

@Before
public void setUp() throws Exception {
initMocks(this);
Expand Down Expand Up @@ -201,13 +209,15 @@ public void testCreateObject() throws Exception {
public void testCreateChildObject() throws Exception {

setField(testObj, "pidMinter", mockPidMinter);
final String pid = "testObject";
final String pid = "testCreateChildObject";
final String path = "/" + pid + "/a";
when(mockNodes.exists(mockSession, "/" + pid)).thenReturn(true);
when(mockPidMinter.mintPid()).thenReturn("a");
when(mockObjects.createObject(mockSession, path)).thenReturn(mockObject);
when(mockObject.getNode()).thenReturn(mockNode);
when(mockNode.getPath()).thenReturn(path);
when(mockSession.getValueFactory()).thenReturn(mockValueFactory);
when(mockValueFactory.createValue("a", PATH)).thenReturn(mockValue);
final Response actual =
testObj.createObject(createPathList(pid), FEDORA_OBJECT, null, null,
null, null, getUriInfoImpl(), null);
Expand All @@ -222,12 +232,15 @@ public void testCreateChildObject() throws Exception {
public void testCreateChildObjectWithSlug() throws Exception {
setField(testObj, "pidMinter", mockPidMinter);

final String pid = "testObject";
final String pid = "testCreateChildObjectWithSlug";
final String path = "/" + pid + "/some-slug";
when(mockNodes.exists(mockSession, "/" + pid)).thenReturn(true);
when(mockObjects.createObject(mockSession, path)).thenReturn(mockObject);
when(mockObject.getNode()).thenReturn(mockNode);
when(mockNode.getPath()).thenReturn(path);
when(mockSession.getValueFactory()).thenReturn(mockValueFactory);
when(mockValueFactory.createValue("a", PATH)).thenReturn(mockValue);

final Response actual =
testObj.createObject(createPathList(pid), FEDORA_OBJECT, null, null,
null, "some-slug", getUriInfoImpl(), null);
Expand Down
Expand Up @@ -44,9 +44,10 @@ public class ExternalIdentifierConverter extends IdentifierConverter<Resource> {
@Inject
protected List<InternalIdentifierConverter> translationChain;

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

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

private Converter<String, String> reverse = identity();
@Override
protected Resource doForward(final String a) {
return doRdfForward(forward.convert(a));
Expand Down
Expand Up @@ -23,6 +23,7 @@
* Subclass {@link BasePidMinter} instead.
*
* @author eddies
* @author ajs6f
* @date Feb 7, 2013
*/
public interface PidMinter {
Expand Down
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo.kernel.identifiers;

import com.google.common.base.Function;
Expand Down
Expand Up @@ -25,6 +25,7 @@
* Simple PidMinter that replies on Java's inbuilt UUID minting.
*
* @author eddies
* @author ajs6f
* @date Feb 7, 2013
*/
public class UUIDPidMinter extends BasePidMinter {
Expand Down
Expand Up @@ -19,17 +19,24 @@
import static java.util.regex.Pattern.compile;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

public class UUIDPidMinterTest {

private static final String PID_PATTERN =
"[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}";
private UUIDPidMinter testMinter;

@Before
public void setUp() {
testMinter = new UUIDPidMinter();
}

@Test
public void testMintPid() throws Exception {

final String pid = new UUIDPidMinter().mintPid();
final String pid = testMinter.mintPid();

assertTrue("PID wasn't a UUID", compile(PID_PATTERN).matcher(pid)
.find());
Expand Down

0 comments on commit 923060a

Please sign in to comment.