Skip to content

Commit 2b67ecb

Browse files
committed
Prevent all collections from SearchSettings from being modifiable, and initialize resourceType lists in search states from new ArrayLists so they can be safely modified. Addresses bug where a blank advanced search would cause the File object type to be removed from default resource types list
1 parent 51815fd commit 2b67ecb

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

search-solr/src/main/java/edu/unc/lib/boxc/search/solr/config/SearchSettings.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package edu.unc.lib.boxc.search.solr.config;
1717

1818
import java.util.ArrayList;
19+
import java.util.Collections;
1920
import java.util.HashMap;
2021
import java.util.HashSet;
2122
import java.util.Iterator;
@@ -173,6 +174,7 @@ public void setProperties(Properties properties) {
173174
setQueryMaxLength(Integer.parseInt(properties.getProperty("search.query.maxLength", "255")));
174175
setDefaultOperator(properties.getProperty("search.query.defaultOperator", ""));
175176
populateCollectionFromProperty("search.query.operators", operators, properties, ",");
177+
operators = Collections.unmodifiableSet(operators);
176178
setDefaultPerPage(Integer.parseInt(properties.getProperty("search.results.defaultPerPage", "0")));
177179
setDefaultCollectionsPerPage(Integer.parseInt(properties.getProperty("search.results.defaultCollectionsPerPage",
178180
"0")));
@@ -200,6 +202,10 @@ public void setProperties(Properties properties) {
200202
",");
201203
populateCollectionFromProperty("search.facet.defaultStructureBrowse", facetNamesStructureBrowse,
202204
properties, ",");
205+
facetNames = Collections.unmodifiableList(facetNames);
206+
searchFacetNames = Collections.unmodifiableList(searchFacetNames);
207+
collectionBrowseFacetNames = Collections.unmodifiableList(collectionBrowseFacetNames);
208+
facetNamesStructureBrowse = Collections.unmodifiableList(facetNamesStructureBrowse);
203209
try {
204210
populateClassMapFromProperty("search.facet.class.", "edu.unc.lib.boxc.search.solr.facets.",
205211
this.facetClasses, properties);
@@ -213,23 +219,35 @@ public void setProperties(Properties properties) {
213219
populateCollectionFromProperty("search.field.searchable", searchableFields, properties, ",");
214220
populateCollectionFromProperty("search.field.rangeSearchable", rangeSearchableFields, properties, ",");
215221
populateCollectionFromProperty("search.field.dateSearchable", dateSearchableFields, properties, ",");
222+
searchableFields = Collections.unmodifiableSet(searchableFields);
223+
rangeSearchableFields = Collections.unmodifiableSet(rangeSearchableFields);
224+
dateSearchableFields = Collections.unmodifiableSet(dateSearchableFields);
225+
216226
populateMapFromProperty("search.field.paramName.", searchFieldParams, properties);
217227
searchFieldKeys = getInvertedHashMap(searchFieldParams);
218228
populateMapFromProperty("search.field.display.", searchFieldLabels, properties);
219229
populateMapFromProperty("search.actions.", actions, properties);
220230
populateMapFromProperty("search.url.param.", searchStateParams, properties);
221231
populateListMapFromProperty("search.results.fields", resultFields, properties);
232+
searchFieldParams = Collections.unmodifiableMap(searchFieldParams);
233+
searchFieldLabels = Collections.unmodifiableMap(searchFieldLabels);
234+
actions = Collections.unmodifiableMap(actions);
235+
searchStateParams = Collections.unmodifiableMap(searchStateParams);
236+
resultFields = Collections.unmodifiableMap(resultFields);
222237

223238
// Populate sort types
224239
setSortReverse(properties.getProperty("search.sort.order.reverse", ""));
225240
setSortNormal(properties.getProperty("search.sort.order.normal", ""));
226241
populateMapFromProperty("search.sort.name.", sortDisplayNames, properties);
227242
populateCollectionFromProperty("search.sort.displayOrder", sortDisplayOrder, properties, "\\|");
243+
sortDisplayOrder = Collections.unmodifiableList(sortDisplayOrder);
228244

229245
// Access field names
230246
this.setAllowPatronAccess(new Boolean(properties.getProperty("search.access.allowPatrons", "true")));
231247
populateCollectionFromProperty("search.access.fields", accessFields, properties, ",");
232248
populateCollectionFromProperty("search.access.filterableFields", accessFilterableFields, properties, ",");
249+
accessFields = Collections.unmodifiableSet(accessFields);
250+
accessFilterableFields = Collections.unmodifiableSet(accessFilterableFields);
233251

234252
// Resource Types
235253
setResourceTypeFile(properties.getProperty("search.resource.type.file", ""));
@@ -242,6 +260,9 @@ public void setProperties(Properties properties) {
242260
populateCollectionFromProperty("search.resource.searchDefault", defaultResourceTypes, properties, ",");
243261
populateCollectionFromProperty("search.resource.collectionDefault", defaultCollectionResourceTypes,
244262
properties, ",");
263+
resourceTypes = Collections.unmodifiableSet(resourceTypes);
264+
defaultResourceTypes = Collections.unmodifiableList(defaultResourceTypes);
265+
defaultCollectionResourceTypes = Collections.unmodifiableList(defaultCollectionResourceTypes);
245266

246267
Iterator<Map.Entry<Object, Object>> propIt = properties.entrySet().iterator();
247268
while (propIt.hasNext()) {
@@ -258,6 +279,7 @@ public void setProperties(Properties properties) {
258279
this.sortTypes.put(propertyKey.substring(propertyKey.lastIndexOf(".") + 1), sortFields);
259280
}
260281
}
282+
sortTypes = Collections.unmodifiableMap(sortTypes);
261283
}
262284

263285
public int getFacetsPerGroup() {

search-solr/src/main/java/edu/unc/lib/boxc/search/solr/services/SearchStateFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public SearchState createSearchState() {
7171
SearchState searchState = new SearchState();
7272

7373
searchState.setBaseFacetLimit(searchSettings.facetsPerGroup);
74-
searchState.setResourceTypes(searchSettings.defaultResourceTypes);
74+
searchState.setResourceTypes(new ArrayList<>(searchSettings.defaultResourceTypes));
7575
searchState.setSearchTermOperator(searchSettings.defaultOperator);
7676
searchState.setRowsPerPage(searchSettings.defaultPerPage);
7777
searchState.setFacetsToRetrieve(new ArrayList<>(searchSettings.searchFacetNames));
@@ -180,7 +180,7 @@ public SearchState createHierarchyListSearchState() {
180180
public SearchState createStructureBrowseSearchState() {
181181
SearchState searchState = new SearchState();
182182
searchState.setResultFields(new ArrayList<>(searchSettings.resultFields.get("structure")));
183-
searchState.setResourceTypes(searchSettings.defaultResourceTypes);
183+
searchState.setResourceTypes(new ArrayList<>(searchSettings.defaultResourceTypes));
184184
searchState.setSearchTermOperator(searchSettings.defaultOperator);
185185
searchState.setRowsPerPage(0);
186186
searchState.setStartRow(0);
@@ -206,7 +206,7 @@ public SearchState createHierarchicalBrowseSearchState() {
206206
SearchState searchState = new SearchState();
207207
searchState.setResultFields(new ArrayList<>(searchSettings.resultFields.get("structure")));
208208
searchState.setBaseFacetLimit(searchSettings.facetsPerGroup);
209-
searchState.setResourceTypes(searchSettings.defaultResourceTypes);
209+
searchState.setResourceTypes(new ArrayList<>(searchSettings.defaultResourceTypes));
210210
searchState.setSearchTermOperator(searchSettings.defaultOperator);
211211
searchState.setRowsPerPage(searchSettings.defaultPerPage);
212212
searchState.setStartRow(0);
@@ -244,7 +244,7 @@ public SearchState createHierarchicalBrowseSearchState(Map<String,String[]> requ
244244
public SearchState createFacetSearchState(String facetField, String facetSort, int maxResults) {
245245
SearchState searchState = new SearchState();
246246

247-
searchState.setResourceTypes(searchSettings.defaultResourceTypes);
247+
searchState.setResourceTypes(new ArrayList<>(searchSettings.defaultResourceTypes));
248248
searchState.setRowsPerPage(0);
249249
searchState.setStartRow(0);
250250

@@ -358,7 +358,7 @@ private void populateSearchState(SearchState searchState, Map<String,String[]> r
358358

359359
//Determine resource types selected
360360
parameter = getParameter(request, searchSettings.searchStateParam("RESOURCE_TYPES"));
361-
ArrayList<String> resourceTypes = new ArrayList<>();
361+
var resourceTypes = new ArrayList<String>();
362362
if (parameter == null) {
363363
//If resource types aren't specified, load the defaults.
364364
resourceTypes.addAll(searchSettings.defaultResourceTypes);

web-access-app/src/main/java/edu/unc/lib/boxc/web/access/controllers/SearchActionController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ private void setDefaultRollup(SearchRequest searchRequest, boolean isListing) {
167167
LOG.debug("Rollup not specified in request, determine rollup should be set to {}", enableRollup);
168168
searchRequest.getSearchState().setRollup(enableRollup);
169169
if (!enableRollup && !isListing) {
170+
LOG.debug("Removing File objects from non-rollup query {}",
171+
searchRequest.getSearchState().getResourceTypes());
170172
searchRequest.getSearchState().getResourceTypes().remove(ResourceType.File.name());
171173
}
172174
}

0 commit comments

Comments
 (0)