Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix[hmi-server]: Improving xDD errors #3131

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package software.uncharted.terarium.hmiserver.controller.documentservice;


import feign.FeignException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import software.uncharted.terarium.hmiserver.models.documentservice.responses.XDDDictionariesResponseOK;
import software.uncharted.terarium.hmiserver.models.documentservice.responses.XDDResponse;
import software.uncharted.terarium.hmiserver.proxies.documentservice.DocumentProxy;
Expand All @@ -16,16 +20,16 @@

@RestController
@Slf4j
@RequiredArgsConstructor
public class DictionariesController {

@Autowired
DocumentProxy proxy;
final DocumentProxy proxy;

@GetMapping("/dictionaries")
@Secured(Roles.USER)
public ResponseEntity<XDDResponse<XDDDictionariesResponseOK>> getAvailableDictionaries(@RequestParam(name = "all", defaultValue = "") final String all) {
try {
XDDResponse<XDDDictionariesResponseOK> response = proxy.getAvailableDictionaries(all);
final XDDResponse<XDDDictionariesResponseOK> response = proxy.getAvailableDictionaries(all);

if (response.getErrorMessage() != null) {
return ResponseEntity.internalServerError().build();
Expand All @@ -37,9 +41,12 @@ public ResponseEntity<XDDResponse<XDDDictionariesResponseOK>> getAvailableDictio

return ResponseEntity.ok(response);

} catch (RuntimeException e) {
log.error("Unable to get available dictionaries", e);
return ResponseEntity.internalServerError().build();
} catch (final FeignException e) {
log.error("xDD returned an exception for dictionaries search:", e);
throw new ResponseStatusException(HttpStatusCode.valueOf(e.status()), "There was an issue with the dictionaries request to xDD");
} catch (final Exception e) {
log.error("Unable to find dictionaries, an error occurred", e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Unable to find dictionaries, an error occurred");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package software.uncharted.terarium.hmiserver.controller.documentservice;

import feign.FeignException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import software.uncharted.terarium.hmiserver.models.documentservice.autocomplete.AutoComplete;
import software.uncharted.terarium.hmiserver.models.documentservice.responses.XDDExtractionsResponseOK;
import software.uncharted.terarium.hmiserver.models.documentservice.responses.XDDResponse;
Expand All @@ -19,6 +23,7 @@
@RequestMapping("/document/extractions")
@RestController
@Slf4j
@RequiredArgsConstructor
public class ExtractionController {


Expand All @@ -31,31 +36,29 @@ public class ExtractionController {

@Value("${xdd.api-es-key}")
String ESkey;

@Autowired
ExtractionProxy proxy;
final ExtractionProxy proxy;


@GetMapping
@Secured(Roles.USER)
public ResponseEntity<XDDResponse<XDDExtractionsResponseOK>> searchExtractions(
@RequestParam(required = false, name = "term") final String term,
@RequestParam(required = false, name = "page") final Integer page,
@RequestParam(required = false, name = "ASKEM_CLASS") String askemClass,
@RequestParam(required = false, name = "include_highlights") String include_highlights) {
@RequestParam(required = false, name = "ASKEM_CLASS") final String askemClass,
@RequestParam(required = false, name = "include_highlights") final String include_highlights) {


Matcher matcher = DOI_VALIDATION_PATTERN.matcher(term);
final Matcher matcher = DOI_VALIDATION_PATTERN.matcher(term);

boolean isDoi = matcher.find();
final boolean isDoi = matcher.find();
String apiKey = "";
if (key != null)
apiKey = key;
else
log.info("XDD API key missing. Image assets will not return correctly.");

try {
XDDResponse<XDDExtractionsResponseOK> response;
final XDDResponse<XDDExtractionsResponseOK> response;
if (isDoi) {
response = proxy.getExtractions(term, null, page, askemClass, include_highlights, apiKey);
} else {
Expand All @@ -73,30 +76,35 @@ public ResponseEntity<XDDResponse<XDDExtractionsResponseOK>> searchExtractions(
return ResponseEntity.ok(response);


} catch (RuntimeException e) {
log.error("Unable to search in extractions. An error occurred", e);
return ResponseEntity.internalServerError().build();

} catch (final FeignException e) {
log.error("xDD returned an exception for extraction search:", e);
throw new ResponseStatusException(HttpStatusCode.valueOf(e.status()), "There was an issue with the extraction search to xDD");
} catch (final Exception e) {
log.error("Unable to find extractions, an error occurred", e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Unable to find extractions, an error occurred");
}


}

@GetMapping("/askem-autocomplete/{term}")
@Secured(Roles.USER)
public ResponseEntity<List<String>> getAutocomplete(@PathVariable("term") String term) {
public ResponseEntity<List<String>> getAutocomplete(@PathVariable("term") final String term) {
try {

AutoComplete autoComplete = proxy.getAutocomplete(term);
final AutoComplete autoComplete = proxy.getAutocomplete(term);
if (autoComplete.hasNoSuggestions())
return ResponseEntity.noContent().build();

return ResponseEntity.ok(autoComplete.getAutoCompletes());


} catch (RuntimeException e) {
log.error("Unable to autocomplete");
return ResponseEntity.internalServerError().build();
} catch (final FeignException e) {
log.error("xDD returned an exception for autocomplete:", e);
throw new ResponseStatusException(HttpStatusCode.valueOf(e.status()), "xDD returned an exception for autocomplete");
} catch (final Exception e) {
log.error("Unable to find xdd Autocompletes", e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Unable to find xdd Autocompletes");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package software.uncharted.terarium.hmiserver.controller.documentservice;

import org.springframework.beans.factory.annotation.Autowired;
import feign.FeignException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.server.ResponseStatusException;
import software.uncharted.terarium.hmiserver.models.documentservice.responses.XDDRelatedDocumentsResponse;
import software.uncharted.terarium.hmiserver.models.documentservice.responses.XDDRelatedWordsResponse;
import software.uncharted.terarium.hmiserver.proxies.documentservice.DocumentProxy;
Expand All @@ -17,45 +20,51 @@
@RequestMapping("/document/related")
@RestController
@Slf4j
@RequiredArgsConstructor
public class RelatedController {

@Autowired
DocumentProxy proxy;
final DocumentProxy proxy;

@GetMapping("/document")
@Secured(Roles.USER)
public ResponseEntity<XDDRelatedDocumentsResponse> getRelatedDocuments(
@RequestParam("set") String set,
@RequestParam("docid") String docid) {
@RequestParam("set") final String set,
@RequestParam("docid") final String docid) {
try {
XDDRelatedDocumentsResponse response = proxy.getRelatedDocuments(set, docid);
final XDDRelatedDocumentsResponse response = proxy.getRelatedDocuments(set, docid);
if (response.getData() == null || response.getData().isEmpty())
return ResponseEntity.noContent().build();

return ResponseEntity.ok(response);

} catch (RuntimeException e) {
log.error("There was an error finding related documents", e);
return ResponseEntity.internalServerError().build();
} catch (final FeignException e) {
log.error("xDD returned an exception for related document search:", e);
throw new ResponseStatusException(HttpStatusCode.valueOf(e.status()), "There was an issue with the related request to xDD");
} catch (final Exception e) {
log.error("Unable to find related documents, an error occurred", e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Unable to find related documents, an error occurred");
}
}

@GetMapping("/word")
@Secured(Roles.USER)
public ResponseEntity<XDDRelatedWordsResponse> getRelatedWords(
@RequestParam("set") String set,
@RequestParam("word") String word) {
@RequestParam("set") final String set,
@RequestParam("word") final String word) {
try {
XDDRelatedWordsResponse response = proxy.getRelatedWords(set, word);
final XDDRelatedWordsResponse response = proxy.getRelatedWords(set, word);
if (response.getData() == null || response.getData().isEmpty()) {
return ResponseEntity.noContent().build();
}

return ResponseEntity.ok(response);

} catch (RuntimeException e) {
log.error("An error occurred finding related words", e);
return ResponseEntity.internalServerError().build();
} catch (final FeignException e) {
log.error("xDD returned an exception for related word search:", e);
throw new ResponseStatusException(HttpStatusCode.valueOf(e.status()), "There was an issue with the related word request to xDD");
} catch (final Exception e) {
log.error("Unable to find related words, an error occurred", e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Unable to find related words, an error occurred");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
package software.uncharted.terarium.hmiserver.controller.documentservice;


import feign.FeignException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import software.uncharted.terarium.hmiserver.models.documentservice.responses.XDDSetsResponse;
import software.uncharted.terarium.hmiserver.proxies.documentservice.DocumentProxy;
import software.uncharted.terarium.hmiserver.security.Roles;

@RequestMapping("/document")
@RestController
@Slf4j
@RequiredArgsConstructor
public class SetController {

@Autowired
DocumentProxy proxy;
final DocumentProxy proxy;

@GetMapping("/sets")
@Secured(Roles.USER)
public ResponseEntity<XDDSetsResponse> getAvailableSets() {

try {
XDDSetsResponse response = proxy.getAvailableSets();
final XDDSetsResponse response = proxy.getAvailableSets();

if (response.getAvailableSets() == null || response.getAvailableSets().isEmpty())
return ResponseEntity.noContent().build();

return ResponseEntity.ok(response);

} catch (RuntimeException e) {
log.error("There was an error finding available sets", e);
return ResponseEntity.internalServerError().build();
} catch (final FeignException e) {
log.error("xDD returned an exception for set search:", e);
throw new ResponseStatusException(HttpStatusCode.valueOf(e.status()), "There was an issue with the request to xDD");
} catch (final Exception e) {
log.error("Unable to find sets, an error occurred", e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Unable to find documents, an error occurred");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package software.uncharted.terarium.hmiserver.controller.documentservice;

import feign.FeignException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import software.uncharted.terarium.hmiserver.models.documentservice.responses.DocumentsResponseOK;
import software.uncharted.terarium.hmiserver.models.documentservice.responses.XDDResponse;
import software.uncharted.terarium.hmiserver.proxies.documentservice.DocumentProxy;
Expand All @@ -17,10 +21,10 @@
@RequestMapping("/documents")
@RestController
@Slf4j
@RequiredArgsConstructor
public class XDDDocumentController {

@Autowired
DocumentProxy proxy;
final DocumentProxy proxy;


@Value("${xdd.api-es-key}")
Expand All @@ -30,8 +34,8 @@ public class XDDDocumentController {
@GetMapping
@Secured(Roles.USER)
public ResponseEntity<XDDResponse<DocumentsResponseOK>> getDocuments(
@RequestParam(required = false, name = "docid") String docid,
@RequestParam(required = false, name = "doi") String doi,
@RequestParam(required = false, name = "docid") final String docid,
@RequestParam(required = false, name = "doi") final String doi,
@RequestParam(required = false, name = "title") String title,
@RequestParam(required = false, name = "term") String term,
@RequestParam(required = false, name = "dataset") String dataset,
Expand All @@ -42,17 +46,17 @@ public ResponseEntity<XDDResponse<DocumentsResponseOK>> getDocuments(
@RequestParam(required = false, name = "max") String max,
@RequestParam(required = false, name = "per_page") String per_page,
@RequestParam(required = false, name = "dict") String dict,
@RequestParam(required = false, name = "facets") String facets,
@RequestParam(required = false, name = "facets") final String facets,
@RequestParam(required = false, name = "min_published") String min_published,
@RequestParam(required = false, name = "max_published") String max_published,
@RequestParam(required = false, name = "pubname") String pubname,
@RequestParam(required = false, name = "publisher") String publisher,
@RequestParam(required = false, name = "additional_fields") String additional_fields,
@RequestParam(required = false, name = "match") String match,
@RequestParam(required = false, name = "known_entities") String known_entities,
@RequestParam(required = false, name = "github_url") String github_url,
@RequestParam(required = false, name = "similar_to") String similar_to,
@RequestParam(required = false, name = "askem_object_limit", defaultValue = "5") String askem_object_limit
@RequestParam(required = false, name = "known_entities") final String known_entities,
@RequestParam(required = false, name = "github_url") final String github_url,
@RequestParam(required = false, name = "similar_to") final String similar_to,
@RequestParam(required = false, name = "askem_object_limit", defaultValue = "5") final String askem_object_limit
) {

// only go ahead with the query if at least one param is present
Expand Down Expand Up @@ -86,7 +90,7 @@ public ResponseEntity<XDDResponse<DocumentsResponseOK>> getDocuments(
return ResponseEntity.internalServerError().build();
}

XDDResponse<DocumentsResponseOK> doc = proxy.getDocuments(apiKey,
final XDDResponse<DocumentsResponseOK> doc = proxy.getDocuments(apiKey,
docid, doi, title, term, dataset, include_score, include_highlights, inclusive, full_results, max, per_page, dict, facets,
min_published, max_published, pubname, publisher, additional_fields, match, known_entities, github_url, similar_to, askem_object_limit);

Expand All @@ -101,12 +105,15 @@ public ResponseEntity<XDDResponse<DocumentsResponseOK>> getDocuments(

return ResponseEntity.ok().body(doc);

} catch (RuntimeException e) {
} catch (final FeignException e) {
log.error("xDD returned an exception for document search:", e);
throw new ResponseStatusException(HttpStatusCode.valueOf(e.status()), "There was an issue with the request to xDD");
} catch (final Exception e) {
log.error("Unable to find documents, an error occurred", e);
return ResponseEntity.internalServerError().build();
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Unable to find documents, an error occurred");
}

}
return ResponseEntity.badRequest().build();
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "At least one parameter must be present: docid, doi, term, github_url, similar_to");
}
}
Loading