Skip to content

Commit

Permalink
All endpoints except for FedoraTransactions done.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Jun 12, 2013
1 parent e602ba7 commit 56247d8
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 226 deletions.
73 changes: 35 additions & 38 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraIdentifiers.java
Expand Up @@ -4,8 +4,13 @@
import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.ContiguousSet.create;
import static com.google.common.collect.DiscreteDomain.integers;
import static com.google.common.collect.ImmutableBiMap.of;
import static com.google.common.collect.Range.closed;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static com.hp.hpl.jena.update.GraphStoreFactory.create;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static org.fcrepo.RdfLexicon.HAS_MEMBER_OF_RESULT;
import static org.fcrepo.http.RDFMediaType.N3;
import static org.fcrepo.http.RDFMediaType.N3_ALT1;
import static org.fcrepo.http.RDFMediaType.N3_ALT2;
Expand All @@ -18,7 +23,6 @@
import java.util.List;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand All @@ -31,22 +35,19 @@
import javax.ws.rs.core.UriInfo;

import org.fcrepo.AbstractResource;
import org.fcrepo.RdfLexicon;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;
import com.google.common.collect.ImmutableBiMap;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.update.GraphStoreFactory;

/**
* JAX-RS Resource offering PID creation.
*
* @author ajs6f
* @author cbeer
*
*/
@Component
Expand All @@ -58,51 +59,47 @@ public class FedoraIdentifiers extends AbstractResource {
* @return HTTP 200 with block of PIDs
*/
@POST
@Timed
@Produces({N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON,
NTRIPLES, TEXT_HTML})
public Dataset getNextPid(@PathParam("path") final List<PathSegment> pathList, @QueryParam("numPids") @DefaultValue("1")
final Integer numPids,
@Context final UriInfo uriInfo) throws RepositoryException {
@Timed
@Produces({N3, N3_ALT1, N3_ALT2, TURTLE, RDF_XML, RDF_JSON, NTRIPLES,
TEXT_HTML})
public Dataset getNextPid(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("numPids")
@DefaultValue("1")
final Integer numPids, @Context
final UriInfo uriInfo) throws RepositoryException {

final String path = toPath(pathList);

final Session session = getAuthenticatedSession();
final Model model = createDefaultModel();

String path = toPath(pathList);
final Resource pidsResult =
createResource(uriInfo.getAbsolutePath().toASCIIString());

try {
final Model model = ModelFactory.createDefaultModel();
final Collection<String> identifiers =
transform(create(closed(1, numPids), integers()), pidMinter
.makePid());

final UriBuilder builder =
uriInfo.getBaseUriBuilder().path(FedoraNodes.class);

final Resource pidsResult =
ResourceFactory.createResource(uriInfo.getAbsolutePath()
.toASCIIString());
for (final String identifier : identifiers) {

final Collection<String> identifiers = transform(create(closed(1, numPids),
integers()), pidMinter.makePid());

final UriBuilder builder = uriInfo.getBaseUriBuilder().path(FedoraNodes.class);

for (String identifier : identifiers) {

final String absPath;
if (path.equals("/")) {
absPath = identifier;
} else {
absPath = path.substring(1) + "/" + identifier;
}

final String s = builder.buildFromMap(ImmutableBiMap.of("path", absPath)).toASCIIString();

model.add(pidsResult, RdfLexicon.HAS_MEMBER_OF_RESULT, ResourceFactory.createResource(s));
final String absPath;
if (path.equals("/")) {
absPath = identifier;
} else {
absPath = path.substring(1) + "/" + identifier;
}

return GraphStoreFactory.create(model).toDataset();
final String s =
builder.buildFromMap(of("path", absPath)).toASCIIString();

} finally {
session.logout();
model.add(pidsResult, HAS_MEMBER_OF_RESULT, ResourceFactory
.createResource(s));
}

return create(model).toDataset();

}

}
19 changes: 17 additions & 2 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraImport.java
Expand Up @@ -23,13 +23,23 @@
import org.fcrepo.AbstractResource;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.serialization.FedoraObjectSerializer;
import org.fcrepo.session.InjectedSession;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
* @author ajs6f
* @author cbeer
*/
@Component
@Scope("prototype")
@Path("/{path: .*}/fcr:import")
public class FedoraImport extends AbstractResource {

@InjectedSession
protected Session session;

@Resource
private Map<String, FedoraObjectSerializer> serializers;

Expand All @@ -44,12 +54,13 @@ public Response importObject(@PathParam("path")

final String path = toPath(pathList);
logger.debug("Deserializing at {}", path);
final Session session = getAuthenticatedSession();

try {
serializers.get(format).deserialize(session, path, stream);
session.save();
return created(uriInfo.getAbsolutePathBuilder().path(FedoraNodes.class).build(path.substring(1))).build();
return created(
uriInfo.getAbsolutePathBuilder().path(FedoraNodes.class)
.build(path.substring(1))).build();
} finally {
session.logout();
}
Expand All @@ -59,4 +70,8 @@ public void setSerializers(
final Map<String, FedoraObjectSerializer> serializers) {
this.serializers = serializers;
}

public void setSession(final Session session) {
this.session = session;
}
}
34 changes: 23 additions & 11 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraSitemap.java
Expand Up @@ -28,30 +28,40 @@
import org.fcrepo.jaxb.responses.sitemap.SitemapEntry;
import org.fcrepo.jaxb.responses.sitemap.SitemapIndex;
import org.fcrepo.jaxb.responses.sitemap.SitemapUrlSet;
import org.fcrepo.session.InjectedSession;
import org.modeshape.jcr.api.JcrConstants;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;

/**
* @author ajs6f
* @author cbeer
*/
@Component
@Scope("prototype")
@Path("/sitemap")
public class FedoraSitemap extends AbstractResource {

@InjectedSession
protected Session session;

private static final Logger logger = getLogger(FedoraSitemap.class);

public static final long entriesPerPage = 50000;

@GET
@Timed
@Timed
@Produces(TEXT_XML)
public SitemapIndex getSitemapIndex() throws RepositoryException {

logger.trace("Executing getSitemapIndex()...");
final Session session = getAuthenticatedSession();

try {
final long count =
objectService.getRepositoryObjectCount() /
entriesPerPage;
objectService.getRepositoryObjectCount() / entriesPerPage;

final SitemapIndex sitemapIndex = new SitemapIndex();

Expand All @@ -71,11 +81,10 @@ public SitemapIndex getSitemapIndex() throws RepositoryException {

@GET
@Path("/{page}")
@Timed
@Timed
@Produces(TEXT_XML)
public SitemapUrlSet getSitemap(@PathParam("page")
final String page) throws RepositoryException {
final Session session = getAuthenticatedSession();
try {
final SitemapUrlSet sitemapUrlSet = new SitemapUrlSet();

Expand All @@ -102,8 +111,8 @@ public SitemapUrlSet getSitemap(@PathParam("page")

//TODO expand to more fields
final String sqlExpression =
"SELECT [" + JcrConstants.JCR_NAME + "],[" + JCR_LASTMODIFIED + "] FROM [" + FEDORA_OBJECT +
"]";
"SELECT [" + JcrConstants.JCR_NAME + "],[" + JCR_LASTMODIFIED +
"] FROM [" + FEDORA_OBJECT + "]";
final Query query = queryManager.createQuery(sqlExpression, JCR_SQL2);

query.setOffset(page * entriesPerPage);
Expand All @@ -125,11 +134,14 @@ private SitemapEntry getSitemapEntry(final Row r)
logger.warn("no value for {} on {}", JCR_LASTMODIFIED, path);
lkDateValue = r.getValue(JCR_CREATED);
}
Calendar lastKnownDate = (lkDateValue != null) ? lkDateValue.getDate() : null;
final Calendar lastKnownDate =
(lkDateValue != null) ? lkDateValue.getDate() : null;
return new SitemapEntry(uriInfo.getBaseUriBuilder().path(
FedoraNodes.class)
.build(path.substring(1)), lastKnownDate);
FedoraNodes.class).build(path.substring(1)), lastKnownDate);
}

public void setSession(final Session session) {
this.session = session;
}

}
@@ -1,6 +1,8 @@

package org.fcrepo.api;

import static javax.ws.rs.core.Response.created;
import static org.apache.http.HttpStatus.SC_CONFLICT;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
Expand All @@ -21,20 +23,27 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.apache.http.HttpStatus;
import org.fcrepo.AbstractResource;
import org.fcrepo.FedoraResource;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.session.InjectedSession;
import org.fcrepo.utils.FedoraJcrTypes;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;



/**
* @author ajs6f
* @author cbeer
*/
@Component
@Scope("prototype")
@Path("/{path: .*}/fcr:new")
public class FedoraUnnamedObjects extends AbstractResource {

@InjectedSession
protected Session session;

private static final Logger logger = getLogger(FedoraUnnamedObjects.class);

/**
Expand All @@ -43,34 +52,50 @@ public class FedoraUnnamedObjects extends AbstractResource {
* @return 201
*/
@POST
public Response ingestAndMint(@PathParam("path") final List<PathSegment> pathList,
@QueryParam("mixin") @DefaultValue(FedoraJcrTypes.FEDORA_OBJECT) String mixin,
@QueryParam("checksumType") final String checksumType,
@QueryParam("checksum") final String checksum,
@HeaderParam("Content-Type") final MediaType requestContentType,
final InputStream requestBodyStream, @Context UriInfo uriInfo) throws RepositoryException, IOException, InvalidChecksumException {
public Response ingestAndMint(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("mixin")
@DefaultValue(FedoraJcrTypes.FEDORA_OBJECT)
final String mixin, @QueryParam("checksumType")
final String checksumType, @QueryParam("checksum")
final String checksum, @HeaderParam("Content-Type")
final MediaType requestContentType, final InputStream requestBodyStream,
@Context
final UriInfo uriInfo) throws RepositoryException, IOException,
InvalidChecksumException {
final String pid = pidMinter.mintPid();

String path = toPath(pathList) + "/" + pid;
final String path = toPath(pathList) + "/" + pid;

logger.debug("Attempting to ingest with path: {}", path);

final Session session = getAuthenticatedSession();

try {
if (nodeService.exists(session, path)) {
return Response.status(HttpStatus.SC_CONFLICT).entity(path + " is an existing resource").build();
return Response.status(SC_CONFLICT).entity(
path + " is an existing resource").build();
}

final FedoraResource resource = createObjectOrDatastreamFromRequestContent(FedoraNodes.class, session, path, mixin, uriInfo, requestBodyStream, requestContentType, checksumType, checksum);
final FedoraResource resource =
createObjectOrDatastreamFromRequestContent(
FedoraNodes.class, session, path, mixin, uriInfo,
requestBodyStream, requestContentType,
checksumType, checksum);

session.save();
logger.debug("Finished creating {} with path: {}", mixin, path);
return created(uriInfo.getBaseUriBuilder().path(FedoraNodes.class).build(resource.getPath().substring(1))).entity(path).build();
return created(
uriInfo.getBaseUriBuilder().path(FedoraNodes.class).build(
resource.getPath().substring(1))).entity(path)
.build();

} finally {
session.logout();
}
}

public void setSession(final Session session) {
this.session = session;
}

}

0 comments on commit 56247d8

Please sign in to comment.