-
Notifications
You must be signed in to change notification settings - Fork 129
/
FedoraNodes.java
318 lines (267 loc) · 10.4 KB
/
FedoraNodes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package org.fcrepo.api;
import static javax.ws.rs.core.MediaType.*;
import static javax.ws.rs.core.Response.*;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.jcr.Node;
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.HeaderParam;
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.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.Variant;
import com.codahale.metrics.annotation.Timed;
import com.hp.hpl.jena.update.GraphStore;
import com.hp.hpl.jena.update.UpdateAction;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.apache.jena.riot.WebContent;
import org.fcrepo.AbstractResource;
import org.fcrepo.Datastream;
import org.fcrepo.FedoraObject;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.jaxb.responses.access.DescribeRepository;
import org.fcrepo.jaxb.responses.access.ObjectProfile;
import org.fcrepo.jaxb.responses.management.DatastreamProfile;
import org.fcrepo.serialization.FedoraObjectSerializer;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.services.ObjectService;
import org.fcrepo.utils.FedoraJcrTypes;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Path("/rest/{path: .*}")
public class FedoraNodes extends AbstractResource {
private static final Logger logger = getLogger(FedoraNodes.class);
@Autowired
private LowLevelStorageService llStoreService;
@GET
@Produces({TEXT_XML, APPLICATION_JSON, TEXT_PLAIN})
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();
try {
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();
} finally {
session.logout();
}
}
/**
* 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.getObject(node.getSession(), 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 DatastreamProfile getDatastreamProfile(Node node) throws RepositoryException, IOException {
final String path = node.getPath();
logger.trace("Executing getDatastream() with path: {}", path);
return getDatastreamProfile(datastreamService.getDatastream(node.getSession(), path));
}
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();
dsProfile.dsStores = new DatastreamProfile.DSStores(ds,
llStoreService.getLowLevelCacheEntries(ds.getNode()));
return dsProfile;
}
public DescribeRepository getRepositoryProfile() throws RepositoryException {
final Session session = getAuthenticatedSession();
final DescribeRepository description = new DescribeRepository();
description.repositoryBaseURL = uriInfo.getBaseUri();
description.sampleOAIURL =
uriInfo.getBaseUriBuilder().path("/123/oai_dc")
.build();
description.repositorySize = objectService.getRepositorySize();
description.numberOfObjects =
objectService.getRepositoryObjectCount(session);
session.logout();
return description;
}
/**
* Does nothing yet-- must be improved to handle the FCREPO3 PUT to /objects/{pid}
*
* @param pid
* @return 201
* @throws RepositoryException
*/
@PUT
@Timed
public Response modifyObject(@PathParam("path")
final List<PathSegment> pathList, final InputStream requestBodyStream) throws RepositoryException, IOException {
final Session session = getAuthenticatedSession();
String path = toPath(pathList);
logger.debug("Modifying object with path: {}", path);
try {
final FedoraObject result =
objectService.getObject(session, path);
if (requestBodyStream != null) {
UpdateAction.parseExecute(IOUtils.toString(requestBodyStream), result.getGraphStore());
}
session.save();
return temporaryRedirect(uriInfo.getRequestUri()).build();
} finally {
session.logout();
}
}
/**
* Creates a new object.
*
* @param pid
* @return 201
* @throws RepositoryException
* @throws InvalidChecksumException
* @throws IOException
*/
@POST
@Timed
public Response createObject(
@PathParam("path") final List<PathSegment> pathList,
@QueryParam("label") @DefaultValue("") final String label,
@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
) throws RepositoryException, IOException, InvalidChecksumException {
String path = toPath(pathList);
logger.debug("Attempting to ingest with path: {}", path);
final Session session = getAuthenticatedSession();
try {
if (objectService.exists(session, path)) {
if(requestBodyStream != null && requestContentType != null && requestContentType.toString().equals(WebContent.contentTypeSPARQLUpdate)) {
final FedoraObject result = objectService.getObject(session, path);
UpdateAction.parseExecute(IOUtils.toString(requestBodyStream), result.getGraphStore());
session.save();
return ok().build();
} else {
return Response.status(HttpStatus.SC_CONFLICT).entity(path + " is an existing resource").build();
}
}
if (FedoraJcrTypes.FEDORA_OBJECT.equals(mixin)){
final FedoraObject result =
objectService.createObject(session, path);
if (label != null && !"".equals(label)) {
result.setLabel(label);
}
if(requestBodyStream != null && requestContentType != null && requestContentType.toString().equals(WebContent.contentTypeSPARQLUpdate)) {
UpdateAction.parseExecute(IOUtils.toString(requestBodyStream), result.getGraphStore());
}
}
if (FedoraJcrTypes.FEDORA_DATASTREAM.equals(mixin)){
final MediaType contentType =
requestContentType != null ? requestContentType
: APPLICATION_OCTET_STREAM_TYPE;
final Node result =
datastreamService.createDatastreamNode(session, path, contentType
.toString(), requestBodyStream, checksumType, checksum);
Datastream ds = new Datastream(result);
ds.setLabel(label);
}
session.save();
logger.debug("Finished creating {} with path: {}", mixin, path);
return created(uriInfo.getRequestUri()).entity(path).build();
} finally {
session.logout();
}
}
/**
* Deletes an object.
*
* @param pid
* @return
* @throws RepositoryException
*/
@DELETE
@Timed
public Response deleteObject(@PathParam("path")
final List<PathSegment> path) throws RepositoryException {
final Session session = getAuthenticatedSession();
objectService.deleteObject(session, toPath(path));
session.save();
return noContent().build();
}
public ObjectService getObjectService() {
return objectService;
}
public void setObjectService(ObjectService objectService) {
this.objectService = objectService;
}
public DatastreamService getDatastreamService() {
return datastreamService;
}
public void setDatastreamService(DatastreamService datastreamService) {
this.datastreamService = datastreamService;
}
public LowLevelStorageService getLlStoreService() {
return llStoreService;
}
public void setLlStoreService(final LowLevelStorageService llStoreService) {
this.llStoreService = llStoreService;
}
}