Add Content-length to artifact file response

feature/appm-store/pbac
Turcy 5 years ago
parent c34f3cf4c7
commit 5483a06aa7

@ -17,6 +17,7 @@
package org.wso2.carbon.device.application.mgt.api.services.impl; package org.wso2.carbon.device.application.mgt.api.services.impl;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.api.services.ArtifactDownloadAPI; import org.wso2.carbon.device.application.mgt.api.services.ArtifactDownloadAPI;
@ -27,13 +28,15 @@ import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import org.wso2.carbon.device.application.mgt.core.util.APIUtil;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.InputStream;
/** /**
* Implementation of ApplicationDTO Management related APIs. * Implementation of ApplicationDTO Management related APIs.
@ -54,13 +57,20 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI {
@PathParam("folderName") String folderName, @PathParam("folderName") String folderName,
@PathParam("fileName") String fileName) { @PathParam("fileName") String fileName) {
AppmDataHandler dataHandler = APIUtil.getDataHandler(); AppmDataHandler dataHandler = APIUtil.getDataHandler();
try { try (InputStream fileInputStream = dataHandler.getArtifactStream(tenantId, uuid, folderName, fileName)) {
InputStream fileInputStream = dataHandler.getArtifactStream(tenantId, uuid, folderName, fileName); byte[] content = IOUtils.toByteArray(fileInputStream);
Response.ResponseBuilder response = Response try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) {
.ok(fileInputStream, MediaType.APPLICATION_OCTET_STREAM); Response.ResponseBuilder response = Response
response.status(Response.Status.OK); .ok(binaryDuplicate, MediaType.APPLICATION_OCTET_STREAM);
response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\""); response.status(Response.Status.OK);
return response.build(); response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.header("Content-Length", content.length);
return response.build();
} catch (IOException e) {
String msg = "Error occurred while creating input stream from buffer array. ";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
} catch (NotFoundException e) { } catch (NotFoundException e) {
String msg = "Couldn't find an application release for UUID: " + uuid + " and file name: " + fileName; String msg = "Couldn't find an application release for UUID: " + uuid + " and file name: " + fileName;
log.error(msg, e); log.error(msg, e);
@ -74,6 +84,10 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI {
String msg = "Error occurred while getting the application release artifact file. "; String msg = "Error occurred while getting the application release artifact file. ";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (IOException e) {
String msg = "Error occurred while getting the byte array of application release artifact file. ";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} }
} }

Loading…
Cancel
Save