Merge branch 'master' into 'master'

Add Content-length to artifact file response

See merge request entgra/carbon-device-mgt!391
feature/appm-store/pbac
Dharmakeerthi Lasantha 5 years ago
commit 02b0620c31

@ -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);
try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) {
Response.ResponseBuilder response = Response Response.ResponseBuilder response = Response
.ok(fileInputStream, MediaType.APPLICATION_OCTET_STREAM); .ok(binaryDuplicate, MediaType.APPLICATION_OCTET_STREAM);
response.status(Response.Status.OK); response.status(Response.Status.OK);
response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\""); response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.header("Content-Length", content.length);
return response.build(); 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();
} }
} }

@ -236,7 +236,7 @@ $(document).ready(function () {
deviceType, deviceType,
function (data, textStatus, jqXHR) { function (data, textStatus, jqXHR) {
if (jqXHR.status == 200) { if (jqXHR.status == 200) {
$(errorMsgIdentifier).addClass(" hidden"); $(errorMsgWrapper).addClass(" hidden");
$("#modalDevice").modal('show'); $("#modalDevice").modal('show');
} }
}, },

Loading…
Cancel
Save