Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Commit cf6265d

Browse files
committed
modified SearchService to avoid JSON formatting error resulting in stack
trace and unnecessary error messages (should result in status code 500 return on JSON errors) Signed-off-by: Neal Ensor <[email protected]>
1 parent 0ba07e4 commit cf6265d

File tree

1 file changed

+82
-65
lines changed

1 file changed

+82
-65
lines changed

src/main/java/gov/osti/services/SearchService.java

Lines changed: 82 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.fasterxml.jackson.annotation.JsonFilter;
66
import com.fasterxml.jackson.annotation.JsonInclude;
7+
import com.fasterxml.jackson.core.JsonProcessingException;
78
import com.fasterxml.jackson.databind.ObjectMapper;
89
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
910
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -222,86 +223,102 @@ public Response getSingleRecord(@PathParam("codeId") Long codeId, @QueryParam("f
222223
* @param parameters the JSON SearchData Object of search parameters
223224
* @param format the optional output format (YAML/JSON/XML; JSON is default)
224225
* @return the output of the SOLR search results, if any
225-
* @throws IOException on unexpected IO errors
226-
* @throws URISyntaxException on URI search errors
227226
*/
228227
@POST
229228
@Produces ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "text/yaml"})
230229
@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) {
232231
// no search configured, you get nothing
233232
if ("".equals(SEARCH_URL))
234233
return Response
235234
.status(Response.Status.NO_CONTENT)
236235
.build();
237236

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();
270299
}
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();
291300
} 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()))
298305
.build();
299306
}
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());
302320
return ErrorResponse
303-
.status(response.getStatusLine().getStatusCode())
304-
.message(EntityUtils.toString(response.getEntity()))
321+
.internalServerError("IO Error.")
305322
.build();
306323
}
307324
}

0 commit comments

Comments
 (0)