From 5a8bf10d5a2aa4ea15e38dbad64ef1f56785a78c Mon Sep 17 00:00:00 2001 From: megala21 Date: Fri, 18 Aug 2017 17:33:24 +0530 Subject: [PATCH] Adding Artifacts download APIS --- .../mgt/api/FileStreamingOutput.java | 55 +++++++++ .../services/ApplicationManagementAPI.java | 79 +++++++++++++ .../impl/ApplicationManagementAPIImpl.java | 48 ++++++++ .../mgt/common/ApplicationRelease.java | 12 +- .../services/ApplicationReleaseManager.java | 19 +++ .../services/ApplicationStorageManager.java | 11 ++ .../mgt/core/dao/ApplicationReleaseDAO.java | 20 ++++ .../GenericApplicationReleaseDAOImpl.java | 108 ++++++++++++++++++ .../impl/ApplicationReleaseManagerImpl.java | 93 +++++++++++++-- .../impl/ApplicationStorageManagerImpl.java | 40 ++++++- .../mgt/core/impl/PlatformManagerImpl.java | 37 ++---- 11 files changed, 469 insertions(+), 53 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/FileStreamingOutput.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/FileStreamingOutput.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/FileStreamingOutput.java new file mode 100644 index 0000000000..dd637c2e5e --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/FileStreamingOutput.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.wso2.carbon.device.application.mgt.api; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.StreamingOutput; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * FileStreamingOutput to allow the user to send the files as Stream. + */ +public class FileStreamingOutput implements StreamingOutput { + private InputStream inputStream; + + public FileStreamingOutput(InputStream inputStream) { + this.inputStream = inputStream; + } + + @Override + public void write(OutputStream outputStream) throws IOException, WebApplicationException { + try { + byte[] buffer = new byte[inputStream.available()]; + inputStream.read(buffer); + outputStream.write(buffer); + outputStream.flush(); + } finally { + if (inputStream != null) { + inputStream.close(); + } + if (outputStream != null) { + outputStream.close(); + } + } + + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java index 668e2594c1..6be716ec38 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java @@ -491,4 +491,83 @@ public interface ApplicationManagementAPI { @PathParam("uuid") String applicationUUID, @Multipart(value = "applicationRelease", type = "application/json") ApplicationRelease applicationRelease, @Multipart(value = "binaryFile") Attachment binaryFile); + + @GET + @Path("/release-artifacts/{uuid}/{version}") + @Produces(MediaType.APPLICATION_OCTET_STREAM) + @Consumes(MediaType.MULTIPART_FORM_DATA) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_OCTET_STREAM, + httpMethod = "POST", + value = "Create an application release", + notes = "This will create a new application release", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:application:get") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved the Application release.", + response = Attachment.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while releasing the application.", + response = ErrorResponse.class) + }) + Response getApplicationReleaseArtifacts( + @ApiParam( + name = "UUID", + value = "Unique identifier of the Application", + required = true) + @PathParam("uuid") String applicationUUID, + @ApiParam( + name = "Version", + value = "Version of the Application release need to be retrieved", + required = true) + @PathParam("version") String version); + + @GET + @Path("/release/{uuid}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get all the releases or specific release of an application", + notes = "This will retrieve the all the releases or specific release of an application", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:application:get") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved the Application release."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while releasing the application.", + response = ErrorResponse.class) + }) + Response getApplicationReleases( + @ApiParam( + name = "UUID", + value = "Unique identifier of the Application", + required = true) + @PathParam("uuid") String applicationUUID, + @ApiParam( + name = "version", + value = "Version of the application", + required = false) + @QueryParam("version") String version); } 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/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java index cdf3a47200..2eb60e6b6c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java @@ -23,12 +23,14 @@ import org.apache.commons.logging.LogFactory; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Multipart; import org.wso2.carbon.device.application.mgt.api.APIUtil; +import org.wso2.carbon.device.application.mgt.api.FileStreamingOutput; import org.wso2.carbon.device.application.mgt.api.services.ApplicationManagementAPI; import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.ApplicationList; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationReleaseManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; @@ -44,6 +46,7 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.io.IOException; import java.io.InputStream; @@ -315,4 +318,49 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { Response.Status.INTERNAL_SERVER_ERROR); } } + + @Override + @GET + @Produces(MediaType.APPLICATION_OCTET_STREAM) + @Path("/release-artifacts/{uuid}/{version}") + public Response getApplicationReleaseArtifacts(@PathParam("uuid") String applicationUUID, + @PathParam("version") String version) { + ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); + try { + InputStream binaryFile = applicationStorageManager.getReleasedArtifacts(applicationUUID, version); + FileStreamingOutput fileStreamingOutput = new FileStreamingOutput(binaryFile); + Response.ResponseBuilder response = Response.status(Response.Status.OK).entity(fileStreamingOutput); + response.header("Content-Disposition", "attachment; filename=\"" + version + "\""); + return response.build(); + } catch (ApplicationStorageManagementException e) { + log.error("Error while retrieving the binary file of the applcation release for the application UUID " + + applicationUUID + " and version " + version, e); + if (e.getMessage().contains("Binary file does not exist")) { + return APIUtil.getResponse(e, Response.Status.NOT_FOUND); + } else { + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } + } + + @Override + @Path("/release/{uuid}") + @GET + public Response getApplicationReleases(@PathParam("uuid") String applicationUUID, + @QueryParam("version") String version) { + ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager(); + try { + if (version == null || version.isEmpty()) { + List applicationReleases = applicationReleaseManager.getReleases(applicationUUID); + return Response.status(Response.Status.OK).entity(applicationReleases).build(); + } else { + ApplicationRelease applicationRelease = applicationReleaseManager.getRelease(applicationUUID, version); + return Response.status(Response.Status.OK).entity(applicationRelease).build(); + } + } catch (ApplicationManagementException e) { + log.error("Error while getting all the application releases for the application with the UUID " + + applicationUUID, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationRelease.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationRelease.java index 3f4eb2a622..e43b4fda64 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationRelease.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationRelease.java @@ -61,14 +61,6 @@ public class ApplicationRelease { this.id = id; } - public int getVersionId() { - return versionId; - } - - public void setVersionId(int versionId) { - this.versionId = versionId; - } - public String getVersionName() { return versionName; } @@ -89,8 +81,8 @@ public class ApplicationRelease { return releaseChannel; } - public void setReleaseChannel(Channel releaseChannel) { - this.releaseChannel = releaseChannel; + public void setReleaseChannel(String releaseChannel) { + this.releaseChannel = Channel.valueOf(releaseChannel); } public String getReleaseDetails() { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationReleaseManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationReleaseManager.java index 013a4fe5b4..5e8ba10868 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationReleaseManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationReleaseManager.java @@ -21,6 +21,8 @@ package org.wso2.carbon.device.application.mgt.common.services; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import java.util.List; + /** * ApplicationReleaseManager is responsible for handling all the operations related with * {@link org.wso2.carbon.device.application.mgt.common.ApplicationRelease} which involving addition, updation , @@ -39,6 +41,23 @@ public interface ApplicationReleaseManager { public ApplicationRelease createRelease(String UUID, ApplicationRelease applicationRelease) throws ApplicationManagementException; + /** + * To get the application release of the Application/ + * @param UUID UUID of the Application. + * @param version Version of the ApplicationRelease that need to be retrieved. + * @return ApplicationRelease related with particular Application UUID and version. + * @throws ApplicationManagementException ApplicationManagementException + */ + public ApplicationRelease getRelease(String UUID, String version) throws ApplicationManagementException; + + /** + * To get all the releases of a particular Application. + * @param UUID UUID of the Application to get all the releases. + * @return the List of the Application releases related with the particular Application. + * @throws ApplicationManagementException Application Management Exception. + */ + public List getReleases(String UUID) throws ApplicationManagementException; + /** * To make a release as the default one for an application. * 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 ddc38a2506..4765366d69 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 @@ -22,6 +22,7 @@ package org.wso2.carbon.device.application.mgt.common.services; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import java.io.InputStream; +import java.io.OutputStream; import java.util.List; /** @@ -48,4 +49,14 @@ public interface ApplicationStorageManager { */ public void uploadReleaseArtifacts(String applicationUUID, String versionName, InputStream binaryFile) throws ApplicationStorageManagementException; + + /** + * To get released artifacts for the particular version of the application. + * @param applicationUUID UUID of the Application + * @param versionName Version of the release to be retrieved + * @return the artifact related with the Application Release. + * @throws ApplicationStorageManagementException Application Storage Management Exception. + */ + public InputStream getReleasedArtifacts(String applicationUUID, String versionName) 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 432be683b7..8205a3250d 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 @@ -21,6 +21,8 @@ package org.wso2.carbon.device.application.mgt.core.dao; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import java.util.List; + /** * This is responsible for Application Release related DAO operations. */ @@ -36,4 +38,22 @@ public interface ApplicationReleaseDAO { ApplicationRelease createRelease(ApplicationRelease applicationRelease) throws ApplicationManagementDAOException; + /** + * To get a release details with the particular version. + * @param applicationUuid UUID of the application to get the release. + * @param versionName Name of the version + * @return ApplicationRelease for the particular version of the given application + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + ApplicationRelease getRelease(String applicationUuid, String versionName) throws ApplicationManagementDAOException; + + /** + * To get all the releases of a particular application. + * + * @param applicationUUID Application UUID + * @return list of the application releases + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + List getApplicationReleases(String applicationUUID) 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 9a2a2bad93..f35c63f0db 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 @@ -32,6 +32,9 @@ import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -97,4 +100,109 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Util.cleanupResources(statement, resultSet); } } + + @Override + public ApplicationRelease getRelease(String applicationUuid, String versionName) + throws ApplicationManagementDAOException { + Connection connection; + PreparedStatement statement = null; + ResultSet resultSet = null; + String sql = "SELECT * FROM APPM_APPLICATION_RELEASE WHERE VERSION_NAME = ? AND APPM_APPLICATION_ID = " + + "(SELECT ID FROM APPM_APPLICATION WHERE UUID = ?)"; + ApplicationRelease applicationRelease = null; + ResultSet rsProperties = null; + + try { + connection = this.getDBConnection(); + statement = connection.prepareStatement(sql); + statement.setString(1, versionName); + statement.setString(2, applicationUuid); + resultSet = statement.executeQuery(); + + if (resultSet.next()) { + applicationRelease = new ApplicationRelease(); + applicationRelease.setVersionName(versionName); + applicationRelease.setDefault(resultSet.getBoolean("IS_DEFAULT")); + applicationRelease.setCreatedAt(resultSet.getDate("CREATED_AT")); + applicationRelease.setReleaseChannel(resultSet.getString("RELEASE_CHANNEL")); + applicationRelease.setReleaseDetails(resultSet.getString("RELEASE_DETAILS")); + applicationRelease.setResource(resultSet.getString("RESOURCE")); + + sql = "SELECT * FROM APPM_RELEASE_PROPERTY WHERE APPLICATION_RELEASE_ID=?"; + statement = connection.prepareStatement(sql); + statement.setInt(1, resultSet.getInt("ID")); + rsProperties = statement.executeQuery(); + + Map properties = new HashMap<>(); + while (rsProperties.next()) { + properties.put(rsProperties.getString("PROP_KEY"), + rsProperties.getString("PROP_VAL")); + } + applicationRelease.setProperties(properties); + } + return applicationRelease; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Database connection exception while trying to gett the " + + "release details of the application with UUID " + applicationUuid + " and version " + + versionName, e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error while getting release details of the application " + + applicationUuid + " and version " + versionName + " , while executing the query " + sql, e); + } finally { + Util.cleanupResources(statement, resultSet); + Util.cleanupResources(null, rsProperties); + } + } + + @Override + public List getApplicationReleases(String applicationUUID) + throws ApplicationManagementDAOException { + Connection connection; + PreparedStatement statement = null; + ResultSet resultSet = null; + String sql = "SELECT * FROM APPM_APPLICATION_RELEASE WHERE APPM_APPLICATION_ID = (SELECT ID FROM " + + "APPM_APPLICATION WHERE UUID = ?)"; + List applicationReleases = new ArrayList<>(); + ResultSet rsProperties = null; + + try { + connection = this.getDBConnection(); + statement = connection.prepareStatement(sql); + statement.setString(1, applicationUUID); + resultSet = statement.executeQuery(); + + while (resultSet.next()) { + ApplicationRelease applicationRelease = new ApplicationRelease(); + applicationRelease.setVersionName(resultSet.getString("VERSION_NAME")); + applicationRelease.setDefault(resultSet.getBoolean("IS_DEFAULT")); + applicationRelease.setCreatedAt(resultSet.getDate("CREATED_AT")); + applicationRelease.setReleaseChannel(resultSet.getString("RELEASE_CHANNEL")); + applicationRelease.setReleaseDetails(resultSet.getString("RELEASE_DETAILS")); + applicationRelease.setResource(resultSet.getString("RESOURCE")); + + sql = "SELECT * FROM APPM_RELEASE_PROPERTY WHERE APPLICATION_RELEASE_ID= ?"; + statement = connection.prepareStatement(sql); + statement.setInt(1, resultSet.getInt("ID")); + rsProperties = statement.executeQuery(); + + Map properties = new HashMap<>(); + while (rsProperties.next()) { + properties.put(rsProperties.getString("PROP_KEY"), rsProperties.getString("PROP_VAL")); + } + applicationRelease.setProperties(properties); + applicationReleases.add(applicationRelease); + } + return applicationReleases; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Database connection exception while trying to get the " + + "release details of the application with UUID " + applicationUUID, e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "Error while getting all the release details of the " + "application " + applicationUUID + + ", while executing the query " + sql, e); + } finally { + Util.cleanupResources(statement, resultSet); + Util.cleanupResources(null, rsProperties); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationReleaseManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationReleaseManagerImpl.java index a093b09b8f..ccd1f81a58 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationReleaseManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationReleaseManagerImpl.java @@ -20,7 +20,6 @@ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; @@ -28,10 +27,10 @@ import org.wso2.carbon.device.application.mgt.common.services.ApplicationRelease import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; -import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import java.util.Date; +import java.util.List; public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager { private static Log log = LogFactory.getLog(ApplicationReleaseManagerImpl.class); @@ -39,15 +38,7 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager @Override public ApplicationRelease createRelease(String UUID, ApplicationRelease applicationRelease) throws ApplicationManagementException { - if (UUID == null) { - throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " - + "parameter to do the application release"); - } - Application application = DataHolder.getInstance().getApplicationManager().getApplication(UUID); - if (application == null) { - throw new ApplicationManagementException("Application with UUID " + UUID + " does not exist. Cannot " - + "release an application that is not existing"); - } + Application application = validateReleaseCreateRequest(UUID, applicationRelease); if (log.isDebugEnabled()) { log.debug("Application release request is received for the application " + application.toString()); } @@ -66,6 +57,36 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager } } + @Override + public ApplicationRelease getRelease(String UUID, String version) throws ApplicationManagementException { + Application application = validationGetReleaseRequest(UUID); + if (log.isDebugEnabled()) { + log.debug("Application release retrieval request is received for the application " + + application.toString() + " and version " + version); + } + try { + ConnectionManagerUtil.openDBConnection(); + return DAOFactory.getApplicationReleaseDAO().getRelease(UUID, version); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public List getReleases(String UUID) throws ApplicationManagementException { + Application application = validationGetReleaseRequest(UUID); + if (log.isDebugEnabled()) { + log.debug("Request is received to retrieve all the releases related with the application " + + application.toString()); + } + try { + ConnectionManagerUtil.openDBConnection(); + return DAOFactory.getApplicationReleaseDAO().getApplicationReleases(UUID); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + @Override public void makeDefaultRelease(int id) throws ApplicationManagementException { @@ -75,4 +96,54 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager public void updateRelease(ApplicationRelease applicationRelease) throws ApplicationManagementException { } + + /** + * To validate the pre-request of the ApplicationRelease. + * + * @param UUID UUID of the Application. + * @return Application related with the UUID + */ + private Application validationGetReleaseRequest(String UUID) throws ApplicationManagementException { + if (UUID == null) { + throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " + + "parameter to get the releases related to a particular application."); + } + Application application = DataHolder.getInstance().getApplicationManager().getApplication(UUID); + if (application == null) { + throw new ApplicationManagementException("Application with UUID " + UUID + " does not exist. Cannot " + + "retrieve the releases for a non-existing application."); + } + return application; + } + + /** + * To validate a create release request to make sure all the pre-conditions satisfied. + * + * @param UUID UUID of the Application. + * @param applicationRelease ApplicationRelease that need to be created. + * @return the Application related with the particular Application Release + * @throws ApplicationManagementException Application Management Exception. + */ + private Application validateReleaseCreateRequest(String UUID, ApplicationRelease applicationRelease) + throws ApplicationManagementException { + if (UUID == null) { + throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " + + "parameter to do the application release"); + } + Application application = DataHolder.getInstance().getApplicationManager().getApplication(UUID); + if (application == null) { + throw new ApplicationManagementException("Application with UUID " + UUID + " does not exist. Cannot " + + "release an application that is not existing"); + } + if (applicationRelease == null || applicationRelease.getVersionName() == null){ + throw new ApplicationManagementException("ApplicationRelease version name is a mandatory parameter for " + + "creating release. It cannot be found."); + } + if (getRelease(UUID, applicationRelease.getVersionName()) != null){ + throw new ApplicationManagementException("Application Release for the Application UUID " + UUID + " " + + "with the version " + applicationRelease.getVersionName() + " already exists. Cannot create an " + + "application release with the same version."); + } + return application; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index 112570b963..d848bcaa82 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -28,11 +28,7 @@ import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorage import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.util.Constants; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.util.List; /** @@ -140,6 +136,40 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager{ } + @Override + public InputStream getReleasedArtifacts(String applicationUUID, String versionName) + throws ApplicationStorageManagementException { + Application application; + try { + application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID); + } catch (ApplicationManagementException e) { + throw new ApplicationStorageManagementException("Exception while retrieving the application details for " + + "the application with UUID " + applicationUUID); + } + if (application == null) { + throw new ApplicationStorageManagementException("Application with UUID " + applicationUUID + " does not " + + "exist. Cannot retrieve release artifacts for not existing application."); + } + String artifactPath = Constants.ARTIFACT_PATH + application.getId() + File.separator + versionName; + + if (log.isDebugEnabled()) { + log.debug("ApplicationRelease artifacts are searched in the location " + artifactPath); + } + + File binaryFile = new File(artifactPath); + + if (!binaryFile.exists()) { + throw new ApplicationStorageManagementException("Binary file does not exist for this release"); + } else { + try { + return new FileInputStream(artifactPath); + } catch (FileNotFoundException e) { + throw new ApplicationStorageManagementException("Binary file does not exist for the version " + + versionName + " for the application ", e); + } + } + } + /** * To save a file in a given location. * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java index 44e516217c..5daa733cd3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java @@ -89,16 +89,11 @@ public class PlatformManagerImpl implements PlatformManager { + "PlatformManager level"); } try { - ConnectionManagerUtil.beginDBTransaction(); + ConnectionManagerUtil.openDBConnection(); platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); - ConnectionManagerUtil.commitDBTransaction(); - } catch (DBConnectionException | TransactionManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); + } catch (DBConnectionException e) { throw new PlatformManagementDAOException( "Database Connection Exception while getting the platforms for the tenant : " + tenantId, e); - } catch (PlatformManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw e; } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -136,20 +131,15 @@ public class PlatformManagerImpl implements PlatformManager { Platform platform = getPlatformFromInMemory(tenantId, identifier); if (platform == null) { try { - ConnectionManagerUtil.beginDBTransaction(); + ConnectionManagerUtil.openDBConnection(); platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, identifier); - ConnectionManagerUtil.commitDBTransaction(); if (platform != null) { return platform; } - } catch (DBConnectionException | TransactionManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); + } catch (DBConnectionException e) { throw new PlatformManagementDAOException( "Database Connection Exception while trying to get the " + "platform with the id :" + identifier + " for the tenant : " + tenantId, e); - } catch (PlatformManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw e; } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -455,18 +445,16 @@ public class PlatformManagerImpl implements PlatformManager { private void validateBeforeRegister(int tenantId, Platform platform) throws PlatformManagementException { validatePlatformSharing(tenantId, platform); try { - ConnectionManagerUtil.beginDBTransaction(); + ConnectionManagerUtil.openDBConnection(); int existingPlatformId = DAOFactory.getPlatformDAO() .getSuperTenantAndOwnPlatforms(platform.getIdentifier(), tenantId); - ConnectionManagerUtil.commitDBTransaction(); if (existingPlatformId != -1) { throw new PlatformManagementException( "Another platform exists with the identifier " + platform.getIdentifier() + " in the tenant " + tenantId + " or super-tenant. Please choose a " + "different identifier for your platform"); } - } catch (TransactionManagementException | DBConnectionException e) { - ConnectionManagerUtil.rollbackDBTransaction(); + } catch (DBConnectionException e) { throw new PlatformManagementException( "Error while checking pre-conditions before registering" + " platform identifier '" + platform .getIdentifier() + "' for the tenant :" + tenantId); @@ -488,10 +476,9 @@ public class PlatformManagerImpl implements PlatformManager { PlatformManagementException { validatePlatformSharing(tenantId, platform); try { - ConnectionManagerUtil.beginDBTransaction(); + ConnectionManagerUtil.openDBConnection(); Platform oldPlatform = DAOFactory.getPlatformDAO().getTenantOwnedPlatform(tenantId, oldPlatformIdentifier); if (oldPlatform == null) { - ConnectionManagerUtil.commitDBTransaction(); throw new PlatformManagementException( "Cannot update platform. Platform with identifier : " + oldPlatformIdentifier + " does not exist for the tenant : " + tenantId); @@ -499,7 +486,6 @@ public class PlatformManagerImpl implements PlatformManager { if (platform.getIdentifier() != null && !platform.getIdentifier().equals(oldPlatformIdentifier)) { int existingPlatformID = DAOFactory.getPlatformDAO() .getSuperTenantAndOwnPlatforms(platform.getIdentifier(), tenantId); - ConnectionManagerUtil.commitDBTransaction(); if (existingPlatformID == -1) { throw new PlatformManagementException( "Cannot update the identifier of the platform from '" + oldPlatformIdentifier + "' to '" @@ -509,8 +495,7 @@ public class PlatformManagerImpl implements PlatformManager { } } return oldPlatform; - } catch (TransactionManagementException | DBConnectionException e) { - ConnectionManagerUtil.rollbackDBTransaction(); + } catch (DBConnectionException e) { throw new PlatformManagementException( "Database error while validating the platform update with the " + "platform identifier: " + oldPlatformIdentifier + " for the tenant :" + tenantId); @@ -532,18 +517,16 @@ public class PlatformManagerImpl implements PlatformManager { + " cannot be shared by the tenant domain - " + tenantId); } try { - ConnectionManagerUtil.beginDBTransaction(); + ConnectionManagerUtil.openDBConnection(); if (platform.isShared()) { int sharedPlatform = DAOFactory.getPlatformDAO().getMultiTenantPlatforms(platform.getIdentifier()); - ConnectionManagerUtil.commitDBTransaction(); if (sharedPlatform != -1) { throw new PlatformManagementException( "Platform '" + platform.getIdentifier() + "' cannot be shared as some other tenants have " + "platforms with the same identifier."); } } - } catch (TransactionManagementException | DBConnectionException e) { - ConnectionManagerUtil.rollbackDBTransaction(); + } catch (DBConnectionException e) { throw new PlatformManagementException( "Error while checking platform sharing conditions for " + " platform identifier '" + platform .getIdentifier() + "' for the tenant :" + tenantId);