use of org.hisp.dhis.webapi.controller.exception.BadRequestException in project dhis2-core by dhis2.
the class MetadataVersionController method downloadGZipVersion.
// endpoint to download metadata in gzip format
@PreAuthorize("hasRole('ALL') or hasRole('F_METADATA_MANAGE')")
@GetMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT + "/{versionName}/data.gz", produces = "*/*")
public void downloadGZipVersion(@PathVariable("versionName") String versionName, HttpServletResponse response) throws MetadataVersionException, IOException, BadRequestException {
boolean enabled = isMetadataVersioningEnabled();
try {
if (!enabled) {
throw new BadRequestException("Metadata versioning is not enabled for this instance.");
}
contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_GZIP, CacheStrategy.NO_CACHE, "metadata.json.gz", true);
response.addHeader(ContextUtils.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
String versionData = versionService.getVersionData(versionName);
if (versionData == null) {
throw new MetadataVersionException("No metadata version snapshot found for the given version " + versionName);
}
GZIPOutputStream gos = new GZIPOutputStream(response.getOutputStream());
gos.write(versionData.getBytes(StandardCharsets.UTF_8));
gos.close();
} catch (MetadataVersionServiceException ex) {
throw new MetadataVersionException("Unable to download version from system: " + versionName + ex.getMessage());
}
}
use of org.hisp.dhis.webapi.controller.exception.BadRequestException in project dhis2-core by dhis2.
the class MetadataVersionController method getMetaDataVersion.
// Gets the version by versionName or latest system version
@GetMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT, produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public MetadataVersion getMetaDataVersion(@RequestParam(value = "versionName", required = false) String versionName) throws MetadataVersionException, BadRequestException {
MetadataVersion versionToReturn = null;
boolean enabled = isMetadataVersioningEnabled();
try {
if (!enabled) {
throw new BadRequestException("Metadata versioning is not enabled for this instance.");
}
if (StringUtils.isNotEmpty(versionName)) {
versionToReturn = versionService.getVersionByName(versionName);
if (versionToReturn == null) {
throw new MetadataVersionException("No metadata version with name " + versionName + " exists. Please check again later.");
}
} else {
versionToReturn = versionService.getCurrentVersion();
if (versionToReturn == null) {
throw new MetadataVersionException("No metadata versions exist. Please check again later.");
}
}
return versionToReturn;
} catch (MetadataVersionServiceException ex) {
throw new MetadataVersionException("Exception occurred while getting metadata version." + (StringUtils.isNotEmpty(versionName) ? versionName : " ") + ex.getMessage(), ex);
}
}
use of org.hisp.dhis.webapi.controller.exception.BadRequestException in project dhis2-core by dhis2.
the class DeduplicationControllerMvcTest method shouldThrowPostPotentialDuplicateMissingTei.
@Test
void shouldThrowPostPotentialDuplicateMissingTei() throws Exception {
PotentialDuplicate potentialDuplicate = new PotentialDuplicate(teiA, null);
mockMvc.perform(post(ENDPOINT).content(objectMapper.writeValueAsString(potentialDuplicate)).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andExpect(result -> assertTrue(result.getResolvedException() instanceof BadRequestException));
}
Aggregations