From 9cdc4389700c973bfd0b01f8e844809075f1d009 Mon Sep 17 00:00:00 2001 From: Mohamed Rashd Date: Sat, 26 Feb 2022 22:18:09 +0530 Subject: [PATCH] Change to base64 string for file uploads instead of using multipart form data Make release optional when creating new application Fix screenshots not getting saved if duplicate screenshot file name found Delete artifacts if errors while adding release to db Improve service provider application related apis Refactor code --- .../application/mgt/common/Base64File.java | 18 + .../mgt/common/FileDataHolder.java | 18 + .../mgt/common/IdentityServer.java | 19 +- .../mgt/common/IdentityServerList.java | 18 + .../application/mgt/common/SPApplication.java | 18 + .../mgt/common/SPApplicationListResponse.java | 18 + .../common/services/ApplicationManager.java | 158 +++-- .../common/services/SPApplicationManager.java | 87 ++- .../wrapper/CustomAppReleaseWrapper.java | 12 + .../common/wrapper/EntAppReleaseWrapper.java | 12 + .../wrapper/PublicAppReleaseWrapper.java | 12 + .../common/wrapper/WebAppReleaseWrapper.java | 12 + .../mgt/core/dao/SPApplicationDAO.java | 71 ++- .../GenericApplicationDAOImpl.java | 3 - .../GenericSPApplicationDAOImpl.java | 30 +- .../OracleSPApplicationDAOImpl.java | 27 +- .../PostgreSQLSPApplicationDAOImpl.java | 27 +- .../SQLServerSPApplicationDAOImpl.java | 27 +- .../mgt/core/impl/ApplicationManagerImpl.java | 572 +++++++++--------- .../core/impl/SPApplicationManagerImpl.java | 112 +--- .../core/util/ApplicationManagementUtil.java | 53 +- .../application/mgt/core/util/DAOUtil.java | 3 +- .../util/SPApplicationManagementUtil.java | 40 -- .../management/ApplicationManagementTest.java | 3 +- .../ApplicationManagementPublisherAPI.java | 383 +----------- .../api/services/SPApplicationService.java | 86 ++- ...ApplicationManagementPublisherAPIImpl.java | 435 +++---------- .../impl/SPApplicationServiceImpl.java | 41 +- .../util/SPAppRequestHandlerUtil.java | 61 +- .../device/mgt/core/common/Constants.java | 18 + .../device/mgt/core/common/util/FileUtil.java | 60 ++ .../device/mgt/core/common/util/HttpUtil.java | 2 +- 32 files changed, 1182 insertions(+), 1274 deletions(-) delete mode 100644 components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/SPApplicationManagementUtil.java diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/Base64File.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/Base64File.java index 51e9add372..67ca3429f5 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/Base64File.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/Base64File.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.common; public class Base64File { diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/FileDataHolder.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/FileDataHolder.java index 4bb36c6c64..ec59999afd 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/FileDataHolder.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/FileDataHolder.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.common; import java.io.InputStream; diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServer.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServer.java index 4bc0ddee02..24a3cd7cf7 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServer.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServer.java @@ -1,5 +1,22 @@ -package io.entgra.application.mgt.common; +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.common; public class IdentityServer { private int id; diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServerList.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServerList.java index 5f1deaacf1..a394458000 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServerList.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServerList.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.common; import java.util.List; diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/SPApplication.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/SPApplication.java index 118576cfbf..50d19aca63 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/SPApplication.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/SPApplication.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.common; diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/SPApplicationListResponse.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/SPApplicationListResponse.java index e638364bbd..576093944d 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/SPApplicationListResponse.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/SPApplicationListResponse.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.common; import java.util.List; diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/ApplicationManager.java index 75708c1105..e59021d1e4 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/ApplicationManager.java @@ -34,15 +34,10 @@ import io.entgra.application.mgt.common.response.ApplicationRelease; import io.entgra.application.mgt.common.response.Category; import io.entgra.application.mgt.common.response.Tag; import io.entgra.application.mgt.common.wrapper.CustomAppReleaseWrapper; -import io.entgra.application.mgt.common.wrapper.CustomAppWrapper; import io.entgra.application.mgt.common.wrapper.EntAppReleaseWrapper; import io.entgra.application.mgt.common.wrapper.ApplicationUpdateWrapper; -import io.entgra.application.mgt.common.wrapper.ApplicationWrapper; import io.entgra.application.mgt.common.wrapper.PublicAppReleaseWrapper; -import io.entgra.application.mgt.common.wrapper.PublicAppWrapper; import io.entgra.application.mgt.common.wrapper.WebAppReleaseWrapper; -import io.entgra.application.mgt.common.wrapper.WebAppWrapper; - import java.util.List; /** @@ -50,40 +45,64 @@ import java.util.List; */ public interface ApplicationManager { - - ApplicationReleaseDTO uploadEntAppReleaseArtifacts(EntAppReleaseWrapper releaseWrapper, ApplicationArtifact artifact, - String deviceType) throws ApplicationManagementException; - - /*** - * The method is responsible to add new application into entgra App Manager. + /** + * This method is responsible for handling application creation * - * @param applicationWrapper Application that need to be created. - * @param applicationArtifact contains artifact data. i.e image name and stream, icon name and stream etc. - * @return {@link Application} - * @throws ApplicationManagementException Catch all other throwing exceptions and throw {@link ApplicationManagementException} + * @param appId application id of the application to which the release should be created + * @param releaseWrapper {@link EntAppReleaseWrapper} of the release to be created + * @return Created application release bean + * @throws ApplicationManagementException if any error occurred while creating the application */ - ApplicationDTO uploadEntAppReleaseArtifacts(ApplicationWrapper applicationWrapper, ApplicationArtifact applicationArtifact) + ApplicationRelease createEntAppRelease(int appId, EntAppReleaseWrapper releaseWrapper) throws ApplicationManagementException; - ApplicationReleaseDTO uploadWebAppReleaseArtifacts(WebAppReleaseWrapper releaseWrapper, ApplicationArtifact artifact) + /** + * This method is responsible for handling application creation + * + * @param appId application id of the application to which the release should be created + * @param releaseWrapper {@link WebAppReleaseWrapper} of the release to be created + * @return Created application release bean + * @throws ApplicationManagementException if any error occurred while creating the application + */ + ApplicationRelease createWebAppRelease(int appId, WebAppReleaseWrapper releaseWrapper) throws ApplicationManagementException, ResourceManagementException; - ApplicationDTO uploadWebAppReleaseArtifacts(WebAppWrapper webAppWrapper, ApplicationArtifact applicationArtifact) - throws ApplicationManagementException; - - ApplicationReleaseDTO uploadPubAppReleaseArtifacts(PublicAppReleaseWrapper releaseWrapper, ApplicationArtifact artifact, - String deviceType) throws ResourceManagementException; + /** + * This method is responsible for handling application creation + * + * @param appId application id of the application to which the release should be created + * @param releaseWrapper {@link PublicAppReleaseWrapper} of the release to be created + * @return Created application release bean + * @throws ApplicationManagementException if any error occurred while creating the application + */ + ApplicationRelease createPubAppRelease(int appId, PublicAppReleaseWrapper releaseWrapper) + throws ApplicationManagementException, ResourceManagementException; - ApplicationDTO uploadPublicAppReleaseArtifacts(PublicAppWrapper publicAppWrapper, ApplicationArtifact applicationArtifact) - throws ApplicationManagementException; + /** + * This method is responsible for handling application creation + * + * @param appId application id of the application to which the release should be created + * @param releaseWrapper {@link CustomAppReleaseWrapper} of the release to be created + * @return Created application release bean + * @throws ApplicationManagementException if any error occurred while creating the application + */ + ApplicationRelease createCustomAppRelease(int appId, CustomAppReleaseWrapper releaseWrapper) + throws ResourceManagementException, ApplicationManagementException; - ApplicationReleaseDTO uploadCustomAppReleaseArtifacts(CustomAppReleaseWrapper releaseWrapper, ApplicationArtifact artifact, - String deviceType) throws ResourceManagementException, ApplicationManagementException; + /** + * Checks if release is available for a given application wrapper, and if exists it uploads + * the artifacts of the release + * + * @param app Application wrapper bean of the application + * @param Application Wrapper class + * @return constructed ApplicationDTO after uploading the release artifacts if exist + * @throws ApplicationManagementException if any error occurred while uploading release artifacts + */ + ApplicationDTO uploadReleaseArtifactIfExist(T app) throws ApplicationManagementException; - ApplicationDTO uploadCustomAppReleaseArtifactsAndConstructAppDTO(CustomAppWrapper customAppWrapper, ApplicationArtifact applicationArtifact) - throws ApplicationManagementException; + void validatePublicAppReleasePackageName(String packageName) throws ApplicationManagementException; - /*** + /** * This method is responsible to add application data into APPM database. However, before call this method it is * required to do the validation of request and check the existence of application releaseDTO. * @@ -91,12 +110,18 @@ public interface ApplicationManager { * @return {@link Application} * @throws ApplicationManagementException which throws if error occurs while during application management. */ - Application executeApplicationPersistenceTransaction(ApplicationDTO applicationDTO) throws + Application addAppDataIntoDB(ApplicationDTO applicationDTO) throws ApplicationManagementException; - - Application persistApplication(ApplicationDTO applicationDTO) throws ApplicationManagementException; - + /** + * This method is responsible for handling application creation + * + * @param app Application wrapper object which depends on the application type + * @param Application wrapper class which depends on the application type + * @return Created application bean + * @throws ApplicationManagementException if any error occurred while creating the application + */ + Application createApplication(T app) throws ApplicationManagementException; /** * Check the existence of an application for given application name and the device type. @@ -109,7 +134,6 @@ public interface ApplicationManager { */ boolean isExistingAppName(String appName, String deviceTypeName) throws ApplicationManagementException; - /** * Updates an already existing application. * @@ -145,6 +169,8 @@ public interface ApplicationManager { */ void deleteApplicationRelease(String releaseUuid) throws ApplicationManagementException; + void deleteApplicationArtifacts(List directoryPaths) throws ApplicationManagementException; + /** * To get the applications based on the search filter. * @@ -154,9 +180,21 @@ public interface ApplicationManager { */ ApplicationList getApplications(Filter filter) throws ApplicationManagementException; + /** + * + * @param applicationReleaseDTOs application releases of the application + * @return if application is hide-able + * @throws ApplicationManagementException if any error occurred while checking if hide-able + */ boolean isHideableApp(List applicationReleaseDTOs) throws ApplicationManagementException; + /** + * + * @param applicationReleaseDTOs application releases of the application + * @return if application is deletable + * @throws ApplicationManagementException if any error occurred while checking if deletable + */ boolean isDeletableApp(List applicationReleaseDTOs) throws ApplicationManagementException; @@ -170,7 +208,6 @@ public interface ApplicationManager { */ List getApplications(List packageNames) throws ApplicationManagementException; - /** * To create an application release for an ApplicationDTO. * @@ -244,7 +281,6 @@ public interface ApplicationManager { void updateApplicationImageArtifact(String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException; - /** * To update release images. * @@ -279,7 +315,7 @@ public interface ApplicationManager { * To validate the application creating request * */ - void validateAppCreatingRequest(T param, boolean isReleaseRequired) throws ApplicationManagementException; + void validateAppCreatingRequest(T param) throws ApplicationManagementException, RequestValidatingException; /*** * @@ -287,8 +323,49 @@ public interface ApplicationManager { */ void validateReleaseCreatingRequest(T releases, String deviceType) throws ApplicationManagementException; + /** + * Validate enterprise application release + * + */ + void validateEntAppReleaseCreatingRequest(EntAppReleaseWrapper releaseWrapper, String deviceType) + throws RequestValidatingException, ApplicationManagementException; + + /** + * Validate custom application release + * + */ + void validateCustomAppReleaseCreatingRequest(CustomAppReleaseWrapper releaseWrapper, String deviceType) + throws RequestValidatingException, ApplicationManagementException; + + /** + * Validate web application release + * + */ + void validateWebAppReleaseCreatingRequest(WebAppReleaseWrapper releaseWrapper) + throws RequestValidatingException, ApplicationManagementException; + + /** + * Validate public application release + * + */ + void validatePublicAppReleaseCreatingRequest(PublicAppReleaseWrapper releaseWrapper, String deviceType) + throws RequestValidatingException, ApplicationManagementException; + + /** + * Validates image files of the application release + * + * @param iconFile icon of the application release + * @param screenshots screenshots of the application release + * @throws RequestValidatingException if any image is invalid + */ void validateImageArtifacts(Base64File iconFile, List screenshots) throws RequestValidatingException; + /** + * Validates any base64 files, for example a base64file may an empty file name which is invalid + * + * @param file Base64 File to be validated + * @throws RequestValidatingException if the file is invalid + */ void validateBase64File(Base64File file) throws RequestValidatingException; /*** @@ -301,6 +378,12 @@ public interface ApplicationManager { void validateImageArtifacts(Attachment iconFile, Attachment bannerFile, List attachmentList) throws RequestValidatingException; + /** + * Validates binary file of the application release + * + * @param binaryFile binary file of the application release + * @throws RequestValidatingException if binary file is invalid + */ void validateBinaryArtifact(Base64File binaryFile) throws RequestValidatingException; void validateBinaryArtifact(Attachment binaryFile) throws RequestValidatingException; @@ -343,7 +426,6 @@ public interface ApplicationManager { void updateSubsStatus (int deviceId, int operationId, String status) throws ApplicationManagementException; - /** * Get plist content to download and install the application. * diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SPApplicationManager.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SPApplicationManager.java index ad96347ed6..94ee0cd0ac 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SPApplicationManager.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SPApplicationManager.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.common.services; import io.entgra.application.mgt.common.IdentityServer; @@ -6,28 +24,93 @@ import io.entgra.application.mgt.common.SPApplication; import io.entgra.application.mgt.common.exception.ApplicationManagementException; import io.entgra.application.mgt.common.exception.RequestValidatingException; import io.entgra.application.mgt.common.response.Application; - import java.util.List; public interface SPApplicationManager { - + /** + * This method adds existing consumer applications of service providers to the SPApplication bean + * + * @param identityServerId identity server id of the service provider + * @param applications Service providers list to which the existing applications should be added + * @throws ApplicationManagementException if error occurred while adding existing applications + */ void addExistingApps(int identityServerId, List applications) throws ApplicationManagementException; + /** + * Removes consumer application from service provider + * + * @param identityServerId of the service provider + * @param spUID uid of the service provider + * @param appIds List of application ids to be removed + * @throws ApplicationManagementException if errors while removing appIds from service provider + */ void detachSPApplications(int identityServerId, String spUID, List appIds) throws ApplicationManagementException; + /** + * Maps consumer applications to service provider + * + * @param identityServerId of the service provider + * @param spUID uid of the service provider + * @param appIds List of application ids to be mapped + * @throws ApplicationManagementException if errors while mapping appIds to service provider + */ void attachSPApplications(int identityServerId, String spUID, List appIds) throws ApplicationManagementException; + /** + * + * @param identityServerId of the identity server that is to be retrieved + * @return Identity server for the given ID + * @throws ApplicationManagementException if error occurred while getting identity server + */ IdentityServer getIdentityServer(int identityServerId) throws ApplicationManagementException; + /** + * + * @return Available identity servers + * @throws ApplicationManagementException if error occurred while getting identity servers + */ IdentityServerList getIdentityServers() throws ApplicationManagementException; + /** + * + * @param identityServerId of the service provider + * @param spUID uid of the service provider + * @return Applications that are mapped to given service provider uid and identity server id + * @throws ApplicationManagementException + */ List getSPApplications(int identityServerId, String spUID) throws ApplicationManagementException; + /** + * This method is responsible for creating a new application and mapping it the given service provider uid + * and identity server id + * + * @param app Application wrapper of the application that should be created + * @param identityServerId id of the identity server to which the created application should be mapped + * @param spId uid of the service provder to which the created application should be mapped + * @param Application wrapper class which depends on application type (PUBLIC, ENTERPRISE & etc) + * @return Application bean of the created application + * @throws ApplicationManagementException if errors while creating and mapping the application + * @throws RequestValidatingException if app contains any invalid payload + */ Application createSPApplication(T app, int identityServerId, String spId) throws ApplicationManagementException, RequestValidatingException; + /** + * Validates application ids of the applications that should be attached + * + * @param appIds application ids to be validated + * @throws ApplicationManagementException + */ void validateAttachAppsRequest(int identityServerId, List appIds) throws ApplicationManagementException; + /** + * Validates application ids of the applications that should be detached + * + * @param identityServerId id of identity server + * @param spId uid of service provider from which applications should be detached + * @param appIds applications ids to be detached + * @throws ApplicationManagementException + */ void validateDetachAppsRequest(int identityServerId, String spId, List appIds) throws ApplicationManagementException; } diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/CustomAppReleaseWrapper.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/CustomAppReleaseWrapper.java index adac0bf2f3..566e67d5d5 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/CustomAppReleaseWrapper.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/CustomAppReleaseWrapper.java @@ -79,6 +79,10 @@ public class CustomAppReleaseWrapper { value = "binary file of the application") private Base64File binaryFile; + @ApiModelProperty(name = "icon", + value = "banner of the application") + private Base64File banner; + public String getReleaseType() { return releaseType; } @@ -160,4 +164,12 @@ public class CustomAppReleaseWrapper { public void setBinaryFile(Base64File binaryFile) { this.binaryFile = binaryFile; } + + public Base64File getBanner() { + return banner; + } + + public void setBanner(Base64File banner) { + this.banner = banner; + } } diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/EntAppReleaseWrapper.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/EntAppReleaseWrapper.java index 2aa952ac40..6fdb22dc80 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/EntAppReleaseWrapper.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/EntAppReleaseWrapper.java @@ -82,6 +82,10 @@ public class EntAppReleaseWrapper { value = "binary file of the application") private Base64File binaryFile; + @ApiModelProperty(name = "icon", + value = "banner of the application") + private Base64File banner; + public String getReleaseType() { return releaseType; } @@ -161,4 +165,12 @@ public class EntAppReleaseWrapper { public void setBinaryFile(Base64File binaryFile) { this.binaryFile = binaryFile; } + + public Base64File getBanner() { + return banner; + } + + public void setBanner(Base64File banner) { + this.banner = banner; + } } diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/PublicAppReleaseWrapper.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/PublicAppReleaseWrapper.java index c9c5499bc2..3fdbdab0b4 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/PublicAppReleaseWrapper.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/PublicAppReleaseWrapper.java @@ -82,6 +82,10 @@ public class PublicAppReleaseWrapper { value = "icon of the application") private Base64File icon; + @ApiModelProperty(name = "icon", + value = "banner of the application") + private Base64File banner; + public String getReleaseType() { return releaseType; } @@ -149,4 +153,12 @@ public class PublicAppReleaseWrapper { public void setScreenshots(List screenshots) { this.screenshots = screenshots; } + + public Base64File getBanner() { + return banner; + } + + public void setBanner(Base64File banner) { + this.banner = banner; + } } diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/WebAppReleaseWrapper.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/WebAppReleaseWrapper.java index b2a8c2f3e4..3d0a343c74 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/WebAppReleaseWrapper.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/wrapper/WebAppReleaseWrapper.java @@ -73,6 +73,10 @@ public class WebAppReleaseWrapper { value = "icon of the application") private Base64File icon; + @ApiModelProperty(name = "icon", + value = "banner of the application") + private Base64File banner; + public String getReleaseType() { return releaseType; } @@ -140,4 +144,12 @@ public class WebAppReleaseWrapper { public void setIcon(Base64File icon) { this.icon = icon; } + + public Base64File getBanner() { + return banner; + } + + public void setBanner(Base64File banner) { + this.banner = banner; + } } diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SPApplicationDAO.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SPApplicationDAO.java index 878a032be0..850e11bf88 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SPApplicationDAO.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SPApplicationDAO.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.core.dao; import io.entgra.application.mgt.common.IdentityServer; @@ -7,47 +25,68 @@ import io.entgra.application.mgt.core.exception.ApplicationManagementDAOExceptio import java.util.List; public interface SPApplicationDAO { + /** * - * @param tenantId - * @return the application with the provided installer location - * @throws ApplicationManagementDAOException + * @param identityServerId Id of identity server in which the service provider is in + * @param spUID Service provider uid of which the applications to be retrieved + * @return the service provider applications for the given service provider + * @throws ApplicationManagementDAOException if any db error occurred */ List getSPApplications(int identityServerId, String spUID, int tenantId) throws ApplicationManagementDAOException; /** * - * @param tenantId - * @return the application with the provided installer location - * @throws ApplicationManagementDAOException + * @param identityServerId Id of identity server in which the service provider is in + * @param spUID Id of the service provider to which the application should be mapped + * @param appId Id of the application that should be mapped + * @return Primary key of the new service provider and application mapping entry + * @throws ApplicationManagementDAOException if any db error occurred */ int attachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException; /** * - * @param tenantId - * @return the application with the provided installer location - * @throws ApplicationManagementDAOException + * @param identityServerId Id of identity server in which the service provider is in + * @param spUID Id of the service provider from which the application should be removed + * @param appId Id of the application that should be removed + * @throws ApplicationManagementDAOException if any db error occurred */ void detachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException; + /** + * + * @return All available identity servers + * @throws ApplicationManagementDAOException if any db error occurred + */ List getIdentityServers(int tenantId) throws ApplicationManagementDAOException; + /** + * + * @param id Id of the Identity Server to be retrieved + * @return Identity Server of the given id + * @throws ApplicationManagementDAOException if any db error occurred + */ IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException; /** - * Verify whether application exist for given application name and device type. Because a name and device type is - * unique for an application. + * Verify whether application exist for given identity server id, service provider id and application id. + * Because if an application does not exist for those, it should not be mapped * - * @param appId id of the application. - * @param spUID UID of the service provider. - * @param tenantId ID of the tenant. - * @return ID of the ApplicationDTO. + * @param appId Id of the application. + * @param identityServerId Id of the identity server. + * @param spUID UID of the service provider. * @throws ApplicationManagementDAOException Application Management DAO Exception. */ boolean isSPApplicationExist(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException; + /** + * Delete application from all service providers if exists. When an application is deleted from the database + * it shoulbe be deleted from mapping table as well + * + * @param applicationId Id of the application to be deleted + * @throws ApplicationManagementDAOException if any db error occurred + */ void deleteApplicationFromServiceProviders(int applicationId, int tenantId) throws ApplicationManagementDAOException; - } diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 9972fb05cc..1bcdae86a9 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -262,9 +262,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic ResultSet rs = null; String sql = "SELECT count(AP_APP.ID) AS APP_COUNT " + "FROM AP_APP " - + "INNER JOIN AP_APP_RELEASE ON " - + "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID " - + "INNER JOIN (SELECT ID FROM AP_APP) AS app_data ON app_data.ID = AP_APP.ID " + "WHERE AP_APP.TENANT_ID = ?"; if (filter == null) { diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java index f7ab51eef0..fbbd57c849 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.core.dao.impl.application.spapplication; import io.entgra.application.mgt.common.IdentityServer; @@ -24,7 +42,7 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp private static final Log log = LogFactory.getLog(GenericApplicationDAOImpl.class); @Override public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { - String sql = "SELECT * " + String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ?"; try { @@ -52,7 +70,7 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp @Override public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { - String sql = "SELECT * " + String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ? AND " + "ID = ?"; @@ -181,7 +199,6 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); } - } @Override @@ -196,7 +213,6 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp + "AND AP_APP_ID = ? " + "AND IS_ID = ? " + "AND TENANT_ID = ? "; - try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { @@ -231,7 +247,6 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp + "AP_APP_ID, " + "IS_ID, TENANT_ID) " + "VALUES (?, ?, ?, ?)"; - int mappingId = -1; try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { @@ -242,9 +257,9 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp stmt.executeUpdate(); try (ResultSet rs = stmt.getGeneratedKeys()) { if (rs.next()) { - mappingId = rs.getInt(1); + return rs.getInt(1); } - return mappingId; + return -1; } } } catch (DBConnectionException e) { @@ -286,5 +301,4 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp } } - } diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java index 553daa263c..6372f3a2df 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.core.dao.impl.application.spapplication; import io.entgra.application.mgt.common.IdentityServer; @@ -24,7 +42,7 @@ public class OracleSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp @Override public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { - String sql = "SELECT * " + String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ?"; try { @@ -52,7 +70,7 @@ public class OracleSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp @Override public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { - String sql = "SELECT * " + String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ? AND " + "ID = ?"; @@ -231,7 +249,6 @@ public class OracleSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp + "AP_APP_ID, " + "IS_ID, TENANT_ID) " + "VALUES (?, ?, ?, ?)"; - int mappingId = -1; try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { @@ -242,9 +259,9 @@ public class OracleSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp stmt.executeUpdate(); try (ResultSet rs = stmt.getGeneratedKeys()) { if (rs.next()) { - mappingId = rs.getInt(1); + return rs.getInt(1); } - return mappingId; + return -1; } } } catch (DBConnectionException e) { diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java index 309c77b1d8..5ceb9403fc 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.core.dao.impl.application.spapplication; import io.entgra.application.mgt.common.IdentityServer; @@ -24,7 +42,7 @@ public class PostgreSQLSPApplicationDAOImpl extends AbstractDAOImpl implements S @Override public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { - String sql = "SELECT * " + String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ?"; try { @@ -52,7 +70,7 @@ public class PostgreSQLSPApplicationDAOImpl extends AbstractDAOImpl implements S @Override public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { - String sql = "SELECT * " + String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ? AND " + "ID = ?"; @@ -231,7 +249,6 @@ public class PostgreSQLSPApplicationDAOImpl extends AbstractDAOImpl implements S + "AP_APP_ID, " + "IS_ID, TENANT_ID) " + "VALUES (?, ?, ?, ?)"; - int mappingId = -1; try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { @@ -242,9 +259,9 @@ public class PostgreSQLSPApplicationDAOImpl extends AbstractDAOImpl implements S stmt.executeUpdate(); try (ResultSet rs = stmt.getGeneratedKeys()) { if (rs.next()) { - mappingId = rs.getInt(1); + return rs.getInt(1); } - return mappingId; + return -1; } } } catch (DBConnectionException e) { diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java index e31be22027..f8baebb7a4 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.core.dao.impl.application.spapplication; import io.entgra.application.mgt.common.IdentityServer; @@ -24,7 +42,7 @@ public class SQLServerSPApplicationDAOImpl extends AbstractDAOImpl implements S @Override public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { - String sql = "SELECT * " + String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ?"; try { @@ -52,7 +70,7 @@ public class SQLServerSPApplicationDAOImpl extends AbstractDAOImpl implements S @Override public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { - String sql = "SELECT * " + String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ? AND " + "ID = ?"; @@ -231,7 +249,6 @@ public class SQLServerSPApplicationDAOImpl extends AbstractDAOImpl implements S + "AP_APP_ID, " + "IS_ID, TENANT_ID) " + "VALUES (?, ?, ?, ?)"; - int mappingId = -1; try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { @@ -242,9 +259,9 @@ public class SQLServerSPApplicationDAOImpl extends AbstractDAOImpl implements S stmt.executeUpdate(); try (ResultSet rs = stmt.getGeneratedKeys()) { if (rs.next()) { - mappingId = rs.getInt(1); + return rs.getInt(1); } - return mappingId; + return -1; } } } catch (DBConnectionException e) { diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java index f413fee76c..ffdd342c78 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/ApplicationManagerImpl.java @@ -19,6 +19,7 @@ package io.entgra.application.mgt.core.impl; import io.entgra.application.mgt.common.Base64File; import io.entgra.application.mgt.core.dao.SPApplicationDAO; +import io.entgra.application.mgt.core.util.ApplicationManagementUtil; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringEscapeUtils; @@ -137,112 +138,222 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public ApplicationReleaseDTO uploadEntAppReleaseArtifacts(EntAppReleaseWrapper releaseWrapper, ApplicationArtifact artifact, - String deviceType) throws ApplicationManagementException { - ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - return uploadEntAppReleaseArtifacts(releaseDTO, artifact, deviceType, tenantId, true); + public Application createApplication(T app) throws ApplicationManagementException { + ApplicationDTO applicationDTO = uploadReleaseArtifactIfExist(app); + try { + ConnectionManagerUtil.beginDBTransaction(); + Application application = addAppDataIntoDB(applicationDTO); + ConnectionManagerUtil.commitDBTransaction(); + return application; + } catch (DBConnectionException e) { + String msg = "Error occurred while getting database connection."; + log.error(msg, e); + ApplicationManagementUtil.deleteArtifactIfExist(applicationDTO); + throw new ApplicationManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while disabling AutoCommit."; + log.error(msg, e); + ApplicationManagementUtil.deleteArtifactIfExist(applicationDTO); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + ApplicationManagementUtil.deleteArtifactIfExist(applicationDTO); + String msg = "Error occurred while adding application with the name " + applicationDTO.getName() + " to database "; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } @Override - public ApplicationDTO uploadEntAppReleaseArtifacts(ApplicationWrapper applicationWrapper, - ApplicationArtifact applicationArtifact) - throws ApplicationManagementException{ - if (log.isDebugEnabled()) { - log.debug("Ent. Application create request is received. Application name: " + applicationWrapper.getName() - + " Device type: " + applicationWrapper.getDeviceType()); + public ApplicationRelease createEntAppRelease(int appId, EntAppReleaseWrapper releaseWrapper) + throws ApplicationManagementException { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), releaseWrapper.getScreenshots(), + releaseWrapper.getBinaryFile(), releaseWrapper.getBanner()); + ApplicationDTO applicationDTO = applicationManager.getApplication(appId); + DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); + ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper); + releaseDTO = uploadEntAppReleaseArtifacts(releaseDTO, artifact, deviceType.getName(), true); + try { + return createRelease(applicationDTO, releaseDTO, ApplicationType.ENTERPRISE); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while creating ent app release for application with the name: " + applicationDTO.getName(); + log.error(msg, e); + deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue())); + throw new ApplicationManagementException(msg, e); } - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - ApplicationDTO applicationDTO = APIUtil.convertToAppDTO(applicationWrapper); - - //uploading application artifacts - ApplicationReleaseDTO applicationReleaseDTO = uploadEntAppReleaseArtifacts( - applicationDTO.getApplicationReleaseDTOs().get(0), applicationArtifact, - applicationWrapper.getDeviceType(), tenantId, false); - applicationDTO.getApplicationReleaseDTOs().clear(); - applicationDTO.getApplicationReleaseDTOs().add(applicationReleaseDTO); - return applicationDTO; } @Override - public ApplicationReleaseDTO uploadWebAppReleaseArtifacts(WebAppReleaseWrapper releaseWrapper, ApplicationArtifact artifact) - throws ResourceManagementException { + public ApplicationRelease createWebAppRelease(int appId, WebAppReleaseWrapper releaseWrapper) + throws ApplicationManagementException, ResourceManagementException { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + ApplicationDTO applicationDTO = applicationManager.getApplication(appId); + ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), + releaseWrapper.getScreenshots(), null, releaseWrapper.getBanner()); ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - return uploadWebAppReleaseArtifacts(releaseDTO, artifact, tenantId); + releaseDTO = uploadWebAppReleaseArtifacts(releaseDTO, artifact); + try { + return createRelease(applicationDTO, releaseDTO, ApplicationType.WEB_CLIP); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while creating web app release for application with the name: " + applicationDTO.getName(); + log.error(msg, e); + deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue())); + throw new ApplicationManagementException(msg, e); + } } @Override - public ApplicationDTO uploadWebAppReleaseArtifacts(WebAppWrapper webAppWrapper, - ApplicationArtifact applicationArtifact) - throws ApplicationManagementException{ - if (log.isDebugEnabled()) { - log.debug("Web clip create request is received. App name: " + webAppWrapper.getName() + " Device type: " - + Constants.ANY); - } - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - ApplicationDTO applicationDTO = APIUtil.convertToAppDTO(webAppWrapper); - ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); + public ApplicationRelease createPubAppRelease(int appId, PublicAppReleaseWrapper releaseWrapper) throws + ResourceManagementException, ApplicationManagementException { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + ApplicationDTO applicationDTO = applicationManager.getApplication(appId); + DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); + ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), + releaseWrapper.getScreenshots(), null, releaseWrapper.getBanner()); + ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper); + releaseDTO = uploadPubAppReleaseArtifacts(releaseDTO, artifact, deviceType.getName()); try { - applicationDTO.getApplicationReleaseDTOs().clear(); - applicationDTO.getApplicationReleaseDTOs().add(uploadWebAppReleaseArtifacts(applicationReleaseDTO, applicationArtifact, tenantId)); - return applicationDTO; - } catch (ResourceManagementException e) { - String msg = "Error Occurred when uploading artifacts of the web clip: " + webAppWrapper.getName(); - log.error(msg); + return createRelease(applicationDTO, releaseDTO, ApplicationType.PUBLIC); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while creating ent public release for application with the name: " + applicationDTO.getName(); + log.error(msg, e); + deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue())); throw new ApplicationManagementException(msg, e); } } - public ApplicationReleaseDTO uploadWebAppReleaseArtifacts(ApplicationReleaseDTO releaseDTO, ApplicationArtifact applicationArtifact, - int tenantId) - throws ResourceManagementException { - releaseDTO.setUuid(UUID.randomUUID().toString()); - releaseDTO.setAppHashValue(DigestUtils.md5Hex(releaseDTO.getInstallerName())); - //uploading application artifacts - return addImageArtifacts(releaseDTO, applicationArtifact, tenantId); - } - @Override - public ApplicationReleaseDTO uploadPubAppReleaseArtifacts(PublicAppReleaseWrapper releaseWrapper, ApplicationArtifact artifact, - String deviceType) throws ResourceManagementException { + public ApplicationRelease createCustomAppRelease(int appId, CustomAppReleaseWrapper releaseWrapper) + throws ResourceManagementException, ApplicationManagementException { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + ApplicationDTO applicationDTO = applicationManager.getApplication(appId); + DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); + ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), + releaseWrapper.getScreenshots(), releaseWrapper.getBinaryFile(), releaseWrapper.getBanner()); ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - return uploadPubAppReleaseArtifacts(releaseDTO, artifact, deviceType, tenantId); + releaseDTO = uploadCustomAppReleaseArtifacts(releaseDTO, artifact, deviceType.getName()); + try { + return createRelease(applicationDTO, releaseDTO, ApplicationType.CUSTOM); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while creating custom app release for application with the name: " + applicationDTO.getName(); + log.error(msg, e); + deleteApplicationArtifacts(Collections.singletonList(releaseDTO.getAppHashValue())); + throw new ApplicationManagementException(msg, e); + } } @Override - public ApplicationDTO uploadPublicAppReleaseArtifacts(PublicAppWrapper publicAppWrapper, - ApplicationArtifact applicationArtifact) - throws ApplicationManagementException{ - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + public ApplicationDTO uploadReleaseArtifactIfExist(T app) throws ApplicationManagementException { + if (ApplicationManagementUtil.isReleaseAvailable(app)) { + return uploadReleaseArtifact(app); + } + return APIUtil.convertToAppDTO(app); + } + /** + * Upload release artifacts depending on the application wrapper type + * + * @param app Application wrapper bean + * @param Application Wrapper class + * @return ApplicationDTO that is constructed after uploading the artifacts + * @throws ApplicationManagementException if any error occurred while uploading artifacts + */ + private ApplicationDTO uploadReleaseArtifact(T app) + throws ApplicationManagementException { + ApplicationArtifact artifact; + ApplicationDTO applicationDTO = APIUtil.convertToAppDTO(app); + ApplicationReleaseDTO releaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); if (log.isDebugEnabled()) { - log.debug("Public app creating request is received. App name: " + publicAppWrapper.getName() - + " Device Type: " + publicAppWrapper.getDeviceType()); + log.debug("Ent. Application create request is received. Application name: " + applicationDTO.getName()); } + try { + if (app instanceof ApplicationWrapper) { + ApplicationWrapper wrapper = (ApplicationWrapper) app; + EntAppReleaseWrapper releaseWrapper = wrapper.getEntAppReleaseWrappers().get(0); + artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), + releaseWrapper.getScreenshots(), releaseWrapper.getBinaryFile(), releaseWrapper.getBanner()); + releaseDTO = uploadEntAppReleaseArtifacts(releaseDTO, + artifact, wrapper.getDeviceType(), false); + } else if (app instanceof PublicAppWrapper) { + PublicAppWrapper wrapper = (PublicAppWrapper) app; + PublicAppReleaseWrapper releaseWrapper = wrapper.getPublicAppReleaseWrappers().get(0); + artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), + releaseWrapper.getScreenshots(), null, releaseWrapper.getBanner()); + releaseDTO = uploadPubAppReleaseArtifacts(releaseDTO, artifact, wrapper.getDeviceType()); + } else if (app instanceof WebAppWrapper) { + WebAppWrapper wrapper = (WebAppWrapper) app; + WebAppReleaseWrapper releaseWrapper = wrapper.getWebAppReleaseWrappers().get(0); + artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), + releaseWrapper.getScreenshots(), null, releaseWrapper.getBanner()); + releaseDTO = uploadWebAppReleaseArtifacts(releaseDTO, artifact); + } else if (app instanceof CustomAppWrapper) { + CustomAppWrapper wrapper = (CustomAppWrapper) app; + CustomAppReleaseWrapper releaseWrapper = wrapper.getCustomAppReleaseWrappers().get(0); + artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), + releaseWrapper.getScreenshots(), releaseWrapper.getBinaryFile(), releaseWrapper.getBanner()); + try { + releaseDTO = uploadCustomAppReleaseArtifacts(releaseDTO, artifact, wrapper.getDeviceType()); + } catch (ResourceManagementException e) { + String msg = "Error Occurred when uploading artifacts of the web clip: " + wrapper.getName(); + log.error(msg); + throw new ApplicationManagementException(msg, e); + } + } else { + String msg = "Invalid payload found with the request. Hence verify the request payload object."; + log.error(msg); + throw new ApplicationManagementException(msg); + } + } catch (ResourceManagementException e) { + String msg = "Error Occurred when uploading artifacts of the web clip: " + applicationDTO.getName(); + log.error(msg); + throw new ApplicationManagementException(msg, e); + } + applicationDTO.getApplicationReleaseDTOs().clear(); + applicationDTO.getApplicationReleaseDTOs().add(releaseDTO); + return applicationDTO; + } - ApplicationDTO applicationDTO = APIUtil.convertToAppDTO(publicAppWrapper); - ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); - validatePublicAppReleasePackageName(applicationReleaseDTO.getPackageName()); - + /** + * Upload enterprise application release artifact into file system. + * + * @param releaseDTO Application Release + * @param applicationArtifact Application Release artifacts + * @param deviceTypeName Device Type name + * @param isNewRelease New Release or Not + * @return {@link ApplicationReleaseDTO} + * @throws ApplicationManagementException if error occurred while uploading artifacts into file system. + */ + private ApplicationReleaseDTO uploadEntAppReleaseArtifacts(ApplicationReleaseDTO releaseDTO, + ApplicationArtifact applicationArtifact, String deviceTypeName, boolean isNewRelease) + throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { - //uploading application artifacts - applicationReleaseDTO = uploadPubAppReleaseArtifacts(applicationReleaseDTO, applicationArtifact, - publicAppWrapper.getDeviceType(), tenantId); - applicationDTO.getApplicationReleaseDTOs().clear(); - applicationDTO.getApplicationReleaseDTOs().add(applicationReleaseDTO); - return applicationDTO; + ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(deviceTypeName, releaseDTO, + applicationArtifact, isNewRelease); + return addImageArtifacts(applicationReleaseDTO, applicationArtifact, tenantId); } catch (ResourceManagementException e) { - String msg = "Error Occured when uploading artifacts of the public app: " + publicAppWrapper.getName(); + String msg = "Error occurred while uploading application release artifacts."; log.error(msg, e); throw new ApplicationManagementException(msg, e); } } - public ApplicationReleaseDTO uploadPubAppReleaseArtifacts(ApplicationReleaseDTO releaseDTO, ApplicationArtifact applicationArtifact, - String deviceType, int tenantId) + private ApplicationReleaseDTO uploadWebAppReleaseArtifacts(ApplicationReleaseDTO releaseDTO, ApplicationArtifact applicationArtifact) + throws ResourceManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + releaseDTO.setUuid(UUID.randomUUID().toString()); + releaseDTO.setAppHashValue(DigestUtils.md5Hex(releaseDTO.getInstallerName())); + //uploading application artifacts + return addImageArtifacts(releaseDTO, applicationArtifact, tenantId); + } + + private ApplicationReleaseDTO uploadPubAppReleaseArtifacts(ApplicationReleaseDTO releaseDTO, ApplicationArtifact applicationArtifact, + String deviceType) throws ResourceManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String appInstallerUrl = getPublicAppStorePath(deviceType) + releaseDTO.getPackageName(); releaseDTO.setInstallerName(appInstallerUrl); releaseDTO.setUuid(UUID.randomUUID().toString()); @@ -273,38 +384,10 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override - public ApplicationReleaseDTO uploadCustomAppReleaseArtifacts(CustomAppReleaseWrapper releaseWrapper, ApplicationArtifact artifact, - String deviceType) throws ResourceManagementException, ApplicationManagementException { - ApplicationReleaseDTO releaseDTO = APIUtil.releaseWrapperToReleaseDTO(releaseWrapper); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - return uploadCustomAppReleaseArtifacts(releaseDTO, artifact, deviceType, tenantId); - } - - @Override - public ApplicationDTO uploadCustomAppReleaseArtifactsAndConstructAppDTO(CustomAppWrapper customAppWrapper, - ApplicationArtifact applicationArtifact) - throws ApplicationManagementException { - try { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - ApplicationDTO applicationDTO = APIUtil.convertToAppDTO(customAppWrapper); - ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); - applicationReleaseDTO = uploadCustomAppReleaseArtifacts(applicationReleaseDTO, applicationArtifact, - customAppWrapper.getDeviceType(), tenantId); - applicationDTO.getApplicationReleaseDTOs().clear(); - applicationDTO.getApplicationReleaseDTOs().add(applicationReleaseDTO); - return applicationDTO; - } catch (ResourceManagementException e) { - String msg = "Error occurred while uploading application artifact into the server. Application name: " - + customAppWrapper.getName() + " Device type: " + customAppWrapper.getDeviceType(); - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } - } - - public ApplicationReleaseDTO uploadCustomAppReleaseArtifacts(ApplicationReleaseDTO releaseDTO, ApplicationArtifact applicationArtifact, - String deviceType, int tenantId) + private ApplicationReleaseDTO uploadCustomAppReleaseArtifacts(ApplicationReleaseDTO releaseDTO, ApplicationArtifact applicationArtifact, + String deviceType) throws ResourceManagementException, ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); byte[] content = getByteContentOfApp(applicationArtifact); String md5OfApp = generateMD5OfApp(applicationArtifact, content); @@ -379,16 +462,15 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - - /** * Delete Application release artifacts * * @param directoryPaths Directory paths - * @param tenantId Tenant Id * @throws ApplicationManagementException if error occurred while deleting application release artifacts. */ - private void deleteApplicationArtifacts(List directoryPaths, int tenantId) throws ApplicationManagementException { + @Override + public void deleteApplicationArtifacts(List directoryPaths) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { applicationStorageManager.deleteAllApplicationReleaseArtifacts(directoryPaths, tenantId); @@ -693,7 +775,6 @@ public class ApplicationManagerImpl implements ApplicationManager { return applicationReleaseDTO; } - @Override public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -950,32 +1031,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public Application persistApplication(ApplicationDTO applicationDTO) throws ApplicationManagementException { - try { - ConnectionManagerUtil.beginDBTransaction(); - Application application = executeApplicationPersistenceTransaction(applicationDTO); - ConnectionManagerUtil.commitDBTransaction(); - return application; - } catch (DBConnectionException e) { - String msg = "Error occurred while getting database connection."; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (TransactionManagementException e) { - String msg = "Error occurred while disabling AutoCommit."; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (ApplicationManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred while adding application with the name " + applicationDTO.getName() + " to database "; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - @Override - public Application executeApplicationPersistenceTransaction(ApplicationDTO applicationDTO) throws + public Application addAppDataIntoDB(ApplicationDTO applicationDTO) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationReleaseDTO applicationReleaseDTO = null; @@ -986,10 +1042,6 @@ public class ApplicationManagerImpl implements ApplicationManager { // Insert to application table int appId = this.applicationDAO.createApplication(applicationDTO, tenantId); if (appId == -1) { - if (applicationReleaseDTO != null) { - deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue()), - tenantId); - } String msg = "Application data storing is Failed."; log.error(msg); throw new ApplicationManagementDAOException(msg); @@ -1061,35 +1113,22 @@ public class ApplicationManagerImpl implements ApplicationManager { String msg = "Error occurred while adding lifecycle state. application name: " + applicationDTO.getName() + "."; log.error(msg, e); - try { - APIUtil.getApplicationStorageManager().deleteAllApplicationReleaseArtifacts( - Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); - } catch (ApplicationStorageManagementException ex) { - String errorLog = - "Error occurred when deleting application artifacts. Application artifacts are tried to " - + "delete because of lifecycle state adding issue in the application creating operation."; - log.error(errorLog, ex); - throw new ApplicationManagementException(errorLog, ex); - } throw new ApplicationManagementException(msg, e); } catch (ApplicationManagementDAOException e) { String msg = "Error occurred while adding application or application release. application name: " + applicationDTO.getName() + "."; log.error(msg, e); - deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); throw new ApplicationManagementException(msg, e); } catch (LifecycleManagementException e) { String msg = "Error occurred when getting initial lifecycle state. application name: " + applicationDTO.getName() + "."; log.error(msg, e); - deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); throw new ApplicationManagementException(msg, e); } catch (VisibilityManagementDAOException e) { String msg = "Error occurred while adding unrestricted roles. application name: " + applicationDTO.getName() + "."; log.error(msg, e); - deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); throw new ApplicationManagementException(msg, e); } } @@ -1103,7 +1142,6 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("Application release creating request is received for the application id: " + applicationDTO.getId()); } - if (!type.toString().equals(applicationDTO.getType())) { String msg = "It is possible to add new application release for " + type + " app type. But you are requesting to add new application release for " + applicationDTO.getType() @@ -1192,31 +1230,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - /** - * Upload enterprise application release artifact into file system. - * - * @param releaseDTO Apllication Release - * @param applicationArtifact Application Release artifacts - * @param deviceTypeName Device Type name - * @param tenantId Tenant Id - * @param isNewRelease New Release or Not - * @return {@link ApplicationReleaseDTO} - * @throws ApplicationManagementException if error occurred while uploading artifacts into file system. - */ - private ApplicationReleaseDTO uploadEntAppReleaseArtifacts(ApplicationReleaseDTO releaseDTO, - ApplicationArtifact applicationArtifact, String deviceTypeName, int tenantId, boolean isNewRelease) - throws ApplicationManagementException { - try { - ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(deviceTypeName, releaseDTO, - applicationArtifact, isNewRelease); - return addImageArtifacts(applicationReleaseDTO, applicationArtifact, tenantId); - } catch (ResourceManagementException e) { - String msg = "Error occurred while uploading application release artifacts."; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } - } - /** * Check whether given OS range is valid or invalid * @@ -1373,7 +1386,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override public Application getApplicationByUuid(String releaseUuid, String state) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -1658,66 +1670,62 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.closeDBConnection(); } - if (applicationDTO.getApplicationReleaseDTOs().size() == 1) { - deleteApplication(applicationDTO, tenantId); - } else { - for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) { - if (releaseUuid.equals(applicationReleaseDTO.getUuid())) { - if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())) { - String msg = - "Application state is not in the deletable state. Therefore you are not permitted to " - + "delete the application release."; + for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) { + if (releaseUuid.equals(applicationReleaseDTO.getUuid())) { + if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())) { + String msg = + "Application state is not in the deletable state. Therefore you are not permitted to " + + "delete the application release."; + log.error(msg); + throw new ForbiddenException(msg); + } + try { + ConnectionManagerUtil.beginDBTransaction(); + List deviceSubscriptionDTOS = subscriptionDAO + .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId, null, null); + if (!deviceSubscriptionDTOS.isEmpty()) { + String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() + + " either subscribed to device/s or it had subscribed to device/s. Therefore you " + + "are not permitted to delete the application release."; log.error(msg); throw new ForbiddenException(msg); } - try { - ConnectionManagerUtil.beginDBTransaction(); - List deviceSubscriptionDTOS = subscriptionDAO - .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId, null, null); - if (!deviceSubscriptionDTOS.isEmpty()) { - String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() - + " either subscribed to device/s or it had subscribed to device/s. Therefore you " - + "are not permitted to delete the application release."; - log.error(msg); - throw new ForbiddenException(msg); - } - lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId()); - applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId()); - applicationStorageManager.deleteAllApplicationReleaseArtifacts( - Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); - ConnectionManagerUtil.commitDBTransaction(); - } catch (DBConnectionException e) { - String msg = "Error occurred while observing the database connection to delete application " - + "release which has the UUID:" + releaseUuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (TransactionManagementException e) { - String msg = "Database access error is occurred when deleting application release which has " - + "the UUID: " + releaseUuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (ApplicationManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred while verifying whether application relase has an subscription or " - + "not. Application release UUID: " + releaseUuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (ApplicationStorageManagementException e) { - String msg = "Error occurred when deleting the application release artifact from the file " - + "system. Application release UUID: " + releaseUuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (LifeCycleManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred when deleting lifecycle data for application release UUID: " - + releaseUuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - break; + lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId()); + applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId()); + applicationStorageManager.deleteAllApplicationReleaseArtifacts( + Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); + ConnectionManagerUtil.commitDBTransaction(); + } catch (DBConnectionException e) { + String msg = "Error occurred while observing the database connection to delete application " + + "release which has the UUID:" + releaseUuid; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Database access error is occurred when deleting application release which has " + + "the UUID: " + releaseUuid; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred while verifying whether application relase has an subscription or " + + "not. Application release UUID: " + releaseUuid; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationStorageManagementException e) { + String msg = "Error occurred when deleting the application release artifact from the file " + + "system. Application release UUID: " + releaseUuid; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (LifeCycleManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred when deleting lifecycle data for application release UUID: " + + releaseUuid; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); } + break; } } } @@ -3187,9 +3195,8 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override - public void validateAppCreatingRequest(T param, boolean isReleaseRequired) throws ApplicationManagementException { + public void validateAppCreatingRequest(T param) throws ApplicationManagementException, RequestValidatingException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); int deviceTypeId = -1; @@ -3223,17 +3230,12 @@ public class ApplicationManagerImpl implements ApplicationManager { } DeviceType deviceType = APIUtil.getDeviceTypeData(applicationWrapper.getDeviceType()); deviceTypeId = deviceType.getId(); - - List entAppReleaseWrappers; - entAppReleaseWrappers = applicationWrapper.getEntAppReleaseWrappers(); - - if (isReleaseRequired && entAppReleaseWrappers.size() != 1) { - String msg = "Invalid application creating request. Application creating request must have single " - + "application release. Application name:" + applicationWrapper.getName() + "."; - log.error(msg); - throw new BadRequestException(msg); - } unrestrictedRoles = applicationWrapper.getUnrestrictedRoles(); + List releaseWrappers = applicationWrapper.getEntAppReleaseWrappers(); + if (!releaseWrappers.isEmpty()) { + EntAppReleaseWrapper releaseWrapper = releaseWrappers.get(0); + validateEntAppReleaseCreatingRequest(releaseWrapper, applicationWrapper.getDeviceType()); + } } else if (param instanceof WebAppWrapper) { WebAppWrapper webAppWrapper = (WebAppWrapper) param; appName = webAppWrapper.getName(); @@ -3261,17 +3263,12 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg); throw new BadRequestException(msg); } - - List webAppReleaseWrappers; - webAppReleaseWrappers = webAppWrapper.getWebAppReleaseWrappers(); - - if (isReleaseRequired && webAppReleaseWrappers.size() != 1) { - String msg = "Invalid web clip creating request. Web clip creating request must have single " - + "web clip release. Web clip name:" + webAppWrapper.getName() + "."; - log.error(msg); - throw new BadRequestException(msg); - } unrestrictedRoles = webAppWrapper.getUnrestrictedRoles(); + List releaseWrappers = webAppWrapper.getWebAppReleaseWrappers(); + if(!releaseWrappers.isEmpty()) { + WebAppReleaseWrapper releaseWrapper = releaseWrappers.get(0); + validateWebAppReleaseCreatingRequest(releaseWrapper); + } } else if (param instanceof PublicAppWrapper) { PublicAppWrapper publicAppWrapper = (PublicAppWrapper) param; appName = publicAppWrapper.getName(); @@ -3298,17 +3295,12 @@ public class ApplicationManagerImpl implements ApplicationManager { } DeviceType deviceType = APIUtil.getDeviceTypeData(publicAppWrapper.getDeviceType()); deviceTypeId = deviceType.getId(); - - List publicAppReleaseWrappers; - publicAppReleaseWrappers = publicAppWrapper.getPublicAppReleaseWrappers(); - - if (isReleaseRequired && publicAppReleaseWrappers.size() != 1) { - String msg = "Invalid public app creating request. Request must have single release. Application name:" - + publicAppWrapper.getName() + "."; - log.error(msg); - throw new BadRequestException(msg); - } unrestrictedRoles = publicAppWrapper.getUnrestrictedRoles(); + List releaseWrappers = publicAppWrapper.getPublicAppReleaseWrappers(); + if(!releaseWrappers.isEmpty()) { + PublicAppReleaseWrapper releaseWrapper = releaseWrappers.get(0); + validatePublicAppReleaseCreatingRequest(releaseWrapper, publicAppWrapper.getDeviceType()); + } } else if (param instanceof CustomAppWrapper) { CustomAppWrapper customAppWrapper = (CustomAppWrapper) param; appName = customAppWrapper.getName(); @@ -3335,17 +3327,12 @@ public class ApplicationManagerImpl implements ApplicationManager { } DeviceType deviceType = APIUtil.getDeviceTypeData(customAppWrapper.getDeviceType()); deviceTypeId = deviceType.getId(); - - List customAppReleaseWrappers; - customAppReleaseWrappers = customAppWrapper.getCustomAppReleaseWrappers(); - - if (isReleaseRequired && customAppReleaseWrappers.size() != 1) { - String msg = "Invalid custom app creating request. Application creating request must have single " - + "application release. Application name:" + customAppWrapper.getName() + "."; - log.error(msg); - throw new BadRequestException(msg); - } unrestrictedRoles = customAppWrapper.getUnrestrictedRoles(); + List releaseWrappers = customAppWrapper.getCustomAppReleaseWrappers(); + if(!releaseWrappers.isEmpty()) { + CustomAppReleaseWrapper releaseWrapper = releaseWrappers.get(0); + validateCustomAppReleaseCreatingRequest(releaseWrapper, customAppWrapper.getDeviceType()); + } } else { String msg = "Invalid payload found with the request. Hence verify the request payload object."; log.error(msg); @@ -3518,6 +3505,37 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + @Override + public void validateEntAppReleaseCreatingRequest(EntAppReleaseWrapper releaseWrapper, String deviceType) + throws RequestValidatingException, ApplicationManagementException { + validateReleaseCreatingRequest(releaseWrapper, deviceType); + validateBinaryArtifact(releaseWrapper.getBinaryFile()); + validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots()); + } + + @Override + public void validateCustomAppReleaseCreatingRequest(CustomAppReleaseWrapper releaseWrapper, String deviceType) + throws RequestValidatingException, ApplicationManagementException { + validateReleaseCreatingRequest(releaseWrapper, deviceType); + validateBinaryArtifact(releaseWrapper.getBinaryFile()); + validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots()); + } + + @Override + public void validateWebAppReleaseCreatingRequest(WebAppReleaseWrapper releaseWrapper) + throws RequestValidatingException, ApplicationManagementException { + validateReleaseCreatingRequest(releaseWrapper, Constants.ANY); + validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots()); + } + + @Override + public void validatePublicAppReleaseCreatingRequest(PublicAppReleaseWrapper releaseWrapper, String deviceType) + throws RequestValidatingException, ApplicationManagementException { + validateReleaseCreatingRequest(releaseWrapper, deviceType); + validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots()); + validatePublicAppReleasePackageName(releaseWrapper.getPackageName()); + } + @Override public void validateImageArtifacts(Base64File iconFile, List screenshots) throws RequestValidatingException { diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SPApplicationManagerImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SPApplicationManagerImpl.java index 447487e0d4..1f90f5db06 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SPApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SPApplicationManagerImpl.java @@ -1,25 +1,33 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.core.impl; -import io.entgra.application.mgt.common.ApplicationArtifact; import io.entgra.application.mgt.common.IdentityServer; import io.entgra.application.mgt.common.IdentityServerList; import io.entgra.application.mgt.common.SPApplication; import io.entgra.application.mgt.common.dto.ApplicationDTO; import io.entgra.application.mgt.common.exception.ApplicationManagementException; import io.entgra.application.mgt.common.exception.DBConnectionException; -import io.entgra.application.mgt.common.exception.RequestValidatingException; import io.entgra.application.mgt.common.exception.TransactionManagementException; import io.entgra.application.mgt.common.response.Application; import io.entgra.application.mgt.common.services.ApplicationManager; import io.entgra.application.mgt.common.services.SPApplicationManager; -import io.entgra.application.mgt.common.wrapper.ApplicationWrapper; -import io.entgra.application.mgt.common.wrapper.CustomAppReleaseWrapper; -import io.entgra.application.mgt.common.wrapper.CustomAppWrapper; -import io.entgra.application.mgt.common.wrapper.EntAppReleaseWrapper; -import io.entgra.application.mgt.common.wrapper.PublicAppReleaseWrapper; -import io.entgra.application.mgt.common.wrapper.PublicAppWrapper; -import io.entgra.application.mgt.common.wrapper.WebAppReleaseWrapper; -import io.entgra.application.mgt.common.wrapper.WebAppWrapper; import io.entgra.application.mgt.core.dao.ApplicationDAO; import io.entgra.application.mgt.core.dao.SPApplicationDAO; import io.entgra.application.mgt.core.dao.VisibilityDAO; @@ -31,12 +39,9 @@ import io.entgra.application.mgt.core.lifecycle.LifecycleStateManager; import io.entgra.application.mgt.core.util.APIUtil; import io.entgra.application.mgt.core.util.ApplicationManagementUtil; import io.entgra.application.mgt.core.util.ConnectionManagerUtil; -import io.entgra.application.mgt.core.util.Constants; -import io.entgra.application.mgt.core.util.SPApplicationManagementUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; - import java.util.ArrayList; import java.util.List; @@ -218,7 +223,6 @@ public class SPApplicationManagerImpl implements SPApplicationManager { } } - public void detachSPApplications(int identityServerId, String spUID, List appIds) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { @@ -239,54 +243,40 @@ public class SPApplicationManagerImpl implements SPApplicationManager { } @Override - public Application createSPApplication(T app, int identityServerId, String spId) throws ApplicationManagementException, - RequestValidatingException { + public Application createSPApplication(T app, int identityServerId, String spId) throws ApplicationManagementException { ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance(); - applicationManager.validateAppCreatingRequest(app, false); - ApplicationDTO applicationDTO = constructApplicationDTO(app); + ApplicationDTO applicationDTO = applicationManager.uploadReleaseArtifactIfExist(app); + if (log.isDebugEnabled()) { + log.debug("Application release create request is received. Application name: " + applicationDTO.getName() + + " Device type ID: " + applicationDTO.getDeviceTypeId()); + } try { ConnectionManagerUtil.beginDBTransaction(); - Application createdApp = createSPApplication(applicationDTO); + Application createdApp = applicationManager.addAppDataIntoDB(applicationDTO); attachCreatedSPApplication(createdApp, identityServerId, spId); ConnectionManagerUtil.commitDBTransaction(); return createdApp; } catch (DBConnectionException e) { String msg = "Error occurred while getting database connection."; log.error(msg, e); + ApplicationManagementUtil.deleteArtifactIfExist(applicationDTO); throw new ApplicationManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while disabling AutoCommit."; log.error(msg, e); + ApplicationManagementUtil.deleteArtifactIfExist(applicationDTO); throw new ApplicationManagementException(msg, e); } catch (ApplicationManagementException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred while creating and attaching application with the name " + applicationDTO.getName() ; log.error(msg, e); + ApplicationManagementUtil.deleteArtifactIfExist(applicationDTO); throw new ApplicationManagementException(msg, e); } finally { ConnectionManagerUtil.closeDBConnection(); } } - private ApplicationDTO constructApplicationDTO(T app) throws ApplicationManagementException, - RequestValidatingException { - if (ApplicationManagementUtil.isReleaseAvailable(app)) { - return uploadReleaseArtifactAndConstructApplicationDTO(app); - } - return APIUtil.convertToAppDTO(app); - } - - public Application createSPApplication(ApplicationDTO applicationDTO) throws ApplicationManagementException { - if (log.isDebugEnabled()) { - log.debug("Application release create request is received. Application name: " + applicationDTO.getName() - + " Device type ID: " + applicationDTO.getDeviceTypeId()); - } - ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance(); - return applicationManager.executeApplicationPersistenceTransaction(applicationDTO); - } - - - public void attachCreatedSPApplication(Application createdApp, int identityServerId, String spUID) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { @@ -298,50 +288,4 @@ public class SPApplicationManagerImpl implements SPApplicationManager { } } - public ApplicationDTO uploadReleaseArtifactAndConstructApplicationDTO(T app) throws ApplicationManagementException, - RequestValidatingException { - ApplicationArtifact artifact; - ApplicationDTO applicationDTO; - ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance(); - if (app instanceof ApplicationWrapper) { - ApplicationWrapper wrapper = (ApplicationWrapper) app; - EntAppReleaseWrapper releaseWrapper = wrapper.getEntAppReleaseWrappers().get(0); - applicationManager.validateReleaseCreatingRequest(releaseWrapper, wrapper.getDeviceType()); - applicationManager.validateBinaryArtifact(releaseWrapper.getBinaryFile()); - applicationManager.validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots()); - artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), - releaseWrapper.getScreenshots(), releaseWrapper.getBinaryFile(), null); - applicationDTO = applicationManager.uploadEntAppReleaseArtifacts(wrapper, artifact); - } else if (app instanceof PublicAppWrapper) { - PublicAppWrapper wrapper = (PublicAppWrapper) app; - PublicAppReleaseWrapper releaseWrapper = wrapper.getPublicAppReleaseWrappers().get(0); - applicationManager.validateReleaseCreatingRequest(releaseWrapper, wrapper.getDeviceType()); - applicationManager.validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots()); - artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), - releaseWrapper.getScreenshots(), null, null); - applicationDTO = applicationManager.uploadPublicAppReleaseArtifacts(wrapper, artifact); - } else if (app instanceof WebAppWrapper) { - WebAppWrapper wrapper = (WebAppWrapper) app; - WebAppReleaseWrapper releaseWrapper = wrapper.getWebAppReleaseWrappers().get(0); - applicationManager.validateReleaseCreatingRequest(releaseWrapper, Constants.ANY); - applicationManager.validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots()); - artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), - releaseWrapper.getScreenshots(), null, null); - applicationDTO = applicationManager.uploadWebAppReleaseArtifacts(wrapper, artifact); - } else if (app instanceof CustomAppWrapper) { - CustomAppWrapper wrapper = (CustomAppWrapper) app; - CustomAppReleaseWrapper releaseWrapper = wrapper.getCustomAppReleaseWrappers().get(0); - applicationManager.validateReleaseCreatingRequest(releaseWrapper, wrapper.getDeviceType()); - applicationManager.validateBinaryArtifact(releaseWrapper.getBinaryFile()); - applicationManager.validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots()); - artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(), - releaseWrapper.getScreenshots(), releaseWrapper.getBinaryFile(), null); - applicationDTO = applicationManager.uploadCustomAppReleaseArtifactsAndConstructAppDTO(wrapper, artifact); - } else { - String msg = "Invalid payload found with the request. Hence verify the request payload object."; - log.error(msg); - throw new ApplicationManagementException(msg); - } - return applicationDTO; - } } diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/ApplicationManagementUtil.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/ApplicationManagementUtil.java index ad5cabf8a4..f0ef6c4b29 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/ApplicationManagementUtil.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/ApplicationManagementUtil.java @@ -18,12 +18,13 @@ package io.entgra.application.mgt.core.util; import io.entgra.application.mgt.common.ApplicationArtifact; -import io.entgra.application.mgt.common.ApplicationSubscriptionType; -import io.entgra.application.mgt.common.ApplicationType; import io.entgra.application.mgt.common.Base64File; import io.entgra.application.mgt.common.FileDataHolder; +import io.entgra.application.mgt.common.dto.ApplicationDTO; +import io.entgra.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.application.mgt.common.exception.ApplicationManagementException; import io.entgra.application.mgt.common.exception.RequestValidatingException; +import io.entgra.application.mgt.common.exception.ResourceManagementException; import io.entgra.application.mgt.common.services.SPApplicationManager; import io.entgra.application.mgt.common.wrapper.ApplicationWrapper; import io.entgra.application.mgt.common.wrapper.CustomAppReleaseWrapper; @@ -46,11 +47,10 @@ import io.entgra.application.mgt.core.config.Extension; import io.entgra.application.mgt.core.lifecycle.LifecycleStateManager; import org.wso2.carbon.device.mgt.core.common.util.FileUtil; -import java.io.ByteArrayInputStream; import java.io.InputStream; import java.lang.reflect.Constructor; -import java.util.ArrayList; -import java.util.Base64; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -63,6 +63,16 @@ public class ApplicationManagementUtil { private static Log log = LogFactory.getLog(ApplicationManagementUtil.class); + /** + * Construct ApplicationArtifact from given base64 artifact files + * + * @param iconBase64 icon of the application + * @param screenshotsBase64 screenshots of the application + * @param binaryFileBase64 binary file of the application + * @param bannerFileBase64 banner of the application + * @return ApplicationArtifact the give base64 release artifact files + * @throws BadRequestException if any invalid payload is found + */ public static ApplicationArtifact constructApplicationArtifact(Base64File iconBase64, List screenshotsBase64, Base64File binaryFileBase64, Base64File bannerFileBase64) throws BadRequestException { @@ -107,6 +117,8 @@ public class ApplicationManagementUtil { if (screenshotsBase64 != null) { Map screenshotData = new TreeMap<>(); + // This is to handle cases in which multiple screenshots have the same name + Map screenshotNameCount = new HashMap<>(); for (Base64File screenshot : screenshotsBase64) { try { applicationManager.validateBase64File(screenshot); @@ -116,13 +128,21 @@ public class ApplicationManagementUtil { throw new BadRequestException(msg, e); } FileDataHolder screenshotFile = base64FileToFileDataHolder(screenshot); - screenshotData.put(screenshotFile.getName(), screenshotFile.getFile()); + String screenshotName = screenshotFile.getName(); + screenshotNameCount.put(screenshotName, screenshotNameCount.getOrDefault(screenshotName, 0) + 1); + screenshotName = FileUtil.generateDuplicateFileName(screenshotName, screenshotNameCount.get(screenshotName)); + screenshotData.put(screenshotName, screenshotFile.getFile()); } applicationArtifact.setScreenshots(screenshotData); } return applicationArtifact; } + /** + * + * @param base64File Base64File that should be converted to FileDataHolder bean + * @return FileDataHolder bean which contains input stream and name of the file + */ public static FileDataHolder base64FileToFileDataHolder(Base64File base64File) { InputStream stream = FileUtil.base64ToInputStream(base64File.getBase64String()); return new FileDataHolder(base64File.getName(), stream); @@ -165,6 +185,27 @@ public class ApplicationManagementUtil { return getInstance(extension, LifecycleStateManager.class); } + /** + * This is useful to delete application artifacts if any error occurred while creating release/application + * after uploading the artifacts + * + * @param app ApplicationDTO of the application of which the artifacts should be deleted + * @throws ApplicationManagementException if error occurred while deleting artifacts + */ + public static void deleteArtifactIfExist(ApplicationDTO app) throws ApplicationManagementException { + ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance(); + if (!app.getApplicationReleaseDTOs().isEmpty()) { + applicationManager.deleteApplicationArtifacts(Collections.singletonList(app.getApplicationReleaseDTOs().get(0).getAppHashValue())); + } + } + + /** + * Check if application release available for a given application wrapper. This is useful since + * if a release is available for an application that needs to handled separately + * + * @param appWrapper Application wrapper bean of the application + * @return if release is available or not + */ public static boolean isReleaseAvailable(T appWrapper) { if (appWrapper instanceof ApplicationWrapper) { List entAppReleaseWrappers = ((ApplicationWrapper) appWrapper).getEntAppReleaseWrappers(); diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/DAOUtil.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/DAOUtil.java index dc276ddf25..4502c8987b 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/DAOUtil.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/DAOUtil.java @@ -194,7 +194,7 @@ public class DAOUtil { * To create application object from the result set retrieved from the Database. * * @param rs ResultSet - * @return List of Applications that is retrieved from the Database. + * @return List of Identity Servers that is retrieved from the Database. * @throws SQLException SQL Exception * @throws JSONException JSONException. */ @@ -215,7 +215,6 @@ public class DAOUtil { return identityServers; } - /** * To create application object from the result set retrieved from the Database. * diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/SPApplicationManagementUtil.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/SPApplicationManagementUtil.java deleted file mode 100644 index 53c11fd6f3..0000000000 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/SPApplicationManagementUtil.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2019, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. - * - * Entgra (pvt) Ltd. 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 io.entgra.application.mgt.core.util; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import java.util.ArrayList; -import java.util.List; - -/** - * This DAOUtil class is responsible for making sure single instance of each Extension Manager is used throughout for - * all the tasks. - */ -public class SPApplicationManagementUtil { - - private static Log log = LogFactory.getLog(SPApplicationManagementUtil.class); - - private static List getDefaultSPAppCategories() { - List categories = new ArrayList<>(); - categories.add(Constants.SP_APP_CATEGORY); - return categories; - } - - -} diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/test/java/io/entgra/application/mgt/core/management/ApplicationManagementTest.java b/components/application-mgt/io.entgra.application.mgt.core/src/test/java/io/entgra/application/mgt/core/management/ApplicationManagementTest.java index c781b674df..7cb9e9491e 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/test/java/io/entgra/application/mgt/core/management/ApplicationManagementTest.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/test/java/io/entgra/application/mgt/core/management/ApplicationManagementTest.java @@ -122,8 +122,7 @@ public class ApplicationManagementTest extends BaseTestCase { applicationArtifact.setScreenshots(screenshots); ApplicationManager manager = new ApplicationManagerImpl(); - ApplicationDTO applicationDTO = manager.uploadEntAppReleaseArtifacts(applicationWrapper, applicationArtifact); - manager.persistApplication(applicationDTO); + manager.createApplication(applicationWrapper); } @DataProvider(name = "applicationIdDataProvider") diff --git a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java index 905f140de7..23ca981b00 100644 --- a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java +++ b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java @@ -17,6 +17,7 @@ */ package io.entgra.application.mgt.publisher.api.services; +import io.entgra.application.mgt.common.Base64File; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; @@ -27,8 +28,6 @@ import io.swagger.annotations.ExtensionProperty; import io.swagger.annotations.Info; import io.swagger.annotations.SwaggerDefinition; import io.swagger.annotations.Tag; -import org.apache.cxf.jaxrs.ext.multipart.Attachment; -import org.apache.cxf.jaxrs.ext.multipart.Multipart; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import io.entgra.application.mgt.common.ApplicationList; @@ -284,7 +283,7 @@ public interface ApplicationManagementPublisherAPI { @POST @Path("/ent-app") @Produces(MediaType.APPLICATION_JSON) - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, @@ -318,42 +317,12 @@ public interface ApplicationManagementPublisherAPI { name = "application", value = "The application that need to be created.", required = true) - @Multipart("application") ApplicationWrapper application, - @ApiParam( - name = "binaryFile", - value = "Binary file of uploading application", - required = true) - @Multipart(value = "binaryFile") Attachment binaryFile, - @ApiParam( - name = "icon", - value = "Icon of the uploading application", - required = true) - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "Banner of the uploading application") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "Screen Shots of the uploading application", - required = true) - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot3") Attachment screenshot3 - ); + ApplicationWrapper application); @POST @Path("/web-app") @Produces(MediaType.APPLICATION_JSON) - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, @@ -387,37 +356,13 @@ public interface ApplicationManagementPublisherAPI { name = "webapp", value = "The web app that need to be created.", required = true) - @Multipart("webapp") WebAppWrapper webAppWrapper, - @ApiParam( - name = "icon", - value = "Icon of the uploading web app", - required = true) - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "Banner of the uploading web app") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "Screen Shots of the uploading web app", - required = true) - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Screen Shots of the uploading web app", - required = false) - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Screen Shots of the uploading web app", - required = false) - @Multipart(value = "screenshot3") Attachment screenshot3 + WebAppWrapper webAppWrapper ); @POST @Path("/public-app") @Produces(MediaType.APPLICATION_JSON) - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, @@ -451,35 +396,13 @@ public interface ApplicationManagementPublisherAPI { name = "public-app", value = "The public app that need to be created.", required = true) - @Multipart("public-app") PublicAppWrapper publicAppWrapper, - @ApiParam( - name = "icon", - value = "Icon of the uploading public app", - required = true) - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "Banner of the uploading public app") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "Screen Shots of the uploading public app", - required = true) - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Screen Shots of the uploading public app") - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Screen Shots of the uploading public app") - @Multipart(value = "screenshot3") Attachment screenshot3 + PublicAppWrapper publicAppWrapper ); @POST @Path("/custom-app") @Produces(MediaType.APPLICATION_JSON) - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, @@ -513,41 +436,12 @@ public interface ApplicationManagementPublisherAPI { name = "application", value = "The application that need to be created.", required = true) - @Multipart("application") CustomAppWrapper customAppWrapper, - @ApiParam( - name = "binaryFile", - value = "Binary file of uploading application", - required = true) - @Multipart(value = "binaryFile") Attachment binaryFile, - @ApiParam( - name = "icon", - value = "Icon of the uploading application", - required = true) - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "Banner of the uploading application") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "Screen Shots of the uploading application", - required = true) - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot3") Attachment screenshot3 + CustomAppWrapper customAppWrapper ); @POST @Produces(MediaType.APPLICATION_JSON) - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/{deviceType}/ent-app/{appId}") @ApiOperation( consumes = MediaType.APPLICATION_JSON, @@ -592,41 +486,12 @@ public interface ApplicationManagementPublisherAPI { name = "applicationRelease", value = "The application release that need to be created.", required = true) - @Multipart("applicationRelease") EntAppReleaseWrapper entAppReleaseWrapper, - @ApiParam( - name = "binaryFile", - value = "Binary file of uploading application", - required = true) - @Multipart(value = "binaryFile") Attachment binaryFile, - @ApiParam( - name = "icon", - value = "Icon of the uploading application", - required = true) - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "Banner of the uploading application") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "Screen Shots of the uploading application", - required = true) - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot3") Attachment screenshot3 + EntAppReleaseWrapper entAppReleaseWrapper ); @POST @Produces(MediaType.APPLICATION_JSON) - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/{deviceType}/public-app/{appId}") @ApiOperation( consumes = MediaType.APPLICATION_JSON, @@ -671,36 +536,12 @@ public interface ApplicationManagementPublisherAPI { name = "applicationRelease", value = "The application release that need to be created.", required = true) - @Multipart("applicationRelease") PublicAppReleaseWrapper publicAppReleaseWrapper, - @ApiParam( - name = "icon", - value = "Icon of the uploading application", - required = true) - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "Banner of the uploading application") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "Screen Shots of the uploading application", - required = true) - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot3") Attachment screenshot3 + PublicAppReleaseWrapper publicAppReleaseWrapper ); @POST @Produces(MediaType.APPLICATION_JSON) - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/web-app/{appId}") @ApiOperation( consumes = MediaType.APPLICATION_JSON, @@ -740,36 +581,12 @@ public interface ApplicationManagementPublisherAPI { name = "applicationRelease", value = "The application release that need to be created.", required = true) - @Multipart("applicationRelease") WebAppReleaseWrapper webAppReleaseWrapper, - @ApiParam( - name = "icon", - value = "Icon of the uploading application", - required = true) - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "Banner of the uploading application") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "Screen Shots of the uploading application", - required = true) - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot3") Attachment screenshot3 + WebAppReleaseWrapper webAppReleaseWrapper ); @POST @Produces(MediaType.APPLICATION_JSON) - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/{deviceType}/custom-app/{appId}") @ApiOperation( consumes = MediaType.APPLICATION_JSON, @@ -814,42 +631,13 @@ public interface ApplicationManagementPublisherAPI { name = "applicationRelease", value = "The application release that need to be created.", required = true) - @Multipart("applicationRelease") CustomAppReleaseWrapper customAppReleaseWrapper, - @ApiParam( - name = "binaryFile", - value = "Binary file of uploading application", - required = true) - @Multipart(value = "binaryFile") Attachment binaryFile, - @ApiParam( - name = "icon", - value = "Icon of the uploading application", - required = true) - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "Banner of the uploading application") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "Screen Shots of the uploading application", - required = true) - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Screen Shots of the uploading application", - required = false) - @Multipart(value = "screenshot3") Attachment screenshot3 + CustomAppReleaseWrapper customAppReleaseWrapper ); @PUT @Path("/image-artifacts/{uuid}") @Produces(MediaType.APPLICATION_JSON) - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.APPLICATION_JSON, @@ -885,23 +673,15 @@ public interface ApplicationManagementPublisherAPI { @ApiParam( name = "icon", value = "Icon of the uploading application") - @Multipart(value = "icon") Attachment iconFile, + Base64File iconFile, @ApiParam( name = "banner", value = "Banner of the uploading application") - @Multipart(value = "banner") Attachment bannerFile, + Base64File bannerFile, @ApiParam( name = "screenshot1", value = "Screen Shots of the uploading application") - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Screen Shots of the uploading application") - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Screen Shots of the uploading application") - @Multipart(value = "screenshot3") Attachment screenshot3 + List screenshots ); @GET @@ -951,7 +731,7 @@ public interface ApplicationManagementPublisherAPI { @PUT @Path("/ent-app-artifacts/{deviceType}/{appId}/{uuid}") @Produces(MediaType.APPLICATION_JSON) - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.APPLICATION_JSON, @@ -994,13 +774,13 @@ public interface ApplicationManagementPublisherAPI { value = "UUID of the application", required = true) @PathParam("uuid") String applicationUUID, - @Multipart("binaryFile") Attachment binaryFile + Base64File binaryFile ); @PUT @Path("/ent-app-release/{uuid}") @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.MULTIPART_FORM_DATA) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.APPLICATION_JSON, @@ -1039,39 +819,12 @@ public interface ApplicationManagementPublisherAPI { name = "entAppReleaseWrapper", value = "Application release wrapper which is going to update.", required = true) - @Multipart( - value = "entAppReleaseWrapper", - type = "application/json") EntAppReleaseWrapper entAppReleaseWrapper, - @ApiParam( - name = "binaryFile", - value = "Application installer file.", - required = true) - @Multipart(value = "binaryFile") Attachment binaryFile, - @ApiParam( - name = "icon", - value = "Icon file of the application release.") - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "banner file of the application release.") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "First screenshot of the uploading application") - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Second screenshot 2 of the uploading application") - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Third screenshot of the uploading application") - @Multipart(value = "screenshot3") Attachment screenshot3); + EntAppReleaseWrapper entAppReleaseWrapper); @PUT @Path("/public-app-release/{uuid}") @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.MULTIPART_FORM_DATA) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.APPLICATION_JSON, @@ -1110,34 +863,12 @@ public interface ApplicationManagementPublisherAPI { name = "pubAppReleaseWrapper", value = "Application release wrapper which is going to update.", required = true) - @Multipart( - value = "pubAppReleaseWrapper", - type = "application/json") PublicAppReleaseWrapper publicAppReleaseWrapper, - @ApiParam( - name = "icon", - value = "Icon file of the application release.") - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "banner file of the application release.") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "First screenshot of the uploading application") - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Second screenshot 2 of the uploading application") - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Third screenshot of the uploading application") - @Multipart(value = "screenshot3") Attachment screenshot3); + PublicAppReleaseWrapper publicAppReleaseWrapper); @PUT @Path("/web-app-release/{uuid}") @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.MULTIPART_FORM_DATA) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.APPLICATION_JSON, @@ -1176,34 +907,13 @@ public interface ApplicationManagementPublisherAPI { name = "pubAppReleaseWrapper", value = "Application release wrapper which is going to update.", required = true) - @Multipart( - value = "pubAppReleaseWrapper", - type = "application/json") WebAppReleaseWrapper webAppReleaseWrapper, - @ApiParam( - name = "icon", - value = "Icon file of the application release.") - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "banner file of the application release.") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "First screenshot of the uploading application") - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Second screenshot 2 of the uploading application") - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Third screenshot of the uploading application") - @Multipart(value = "screenshot3") Attachment screenshot3); + WebAppReleaseWrapper webAppReleaseWrapper + ); @PUT @Path("/custom-app-release/{uuid}") @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.MULTIPART_FORM_DATA) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.APPLICATION_JSON, @@ -1242,34 +952,7 @@ public interface ApplicationManagementPublisherAPI { name = "entAppReleaseWrapper", value = "Application release wrapper which is going to update.", required = true) - @Multipart( - value = "entAppReleaseWrapper", - type = "application/json") CustomAppReleaseWrapper customAppReleaseWrapper, - @ApiParam( - name = "binaryFile", - value = "Application installer file.", - required = true) - @Multipart(value = "binaryFile") Attachment binaryFile, - @ApiParam( - name = "icon", - value = "Icon file of the application release.") - @Multipart(value = "icon") Attachment iconFile, - @ApiParam( - name = "banner", - value = "banner file of the application release.") - @Multipart(value = "banner") Attachment bannerFile, - @ApiParam( - name = "screenshot1", - value = "First screenshot of the uploading application") - @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam( - name = "screenshot2", - value = "Second screenshot 2 of the uploading application") - @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam( - name = "screenshot3", - value = "Third screenshot of the uploading application") - @Multipart(value = "screenshot3") Attachment screenshot3); + CustomAppReleaseWrapper customAppReleaseWrapper); @GET @Path("/life-cycle/state-changes/{uuid}") @@ -1634,7 +1317,7 @@ public interface ApplicationManagementPublisherAPI { name = "appId", value = "application Id", required = true) - List tagNames + List tagNames ); @GET diff --git a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/SPApplicationService.java b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/SPApplicationService.java index c39c9b8ea7..002c54de46 100644 --- a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/SPApplicationService.java +++ b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/SPApplicationService.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2022, 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 @@ -38,20 +38,14 @@ 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.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriInfo; import java.util.List; -/** - * This is the application registration service that exposed for apimApplicationRegistration - */ - @SwaggerDefinition( info = @Info( version = "1.0.0", - title = "Service Provider Application Management Publisher Service", + title = "Service Provider Application Management Service", extensions = { @Extension(properties = { @ExtensionProperty(name = "name", value = "SPApplicationService"), @@ -67,28 +61,28 @@ import java.util.List; scopes = { @Scope( name = "view a service provider applications", - description = "Get application details", + description = "Get service provider application details", key = "perm:app:publisher:service-provider:view", roles = {"Internal/devicemgt-user"}, permissions = {"/app-mgt/publisher/service-provider/application/view"} ), @Scope( name = "Create a service provider application", - description = "Update an application", + description = "Create an application and attach (map) to service provider", key = "perm:app:publisher:service-provider:create", roles = {"Internal/devicemgt-user"}, permissions = {"/app-mgt/publisher/service-provider/application/create"} ), @Scope( name = "Attach a service provider application", - description = "Update an application", + description = "Map an application to service provider", key = "perm:app:publisher:service-provider:attach", roles = {"Internal/devicemgt-user"}, permissions = {"/app-mgt/publisher/service-provider/application/attach"} ), @Scope( name = "Detach a service provider application", - description = "Update an application", + description = "Remove an application from service provider", key = "perm:app:publisher:service-provider:detach", roles = {"Internal/devicemgt-user"}, permissions = {"/app-mgt/publisher/service-provider/application/detach"} @@ -102,18 +96,14 @@ public interface SPApplicationService { String SCOPE = "scope"; - /** - * This method is used to register an APIM application for tenant domain. - */ @GET @Produces(MediaType.APPLICATION_JSON) @Path("/identity-servers") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", - value = "get the application of requesting application id and state", - notes = "This will get the application identified by the application id and state, if exists", - tags = "ApplicationDTO Management", + value = "get all identity servers", + tags = "Identity Server Management", extensions = { @Extension(properties = { @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:view") @@ -128,9 +118,8 @@ public interface SPApplicationService { @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", - value = "get the application of requesting application id and state", - notes = "This will get the application identified by the application id and state, if exists", - tags = "ApplicationDTO Management", + value = "get identity server by id", + tags = "Identity Server Management", extensions = { @Extension(properties = { @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:view") @@ -148,9 +137,9 @@ public interface SPApplicationService { @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", - value = "get the application of requesting application id and state", - notes = "This will get the application identified by the application id and state, if exists", - tags = "ApplicationDTO Management", + value = "get service providers by identity server id", + notes = "This will get service providers with the existing applications", + tags = "Service Provider Management", extensions = { @Extension(properties = { @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:view") @@ -160,15 +149,15 @@ public interface SPApplicationService { Response getServiceProviders(@QueryParam("limit") Integer limit, @QueryParam("offset") Integer offset, @PathParam("identity-server-id") int identityServerId); - @Path("/{identity-server-id}/{service-provider-id}/attach") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/applications") @POST @Produces(MediaType.APPLICATION_JSON) @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "POST", - value = "get the application of requesting application id and state", - notes = "This will get the application identified by the application id and state, if exists", - tags = "ApplicationDTO Management", + value = "map application to service provider", + notes = "This will map a given list application ids with the given service provider id", + tags = "Service Provider Management", extensions = { @Extension(properties = { @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:attach") @@ -181,15 +170,15 @@ public interface SPApplicationService { /** * This method is used to register an APIM application for tenant domain. */ - @Path("/{identity-server-id}/{service-provider-id}/detach") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/delete/applications") @POST @Produces(MediaType.APPLICATION_JSON) @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "POST", - value = "get the application of requesting application id and state", - notes = "This will get the application identified by the application id and state, if exists", - tags = "ApplicationDTO Management", + value = "Remove mapping with service provider of the given application ids", + notes = "This will remove applications from service provider", + tags = "Service Provider Management", extensions = { @Extension(properties = { @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:detach") @@ -202,15 +191,15 @@ public interface SPApplicationService { /** * This method is used to register an APIM application for tenant domain. */ - @Path("/{identity-server-id}/{service-provider-id}/create/ent-app") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/create/ent-app") @POST @Produces(MediaType.APPLICATION_JSON) @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "POST", - value = "get the application of requesting application id and state", - notes = "This will get the application identified by the application id and state, if exists", - tags = "ApplicationDTO Management", + value = "Create enterprise application and attach to service provider", + notes = "This will get create an enterprise application and map with service provider", + tags = "Service Provider Management", extensions = { @Extension(properties = { @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:create") @@ -220,19 +209,18 @@ public interface SPApplicationService { Response createEntApp(@PathParam("identity-server-id") int identityServerId, @PathParam("service-provider-id") String serviceProviderId, ApplicationWrapper app); - /** * This method is used to register an APIM application for tenant domain. */ - @Path("/{identity-server-id}/{service-provider-id}/create/public-app") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/create/public-app") @POST @Produces(MediaType.APPLICATION_JSON) @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "POST", - value = "get the application of requesting application id and state", - notes = "This will get the application identified by the application id and state, if exists", - tags = "ApplicationDTO Management", + value = "Create public application and attach to service provider", + notes = "This will get create a public application and map with service provider", + tags = "Service Provider Management", extensions = { @Extension(properties = { @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:create") @@ -242,15 +230,15 @@ public interface SPApplicationService { Response createPubApp(@PathParam("identity-server-id") int identityServerId, @PathParam("service-provider-id") String serviceProviderId, PublicAppWrapper app); - @Path("/{identity-server-id}/{service-provider-id}/create/web-app") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/create/web-app") @POST @Produces(MediaType.APPLICATION_JSON) @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "POST", - value = "get the application of requesting application id and state", - notes = "This will get the application identified by the application id and state, if exists", - tags = "ApplicationDTO Management", + value = "Create web application and attach to service provider", + notes = "This will get create a web application and map with service provider", + tags = "Service Provider Management", extensions = { @Extension(properties = { @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:create") @@ -260,15 +248,15 @@ public interface SPApplicationService { Response createWebApp(@PathParam("identity-server-id") int identityServerId, @PathParam("service-provider-id") String serviceProviderId, WebAppWrapper app); - @Path("/{identity-server-id}/{service-provider-id}/create/custom-app") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/create/custom-app") @POST @Produces(MediaType.APPLICATION_JSON) @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "POST", - value = "get the application of requesting application id and state", - notes = "This will get the application identified by the application id and state, if exists", - tags = "ApplicationDTO Management", + value = "Create custom application and attach to service provider", + notes = "This will get create an custom application and map with service provider", + tags = "Service Provider Management", extensions = { @Extension(properties = { @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:create") diff --git a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java index 9aaeeb1d5a..561a65b767 100644 --- a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java @@ -16,14 +16,15 @@ */ package io.entgra.application.mgt.publisher.api.services.impl; -import io.entgra.application.mgt.common.dto.ApplicationDTO; -import io.entgra.application.mgt.common.dto.ApplicationReleaseDTO; +import io.entgra.application.mgt.common.ApplicationArtifact; +import io.entgra.application.mgt.common.ApplicationList; +import io.entgra.application.mgt.common.Base64File; +import io.entgra.application.mgt.common.Filter; +import io.entgra.application.mgt.common.LifecycleChanger; import io.entgra.application.mgt.common.exception.ResourceManagementException; +import io.entgra.application.mgt.core.util.ApplicationManagementUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.cxf.jaxrs.ext.multipart.Attachment; -import org.apache.cxf.jaxrs.ext.multipart.Multipart; -import io.entgra.application.mgt.common.*; import io.entgra.application.mgt.common.LifecycleState; import io.entgra.application.mgt.common.exception.LifecycleManagementException; import io.entgra.application.mgt.common.exception.RequestValidatingException; @@ -45,20 +46,12 @@ import io.entgra.application.mgt.core.exception.BadRequestException; import io.entgra.application.mgt.core.exception.ForbiddenException; import io.entgra.application.mgt.core.exception.UnexpectedServerErrorException; import io.entgra.application.mgt.core.util.APIUtil; -import io.entgra.application.mgt.core.util.Constants; import io.entgra.application.mgt.publisher.api.services.ApplicationManagementPublisherAPI; import io.entgra.application.mgt.common.exception.ApplicationManagementException; import io.entgra.application.mgt.common.services.ApplicationManager; import io.entgra.application.mgt.core.exception.NotFoundException; -import org.wso2.carbon.device.mgt.core.dto.DeviceType; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.TreeMap; -import javax.activation.DataHandler; import javax.validation.Valid; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -173,36 +166,12 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } @POST - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/ent-app") public Response createEntApp( - @Multipart("application") ApplicationWrapper applicationWrapper, - @Multipart("binaryFile") Attachment binaryFile, - @Multipart("icon") Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart("screenshot1") Attachment screenshot1, - @Multipart("screenshot2") Attachment screenshot2, - @Multipart("screenshot3") Attachment screenshot3) { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - List attachmentList = constructAttachmentList(screenshot1, screenshot2, screenshot3); + ApplicationWrapper applicationWrapper) { try { - applicationManager.validateAppCreatingRequest(applicationWrapper, true); - applicationManager.validateReleaseCreatingRequest(applicationWrapper.getEntAppReleaseWrappers().get(0), - applicationWrapper.getDeviceType()); - applicationManager.validateBinaryArtifact(binaryFile); - applicationManager.validateImageArtifacts(iconFile, bannerFile, attachmentList); - - // Created new Ent App - ApplicationDTO applicationDTO = applicationManager.uploadEntAppReleaseArtifacts(applicationWrapper, - constructApplicationArtifact(binaryFile, iconFile, bannerFile, attachmentList)); - Application application = applicationManager.persistApplication(applicationDTO); - if (application != null) { - return Response.status(Response.Status.CREATED).entity(application).build(); - } else { - String msg = "Application creation is failed"; - log.error(msg); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } + return createApplication(applicationWrapper); } catch (BadRequestException e) { String msg = "Found incompatible payload with ent. app creating request."; log.error(msg, e); @@ -219,34 +188,12 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } @POST - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/web-app") public Response createWebApp( - @Multipart("webapp") WebAppWrapper webAppWrapper, - @Multipart("icon") Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart("screenshot1") Attachment screenshot1, - @Multipart("screenshot2") Attachment screenshot2, - @Multipart("screenshot3") Attachment screenshot3) { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - List attachmentList = constructAttachmentList(screenshot1, screenshot2, screenshot3); + WebAppWrapper webAppWrapper) { try { - applicationManager.validateAppCreatingRequest(webAppWrapper, true); - applicationManager - .validateReleaseCreatingRequest(webAppWrapper.getWebAppReleaseWrappers().get(0), Constants.ANY); - applicationManager.validateImageArtifacts(iconFile, bannerFile, attachmentList); - - // Created new Web App - ApplicationDTO applicationDTO = applicationManager.uploadWebAppReleaseArtifacts(webAppWrapper, - constructApplicationArtifact(null, iconFile, bannerFile, attachmentList)); - Application application = applicationManager.persistApplication(applicationDTO); - if (application != null) { - return Response.status(Response.Status.CREATED).entity(application).build(); - } else { - String msg = "Web app creation is failed"; - log.error(msg); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } + return createApplication(webAppWrapper); } catch (BadRequestException e) { String msg = "Found incompatible payload with web app creating request."; log.error(msg, e); @@ -263,34 +210,12 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } @POST - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/public-app") public Response createPubApp( - @Multipart("public-app") PublicAppWrapper publicAppWrapper, - @Multipart("icon") Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart("screenshot1") Attachment screenshot1, - @Multipart("screenshot2") Attachment screenshot2, - @Multipart("screenshot3") Attachment screenshot3) { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - List attachmentList = constructAttachmentList(screenshot1, screenshot2, screenshot3); + PublicAppWrapper publicAppWrapper) { try { - applicationManager.validateAppCreatingRequest(publicAppWrapper, true); - applicationManager.validateReleaseCreatingRequest(publicAppWrapper.getPublicAppReleaseWrappers().get(0), - publicAppWrapper.getDeviceType()); - applicationManager.validateImageArtifacts(iconFile, bannerFile, attachmentList); - - // Created new Public App - ApplicationDTO applicationDTO = applicationManager.uploadPublicAppReleaseArtifacts(publicAppWrapper, - constructApplicationArtifact(null, iconFile, bannerFile, attachmentList)); - Application application = applicationManager.persistApplication(applicationDTO); - if (application != null) { - return Response.status(Response.Status.CREATED).entity(application).build(); - } else { - String msg = "Web app creation is failed"; - log.error(msg); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } + return createApplication(publicAppWrapper); } catch (BadRequestException e) { String msg = "Found incompatible payload with pub app creating request."; log.error(msg, e); @@ -307,36 +232,12 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } @POST - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/custom-app") public Response createCustomApp( - @Multipart("application") CustomAppWrapper customAppWrapper, - @Multipart("binaryFile") Attachment binaryFile, - @Multipart("icon") Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart("screenshot1") Attachment screenshot1, - @Multipart("screenshot2") Attachment screenshot2, - @Multipart("screenshot3") Attachment screenshot3) { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - List attachmentList = constructAttachmentList(screenshot1, screenshot2, screenshot3); + CustomAppWrapper customAppWrapper) { try { - applicationManager.validateAppCreatingRequest(customAppWrapper, true); - applicationManager.validateReleaseCreatingRequest(customAppWrapper.getCustomAppReleaseWrappers().get(0), - customAppWrapper.getDeviceType()); - applicationManager.validateBinaryArtifact(binaryFile); - applicationManager.validateImageArtifacts(iconFile, bannerFile, attachmentList); - - // Created new Custom App - ApplicationDTO applicationDTO = applicationManager.uploadCustomAppReleaseArtifactsAndConstructAppDTO(customAppWrapper, - constructApplicationArtifact(binaryFile, iconFile, bannerFile, attachmentList)); - Application application = applicationManager.persistApplication(applicationDTO); - if (application != null) { - return Response.status(Response.Status.CREATED).entity(application).build(); - } else { - String msg = "Custom app creation is failed"; - log.error(msg); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } + return createApplication(customAppWrapper); } catch (BadRequestException e) { String msg = "Found incompatible payload with custom app creating request."; log.error(msg, e); @@ -353,28 +254,16 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } @POST - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/{deviceType}/ent-app/{appId}") public Response createEntAppRelease( @PathParam("deviceType") String deviceTypeName, @PathParam("appId") int appId, - @Multipart("applicationRelease") EntAppReleaseWrapper entAppReleaseWrapper, - @Multipart("binaryFile") Attachment binaryFile, - @Multipart("icon") Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart("screenshot1") Attachment screenshot1, - @Multipart("screenshot2") Attachment screenshot2, - @Multipart("screenshot3") Attachment screenshot3) { + EntAppReleaseWrapper entAppReleaseWrapper) { try { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - applicationManager.validateBinaryArtifact(binaryFile); - ApplicationDTO applicationDTO = applicationManager.getApplication(appId); - DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); - List attachmentList = constructAttachmentList(screenshot1, screenshot2, screenshot3); - ApplicationArtifact artifact = constructApplicationArtifact(binaryFile, iconFile, bannerFile, attachmentList); - ApplicationReleaseDTO applicationReleaseDTO = applicationManager.uploadEntAppReleaseArtifacts( - entAppReleaseWrapper, artifact, deviceType.getName()); - ApplicationRelease release = applicationManager.createRelease(applicationDTO, applicationReleaseDTO, ApplicationType.ENTERPRISE); + applicationManager.validateEntAppReleaseCreatingRequest(entAppReleaseWrapper, deviceTypeName); + ApplicationRelease release = applicationManager.createEntAppRelease(appId, entAppReleaseWrapper); return Response.status(Response.Status.CREATED).entity(release).build(); } catch (RequestValidatingException e) { String msg = "Error occurred while validating binaryArtifact"; @@ -388,29 +277,19 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } @POST - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/{deviceType}/public-app/{appId}") @Override public Response createPubAppRelease( @PathParam("deviceType") String deviceTypeName, @PathParam("appId") int appId, - @Multipart("applicationRelease") PublicAppReleaseWrapper publicAppReleaseWrapper, - @Multipart("icon") Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart("screenshot1") Attachment screenshot1, - @Multipart("screenshot2") Attachment screenshot2, - @Multipart("screenshot3") Attachment screenshot3) { + PublicAppReleaseWrapper publicAppReleaseWrapper) { try { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationDTO applicationDTO = applicationManager.getApplication(appId); - DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); - List attachmentList = constructAttachmentList(screenshot1, screenshot2, screenshot3); - ApplicationArtifact artifact = constructApplicationArtifact(null, iconFile, bannerFile, attachmentList); - ApplicationReleaseDTO applicationReleaseDTO = applicationManager.uploadPubAppReleaseArtifacts( - publicAppReleaseWrapper, artifact, deviceType.getName()); - ApplicationRelease release = applicationManager.createRelease(applicationDTO, applicationReleaseDTO, ApplicationType.PUBLIC); - return Response.status(Response.Status.CREATED).entity(release).build(); + applicationManager.validatePublicAppReleaseCreatingRequest(publicAppReleaseWrapper, deviceTypeName); + ApplicationRelease applicationRelease = applicationManager.createPubAppRelease(appId, publicAppReleaseWrapper); + return Response.status(Response.Status.CREATED).entity(applicationRelease).build(); } catch (ApplicationManagementException e) { String msg = "Error occurred while creating application release for the application with the id " + appId; log.error(msg, e); @@ -419,29 +298,25 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem String msg = "Error occurred while uploading application release artifacts"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (RequestValidatingException e) { + String msg = "Invalid payload found in public app release create request"; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } } @POST - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/web-app/{appId}") @Override public Response createWebAppRelease( @PathParam("appId") int appId, - @Multipart("applicationRelease") WebAppReleaseWrapper webAppReleaseWrapper, - @Multipart("icon") Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart("screenshot1") Attachment screenshot1, - @Multipart("screenshot2") Attachment screenshot2, - @Multipart("screenshot3") Attachment screenshot3) { + WebAppReleaseWrapper webAppReleaseWrapper) { try { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationDTO applicationDTO = applicationManager.getApplication(appId); - List attachmentList = constructAttachmentList(screenshot1, screenshot2, screenshot3); - ApplicationArtifact artifact = constructApplicationArtifact(null, iconFile, bannerFile, attachmentList); - ApplicationReleaseDTO applicationReleaseDTO = applicationManager.uploadWebAppReleaseArtifacts(webAppReleaseWrapper, artifact); - ApplicationRelease release = applicationManager.createRelease(applicationDTO, applicationReleaseDTO, ApplicationType.WEB_CLIP); - return Response.status(Response.Status.CREATED).entity(release).build(); + applicationManager.validateWebAppReleaseCreatingRequest(webAppReleaseWrapper); + ApplicationRelease applicationRelease= applicationManager.createWebAppRelease(appId, webAppReleaseWrapper); + return Response.status(Response.Status.CREATED).entity(applicationRelease).build(); } catch (ResourceManagementException e) { String msg = "Error occurred while uploading application release artifacts"; log.error(msg, e); @@ -450,33 +325,25 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem String msg = "Error occurred while creating application release for the application with the id " + appId; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (RequestValidatingException e) { + String msg = "Invalid payload found in web app release create request"; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } } @POST - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/{deviceType}/custom-app/{appId}") @Override public Response createCustomAppRelease( @PathParam("deviceType") String deviceTypeName, @PathParam("appId") int appId, - @Multipart("applicationRelease") CustomAppReleaseWrapper customAppReleaseWrapper, - @Multipart("binaryFile") Attachment binaryFile, - @Multipart("icon") Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart("screenshot1") Attachment screenshot1, - @Multipart("screenshot2") Attachment screenshot2, - @Multipart("screenshot3") Attachment screenshot3) { + CustomAppReleaseWrapper customAppReleaseWrapper) { try { - APIUtil.getApplicationManager().validateBinaryArtifact(binaryFile); ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationDTO applicationDTO = applicationManager.getApplication(appId); - DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); - List attachmentList = constructAttachmentList(screenshot1, screenshot2, screenshot3); - ApplicationArtifact artifact = constructApplicationArtifact(binaryFile, iconFile, bannerFile, attachmentList); - ApplicationReleaseDTO applicationReleaseDTO = applicationManager.uploadCustomAppReleaseArtifacts( - customAppReleaseWrapper, artifact, deviceType.getName()); - ApplicationRelease release = applicationManager.createRelease(applicationDTO, applicationReleaseDTO, ApplicationType.CUSTOM); + applicationManager.validateCustomAppReleaseCreatingRequest(customAppReleaseWrapper, deviceTypeName); + ApplicationRelease release = applicationManager.createCustomAppRelease(appId, customAppReleaseWrapper); return Response.status(Response.Status.CREATED).entity(release).build(); } catch (RequestValidatingException e) { String msg = "Error occurred while validating binaryArtifact"; @@ -521,21 +388,20 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Override @PUT - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("/image-artifacts/{uuid}") public Response updateApplicationImageArtifacts( @PathParam("uuid") String applicationReleaseUuid, - @Multipart(value = "icon", required = false) Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart(value = "screenshot1", required = false) Attachment screenshot1, - @Multipart(value = "screenshot2", required = false) Attachment screenshot2, - @Multipart(value = "screenshot3", required = false) Attachment screenshot3) { + Base64File iconFile, + Base64File bannerFile, + List screenshots) { try { - List attachments = constructAttachmentList(screenshot1, screenshot2, screenshot3); + ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(iconFile, screenshots, + null, bannerFile); ApplicationManager applicationManager = APIUtil.getApplicationManager(); applicationManager.updateApplicationImageArtifact(applicationReleaseUuid, - constructApplicationArtifact(null, iconFile, bannerFile, attachments)); + artifact); return Response.status(Response.Status.OK) .entity("Successfully uploaded artifacts for the application " + applicationReleaseUuid).build(); } catch (NotFoundException e) { @@ -554,18 +420,19 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Override @PUT - @Consumes({"multipart/mixed", MediaType.MULTIPART_FORM_DATA}) + @Consumes(MediaType.APPLICATION_JSON) @Path("/ent-app-artifact/{deviceType}/{uuid}") public Response updateApplicationArtifact( @PathParam("deviceType") String deviceType, @PathParam("uuid") String applicationReleaseUuid, - @Multipart("binaryFile") Attachment binaryFile) { + Base64File binaryFile) { try { ApplicationManager applicationManager = APIUtil.getApplicationManager(); applicationManager.validateBinaryArtifact(binaryFile); applicationManager.updateApplicationArtifact(deviceType, applicationReleaseUuid, - constructApplicationArtifact(binaryFile, null, null, null)); + ApplicationManagementUtil.constructApplicationArtifact(null, null, + binaryFile, null)); return Response.status(Response.Status.OK) .entity("Successfully uploaded artifacts for the application release. UUID is " + applicationReleaseUuid).build(); @@ -622,19 +489,13 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Path("/ent-app-release/{uuid}") public Response updateEntAppRelease( @PathParam("uuid") String applicationUUID, - @Multipart("applicationRelease") EntAppReleaseWrapper entAppReleaseWrapper, - @Multipart(value = "binaryFile", required = false) Attachment binaryFile, - @Multipart(value = "icon", required = false) Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart(value = "screenshot1", required = false) Attachment screenshot1, - @Multipart(value = "screenshot2", required = false) Attachment screenshot2, - @Multipart(value = "screenshot3", required = false) Attachment screenshot3) { + EntAppReleaseWrapper entAppReleaseWrapper) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - List screenshots = constructAttachmentList(screenshot1, screenshot2, screenshot3); try { + ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(entAppReleaseWrapper.getIcon(), + entAppReleaseWrapper.getScreenshots(), entAppReleaseWrapper.getBinaryFile(), entAppReleaseWrapper.getBanner()); ApplicationRelease applicationRelease = applicationManager - .updateEntAppRelease(applicationUUID, entAppReleaseWrapper, - constructApplicationArtifact(binaryFile, iconFile, bannerFile, screenshots)); + .updateEntAppRelease(applicationUUID, entAppReleaseWrapper, artifact); if (applicationRelease == null) { String msg ="Ent app release updating is failed. Please contact the administrator. Application release " + "UUID: " + applicationUUID; @@ -669,18 +530,13 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Path("/public-app-release/{uuid}") public Response updatePubAppRelease( @PathParam("uuid") String applicationUUID, - @Multipart("applicationRelease") PublicAppReleaseWrapper publicAppReleaseWrapper, - @Multipart(value = "icon", required = false) Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart(value = "screenshot1", required = false) Attachment screenshot1, - @Multipart(value = "screenshot2", required = false) Attachment screenshot2, - @Multipart(value = "screenshot3", required = false) Attachment screenshot3) { + PublicAppReleaseWrapper publicAppReleaseWrapper) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - List screenshots = constructAttachmentList(screenshot1, screenshot2, screenshot3); try { + ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(publicAppReleaseWrapper.getIcon(), + publicAppReleaseWrapper.getScreenshots(), null, publicAppReleaseWrapper.getBanner()); ApplicationRelease applicationRelease = applicationManager - .updatePubAppRelease(applicationUUID, publicAppReleaseWrapper, - constructApplicationArtifact(null, iconFile, bannerFile, screenshots)); + .updatePubAppRelease(applicationUUID, publicAppReleaseWrapper, artifact); if (applicationRelease == null) { String msg ="Public app release updating is failed. Please contact the administrator. " + "Application release UUID: " + applicationUUID + ", Supported device type:"; @@ -714,18 +570,13 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Path("/web-app-release/{uuid}") public Response updateWebAppRelease( @PathParam("uuid") String applicationUUID, - @Multipart("applicationRelease") WebAppReleaseWrapper webAppReleaseWrapper, - @Multipart(value = "icon", required = false) Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart(value = "screenshot1", required = false) Attachment screenshot1, - @Multipart(value = "screenshot2", required = false) Attachment screenshot2, - @Multipart(value = "screenshot3", required = false) Attachment screenshot3) { + WebAppReleaseWrapper webAppReleaseWrapper) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - List screenshots = constructAttachmentList(screenshot1, screenshot2, screenshot3); try { + ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(webAppReleaseWrapper.getIcon(), + webAppReleaseWrapper.getScreenshots(), null, webAppReleaseWrapper.getBanner()); ApplicationRelease applicationRelease = applicationManager - .updateWebAppRelease(applicationUUID, webAppReleaseWrapper, - constructApplicationArtifact(null, iconFile, bannerFile, screenshots)); + .updateWebAppRelease(applicationUUID, webAppReleaseWrapper, artifact); if (applicationRelease == null) { String msg ="web app release updating is failed. Please contact the administrator. Application " + "release UUID: " + applicationUUID; @@ -758,19 +609,13 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Path("/custom-app-release/{uuid}") public Response updateCustomAppRelease( @PathParam("uuid") String applicationUUID, - @Multipart("applicationRelease") CustomAppReleaseWrapper customAppReleaseWrapper, - @Multipart(value = "binaryFile", required = false) Attachment binaryFile, - @Multipart(value = "icon", required = false) Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @Multipart(value = "screenshot1", required = false) Attachment screenshot1, - @Multipart(value = "screenshot2", required = false) Attachment screenshot2, - @Multipart(value = "screenshot3", required = false) Attachment screenshot3) { + CustomAppReleaseWrapper customAppReleaseWrapper) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - List screenshots = constructAttachmentList(screenshot1, screenshot2, screenshot3); try { + ApplicationArtifact artifact = ApplicationManagementUtil.constructApplicationArtifact(customAppReleaseWrapper.getIcon(), + customAppReleaseWrapper.getScreenshots(), customAppReleaseWrapper.getBinaryFile(), customAppReleaseWrapper.getBanner()); ApplicationRelease applicationRelease = applicationManager - .updateCustomAppRelease(applicationUUID, customAppReleaseWrapper, - constructApplicationArtifact(binaryFile, iconFile, bannerFile, screenshots)); + .updateCustomAppRelease(applicationUUID, customAppReleaseWrapper, artifact); if (applicationRelease == null) { String msg ="Custom app release updating is failed. Please contact the administrator. Application " + "release UUID: " + applicationUUID; @@ -1019,134 +864,16 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } - /*** - * Construct the screenshot list by evaluating the availability of each screenshot. - * - * @param screenshot1 First Screenshot - * @param screenshot2 Second Screenshot - * @param screenshot3 Third Screenshot - * @return List of {@link Attachment} - */ - private List constructAttachmentList(Attachment screenshot1, Attachment screenshot2, - Attachment screenshot3) { - List attachments = new ArrayList<>(); - if (screenshot1 != null) { - attachments.add(screenshot1); - } - if (screenshot2 != null) { - attachments.add(screenshot2); - } - if (screenshot3 != null) { - attachments.add(screenshot3); - } - return attachments; - } - - /*** - * This method can be used to construct {@link ApplicationArtifact} - * - * @param binaryFile binary file of the application release - * @param iconFile icon file of the application release - * @param bannerFile banner file of the application release - * @param attachmentList list of screenshot of the application release - * @return {@link ApplicationArtifact} - * @throws ApplicationManagementException if an error occurs when reading the attached data. - */ - private ApplicationArtifact constructApplicationArtifact(Attachment binaryFile, Attachment iconFile, - Attachment bannerFile, List attachmentList) throws ApplicationManagementException { - try { - ApplicationArtifact applicationArtifact = new ApplicationArtifact(); - DataHandler dataHandler; - if (binaryFile != null) { - dataHandler = binaryFile.getDataHandler(); - InputStream installerStream = dataHandler.getInputStream(); - String installerFileName = dataHandler.getName(); - if (installerStream == null) { - String msg = "Stream of the application release installer is null. Hence can't proceed. Please " - + "verify the installer file."; - log.error(msg); - throw new BadRequestException(msg); - } - if (installerFileName == null) { - String msg = "Installer file name retrieving is failed.. Hence can't proceed. Please verify the " - + "installer file."; - log.error(msg); - throw new BadRequestException(msg); - } - applicationArtifact.setInstallerName(installerFileName.replaceAll("\\s", "")); - applicationArtifact.setInstallerStream(installerStream); - } - - if (iconFile != null) { - dataHandler = iconFile.getDataHandler(); - String iconFileName = dataHandler.getName(); - InputStream iconStream = dataHandler.getInputStream(); - - if (iconStream == null) { - String msg = "Stream of the application release icon is null. Hence can't proceed. Please " - + "verify the uploaded icon file."; - log.error(msg); - throw new BadRequestException(msg); - } - if (iconFileName == null) { - String msg = "Icon file name retrieving is failed.. Hence can't proceed. Please verify the " - + "icon file."; - log.error(msg); - throw new BadRequestException(msg); - } - applicationArtifact.setIconName(iconFileName); - applicationArtifact.setIconStream(iconStream); - } - - if (bannerFile != null) { - dataHandler = bannerFile.getDataHandler(); - String bannerFileName = dataHandler.getName(); - InputStream bannerStream = dataHandler.getInputStream(); - if (bannerStream == null) { - String msg = "Stream of the application release banner is null. Hence can't proceed. Please " - + "verify the uploaded banner file."; - log.error(msg); - throw new BadRequestException(msg); - } - if (bannerFileName == null) { - String msg = "Banner file name retrieving is failed.. Hence can't proceed. Please verify the " - + "banner file."; - log.error(msg); - throw new BadRequestException(msg); - } - applicationArtifact.setBannerName(bannerFileName); - applicationArtifact.setBannerStream(bannerStream); - } - - if (attachmentList != null && !attachmentList.isEmpty()) { - Map screenshotData = new TreeMap<>(); - for (Attachment sc : attachmentList) { - dataHandler = sc.getDataHandler(); - String screenshotFileName = dataHandler.getName(); - InputStream screenshotStream = dataHandler.getInputStream(); - if (screenshotStream == null) { - String msg = - "Stream of one of the application release screenshot is null. Hence can't proceed. Please " - + "verify the uploaded screenshots."; - log.error(msg); - throw new BadRequestException(msg); - } - if (screenshotFileName == null) { - String msg = - "Screenshot file name retrieving is failed for one screenshot. Hence can't proceed. " - + "Please verify the screenshots."; - log.error(msg); - throw new BadRequestException(msg); - } - screenshotData.put(screenshotFileName, screenshotStream); - } - applicationArtifact.setScreenshots(screenshotData); - } - return applicationArtifact; - } catch (IOException e) { - String msg = "Error occurred when reading attachment data."; - log.error(msg, e); - throw new ApplicationManagementException(msg); + public Response createApplication(T appWrapper) throws ApplicationManagementException, RequestValidatingException { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + applicationManager.validateAppCreatingRequest(appWrapper); + Application application = applicationManager.createApplication(appWrapper); + if (application != null) { + return Response.status(Response.Status.CREATED).entity(application).build(); + } else { + String msg = "Application creation is failed"; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } } diff --git a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/SPApplicationServiceImpl.java b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/SPApplicationServiceImpl.java index 79a8665521..52696b0981 100644 --- a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/SPApplicationServiceImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/SPApplicationServiceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2022, 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 @@ -35,7 +35,6 @@ import io.entgra.application.mgt.publisher.api.services.SPApplicationService; import io.entgra.application.mgt.publisher.api.services.util.SPAppRequestHandlerUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; @@ -46,7 +45,6 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.List; - @Produces(MediaType.APPLICATION_JSON) @Path("/identity-server-applications") public class SPApplicationServiceImpl implements SPApplicationService { @@ -90,7 +88,7 @@ public class SPApplicationServiceImpl implements SPApplicationService { try { SPApplicationManager spAppManager = APIUtil.getSPApplicationManager(); SPApplicationListResponse applications = SPAppRequestHandlerUtil. - getServiceProvidersFromIdentityServer(identityServerId, limit, offset); + retrieveSPApplications(identityServerId, limit, offset); spAppManager.addExistingApps(identityServerId, applications.getApplications()); return Response.status(Response.Status.OK).entity(applications).build(); } catch (ApplicationManagementException e) { @@ -100,7 +98,7 @@ public class SPApplicationServiceImpl implements SPApplicationService { } } - @Path("/{identity-server-id}/{service-provider-id}/attach") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/applications") @POST @Override public Response attachApps(@PathParam("identity-server-id") int identityServerId, @@ -118,7 +116,7 @@ public class SPApplicationServiceImpl implements SPApplicationService { return Response.status(Response.Status.OK).build(); } - @Path("/{identity-server-id}/{service-provider-id}/detach") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/delete/applications") @POST @Override public Response detachApps(@PathParam("identity-server-id") int identityServerId, @@ -136,7 +134,7 @@ public class SPApplicationServiceImpl implements SPApplicationService { return Response.status(Response.Status.OK).build(); } - @Path("/{identity-server-id}/{service-provider-id}/create/ent-app") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/create/ent-app") @POST @Override public Response createEntApp(@PathParam("identity-server-id") int identityServerId, @@ -144,7 +142,7 @@ public class SPApplicationServiceImpl implements SPApplicationService { return createSPApplication(identityServerId, serviceProviderId, app); } - @Path("/{identity-server-id}/{service-provider-id}/create/public-app") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/create/public-app") @POST @Override public Response createPubApp(@PathParam("identity-server-id") int identityServerId, @@ -152,7 +150,7 @@ public class SPApplicationServiceImpl implements SPApplicationService { return createSPApplication(identityServerId, serviceProviderId, app); } - @Path("/{identity-server-id}/{service-provider-id}/create/web-app") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/create/web-app") @POST @Override public Response createWebApp(@PathParam("identity-server-id") int identityServerId, @@ -160,7 +158,7 @@ public class SPApplicationServiceImpl implements SPApplicationService { return createSPApplication(identityServerId, serviceProviderId, app); } - @Path("/{identity-server-id}/{service-provider-id}/create/custom-app") + @Path("/{identity-server-id}/service-provider/{service-provider-id}/create/custom-app") @POST @Override public Response createCustomApp(@PathParam("identity-server-id") int identityServerId, @@ -168,11 +166,20 @@ public class SPApplicationServiceImpl implements SPApplicationService { return createSPApplication(identityServerId, serviceProviderId, app); } - private Response createSPApplication(int identityServerId, String serviceProviderId, T appWrapper) { + /** + * Validates and creates service provider application + * + * @param identityServerId id of the identity server + * @param spUID uid of the service provider + * @param appWrapper application wrapper + * @param application wrapper class + * @return Response + */ + private Response createSPApplication(int identityServerId, String spUID, T appWrapper) { try { - validateServiceProviderUID(identityServerId, serviceProviderId); + validateServiceProviderUID(identityServerId, spUID); SPApplicationManager spApplicationManager = APIUtil.getSPApplicationManager(); - Application createdApp = spApplicationManager.createSPApplication(appWrapper, identityServerId, serviceProviderId); + Application createdApp = spApplicationManager.createSPApplication(appWrapper, identityServerId, spUID); return Response.status(Response.Status.CREATED).entity(createdApp).build(); } catch (BadRequestException e) { String msg = "Found incompatible payload with create service provider app request."; @@ -190,6 +197,13 @@ public class SPApplicationServiceImpl implements SPApplicationService { } } + /** + * Responsible for validating service provider in requests + * + * @param identityServerId identity server id of the service provider + * @param spUID uid of the service provider + * @throws ApplicationManagementException + */ private void validateServiceProviderUID(int identityServerId, String spUID) throws ApplicationManagementException { try { @@ -207,5 +221,4 @@ public class SPApplicationServiceImpl implements SPApplicationService { } } - } \ No newline at end of file diff --git a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/util/SPAppRequestHandlerUtil.java b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/util/SPAppRequestHandlerUtil.java index 307cb91342..b9d6b60e99 100644 --- a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/util/SPAppRequestHandlerUtil.java +++ b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/util/SPAppRequestHandlerUtil.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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 io.entgra.application.mgt.publisher.api.services.util; import com.google.gson.Gson; @@ -27,19 +45,26 @@ public class SPAppRequestHandlerUtil { private static final Log log = LogFactory.getLog(SPAppRequestHandlerUtil.class); + /** + * Check if service provider application exists + * + * @param identityServerId id of the identity server + * @param spAppId uid of the service provider + * @return if service provider exist + * @throws ApplicationManagementException + */ public static boolean isSPApplicationExist(int identityServerId, String spAppId) throws ApplicationManagementException { SPApplication application = retrieveSPApplication(identityServerId, spAppId); - if (application == null) { - return false; - } - return true; - } - - public static SPApplicationListResponse getServiceProvidersFromIdentityServer(int identityServerId, Integer limit, Integer offSet) - throws ApplicationManagementException { - return retrieveSPApplications(identityServerId, limit, offSet); + return application != null; } + /** + * Get service provider by identity server id and service provider uid + * @param identityServerId id of the identity server + * @param spAppId uid of service provider to be retrieved + * @return {@link SPApplication} + * @throws ApplicationManagementException + */ public static SPApplication retrieveSPApplication(int identityServerId, String spAppId) throws ApplicationManagementException { IdentityServer identityServer = getIdentityServer(identityServerId); @@ -74,7 +99,13 @@ public class SPAppRequestHandlerUtil { } } - + /** + * Retrieve service provider apps from identity server + * + * @param identityServerId id of the identity server + * @return {@link SPApplicationListResponse} + * @throws ApplicationManagementException + */ public static SPApplicationListResponse retrieveSPApplications(int identityServerId, Integer limit, Integer offset) throws ApplicationManagementException { IdentityServer identityServer = getIdentityServer(identityServerId); @@ -113,17 +144,23 @@ public class SPAppRequestHandlerUtil { } } + /** + * + * @param identityServerId id of the identity server + * @return {@link IdentityServer} + * @throws ApplicationManagementException + */ public static IdentityServer getIdentityServer(int identityServerId) throws ApplicationManagementException { SPApplicationManager spApplicationManager = APIUtil.getSPApplicationManager(); return spApplicationManager.getIdentityServer(identityServerId); } - public static HttpResponse invokeISAPI(IdentityServer identityServer, HttpClient client, HttpRequestBase request) throws IOException { + private static HttpResponse invokeISAPI(IdentityServer identityServer, HttpClient client, HttpRequestBase request) throws IOException { setBasicAuthHeader(identityServer, request); return client.execute(request); } - public static void setBasicAuthHeader(IdentityServer identityServer, HttpRequestBase request) { + private static void setBasicAuthHeader(IdentityServer identityServer, HttpRequestBase request) { String basicAuthHeader = HttpUtil.getBasicAuthBase64Header(identityServer.getUserName(), identityServer.getPassword()); request.setHeader(HttpHeaders.AUTHORIZATION, basicAuthHeader); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/Constants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/Constants.java index 2f9a7207b8..0ff71608f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/Constants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/Constants.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, 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.mgt.core.common; public class Constants { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/FileUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/FileUtil.java index 28a5084003..dc9ce7cc58 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/FileUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/FileUtil.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. 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.mgt.core.common.util; import java.io.ByteArrayInputStream; @@ -20,5 +38,47 @@ public class FileUtil { return new ByteArrayInputStream(base64Bytes); } + /** + * This generates file name with a suffix depending on the duplicate name count, useful when saving + * files with the same name + * + * @param fileNameCount File count + * @return generated file name with suffix + */ + public static String generateDuplicateFileName(String fileName, int fileNameCount) { + String suffix = generateDuplicateFileNameSuffix(fileNameCount); + String fileNameWithoutExtension = extractFileNameWithoutExtension(fileName); + String fileNameWithSuffix = fileNameWithoutExtension + suffix; + fileNameWithSuffix = fileNameWithSuffix + '.' + extractFileExtension(fileName); + return fileNameWithSuffix; + } + + /** + * This generates file name suffix for duplicate file names. For example, + * if it's the first file, the count is 1 in which case the name doesn't have any suffix + * else it adds suffix with (count - 1) + * + * @param fileNameCount File count + * @return generated file name suffix + */ + private static String generateDuplicateFileNameSuffix(int fileNameCount) { + String suffix = ""; + if (fileNameCount < 1) { + throw new IllegalArgumentException("file name count must be above 0"); + } + if (fileNameCount > 1) { + suffix = "(" + (fileNameCount - 1) + ")"; + } + return suffix; + } + + private static String extractFileExtension(String fileName) { + return fileName.substring(fileName.lastIndexOf('.') + 1); + } + + private static String extractFileNameWithoutExtension(String fileName) { + return fileName.substring(0, fileName.lastIndexOf('.')); + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/HttpUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/HttpUtil.java index 130cd07c49..05db183b2a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/HttpUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/HttpUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * Copyright (c) 2022, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. * * Entgra (pvt) Ltd. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except