Moving JSON:API reply separate methods

This commit is contained in:
Luca Frosini 2023-02-17 17:39:38 +01:00
parent cbcb4804b7
commit 2d9f469a46
1 changed files with 21 additions and 12 deletions

View File

@ -39,20 +39,29 @@ public class License extends REST<CKANLicense> implements org.gcube.gcat.api.int
* @responseExample application/json classpath:/api-docs-examples/license/list-license-response.json * @responseExample application/json classpath:/api-docs-examples/license/list-license-response.json
*/ */
@GET @GET
@Produces({MediaType.APPLICATION_JSON, GCatConstants.APPLICATION_JSON_API}) @Produces(MediaType.APPLICATION_JSON)
public String list() throws WebServiceException { public String list() throws WebServiceException {
return super.list(-1, -1);
}
/**
* @return the json array containing the licenses available in the catalogue
*
* @pathExample /licenses
* @responseExample application/json classpath:/api-docs-examples/license/list-license-response.json
*/
@GET
@Produces(GCatConstants.APPLICATION_JSON_API)
public String listAsJsonAPI() throws WebServiceException {
String ret = super.list(-1, -1); String ret = super.list(-1, -1);
String accept = httpHeaders.getHeaderString("Accept"); ObjectMapper objectMapper = new ObjectMapper();
if(accept.startsWith(GCatConstants.APPLICATION_JSON_API)) { ObjectNode objectNode = objectMapper.createObjectNode();
ObjectMapper objectMapper = new ObjectMapper(); try {
ObjectNode objectNode = objectMapper.createObjectNode(); JsonNode licenses = objectMapper.readTree(ret);
try { objectNode.set(GCatConstants.JSON_API_DATA_FIELD_NAME, licenses);
JsonNode licenses = objectMapper.readTree(ret); ret = objectMapper.writeValueAsString(objectNode);
objectNode.set(GCatConstants.JSON_API_DATA_FIELD_NAME, licenses); }catch (Exception e) {
ret = objectMapper.writeValueAsString(objectNode); throw new InternalServerErrorException(e);
}catch (Exception e) {
throw new InternalServerErrorException(e);
}
} }
return ret; return ret;
} }