/
FedoraFieldSearch.java
243 lines (214 loc) · 9.52 KB
/
FedoraFieldSearch.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
/**
* 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.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
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.FIRST_PAGE;
import static org.fcrepo.kernel.RdfLexicon.NEXT_PAGE;
import static org.fcrepo.kernel.RdfLexicon.PAGE;
import static org.fcrepo.kernel.RdfLexicon.PAGE_OF;
import static org.fcrepo.kernel.RdfLexicon.SEARCH_HAS_MORE;
import static org.fcrepo.kernel.RdfLexicon.SEARCH_ITEMS_PER_PAGE;
import static org.fcrepo.kernel.RdfLexicon.SEARCH_OFFSET;
import static org.fcrepo.kernel.RdfLexicon.SEARCH_PAGE;
import static org.fcrepo.kernel.RdfLexicon.SEARCH_TERMS;
import static org.slf4j.LoggerFactory.getLogger;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
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.Request;
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.responses.HtmlTemplate;
import org.fcrepo.http.commons.session.InjectedSession;
import org.fcrepo.jcr.FedoraJcrTypes;
import org.slf4j.Logger;
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;
import com.hp.hpl.jena.vocabulary.RDF;
/**
* Basic administrative search across the repository
*
* @author Frank Asseg
* @author ajs6f
*/
@Component
@Scope("prototype")
@Path("/fcr:search")
public class FedoraFieldSearch extends AbstractResource implements
FedoraJcrTypes {
@InjectedSession
protected Session session;
private static final Logger LOGGER = getLogger(FedoraFieldSearch.class);
/**
* A stub method so we can return a text/html representation using
* the right template.
*
* {@link #searchSubmitHtml(String, long, int, javax.ws.rs.core.Request,
* javax.ws.rs.core.UriInfo)}
*
* @param terms
* @param offset
* @param limit
* @param request
* @param uriInfo
* @return
* @throws RepositoryException
*/
@GET
@Timed
@HtmlTemplate("search:results")
@Produces({TEXT_HTML})
public Dataset searchSubmitHtml(@QueryParam("q")
final String terms,
@QueryParam("offset") @DefaultValue("0")
final long offset,
@QueryParam("limit")
@DefaultValue("25")
final int limit,
@Context
final Request request,
@Context HttpServletResponse servletResponse,
@Context
final UriInfo uriInfo) throws RepositoryException {
return getSearchDataset(terms, offset, limit, servletResponse, uriInfo);
}
/**
* Execute a basic full-text search across the repository
*
* GET /fcr:search?q=term
*
* @param terms
* @param offset
* @param limit
* @param request
* @param uriInfo
* @return
* @throws RepositoryException
*/
@GET
@Timed
@Produces({TURTLE, N3, N3_ALT1, N3_ALT2, RDF_XML, RDF_JSON, NTRIPLES})
public Dataset searchSubmitRdf(@QueryParam("q") final String terms,
@QueryParam("offset") @DefaultValue("0") final long offset,
@QueryParam("limit") @DefaultValue("25") final int limit,
@Context final Request request,
@Context HttpServletResponse servletResponse,
@Context final UriInfo uriInfo) throws RepositoryException {
if (terms == null) {
LOGGER.trace("Received search request, but terms was empty. Aborting.");
throw new WebApplicationException(status(BAD_REQUEST).entity(
"q parameter is mandatory").build());
}
return getSearchDataset(terms, offset, limit, servletResponse, uriInfo);
}
private Dataset getSearchDataset(final String terms,
final long offset,
final int limit,
final HttpServletResponse servletResponse,
final UriInfo uriInfo)
throws RepositoryException {
try {
LOGGER.debug(
"Received search request with search terms {}, offset {}, and limit {}",
terms, offset, limit);
final Resource searchResult;
if (terms == null) {
searchResult = createResource(uriInfo.getBaseUriBuilder()
.path(FedoraFieldSearch.class)
.build().toString());
} else {
searchResult = createResource(uriInfo.getBaseUriBuilder()
.path(FedoraFieldSearch.class)
.queryParam("q", terms)
.build().toString());
}
final HttpGraphSubjects subjects = new HttpGraphSubjects(session, FedoraNodes.class, uriInfo);
final Dataset dataset =
nodeService.searchRepository(subjects, searchResult,
session, terms, limit, offset);
final Model searchModel = createDefaultModel();
if (terms != null) {
final Resource pageResource = createResource(uriInfo.getRequestUri().toASCIIString());
searchModel.add(pageResource, RDF.type, SEARCH_PAGE);
searchModel.add(pageResource, RDF.type, PAGE);
searchModel.add(pageResource, PAGE_OF, searchResult);
searchModel.add(pageResource,
SEARCH_ITEMS_PER_PAGE,
searchModel.createTypedLiteral(limit));
searchModel.add(pageResource,
SEARCH_OFFSET,
searchModel.createTypedLiteral(offset));
searchModel.add(pageResource, SEARCH_TERMS, terms);
if (dataset.getDefaultModel()
.contains(searchResult,
SEARCH_HAS_MORE,
searchModel.createTypedLiteral(true))) {
final Resource nextPageResource =
searchModel.createResource(uriInfo
.getBaseUriBuilder()
.path(FedoraFieldSearch.class)
.queryParam("q", terms)
.queryParam("offset", offset + limit)
.queryParam("limit", limit)
.build()
.toString());
searchModel.add(pageResource, NEXT_PAGE, nextPageResource);
} else {
searchModel.add(pageResource, NEXT_PAGE, RDF.nil);
}
final String firstPage = uriInfo
.getBaseUriBuilder()
.path(FedoraFieldSearch.class)
.queryParam("q", terms)
.queryParam("offset", 0)
.queryParam("limit", limit)
.build()
.toString();
final Resource firstPageResource =
searchModel.createResource(firstPage);
searchModel.add(subjects.getContext(), FIRST_PAGE, firstPageResource);
servletResponse.addHeader("Link", firstPage + ";rel=\"first\"");
dataset.addNamedModel("search-pagination", searchModel);
}
return dataset;
} finally {
session.logout();
}
}
}