|
5 | 5 | import com.fasterxml.jackson.annotation.JsonFilter; |
6 | 6 | import com.fasterxml.jackson.annotation.JsonInclude; |
7 | 7 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 8 | +import com.fasterxml.jackson.core.type.TypeReference; |
8 | 9 | import com.fasterxml.jackson.databind.ObjectMapper; |
9 | 10 | import com.fasterxml.jackson.databind.PropertyNamingStrategy; |
10 | 11 | import com.fasterxml.jackson.databind.SerializationFeature; |
11 | 12 | import com.fasterxml.jackson.databind.module.SimpleModule; |
| 13 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
12 | 14 | import com.fasterxml.jackson.databind.node.ObjectNode; |
13 | 15 | import com.fasterxml.jackson.databind.ser.FilterProvider; |
14 | 16 | import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; |
|
25 | 27 | import gov.osti.search.SolrFacet; |
26 | 28 | import java.io.IOException; |
27 | 29 | import java.io.StringReader; |
| 30 | +import java.lang.reflect.Field; |
28 | 31 | import java.net.URISyntaxException; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Arrays; |
29 | 34 | import java.util.List; |
30 | 35 | import java.util.Map; |
31 | 36 | import java.util.TimeZone; |
| 37 | +import java.util.stream.Collectors; |
32 | 38 | import javax.servlet.ServletContext; |
33 | 39 | import javax.ws.rs.Consumes; |
34 | 40 | import javax.ws.rs.GET; |
@@ -241,12 +247,62 @@ public Response searchGet(@Context UriInfo uriInfo, @QueryParam("format") String |
241 | 247 | // map parameters into JSON |
242 | 248 | ObjectNode getParams = mapper.createObjectNode(); |
243 | 249 |
|
| 250 | + SearchData so = new SearchData(); |
| 251 | + List<String> arrayFields = new ArrayList<>(); |
| 252 | + // determine string array parameters from the target object |
| 253 | + for (Field field : so.getClass().getDeclaredFields()) { |
| 254 | + if (field.getType().getSimpleName().equalsIgnoreCase("string[]")) |
| 255 | + arrayFields.add(field.getName()); |
| 256 | + } |
| 257 | + |
244 | 258 | for (Map.Entry<String, List<String>> entry : params.entrySet()) { |
245 | 259 | String key = entry.getKey(); |
246 | 260 | String value = entry.getValue().get(0); |
247 | 261 |
|
248 | | - // if key and value, store in JSON |
249 | | - if (!StringUtils.isBlank(key) && !StringUtils.isBlank(value)) |
| 262 | + // if no key/value skip |
| 263 | + if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) |
| 264 | + continue; |
| 265 | + |
| 266 | + // convert snake_case input into camelCase for reflection matching |
| 267 | + String keyCamelCase = |
| 268 | + Arrays.stream(key.split("_")) |
| 269 | + .map(String::toLowerCase) |
| 270 | + .map(s -> s.substring(0, 1).toUpperCase() + s.substring(1)) |
| 271 | + .collect(Collectors.joining()); |
| 272 | + keyCamelCase = keyCamelCase.substring(0, 1).toLowerCase() + keyCamelCase.substring(1); |
| 273 | + |
| 274 | + // determine if field or value indicate an array |
| 275 | + boolean isArrayField = false; |
| 276 | + boolean isArrayValue = value.startsWith("[") && value.endsWith("]"); |
| 277 | + if (arrayFields.contains(keyCamelCase)) |
| 278 | + isArrayField = true; |
| 279 | + |
| 280 | + // convert input into JSON format |
| 281 | + if (isArrayField) { |
| 282 | + // array field requires special formatting for JSON mapper |
| 283 | + ArrayNode values = mapper.createArrayNode(); |
| 284 | + |
| 285 | + // input is already in JSON array format, map it |
| 286 | + if (isArrayValue) { |
| 287 | + try { |
| 288 | + values = mapper.readValue(value, new TypeReference<ArrayNode>(){}); |
| 289 | + } catch (IOException ex) { |
| 290 | + log.warn("Unable to process JSON ARRAY from GET parameter."); |
| 291 | + log.warn("Message: " + ex.getMessage()); |
| 292 | + return ErrorResponse |
| 293 | + .internalServerError("JSON ARRAY parameter formatting error.") |
| 294 | + .build(); |
| 295 | + } |
| 296 | + } |
| 297 | + // input is a normal string, add it to array node |
| 298 | + else |
| 299 | + values.add(value); |
| 300 | + |
| 301 | + // set key with string array value |
| 302 | + getParams.set(key, values); |
| 303 | + } |
| 304 | + else |
| 305 | + // set key with string value |
250 | 306 | getParams.put(key, value); |
251 | 307 | } |
252 | 308 |
|
@@ -283,6 +339,7 @@ public Response searchPost(String parameters, @QueryParam("format") String forma |
283 | 339 | .status(Response.Status.NO_CONTENT) |
284 | 340 | .build(); |
285 | 341 |
|
| 342 | + // call search |
286 | 343 | return search(parameters, format); |
287 | 344 | } |
288 | 345 |
|
|
0 commit comments