/
FedoraIdentifiers.java
127 lines (107 loc) · 4.26 KB
/
FedoraIdentifiers.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
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.http.api;
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.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.http.commons.domain.RDFMediaType.N3;
import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT1;
import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2;
import static org.fcrepo.http.commons.domain.RDFMediaType.NTRIPLES;
import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_JSON;
import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML;
import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
import static org.fcrepo.kernel.RdfLexicon.HAS_MEMBER_OF_RESULT;
import java.util.Collection;
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;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.UriInfo;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.HttpGraphSubjects;
import org.fcrepo.http.commons.session.InjectedSession;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.codahale.metrics.annotation.Timed;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
/**
* JAX-RS Resource offering PID creation.
*
* @author ajs6f
* @author cbeer
*/
@Component
@Scope("prototype")
@Path("/{path: .*}/fcr:pid")
public class FedoraIdentifiers extends AbstractResource {
@InjectedSession
protected Session session;
/**
* Mint identifiers (without creating the objects)
*
* POST /path/to/mint/from/fcr:pid?numPids=15
*
* @param numPids number of PIDs to return
* @return HTTP 200 with block of PIDs
*/
@POST
@Timed
@Produces({TURTLE, N3, N3_ALT1, N3_ALT2, 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 Model model = createDefaultModel();
final Resource pidsResult =
createResource(uriInfo.getAbsolutePath().toASCIIString());
final Collection<String> identifiers =
transform(create(closed(1, numPids), integers()), pidMinter
.makePid());
final HttpGraphSubjects subjects =
new HttpGraphSubjects(session, FedoraNodes.class, uriInfo);
for (final String identifier : identifiers) {
final String absPath;
if (path.equals("/")) {
absPath = "/" + identifier;
} else {
absPath = path + "/" + identifier;
}
final Resource s = subjects.getGraphSubject(absPath);
model.add(pidsResult, HAS_MEMBER_OF_RESULT, s);
}
return create(model).toDataset();
}
}