From be564ca0ff22c2f629e83f62aebcb454e0286dd3 Mon Sep 17 00:00:00 2001 From: Shabirmean Date: Wed, 16 Dec 2015 16:46:20 +0530 Subject: [PATCH] Setting the content-type of the response in for the agent download method --- .../service/VirtualFireAlarmService.java | 21 +++++++++++++------ .../VirtualFireAlarmXMPPConnector.java | 8 +++++-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/VirtualFireAlarmService.java b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/VirtualFireAlarmService.java index 544a627439..a92567a80e 100644 --- a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/VirtualFireAlarmService.java +++ b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/VirtualFireAlarmService.java @@ -18,8 +18,12 @@ package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service; +import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.cxf.helpers.IOUtils; +import org.apache.http.Header; +import org.apache.woden.wsdl20.extensions.http.HTTPHeader; import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse; import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementService; @@ -65,7 +69,11 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -325,13 +333,13 @@ public class VirtualFireAlarmService { public Response downloadSketch(@QueryParam("owner") String owner, @QueryParam("deviceName") String customDeviceName, @PathParam("sketch_type") String sketchType) { - //TODO:: null check customDeviceName at UI level try { ZipArchive zipFile = createDownloadFile(owner, customDeviceName, sketchType); - Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile()); - rb.header("Content-Disposition", - "attachment; filename=\"" + zipFile.getFileName() + "\""); - return rb.build(); + Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile())); + response.type(MediaType.MULTIPART_FORM_DATA_TYPE); + response.header("Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\""); + return response.build(); + } catch (IllegalArgumentException ex) { return Response.status(400).entity(ex.getMessage()).build();//bad request } catch (DeviceManagementException ex) { @@ -340,8 +348,9 @@ public class VirtualFireAlarmService { return Response.status(500).entity(ex.getMessage()).build(); } catch (DeviceControllerException ex) { return Response.status(500).entity(ex.getMessage()).build(); + } catch (IOException ex) { + return Response.status(500).entity(ex.getMessage()).build(); } - } @Path("manager/device/{sketch_type}/generate_link") diff --git a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/transport/VirtualFireAlarmXMPPConnector.java b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/transport/VirtualFireAlarmXMPPConnector.java index 5fb6883c82..6be53dc459 100644 --- a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/transport/VirtualFireAlarmXMPPConnector.java +++ b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/transport/VirtualFireAlarmXMPPConnector.java @@ -73,7 +73,9 @@ public class VirtualFireAlarmXMPPConnector extends XmppConnector { String deviceId = from.substring(0, indexOfAt); String owner = from.substring(indexOfSlash + 1, from.length()); - log.info("Received XMPP message for: {OWNER-" + owner + "} & {DEVICE.ID-" + deviceId + "}"); + if (log.isDebugEnabled()) { + log.debug("Received XMPP message for: {OWNER-" + owner + "} & {DEVICE.ID-" + deviceId + "}"); + } if (subject != null) { switch (subject) { @@ -98,7 +100,9 @@ public class VirtualFireAlarmXMPPConnector extends XmppConnector { tempVal, Calendar.getInstance().getTimeInMillis()); break; default: - log.info("Unknown XMPP Message [" + message + "] from [" + from + "] received"); + if (log.isDebugEnabled()) { + log.warn("Unknown XMPP Message [" + message + "] from [" + from + "] received"); + } break; } }