Skip to content

Commit

Permalink
Small code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed May 8, 2013
1 parent 6342cee commit 89f3a22
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 153 deletions.
1 change: 0 additions & 1 deletion fcrepo-http-api/pom.xml
Expand Up @@ -154,7 +154,6 @@
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>

Expand Down
Expand Up @@ -36,18 +36,19 @@ public class FedoraImport extends AbstractResource {
private final Logger logger = getLogger(this.getClass());

@POST
public Response importObject(@PathParam("path") final List<PathSegment> pathList, @QueryParam("format")
public Response importObject(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("format")
@DefaultValue("jcr/xml")
final String format, final InputStream stream) throws IOException,
RepositoryException, InvalidChecksumException {

final String path = toPath(pathList);

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

try {
serializers.get(format).deserialize(session, path, stream);
session.save();
session.save();
// TODO return proper URI for new resource
return created(uriInfo.getAbsolutePath()).build();
} finally {
Expand Down
314 changes: 166 additions & 148 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraVersions.java
@@ -1,3 +1,4 @@

package org.fcrepo.api;

import java.io.IOException;
Expand Down Expand Up @@ -39,152 +40,169 @@
@Path("/rest/{path: .*}/fcr:versions")
public class FedoraVersions extends AbstractResource {

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

private DateTimeFormatter jcrDateFormat = ISODateTimeFormat.dateTime();

@Autowired
private DatastreamService datastreamService;

@Autowired
private ObjectService objectService;

public void setDatastreamService(DatastreamService datastreamService) {
this.datastreamService = datastreamService;
}

public void setObjectService(ObjectService objectService) {
this.objectService = objectService;
}

@GET
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
public List<Version> getVersionProfile(@PathParam("path") final List<PathSegment> segments) throws RepositoryException {
final String path = toPath(segments);
final Session session = getAuthenticatedSession();
try {
final Node node = session.getNode(path);

if (node.isNodeType("nt:file")) {
Datastream ds = datastreamService.getDatastream(session, path);
Version v = new Version(path, ds.getDsId(), ds.getLabel(), ds.getCreatedDate());
return Arrays.asList(v);
}
if (node.isNodeType("nt:folder")) {
FedoraObject obj = objectService.getObject(session, path);
Version v = new Version(path, obj.getName(), obj.getName(), jcrDateFormat.parseDateTime(obj.getCreated()).toDate());
return Arrays.asList(v);
}
} finally {
session.logout();
}

return Arrays.asList();
}

@Path("/{id}")
@GET
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
public Response getVersion(@PathParam("path") final List<PathSegment> segments, @PathParam("id") final String versionId)
throws RepositoryException, IOException {
final String path = toPath(segments);
final Session session = getAuthenticatedSession();

try {
final Node node = session.getNode(path);

if (node.isNodeType("nt:file")) {
/* TODO: this should be moved to datastreamservice */
Datastream ds = datastreamService.getDatastream(session, path);
return Response.ok(getDSProfile(ds)).build();
}

if (node.isNodeType("nt:folder")) {
/* TODO: this should be moved to objectservice */
return Response.ok(getObjectProfile(objectService.getObject(session, path))).build();
}
} finally {
session.logout();
}

return Response.status(Response.Status.NOT_FOUND).build();
}

private ObjectProfile getObjectProfile(FedoraObject object) throws RepositoryException {
ObjectProfile prof = new ObjectProfile();
prof.objCreateDate = object.getCreated();
prof.objLabel = object.getLabel();
prof.objLastModDate = object.getLastModified();
prof.objSize = object.getSize();
prof.objOwnerId = object.getOwnerId();
prof.objModels = object.getModels();
return prof;
}

/* TODO: this is a duplicate of FedoraDatatstreams.getDSProfile and should be merged into one method */
private DatastreamProfile getDSProfile(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 = DatastreamStates.A;
dsProfile.dsMIME = ds.getMimeType();
dsProfile.dsSize = ds.getSize();
dsProfile.dsCreateDate = ds.getCreatedDate().toString();
return dsProfile;
}

@XmlRootElement(name = "datastream-version")
@XmlAccessorType(XmlAccessType.FIELD)
public static class Version {

@XmlAttribute(name = "path")
private String path;

@XmlAttribute(name = "name")
private String name;

@XmlAttribute(name = "pid")
private String id;

@XmlAttribute(name = "created")
private Date created;

public Version(String path, String id, String name, Date created) {
super();
this.path = path;
this.name = name;
this.id = id;
this.created = created;
}

private Version() {
super();
}

public String getId() {
return id;
}

public Date getCreated() {
return created;
}

public String getName() {
return name;
}

public String getPath() {
return path;
}
}
private static final Logger logger = LoggerFactory
.getLogger(FedoraVersions.class);

private DateTimeFormatter jcrDateFormat = ISODateTimeFormat.dateTime();

@Autowired
private DatastreamService datastreamService;

@Autowired
private ObjectService objectService;

public void setDatastreamService(final DatastreamService datastreamService) {
this.datastreamService = datastreamService;
}

public void setObjectService(final ObjectService objectService) {
this.objectService = objectService;
}

@GET
@Produces({MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
public List<Version> getVersionProfile(@PathParam("path")
final List<PathSegment> segments) throws RepositoryException {
final String path = toPath(segments);
final Session session = getAuthenticatedSession();
try {
final Node node = session.getNode(path);

if (node.isNodeType("nt:file")) {
final Datastream ds =
datastreamService.getDatastream(session, path);
final Version v =
new Version(path, ds.getDsId(), ds.getLabel(), ds
.getCreatedDate());
return Arrays.asList(v);
}
if (node.isNodeType("nt:folder")) {
final FedoraObject obj = objectService.getObject(session, path);
final Version v =
new Version(path, obj.getName(), obj.getName(),
jcrDateFormat.parseDateTime(obj.getCreated())
.toDate());
return Arrays.asList(v);
}
} finally {
session.logout();
}

return Arrays.asList();
}

@Path("/{id}")
@GET
@Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML})
public Response getVersion(@PathParam("path")
final List<PathSegment> segments, @PathParam("id")
final String versionId) throws RepositoryException, IOException {
final String path = toPath(segments);
final Session session = getAuthenticatedSession();

try {
final Node node = session.getNode(path);

if (node.isNodeType("nt:file")) {
/* TODO: this should be moved to datastreamservice */
final Datastream ds =
datastreamService.getDatastream(session, path);
return Response.ok(getDSProfile(ds)).build();
}

if (node.isNodeType("nt:folder")) {
/* TODO: this should be moved to objectservice */
return Response
.ok(getObjectProfile(objectService.getObject(session,
path))).build();
}
} finally {
session.logout();
}

return Response.status(Response.Status.NOT_FOUND).build();
}

private ObjectProfile getObjectProfile(final FedoraObject object)
throws RepositoryException {
final ObjectProfile prof = new ObjectProfile();
prof.objCreateDate = object.getCreated();
prof.objLabel = object.getLabel();
prof.objLastModDate = object.getLastModified();
prof.objSize = object.getSize();
prof.objOwnerId = object.getOwnerId();
prof.objModels = object.getModels();
return prof;
}

/*
* TODO: this is a duplicate of FedoraDatatstreams.getDSProfile and should
* be merged into one method
*/
private DatastreamProfile getDSProfile(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 = DatastreamStates.A;
dsProfile.dsMIME = ds.getMimeType();
dsProfile.dsSize = ds.getSize();
dsProfile.dsCreateDate = ds.getCreatedDate().toString();
return dsProfile;
}

@XmlRootElement(name = "datastream-version")
@XmlAccessorType(XmlAccessType.FIELD)
public static class Version {

@XmlAttribute(name = "path")
private String path;

@XmlAttribute(name = "name")
private String name;

@XmlAttribute(name = "pid")
private String id;

@XmlAttribute(name = "created")
private Date created;

public Version(final String path, final String id, final String name,
final Date created) {
super();
this.path = path;
this.name = name;
this.id = id;
this.created = created;
}

public Version() {
super();
}

public String getId() {
return id;
}

public Date getCreated() {
return created;
}

public String getName() {
return name;
}

public String getPath() {
return path;
}
}
}

0 comments on commit 89f3a22

Please sign in to comment.