Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start refactoring into kind-of-globbing postfixes with a little bit m…
…ore RESTful stuff
- Loading branch information
Showing
11 changed files
with
417 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraDescribe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
|
||
package org.fcrepo.api; | ||
|
||
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static javax.ws.rs.core.MediaType.TEXT_HTML; | ||
import static javax.ws.rs.core.MediaType.TEXT_XML; | ||
import static javax.ws.rs.core.Response.created; | ||
import static javax.ws.rs.core.Response.noContent; | ||
import static javax.ws.rs.core.Response.ok; | ||
import static org.fcrepo.services.PathService.OBJECT_PATH; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import javax.jcr.Node; | ||
import javax.jcr.Repository; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import javax.ws.rs.DELETE; | ||
import javax.ws.rs.DefaultValue; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.PUT; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.core.PathSegment; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.fcrepo.AbstractResource; | ||
import org.fcrepo.Datastream; | ||
import org.fcrepo.FedoraObject; | ||
import org.fcrepo.jaxb.responses.access.DescribeRepository; | ||
import org.fcrepo.jaxb.responses.access.ObjectProfile; | ||
import org.fcrepo.jaxb.responses.management.DatastreamProfile; | ||
import org.fcrepo.provider.VelocityViewer; | ||
import org.fcrepo.services.DatastreamService; | ||
import org.fcrepo.services.ObjectService; | ||
import org.slf4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Path("/rest/{path: .*}/fcr:describe") | ||
public class FedoraDescribe extends AbstractResource { | ||
|
||
private static final Logger logger = getLogger(FedoraDescribe.class); | ||
|
||
@Autowired | ||
private Repository repo; | ||
|
||
@Autowired | ||
private ObjectService objectService; | ||
|
||
@Autowired | ||
private DatastreamService datastreamService; | ||
|
||
@GET | ||
@Produces({TEXT_XML, APPLICATION_JSON, TEXT_HTML}) | ||
public Response describe(@PathParam("path") | ||
final List<PathSegment> pathList) throws RepositoryException, IOException { | ||
|
||
final String path = toPath(pathList); | ||
logger.trace("getting profile for {}", path); | ||
if ("/".equals(path)) { | ||
return Response.ok(getRepositoryProfile()).build(); | ||
} | ||
final Session session = getAuthenticatedSession(); | ||
Node node = session.getNode(path); | ||
if (node.isNodeType("nt:file")) { | ||
return Response.ok(getDatastreamProfile(node)).build(); | ||
} | ||
if (node.isNodeType("nt:folder")) { | ||
return Response.ok(getObjectProfile(node)).build(); | ||
} | ||
return Response.status(406).entity("Unexpected node type: " + node.getPrimaryNodeType()).build(); | ||
} | ||
|
||
/** | ||
* Returns an object profile. | ||
* | ||
* @param path | ||
* @return 200 | ||
* @throws RepositoryException | ||
* @throws IOException | ||
*/ | ||
public ObjectProfile getObjectProfile(Node node) | ||
throws RepositoryException, IOException { | ||
|
||
final String path = node.getPath(); | ||
logger.trace("getting object profile {}", path); | ||
final ObjectProfile objectProfile = new ObjectProfile(); | ||
final FedoraObject obj = objectService.getObjectByPath(path); | ||
objectProfile.pid = obj.getName(); | ||
objectProfile.objLabel = obj.getLabel(); | ||
objectProfile.objOwnerId = obj.getOwnerId(); | ||
objectProfile.objCreateDate = obj.getCreated(); | ||
objectProfile.objLastModDate = obj.getLastModified(); | ||
objectProfile.objSize = obj.getSize(); | ||
objectProfile.objItemIndexViewURL = | ||
uriInfo.getAbsolutePathBuilder().path("datastreams").build(); | ||
objectProfile.objState = ObjectProfile.ObjectStates.A; | ||
objectProfile.objModels = obj.getModels(); | ||
return objectProfile; | ||
|
||
} | ||
|
||
public ObjectService getObjectService() { | ||
return objectService; | ||
} | ||
|
||
|
||
public void setObjectService(ObjectService objectService) { | ||
this.objectService = objectService; | ||
} | ||
|
||
public DatastreamProfile getDatastreamProfile(Node node) throws RepositoryException, IOException { | ||
final String path = node.getParent().getPath(); | ||
final String dsId = node.getName(); | ||
logger.trace("Executing getDatastream() with path: {}/{}", path, dsId); | ||
return getDatastreamProfile(datastreamService.getDatastream(path, dsId)); | ||
|
||
} | ||
|
||
private DatastreamProfile getDatastreamProfile(final Datastream ds) | ||
throws RepositoryException, IOException { | ||
logger.trace("Executing getDSProfile() with node: " + ds.getDsId()); | ||
final DatastreamProfile dsProfile = new DatastreamProfile(); | ||
dsProfile.dsID = ds.getDsId(); | ||
dsProfile.pid = ds.getObject().getName(); | ||
logger.trace("Retrieved datastream " + ds.getDsId() + "'s parent: " + | ||
dsProfile.pid); | ||
dsProfile.dsLabel = ds.getLabel(); | ||
logger.trace("Retrieved datastream " + ds.getDsId() + "'s label: " + | ||
ds.getLabel()); | ||
dsProfile.dsOwnerId = ds.getOwnerId(); | ||
dsProfile.dsChecksumType = ds.getContentDigestType(); | ||
dsProfile.dsChecksum = ds.getContentDigest(); | ||
dsProfile.dsState = DatastreamProfile.DatastreamStates.A; | ||
dsProfile.dsMIME = ds.getMimeType(); | ||
dsProfile.dsSize = ds.getSize(); | ||
dsProfile.dsCreateDate = ds.getCreatedDate().toString(); | ||
return dsProfile; | ||
} | ||
|
||
public DatastreamService getDatastreamService() { | ||
return datastreamService; | ||
} | ||
|
||
|
||
public void setDatastreamService(DatastreamService datastreamService) { | ||
this.datastreamService = datastreamService; | ||
} | ||
|
||
public DescribeRepository getRepositoryProfile() throws RepositoryException { | ||
|
||
final Session session = getAuthenticatedSession(); | ||
final DescribeRepository description = new DescribeRepository(); | ||
description.repositoryBaseURL = uriInfo.getBaseUri(); | ||
description.sampleOAIURL = | ||
uriInfo.getBaseUriBuilder().path(OBJECT_PATH + "/123/oai_dc") | ||
.build(); | ||
description.repositorySize = objectService.getRepositorySize(session); | ||
description.numberOfObjects = | ||
objectService.getRepositoryObjectCount(session); | ||
session.logout(); | ||
return description; | ||
} | ||
|
||
//TODO Figure out how to call this | ||
public String getRepositoryProfileHtml() throws RepositoryException { | ||
|
||
final VelocityViewer view = new VelocityViewer(); | ||
return view.getRepoInfo(getRepositoryProfile()); | ||
} | ||
|
||
/** | ||
* A testing convenience setter for otherwise injected resources | ||
* @param repo | ||
*/ | ||
public void setRepository(final Repository repo) { | ||
this.repo = repo; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraUnnamedObjects.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.fcrepo.api; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
import javax.jcr.RepositoryException; | ||
import javax.ws.rs.HeaderParam; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.core.PathSegment; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.fcrepo.AbstractResource; | ||
import org.fcrepo.exception.InvalidChecksumException; | ||
import org.slf4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
|
||
@Component | ||
@Path("/rest/{path: .*}/fcr:new") | ||
public class FedoraUnnamedObjects extends AbstractResource { | ||
|
||
private static final Logger logger = getLogger(FedoraUnnamedObjects.class); | ||
|
||
@Autowired | ||
FedoraDatastreams datastreamsResource; | ||
|
||
@Autowired | ||
FedoraObjects objectsResource; | ||
|
||
/** | ||
* Create an anonymous object with a newly minted name | ||
* @param pathList | ||
* @return 201 | ||
*/ | ||
@POST | ||
public Response ingestAndMint(@PathParam("path") | ||
final List<PathSegment> pathList) throws RepositoryException { | ||
logger.debug("Creating a new unnamed object"); | ||
final String pid = pidMinter.mintPid(); | ||
PathSegment path = new PathSegment() { | ||
|
||
@Override | ||
public String getPath() { | ||
return pid; | ||
} | ||
|
||
@Override | ||
public MultivaluedMap<String, String> getMatrixParameters() { | ||
return null; | ||
} | ||
|
||
}; | ||
ImmutableList.Builder<PathSegment> segments = ImmutableList.builder(); | ||
segments.addAll(pathList.subList(0, pathList.size() - 1)); | ||
segments.add(path); | ||
return objectsResource.ingest(segments.build(), ""); | ||
} | ||
|
||
/** | ||
* Create an anonymous DS with a newly minted name | ||
* and content from request body | ||
* @param pathList | ||
* @throws RepositoryException | ||
*/ | ||
@POST | ||
@Path("fcr:content") | ||
public Response createDS(@PathParam("path") | ||
final List<PathSegment> pathList, @QueryParam("checksumType") | ||
final String checksumType, @QueryParam("checksum") | ||
final String checksum, @HeaderParam("Content-Type") | ||
final MediaType requestContentType, final InputStream requestBodyStream) | ||
throws IOException, InvalidChecksumException, RepositoryException { | ||
logger.debug("Creating a new unnamed object"); | ||
final String dsid = pidMinter.mintPid(); | ||
ImmutableList.Builder<PathSegment> segments = ImmutableList.builder(); | ||
segments.addAll(pathList.subList(0, pathList.size() - 2)); | ||
return datastreamsResource.addDatastream( | ||
segments.build(), checksumType, | ||
checksum, dsid, | ||
requestContentType, requestBodyStream); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.