forked from DSpace/DSpace
/
DiscoveryRestController.java
199 lines (167 loc) · 9.91 KB
/
DiscoveryRestController.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
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dspace.app.rest.link.HalLinkService;
import org.dspace.app.rest.model.FacetConfigurationRest;
import org.dspace.app.rest.model.FacetResultsRest;
import org.dspace.app.rest.model.SearchConfigurationRest;
import org.dspace.app.rest.model.SearchResultsRest;
import org.dspace.app.rest.model.SearchSupportRest;
import org.dspace.app.rest.model.hateoas.FacetConfigurationResource;
import org.dspace.app.rest.model.hateoas.FacetResultsResource;
import org.dspace.app.rest.model.hateoas.FacetsResource;
import org.dspace.app.rest.model.hateoas.SearchConfigurationResource;
import org.dspace.app.rest.model.hateoas.SearchResultsResource;
import org.dspace.app.rest.model.hateoas.SearchSupportResource;
import org.dspace.app.rest.parameter.SearchFilter;
import org.dspace.app.rest.repository.DiscoveryRestRepository;
import org.dspace.app.rest.utils.ScopeResolver;
import org.dspace.app.rest.utils.Utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* The controller for the api/discover endpoint
*/
@RestController
@RequestMapping("/api/" + SearchResultsRest.CATEGORY)
public class DiscoveryRestController implements InitializingBean {
private static final Logger log = Logger.getLogger(ScopeResolver.class);
@Autowired
protected Utils utils;
@Autowired
private DiscoverableEndpointsService discoverableEndpointsService;
@Autowired
private DiscoveryRestRepository discoveryRestRepository;
@Autowired
private HalLinkService halLinkService;
@Override
public void afterPropertiesSet() throws Exception {
discoverableEndpointsService
.register(this, Arrays.asList(new Link("/api/" + SearchResultsRest.CATEGORY, SearchResultsRest.CATEGORY)));
}
@RequestMapping(method = RequestMethod.GET)
public SearchSupportResource getSearchSupport(@RequestParam(name = "scope", required = false) String dsoScope,
@RequestParam(name = "configuration", required = false) String
configurationName)
throws Exception {
SearchSupportRest searchSupportRest = discoveryRestRepository.getSearchSupport();
SearchSupportResource searchSupportResource = new SearchSupportResource(searchSupportRest);
halLinkService.addLinks(searchSupportResource);
return searchSupportResource;
}
@RequestMapping(method = RequestMethod.GET, value = "/search")
public SearchConfigurationResource getSearchConfiguration(
@RequestParam(name = "scope", required = false) String dsoScope,
@RequestParam(name = "configuration", required = false) String configurationName) throws Exception {
if (log.isTraceEnabled()) {
log.trace("Retrieving search configuration for scope " + StringUtils.trimToEmpty(dsoScope)
+ " and configuration name " + StringUtils.trimToEmpty(configurationName));
}
SearchConfigurationRest searchConfigurationRest = discoveryRestRepository
.getSearchConfiguration(dsoScope, configurationName);
SearchConfigurationResource searchConfigurationResource = new SearchConfigurationResource(
searchConfigurationRest);
halLinkService.addLinks(searchConfigurationResource);
return searchConfigurationResource;
}
@RequestMapping(method = RequestMethod.GET, value = "/search/facets")
public FacetsResource getFacets(@RequestParam(name = "query", required = false) String query,
@RequestParam(name = "dsoType", required = false) String dsoType,
@RequestParam(name = "scope", required = false) String dsoScope,
@RequestParam(name = "configuration", required = false) String configurationName,
List<SearchFilter> searchFilters,
Pageable page) throws Exception {
if (log.isTraceEnabled()) {
log.trace("Searching with scope: " + StringUtils.trimToEmpty(dsoScope)
+ ", configuration name: " + StringUtils.trimToEmpty(configurationName)
+ ", dsoType: " + StringUtils.trimToEmpty(dsoType)
+ ", query: " + StringUtils.trimToEmpty(dsoType)
+ ", filters: " + Objects.toString(searchFilters));
}
SearchResultsRest searchResultsRest = discoveryRestRepository
.getAllFacets(query, dsoType, dsoScope, configurationName, searchFilters);
FacetsResource facetsResource = new FacetsResource(searchResultsRest, page);
halLinkService.addLinks(facetsResource, page);
return facetsResource;
}
@RequestMapping(method = RequestMethod.GET, value = "/search/objects")
public SearchResultsResource getSearchObjects(@RequestParam(name = "query", required = false) String query,
@RequestParam(name = "dsoType", required = false) String dsoType,
@RequestParam(name = "scope", required = false) String dsoScope,
@RequestParam(name = "configuration", required = false) String
configurationName,
List<SearchFilter> searchFilters,
Pageable page) throws Exception {
if (log.isTraceEnabled()) {
log.trace("Searching with scope: " + StringUtils.trimToEmpty(dsoScope)
+ ", configuration name: " + StringUtils.trimToEmpty(configurationName)
+ ", dsoType: " + StringUtils.trimToEmpty(dsoType)
+ ", query: " + StringUtils.trimToEmpty(dsoType)
+ ", filters: " + Objects.toString(searchFilters)
+ ", page: " + Objects.toString(page));
}
//Get the Search results in JSON format
SearchResultsRest searchResultsRest = null;
searchResultsRest = discoveryRestRepository
.getSearchObjects(query, dsoType, dsoScope, configurationName, searchFilters, page);
//Convert the Search JSON results to paginated HAL resources
SearchResultsResource searchResultsResource = new SearchResultsResource(searchResultsRest, utils, page);
halLinkService.addLinks(searchResultsResource, page);
return searchResultsResource;
}
@RequestMapping(method = RequestMethod.GET, value = "/facets")
public FacetConfigurationResource getFacetsConfiguration(
@RequestParam(name = "scope", required = false) String dsoScope,
@RequestParam(name = "configuration", required = false) String configurationName,
Pageable pageable) throws Exception {
if (log.isTraceEnabled()) {
log.trace("Retrieving facet configuration for scope " + StringUtils.trimToEmpty(dsoScope)
+ " and configuration name " + StringUtils.trimToEmpty(configurationName));
}
FacetConfigurationRest facetConfigurationRest = discoveryRestRepository
.getFacetsConfiguration(dsoScope, configurationName);
FacetConfigurationResource facetConfigurationResource = new FacetConfigurationResource(facetConfigurationRest);
halLinkService.addLinks(facetConfigurationResource, pageable);
return facetConfigurationResource;
}
@RequestMapping(method = RequestMethod.GET, value = "/facets/{name}")
public ResourceSupport getFacetValues(@PathVariable("name") String facetName,
@RequestParam(name = "prefix", required = false) String prefix,
@RequestParam(name = "query", required = false) String query,
@RequestParam(name = "dsoType", required = false) String dsoType,
@RequestParam(name = "scope", required = false) String dsoScope,
List<SearchFilter> searchFilters,
Pageable page) throws Exception {
if (log.isTraceEnabled()) {
log.trace("Facetting on facet " + facetName + " with scope: " + StringUtils.trimToEmpty(dsoScope)
+ ", dsoType: " + StringUtils.trimToEmpty(dsoType)
+ ", prefix: " + StringUtils.trimToEmpty(prefix)
+ ", query: " + StringUtils.trimToEmpty(dsoType)
+ ", filters: " + Objects.toString(searchFilters)
+ ", page: " + Objects.toString(page));
}
FacetResultsRest facetResultsRest = discoveryRestRepository
.getFacetObjects(facetName, prefix, query, dsoType, dsoScope, searchFilters, page);
FacetResultsResource facetResultsResource = new FacetResultsResource(facetResultsRest);
halLinkService.addLinks(facetResultsResource, page);
return facetResultsResource;
}
}