|
4 | 4 |
|
5 | 5 | import com.fasterxml.jackson.annotation.JsonFilter; |
6 | 6 | import com.fasterxml.jackson.annotation.JsonInclude; |
| 7 | +import com.fasterxml.jackson.core.JsonProcessingException; |
7 | 8 | import com.fasterxml.jackson.databind.ObjectMapper; |
8 | 9 | import com.fasterxml.jackson.databind.PropertyNamingStrategy; |
9 | 10 | import com.fasterxml.jackson.databind.SerializationFeature; |
@@ -222,86 +223,102 @@ public Response getSingleRecord(@PathParam("codeId") Long codeId, @QueryParam("f |
222 | 223 | * @param parameters the JSON SearchData Object of search parameters |
223 | 224 | * @param format the optional output format (YAML/JSON/XML; JSON is default) |
224 | 225 | * @return the output of the SOLR search results, if any |
225 | | - * @throws IOException on unexpected IO errors |
226 | | - * @throws URISyntaxException on URI search errors |
227 | 226 | */ |
228 | 227 | @POST |
229 | 228 | @Produces ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "text/yaml"}) |
230 | 229 | @Consumes (MediaType.APPLICATION_JSON) |
231 | | - public Response search(String parameters, @QueryParam("format") String format) throws IOException, URISyntaxException { |
| 230 | + public Response search(String parameters, @QueryParam("format") String format) { |
232 | 231 | // no search configured, you get nothing |
233 | 232 | if ("".equals(SEARCH_URL)) |
234 | 233 | return Response |
235 | 234 | .status(Response.Status.NO_CONTENT) |
236 | 235 | .build(); |
237 | 236 |
|
238 | | - // get a set of search parameters |
239 | | - SearchData searchFor = SearchData.parseJson(new StringReader(parameters)); |
240 | | - |
241 | | - CloseableHttpClient hc = HttpClientBuilder |
242 | | - .create() |
243 | | - .build(); |
244 | | - |
245 | | - URIBuilder builder = new URIBuilder(SEARCH_URL) |
246 | | - .addParameter("q", searchFor.toQ()) |
247 | | - .addParameter("fl", "json") |
248 | | - .addParameter("sort", searchFor.getSort()); |
249 | | - // if values are specified for rows and start, supply those. |
250 | | - if (null!=searchFor.getRows()) |
251 | | - builder.addParameter("rows", String.valueOf(searchFor.getRows())); |
252 | | - if (null!=searchFor.getStart()) |
253 | | - builder.addParameter("start", String.valueOf(searchFor.getStart())); |
254 | | - |
255 | | - HttpGet get = new HttpGet(builder.build()); |
256 | | - |
257 | | - HttpResponse response = hc.execute(get); |
258 | | - |
259 | | - if (HttpStatus.SC_OK==response.getStatusLine().getStatusCode()) { |
260 | | - SolrResult result = JSON_MAPPER.readValue(EntityUtils.toString(response.getEntity()), SolrResult.class); |
261 | | - // construct a search response object |
262 | | - SearchResponse query = new SearchResponse(); |
263 | | - query.setStart(result.getSearchResponse().getStart()); |
264 | | - query.setNumFound(result.getSearchResponse().getNumFound()); |
265 | | - |
266 | | - // if there are matched documents, load them in |
267 | | - if ( null!=result.getSearchResponse().getDocuments() ) { |
268 | | - for ( SolrDocument doc : result.getSearchResponse().getDocuments() ) { |
269 | | - query.add(JSON_MAPPER.readValue(doc.getJson(), DOECodeMetadata.class)); |
| 237 | + try { |
| 238 | + // get a set of search parameters |
| 239 | + SearchData searchFor = SearchData.parseJson(new StringReader(parameters)); |
| 240 | + |
| 241 | + CloseableHttpClient hc = HttpClientBuilder |
| 242 | + .create() |
| 243 | + .build(); |
| 244 | + |
| 245 | + URIBuilder builder = new URIBuilder(SEARCH_URL) |
| 246 | + .addParameter("q", searchFor.toQ()) |
| 247 | + .addParameter("fl", "json") |
| 248 | + .addParameter("sort", searchFor.getSort()); |
| 249 | + // if values are specified for rows and start, supply those. |
| 250 | + if (null!=searchFor.getRows()) |
| 251 | + builder.addParameter("rows", String.valueOf(searchFor.getRows())); |
| 252 | + if (null!=searchFor.getStart()) |
| 253 | + builder.addParameter("start", String.valueOf(searchFor.getStart())); |
| 254 | + |
| 255 | + HttpGet get = new HttpGet(builder.build()); |
| 256 | + |
| 257 | + HttpResponse response = hc.execute(get); |
| 258 | + |
| 259 | + if (HttpStatus.SC_OK==response.getStatusLine().getStatusCode()) { |
| 260 | + SolrResult result = JSON_MAPPER.readValue(EntityUtils.toString(response.getEntity()), SolrResult.class); |
| 261 | + // construct a search response object |
| 262 | + SearchResponse query = new SearchResponse(); |
| 263 | + query.setStart(result.getSearchResponse().getStart()); |
| 264 | + query.setNumFound(result.getSearchResponse().getNumFound()); |
| 265 | + |
| 266 | + // if there are matched documents, load them in |
| 267 | + if ( null!=result.getSearchResponse().getDocuments() ) { |
| 268 | + for ( SolrDocument doc : result.getSearchResponse().getDocuments() ) { |
| 269 | + query.add(JSON_MAPPER.readValue(doc.getJson(), DOECodeMetadata.class)); |
| 270 | + } |
| 271 | + // check out the FACETS |
| 272 | + query.setFacets(result.getSolrFacet().getValues()); |
| 273 | + } |
| 274 | + // respond with the appropriate format based on the input parameter |
| 275 | + if ("xml".equals(format)) { |
| 276 | + return Response |
| 277 | + .ok() |
| 278 | + .header("Content-Type", MediaType.APPLICATION_XML) |
| 279 | + .entity(XML_MAPPER |
| 280 | + .writer(filter) |
| 281 | + .writeValueAsString(query)) |
| 282 | + .build(); |
| 283 | + } else if ("yaml".equals(format)) { |
| 284 | + return Response |
| 285 | + .ok() |
| 286 | + .header("Content-Type", "text/yaml") |
| 287 | + .entity(YAML_MAPPER |
| 288 | + .writer(filter) |
| 289 | + .writeValueAsString(query)) |
| 290 | + .build(); |
| 291 | + } else { |
| 292 | + return Response |
| 293 | + .ok() |
| 294 | + .header("Content-Type", MediaType.APPLICATION_JSON) |
| 295 | + .entity(JSON_MAPPER |
| 296 | + .writer(filter) |
| 297 | + .writeValueAsString(query)) |
| 298 | + .build(); |
270 | 299 | } |
271 | | - // check out the FACETS |
272 | | - query.setFacets(result.getSolrFacet().getValues()); |
273 | | - } |
274 | | - // respond with the appropriate format based on the input parameter |
275 | | - if ("xml".equals(format)) { |
276 | | - return Response |
277 | | - .ok() |
278 | | - .header("Content-Type", MediaType.APPLICATION_XML) |
279 | | - .entity(XML_MAPPER |
280 | | - .writer(filter) |
281 | | - .writeValueAsString(query)) |
282 | | - .build(); |
283 | | - } else if ("yaml".equals(format)) { |
284 | | - return Response |
285 | | - .ok() |
286 | | - .header("Content-Type", "text/yaml") |
287 | | - .entity(YAML_MAPPER |
288 | | - .writer(filter) |
289 | | - .writeValueAsString(query)) |
290 | | - .build(); |
291 | 300 | } else { |
292 | | - return Response |
293 | | - .ok() |
294 | | - .header("Content-Type", MediaType.APPLICATION_JSON) |
295 | | - .entity(JSON_MAPPER |
296 | | - .writer(filter) |
297 | | - .writeValueAsString(query)) |
| 301 | + // let the user know something failed |
| 302 | + return ErrorResponse |
| 303 | + .status(response.getStatusLine().getStatusCode()) |
| 304 | + .message(EntityUtils.toString(response.getEntity())) |
298 | 305 | .build(); |
299 | 306 | } |
300 | | - } else { |
301 | | - // let the user know something failed |
| 307 | + } catch ( URISyntaxException e ) { |
| 308 | + log.warn("URI Error: " + e.getMessage()); |
| 309 | + return ErrorResponse |
| 310 | + .internalServerError("Unable to contact search provider.") |
| 311 | + .build(); |
| 312 | + } catch ( JsonProcessingException e ) { |
| 313 | + log.warn("Unable to process JSON from: " + parameters); |
| 314 | + log.warn("Message: " + e.getMessage()); |
| 315 | + return ErrorResponse |
| 316 | + .internalServerError("JSON formatting error.") |
| 317 | + .build(); |
| 318 | + } catch ( IOException e ) { |
| 319 | + log.warn("Unhandled IO Error: " + e.getMessage()); |
302 | 320 | return ErrorResponse |
303 | | - .status(response.getStatusLine().getStatusCode()) |
304 | | - .message(EntityUtils.toString(response.getEntity())) |
| 321 | + .internalServerError("IO Error.") |
305 | 322 | .build(); |
306 | 323 | } |
307 | 324 | } |
|
0 commit comments