Search in sources :

Example 16 with MetadataVersionServiceException

use of org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException 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')")
@RequestMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT + "/{versionName}/data.gz", method = RequestMethod.GET, 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());
    }
}
Also used : MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) GZIPOutputStream(java.util.zip.GZIPOutputStream) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) MetadataVersionException(org.hisp.dhis.webapi.controller.exception.MetadataVersionException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)16 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)10 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 MetadataSyncServiceException (org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException)4 DhisHttpResponse (org.hisp.dhis.system.util.DhisHttpResponse)4 BadRequestException (org.hisp.dhis.webapi.controller.exception.BadRequestException)4 MetadataVersionException (org.hisp.dhis.webapi.controller.exception.MetadataVersionException)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Date (java.util.Date)2 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)2 AvailabilityStatus (org.hisp.dhis.dxf2.synch.AvailabilityStatus)2 RemoteServerUnavailableException (org.hisp.dhis.exception.RemoteServerUnavailableException)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 List (java.util.List)1