Navigation Menu

Skip to content

Commit

Permalink
Add support for creation of resources with repository-supplied paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedurbin authored and Andrew Woods committed Jun 17, 2015
1 parent 8231dba commit 0375aec
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
Expand Up @@ -15,24 +15,23 @@
*/
package org.fcrepo.client.impl;

import static org.fcrepo.kernel.RdfLexicon.CONTAINS;
import static org.fcrepo.kernel.RdfLexicon.HAS_MIXIN_TYPE;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

import org.fcrepo.client.FedoraException;
import org.fcrepo.client.FedoraObject;
import org.fcrepo.client.FedoraRepository;
import org.fcrepo.client.FedoraResource;
import org.fcrepo.client.utils.HttpHelper;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import static org.fcrepo.kernel.RdfLexicon.CONTAINS;
import static org.fcrepo.kernel.RdfLexicon.HAS_MIXIN_TYPE;

/**
* A Fedora Object Impl.
*
Expand Down Expand Up @@ -80,4 +79,9 @@ public Collection<FedoraResource> getChildren(final String mixin) throws FedoraE
}
return set;
}

@Override
public FedoraObject createObject() throws FedoraException {
return repository.createResource(getPath());
}
}
Expand Up @@ -20,6 +20,7 @@
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.fcrepo.client.FedoraContent;
import org.fcrepo.client.FedoraDatastream;
Expand Down Expand Up @@ -219,6 +220,33 @@ public FedoraObject createObject(final String path) throws FedoraException {
}
}

@Override
public FedoraObject createResource(final String containerPath) throws FedoraException {
final HttpPost post = httpHelper.createPostMethod(containerPath == null ? "" : containerPath, null);
try {
final HttpResponse response = httpHelper.execute(post);
final String uri = post.getURI().toString();
final StatusLine status = response.getStatusLine();
final int statusCode = status.getStatusCode();

if (statusCode == SC_CREATED) {
return getObject(response.getFirstHeader("Location").getValue().substring(repositoryURL.length()));
} else if (statusCode == SC_FORBIDDEN) {
LOGGER.error("request to create resource {} is not authorized.", uri);
throw new ForbiddenException("request to create resource " + uri + " is not authorized.");
} else {
LOGGER.error("error creating resource {}: {} {}", uri, statusCode, status.getReasonPhrase());
throw new FedoraException("error creating resource " + uri + ": " + statusCode + " " +
status.getReasonPhrase());
}
} catch (final Exception e) {
LOGGER.error("could not encode URI parameter", e);
throw new FedoraException(e);
} finally {
post.releaseConnection();
}
}

@Override
public FedoraDatastream findOrCreateDatastream(final String path) throws FedoraException {
try {
Expand Down
Expand Up @@ -77,6 +77,26 @@ public void testBasicResourceCreation() throws IOException, FedoraException {
final FedoraDatastream datastream = repo.getDatastream(dsPath);
}

@Test
public void testPidMintedResourceCreation() throws IOException, FedoraException {
final FedoraObject object = repo.createResource("");
Assert.assertTrue("A newly created object should exist at the path given by the createObject() call.",
repo.exists(object.getPath()));

final FedoraObject containedObject = object.createObject();
Assert.assertTrue("A newly created object should exist at the path given by the createObject() call.",
repo.exists(containedObject.getPath()));
Assert.assertTrue("The new object's path should start with the containing object's path.",
containedObject.getPath().startsWith(object.getPath()));
}

@Test
public void testPidMintedResourceCreationWithNullArgument() throws IOException, FedoraException {
final FedoraObject object = repo.createResource(null);
Assert.assertTrue("A newly created object should exist at the path given by the createObject() call.",
repo.exists(object.getPath()));
}

@Test
public void testBasicPropertiesCreation() throws IOException, FedoraException {
final String objectPath = getRandomUniqueId();
Expand Down
Expand Up @@ -29,4 +29,12 @@ public interface FedoraObject extends FedoraResource {
* @param mixin If not null, limit to results that have this mixin.
**/
public Collection<FedoraResource> getChildren( String mixin ) throws FedoraException;

/**
* Create a new resource with a repository-supplied path contained within
* the resource exposed by this FedoraObject instance.
* @return a FedoraObject representing the created resource.
* @throws FedoraException if an error occurs while making the requests against the repository.
**/
public FedoraObject createObject() throws FedoraException;
}
Expand Up @@ -71,6 +71,16 @@ public interface FedoraRepository {
**/
public FedoraObject createObject( String path ) throws FedoraException;

/**
* Create a new Object with a repository-supplied path that is within the container
* at the provided containerPath.
* @param containerPath the path to a container in which this resource will be created. An
* empty String or null will create a new resource at the root level.
* @return a FedoraObject representing the created resource.
* @throws FedoraException if an error occurs while making the requests against the repository.
**/
public FedoraObject createResource(String containerPath) throws FedoraException;

/**
* Get an existing Datastream if it exists, otherwise create a new Datastream.
* @param path The Datastream path.
Expand Down

0 comments on commit 0375aec

Please sign in to comment.