diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ArtifactDownloadAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ArtifactDownloadAPI.java index 676696c67e..fc9dece560 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ArtifactDownloadAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ArtifactDownloadAPI.java @@ -57,7 +57,7 @@ import javax.ws.rs.core.Response; public interface ArtifactDownloadAPI { @GET - @Path("/download-artifact/{md5sum}/{fileName}") + @Path("/download-artifact/{uuid}/{fileName}") @Produces(MediaType.APPLICATION_OCTET_STREAM) @ApiOperation( produces = MediaType.APPLICATION_OCTET_STREAM, @@ -82,10 +82,10 @@ public interface ArtifactDownloadAPI { }) Response getArtifact( @ApiParam( - name = "md5sum", - value = "md5sum of the application release installer", + name = "uuid", + value = "UUID of the application release.", required = true) - @PathParam("md5sum") String md5sum, + @PathParam("uuid") String uuid, @ApiParam( name = "fileName", value = "Name of the artifact", diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ArtifactDownloadAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ArtifactDownloadAPIImpl.java index 581ff3f8a0..2a34e5feac 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ArtifactDownloadAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ArtifactDownloadAPIImpl.java @@ -44,9 +44,9 @@ public class ArtifactDownloadAPIImpl implements ArtifactDownloadAPI { @GET @Override @Produces(MediaType.APPLICATION_OCTET_STREAM) - @Path("/download-artifact/{md5sum}/{fileName}") + @Path("/download-artifact/{uuid}/{fileName}") public Response getArtifact( - @PathParam("md5sum") String md5sum, + @PathParam("uuid") String uuid, @PathParam("fileName") String fileName) { AppmDataHandler dataHandler = APIUtil.getDataHandler(); try { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java index 8403d20c9c..26e426227e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java @@ -99,7 +99,5 @@ public interface ApplicationStorageManager { * @return {@link InputStream} * @throws ApplicationStorageManagementException throws if an error occurs when accessing the file. */ - InputStream getFileSttream (String path) throws ApplicationStorageManagementException; - - - } + InputStream getFileSttream(String path) throws ApplicationStorageManagementException; +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java index 2bdb113c74..0bc1b13580 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java @@ -86,8 +86,8 @@ public interface ApplicationReleaseDAO { * @return the updated ApplicationDTO Release * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception */ - ApplicationReleaseDTO updateRelease(ApplicationReleaseDTO applicationRelease, int tenantId) throws - ApplicationManagementDAOException; + ApplicationReleaseDTO updateRelease(ApplicationReleaseDTO applicationRelease, int tenantId) + throws ApplicationManagementDAOException; /** * To update an ApplicationDTO release. @@ -168,7 +168,7 @@ public interface ApplicationReleaseDAO { */ boolean verifyReleaseExistenceByUuid(String uuid, int tenantId) throws ApplicationManagementDAOException; - ApplicationReleaseArtifactPaths getReleaseArtifactPaths(String uuid, int tenantId) throws ApplicationManagementDAOException; + String getReleaseHashValue(String uuid, int tenantId) throws ApplicationManagementDAOException; /*** * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index cef4937403..a0a2c0c9c6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -656,7 +656,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } @Override - public ApplicationReleaseArtifactPaths getReleaseArtifactPaths(String uuid, int tenantId) throws ApplicationManagementDAOException{ + public String getReleaseHashValue(String uuid, int tenantId) throws ApplicationManagementDAOException{ if (log.isDebugEnabled()) { log.debug("Getting application release artifact stored location paths for: " + uuid); } @@ -664,15 +664,12 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements PreparedStatement stmt = null; ResultSet rs = null; ApplicationReleaseArtifactPaths applicationReleaseArtifactPaths = null; + String releaseHashValue = null; try { conn = this.getDBConnection(); - String sql = "SELECT AR.INSTALLER_LOCATION AS INSTALLER," - + "AR.ICON_LOCATION AS ICON," - + "AR.BANNER_LOCATION AS BANNER," - + "AR.SC_1_LOCATION AS SC1," - + "AR.SC_2_LOCATION AS SC2," - + "AR.SC_3_LOCATION AS SC3 " - + "FROM AP_APP_RELEASE AS AR " + String sql = "SELECT " + + "AR.APP_HASH_VALUE AS HASH_VALUE " + + "FROM AP_APP_RELEASE " + "WHERE AR.UUID = ? AND AR.TENANT_ID = ?;"; stmt = conn.prepareStatement(sql); @@ -686,21 +683,15 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements + uuid); } - if (rs.getFetchSize() == 0 || rs.getFetchSize() >1){ - return null; + if (rs.getFetchSize() >1){ + String msg = "Found more than one application release for UUID: " + uuid; + log.error(msg); + throw new ApplicationManagementDAOException(msg); } while(rs.next()){ - applicationReleaseArtifactPaths = new ApplicationReleaseArtifactPaths(); - List scs = new ArrayList<>(); - applicationReleaseArtifactPaths.setInstallerPath(rs.getString("INSTALLER")); - applicationReleaseArtifactPaths.setIconPath(rs.getString("ICON")); - applicationReleaseArtifactPaths.setBannerPath(rs.getString("BANNER")); - scs.add(rs.getString("SC1")); - scs.add(rs.getString("SC2")); - scs.add(rs.getString("SC3")); - applicationReleaseArtifactPaths.setScreenshotPaths(scs); + releaseHashValue = rs.getString("HASH_VALUE"); } - return applicationReleaseArtifactPaths; + return releaseHashValue; } catch (SQLException e) { throw new ApplicationManagementDAOException( "Error occurred when executing query to get application release artifact paths for App release uuid: " diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java index ca070af3ae..952ae9609f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java @@ -29,6 +29,8 @@ import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; +import org.wso2.carbon.device.application.mgt.core.util.Constants; import java.io.InputStream; @@ -51,53 +53,26 @@ public class AppmDataHandlerImpl implements AppmDataHandler { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); - String artifactPath = null; + String artifactPath; - if (StringUtils.isEmpty(uuid) || StringUtils.isEmpty(artifactName)) { - // todo throw - } - ApplicationReleaseArtifactPaths applicationReleaseArtifactPaths = null; + String appReleaseHashValue = null; try { - applicationReleaseArtifactPaths = applicationReleaseDAO - .getReleaseArtifactPaths(uuid, tenantId); + appReleaseHashValue = applicationReleaseDAO.getReleaseHashValue(uuid, tenantId); + artifactPath = appReleaseHashValue + Constants.FORWARD_SLASH + artifactName; + return applicationStorageManager.getFileSttream(artifactPath); + + } catch (ApplicationManagementDAOException e) { // todo throw // throw new ApplicationManagementException(); // e.printStackTrace(); + }catch (ApplicationStorageManagementException e) { + // todo throw + // throw new ApplicationManagementException(); + // e.printStackTrace(); } - String installerFileName = applicationReleaseArtifactPaths.getInstallerPath(); - String iconFileName = applicationReleaseArtifactPaths.getIconPath(); - String bannerFileName = applicationReleaseArtifactPaths.getBannerPath(); - - if (StringUtils.isEmpty(installerFileName) && artifactName.equals(installerFileName)) { - artifactPath = applicationReleaseArtifactPaths.getInstallerPath(); - } - - if (StringUtils.isEmpty(iconFileName) && artifactName.equals(iconFileName)) { - artifactPath = applicationReleaseArtifactPaths.getIconPath(); - } - if (StringUtils.isEmpty(bannerFileName) && artifactName.equals(bannerFileName)) { - artifactPath = applicationReleaseArtifactPaths.getBannerPath(); - } - - for (String screenshotPath : applicationReleaseArtifactPaths.getScreenshotPaths()) { - if (screenshotPath != null && screenshotPath.contains(artifactName)) { - artifactPath = screenshotPath; - } - } - - if (artifactPath != null) { - try { - return applicationStorageManager.getFileSttream(artifactPath); - } catch (ApplicationStorageManagementException e) { -// todo throw -// throw new ApplicationManagementException(); -// e.printStackTrace(); - } - - } return null; } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/StorageManagementUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/StorageManagementUtil.java index 45c43a8d57..06f9da533c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/StorageManagementUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/StorageManagementUtil.java @@ -126,6 +126,8 @@ public class StorageManagementUtil { * @throws IOException throws if error occured when reading file or if couldn't find a file in the filePath */ public static InputStream getInputStream (String filePath) throws IOException { + File sourceFile = new File(filePath); + try (InputStream inputStream = new FileInputStream(filePath)){ return inputStream; } catch (FileNotFoundException e) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/InvokerHandler.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/InvokerHandler.java index e4a0dd039e..e98346130f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/InvokerHandler.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/InvokerHandler.java @@ -47,7 +47,8 @@ import java.io.IOException; import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; import static org.wso2.carbon.device.application.mgt.handler.util.HandlerUtil.execute; -@MultipartConfig @WebServlet("/invoke") +@MultipartConfig +@WebServlet("/invoke") public class InvokerHandler extends HttpServlet { private static final Log log = LogFactory.getLog(LoginHandler.class); private static final long serialVersionUID = -6508020875358160165L; @@ -57,7 +58,8 @@ public class InvokerHandler extends HttpServlet { private static String serverUrl; private static String platform; - @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) { + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) { try { if (!validateRequest(req, resp)) { return; @@ -68,8 +70,8 @@ public class InvokerHandler extends HttpServlet { return; } executor.setHeader(HandlerConstants.AUTHORIZATION_HEADER_KEY, "Bearer " + authData.getAccessToken()); - ProxyResponse proxyResponse = execute(executor); + if (HandlerConstants.TOKEN_IS_EXPIRED.equals(proxyResponse.getExecutorResponse())) { if (!refreshToken(req, resp)) { return; @@ -87,9 +89,7 @@ public class InvokerHandler extends HttpServlet { HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse); return; } - HandlerUtil.handleSuccess(req, resp, serverUrl, platform, proxyResponse); - } catch (IOException e) { log.error("Error occured when processing invoke call.", e); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/LoginHandler.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/LoginHandler.java index 1035f27e1e..86fae9e66b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/LoginHandler.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/LoginHandler.java @@ -50,7 +50,8 @@ import java.util.Base64; import static org.wso2.carbon.device.application.mgt.handler.util.HandlerUtil.execute; -@MultipartConfig @WebServlet("/login") +@MultipartConfig +@WebServlet("/login") public class LoginHandler extends HttpServlet { private static final Log log = LogFactory.getLog(LoginHandler.class); private static final long serialVersionUID = 9050048549140517002L; @@ -61,7 +62,8 @@ public class LoginHandler extends HttpServlet { private static String serverUrl; private static String uiConfigUrl; - @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) { + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) { try { validateLoginRequest(req, resp); DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance() @@ -286,7 +288,6 @@ public class LoginHandler extends HttpServlet { "grant_type=password&username=" + username + "&password=" + password + "&scope=" + scopeString, ContentType.APPLICATION_FORM_URLENCODED); tokenEndpoint.setEntity(tokenEPPayload); - return execute(tokenEndpoint); } }