diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ApiOriginFilter.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ApiOriginFilter.java index 7b34c432328..25446e95135 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ApiOriginFilter.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ApiOriginFilter.java @@ -1,17 +1,16 @@ -/* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, + * 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 + * 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 + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/FileStreamingOutput.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/FileStreamingOutput.java index ee89b209ed1..a1ad912d196 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/FileStreamingOutput.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/FileStreamingOutput.java @@ -1,19 +1,18 @@ -/* * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * 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. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.application.mgt.addons; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/JSONMessageHandler.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/JSONMessageHandler.java index 3b6a778a942..6951e02b2ea 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/JSONMessageHandler.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/JSONMessageHandler.java @@ -1,12 +1,11 @@ -/* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, + * 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 + * 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 diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/MultipartCustomProvider.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/MultipartCustomProvider.java index 5e0bd801903..b1370f18138 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/MultipartCustomProvider.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/MultipartCustomProvider.java @@ -1,20 +1,20 @@ package org.wso2.carbon.device.application.mgt.addons;/* -* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -* WSO2 Inc. licenses this file to you under the Apache License, -* Version 2.0 (the "License"); you may not use this file except -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.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. + */ import com.google.gson.Gson; import com.google.gson.GsonBuilder; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ValidationInterceptor.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ValidationInterceptor.java index a05ecd82e1c..d36e9986b1e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ValidationInterceptor.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/ValidationInterceptor.java @@ -1,17 +1,16 @@ -/* - * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, + * 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 + * 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 + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/AnnotationExclusionStrategy.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/AnnotationExclusionStrategy.java index 1bb39f70875..d2a92c4c27c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/AnnotationExclusionStrategy.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/AnnotationExclusionStrategy.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * 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. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.application.mgt.addons.jaxrs; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/Exclude.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/Exclude.java index fc53d16a8b4..e185ececcbd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/Exclude.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.addons/src/main/java/org/wso2/carbon/device/application/mgt/addons/jaxrs/Exclude.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * 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. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.application.mgt.addons.jaxrs; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java index 15c76d251ed..f4e3130c97d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java @@ -81,4 +81,32 @@ public interface ConfigRetrieveAPI { }) Response getUiConfig(); + @GET + @Path("/lifecycle-config") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get application management UI configuration", + notes = "This will get all UI configuration of application management" + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully got Lifecycle Config.", + response = ApplicationList.class), + @ApiResponse( + code = 404, + message = "Not Found. There doesn't have an defined in app management " + + "configuration file." + + "query."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the lifecycle config.", + response = ErrorResponse.class) + }) + Response getLifecycleConfig(); + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java index fe7c97ef640..65269f512ab 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.api.services.ConfigRetrieveAPI; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; @@ -46,7 +47,26 @@ public class ConfigRetrieveAPIImpl implements ConfigRetrieveAPI { public Response getUiConfig() { AppmDataHandler dataHandler = APIUtil.getDataHandler(); UIConfiguration uiConfiguration = dataHandler.getUIConfiguration(); + if (uiConfiguration == null){ + String msg = "UI configuration is not initiated."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } return Response.status(Response.Status.OK).entity(uiConfiguration).build(); } + @GET + @Override + @Consumes("application/json") + @Path("/lifecycle-config") + public Response getLifecycleConfig() { + AppmDataHandler dataHandler = APIUtil.getDataHandler(); + try { + return Response.status(Response.Status.OK).entity(dataHandler.getLifecycleConfiguration()).build(); + } catch (LifecycleManagementException e) { + String msg = "Error Occurred while accessing lifecycle manager."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java index 1b44fb9e7fb..a8ae90ff589 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationArtifact.java @@ -1,4 +1,5 @@ -package org.wso2.carbon.device.application.mgt.common;/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. +package org.wso2.carbon.device.application.mgt.common; +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.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 diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/State.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/State.java similarity index 95% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/State.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/State.java index 17aefa51477..cec1555baf5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/State.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/State.java @@ -1,4 +1,4 @@ -package org.wso2.carbon.device.application.mgt.core.lifecycle; +package org.wso2.carbon.device.application.mgt.common; import java.util.HashSet; import java.util.List; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/config/LifecycleState.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleState.java similarity index 96% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/config/LifecycleState.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleState.java index 7e971754700..298ec1c57a2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/config/LifecycleState.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleState.java @@ -1,4 +1,4 @@ -package org.wso2.carbon.device.application.mgt.core.lifecycle.config; +package org.wso2.carbon.device.application.mgt.common.config; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/DeviceSubscriptionDTO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/DeviceSubscriptionDTO.java new file mode 100644 index 00000000000..c523c66a530 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/DeviceSubscriptionDTO.java @@ -0,0 +1,68 @@ +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.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.application.mgt.common.dto; + +import java.sql.Timestamp; + +public class DeviceSubscriptionDTO { + + private int id; + private String subscribedBy; + private Timestamp subscribedTimestamp; + private boolean isUnsubscribed; + private String unsubscribedBy; + private Timestamp unsubscribedTimestapm; + private String subscribedFrom; + private int deviceId; + + public int getId() { return id; } + + public void setId(int id) { this.id = id; } + + public String getSubscribedBy() { return subscribedBy; } + + public void setSubscribedBy(String subscribedBy) { this.subscribedBy = subscribedBy; } + + public Timestamp getSubscribedTimestamp() { return subscribedTimestamp; } + + public void setSubscribedTimestamp(Timestamp subscribedTimestamp) { + this.subscribedTimestamp = subscribedTimestamp; + } + + public boolean isUnsubscribed() { return isUnsubscribed; } + + public void setUnsubscribed(boolean unsubscribed) { isUnsubscribed = unsubscribed; } + + public String getUnsubscribedBy() { return unsubscribedBy; } + + public void setUnsubscribedBy(String unsubscribedBy) { this.unsubscribedBy = unsubscribedBy; } + + public Timestamp getUnsubscribedTimestapm() { return unsubscribedTimestapm; } + + public void setUnsubscribedTimestapm(Timestamp unsubscribedTimestapm) { + this.unsubscribedTimestapm = unsubscribedTimestapm; + } + + public String getSubscribedFrom() { return subscribedFrom; } + + public void setSubscribedFrom(String subscribedFrom) { this.subscribedFrom = subscribedFrom; } + + public int getDeviceId() { return deviceId; } + + public void setDeviceId(int deviceId) { this.deviceId = deviceId; } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index eb5f11a022d..ad6f457be66 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -33,7 +33,6 @@ import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseW import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationUpdateWrapper; import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper; -import java.io.InputStream; import java.util.List; /** @@ -68,7 +67,7 @@ public interface ApplicationManager { * @param applicationId ID for tha application * @throws ApplicationManagementException ApplicationDTO Management Exception */ - List deleteApplication(int applicationId) throws ApplicationManagementException; + void deleteApplication(int applicationId) throws ApplicationManagementException; /** * Delete an application identified by the unique ID. @@ -77,7 +76,7 @@ public interface ApplicationManager { * @param releaseUuid UUID of tha application release * @throws ApplicationManagementException ApplicationDTO Management Exception */ - String deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException; + void deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException; /** * To get the applications based on the search filter. @@ -89,7 +88,7 @@ public interface ApplicationManager { ApplicationList getApplications(Filter filter) throws ApplicationManagementException; /** - * To get the ApplicationDTO for given Id. + * To get the Application for given Id. * * @param id id of the ApplicationDTO * @param state state of the ApplicationDTO @@ -98,6 +97,15 @@ public interface ApplicationManager { */ Application getApplicationById(int id, String state) throws ApplicationManagementException; + /** + * To get the Application Release for given uuid. + * + * @param uuid uuid of the ApplicationDTO + * @return the Application Release identified by the UUID + * @throws ApplicationManagementException Application Management Exception. + */ + ApplicationRelease getApplicationReleaseByUUID(String uuid) throws ApplicationManagementException; + /** * To get the ApplicationDTO for given application relase UUID. * @@ -172,19 +180,16 @@ public interface ApplicationManager { /*** * - * @param applicationId ID of the application - * @param releaseUuid UUID of the application release - * @param deviceType Supported device type of the application - * @param applicationRelease {@link ApplicationReleaseDTO} - * @param binaryFileStram {@link InputStream} of the binary file - * @param iconFileStream {@link InputStream} of the icon - * @param bannerFileStream {@link InputStream} of the banner - * @param attachments {@link List} of {@link InputStream} of attachments + * @param deviceType Device type which is supported for the Application. + * @param applicationType Application Type + * @param releaseUuid UUID of the application release. + * @param applicationReleaseWrapper {@link ApplicationReleaseDTO} + * @param applicationArtifact {@link ApplicationArtifact} * @return If the application release is updated correctly True returns, otherwise retuen False */ - boolean updateRelease(int applicationId, String releaseUuid, String deviceType, ApplicationReleaseDTO applicationRelease, - InputStream binaryFileStram, InputStream iconFileStream, InputStream bannerFileStream, - List attachments) throws ApplicationManagementException; + boolean updateRelease(String deviceType, String applicationType, String releaseUuid, + ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) + throws ApplicationManagementException; /*** * To validate the application creating request diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java index 26e426227e5..759e6136283 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java @@ -22,7 +22,6 @@ package org.wso2.carbon.device.application.mgt.common.services; import org.wso2.carbon.device.application.mgt.common.ApplicationInstaller; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; -import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; import java.io.InputStream; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java index 508d7afa3b9..7b8a49d1773 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java @@ -17,10 +17,13 @@ package org.wso2.carbon.device.application.mgt.common.services; +import org.wso2.carbon.device.application.mgt.common.State; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import java.io.InputStream; +import java.util.Map; public interface AppmDataHandler { /** @@ -30,5 +33,7 @@ public interface AppmDataHandler { */ UIConfiguration getUIConfiguration(); + Map getLifecycleConfiguration() throws LifecycleManagementException; + InputStream getArtifactStream(String uuid, String artifactName) throws ApplicationManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java index cef7d31e0cb..3d991776474 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationReleaseWrapper.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * 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. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.application.mgt.common.wrapper; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java index 88e3f959079..81d0f1714f1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationUpdateWrapper.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * 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. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.application.mgt.common.wrapper; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java index b83bbc6a7de..3a02f1b3dcf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/wrapper/ApplicationWrapper.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * 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. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.application.mgt.common.wrapper; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java index 364dd81ac41..bc4b706fede 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java @@ -20,7 +20,7 @@ package org.wso2.carbon.device.application.mgt.core.config; import org.wso2.carbon.device.application.mgt.common.config.RatingConfiguration; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; -import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import java.util.List; import javax.xml.bind.annotation.XmlElement; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index efd1bdec20b..d43819d949d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -207,5 +207,7 @@ public interface ApplicationDAO { * @throws ApplicationManagementDAOException if unable to fetch the ApplicationDTO from the data store. */ ApplicationDTO getApplicationByRelease(String appReleaseUUID, int tenantId) throws ApplicationManagementDAOException; + + String getApplicationSubTypeByUUID(String uuid, int tenantId) throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java index 2045bf62ca6..cd46b6a8b31 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.application.mgt.core.dao; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; +import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; @@ -86,4 +87,8 @@ public interface SubscriptionDAO { */ void subscribeGroupToApplication(int tenantId, String subscribedBy, List groupList, int appId, int releaseId) throws ApplicationManagementDAOException; + + public List getDeviceSubscriptions(int appReleaseId, int tenantId) throws + ApplicationManagementDAOException; + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java index 9ee48dc2507..4a4b1f30ed7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java @@ -40,6 +40,8 @@ public interface VisibilityDAO { List getUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException; + List getUnrestrictedRolesByUUID(String uuid, int tenantId) throws VisibilityManagementDAOException; + void deleteUnrestrictedRoles(List unrestrictedRoles, int applicationId, int tenantId) throws VisibilityManagementDAOException; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index 08d7738b14f..2a5031f843d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java @@ -26,6 +26,7 @@ import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.PaginationRequest; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; +import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; import org.wso2.carbon.device.application.mgt.common.exception.ReviewManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; @@ -94,6 +95,31 @@ public class Util { return applications; } + /** + * To create list of device subscription objects from the result set retrieved from the Database. + * + * @param rs ResultSet + * @return List of device subscriptions that is retrieved from the Database. + * @throws SQLException SQL Exception + * @throws JSONException JSONException. + */ + public static List loadDeviceSubscriptions(ResultSet rs) throws SQLException { + List deviceSubscriptionDTOS = new ArrayList<>(); + while (rs.next()) { + DeviceSubscriptionDTO deviceSubscriptionDTO = new DeviceSubscriptionDTO(); + deviceSubscriptionDTO.setId(rs.getInt("ID")); + deviceSubscriptionDTO.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); + deviceSubscriptionDTO.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_AT")); + deviceSubscriptionDTO.setUnsubscribed(rs.getBoolean("IS_UNSUBSCRIBED")); + deviceSubscriptionDTO.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); + deviceSubscriptionDTO.setUnsubscribedTimestapm(rs.getTimestamp("UNSUBSCRIBED_AT")); + deviceSubscriptionDTO.setSubscribedFrom(rs.getString("SUBSCRIBED_FROM")); + deviceSubscriptionDTO.setDeviceId(rs.getInt("DEVICE_ID")); + deviceSubscriptionDTOS.add(deviceSubscriptionDTO); + } + return deviceSubscriptionDTOS; + } + /** * Populates {@link ApplicationReleaseDTO} object with the result obtained from the database. * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index f003862396f..5565eb43b84 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -197,7 +197,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (!StringUtils.isEmpty(filter.getAppReleaseState())) { sql += " AND AP_APP_RELEASE.CURRENT_STATE = ?"; } - if (deviceTypeId > 0) { + if (deviceTypeId != -1) { sql += " AND AP_APP.DEVICE_TYPE_ID = ?"; } @@ -1095,4 +1095,41 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic Util.cleanupResources(stmt, rs); } } + + @Override + public String getApplicationSubTypeByUUID(String uuid, int tenantId) throws ApplicationManagementDAOException { + Connection conn; + String sql; + try { + conn = this.getDBConnection(); + sql = "SELECT AP_APP.SUB_TYPE AS SUB_TYPE " + + "FROM AP_APP " + + "WHERE " + + "AP_APP.ID = (SELECT AP_APP_RELEASE.AP_APP_ID " + + " FROM AP_APP_RELEASE " + + " WHERE AP_APP_RELEASE.UUID = ?) " + + "AND AP_APP.TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)){ + stmt.setString(1, uuid); + stmt.setInt(2, tenantId); + try(ResultSet rs = stmt.executeQuery()){ + if (rs.next()){ + return rs.getString("SUB_TYPE"); + } + return null; + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get subscription type of the application " + + "for given application release uuid. UUID:." + uuid; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while getting application subscribe type for given application release UUID: " + + uuid + " from database."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } + } + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index d8d473932cb..f7c9c634c9c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -241,9 +241,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements + "AR.APP_META_INFO AS RELEASE_META_INFO, " + "AR.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, " + "AR.RATING AS RELEASE_RATING, " - + "AR.CURRENT_STATE AS RELEASE_CURRENT_STATE, AR.RATED_USERS AS RATED_USER_COUNT " + + "AR.CURRENT_STATE AS RELEASE_CURRENT_STATE, " + + "AR.RATED_USERS AS RATED_USER_COUNT " + "FROM AP_APP_RELEASE AS AR " - + "WHERE AR.UUID = ? AND AR.TENAT_ID = ?"; + + "WHERE AR.UUID = ? AND AR.TENANT_ID = ?"; try { connection = this.getDBConnection(); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index e5e4253691e..7eeb71046c6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -19,16 +19,21 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.subscription; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.json.JSONException; +import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; +import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.core.dao.SubscriptionDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; @@ -44,7 +49,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc conn = this.getDBConnection(); long time = System.currentTimeMillis() / 1000; String sql = "INSERT INTO AP_DEVICE_SUBSCRIPTION(TENANT_ID, SUBSCRIBED_BY, SUBSCRIBED_TIMESTAMP, " - + "DM_DEVICE_ID, AP_APP_RELEASE_ID, AP_APP_ID, INSTALL_STATUS) VALUES (?, ?, ?, ?, ?, ?)"; + + "DM_DEVICE_ID, AP_APP_RELEASE_ID, AP_APP_ID, INSTALL_STATUS) VALUES (?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql); for (Device device : deviceList) { stmt.setInt(1, tenantId); @@ -168,4 +173,50 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc Util.cleanupResources(stmt, null); } } + + @Override + public List getDeviceSubscriptions(int appReleaseId, int tenantId) throws + ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Getting device subscriptions for the application release id " + appReleaseId + + " from the database"); + } + Connection conn; + try { + conn = this.getDBConnection(); + String sql = "SELECT " + + "DS.ID AS ID, " + + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " + + "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, " + + "DS.UNSUBSCRIBED AS IS_UNSUBSCRIBED, " + + "DS.UNSUBSCRIBED_BY AS UNSUBSCRIBED_BY, " + + "DS.UNSUBSCRIBED_TIMESTAMP AS UNSUBSCRIBED_AT, " + + "DS.SUBSCRIBED_FROM AS SUBSCRIBED_FROM, " + + "DS.DM_DEVICE_ID AS DEVICE_ID " + + "FROM AP_DEVICE_SUBSCRIPTION DS " + + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.TENANT_ID=?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, appReleaseId); + stmt.setInt(2, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved device subscriptions for application release id " + + appReleaseId); + } + return Util.loadDeviceSubscriptions(rs); + } + } + } catch (SQLException e) { + String msg = + "Error occurred while getting device subscription data for application ID: " + appReleaseId + "."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } catch (DBConnectionException e) { + String msg = + "Error occurred while obtaining the DB connection for getting device subscription for applicationID: " + + appReleaseId + "."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java index 51360b4a47b..26f88165b05 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java @@ -104,6 +104,42 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil } } + + @Override + public List getUnrestrictedRolesByUUID(String uuid, int tenantId) throws VisibilityManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get unrestricted roles for UUID: " + uuid); + } + Connection conn; + List unrestrictedRoles = new ArrayList<>(); + String sql = "SELECT ROLE FROM AP_UNRESTRICTED_ROLE " + + "WHERE " + + "AP_APP_ID = (SELECT AR.AP_APP_ID FROM AP_APP_RELEASE AR WHERE AR.UUID = ? AND AR.TENANT_ID = ? ) " + + "AND TENANT_ID = ?"; + try { + conn = this.getDBConnection(); + conn.setAutoCommit(false); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, uuid); + stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); + try (ResultSet rs = stmt.executeQuery();) { + while (rs.next()) { + unrestrictedRoles.add(rs.getString("ROLE")); + } + return unrestrictedRoles; + } + } + } catch (DBConnectionException e) { + throw new VisibilityManagementDAOException( + "Error occurred while obtaining the DB connection when getting unrestricted roles for UUID: " + + uuid, e); + } catch (SQLException e) { + throw new VisibilityManagementDAOException( + "Error occurred while getting unrestricted roles for UUID: " + uuid, e); + } + } + @Override public void deleteUnrestrictedRoles(List unrestrictedRoles, int applicationId, int tenantId) throws VisibilityManagementDAOException { if (log.isDebugEnabled()) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagementDAOException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagementDAOException.java index 44c78804a6d..c9059f3ea95 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagementDAOException.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagementDAOException.java @@ -1,20 +1,18 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) 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 + * 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. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.wso2.carbon.device.application.mgt.core.exception; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/SubscriptionManagementDAOException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/SubscriptionManagementDAOException.java new file mode 100644 index 00000000000..8419ed643db --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/SubscriptionManagementDAOException.java @@ -0,0 +1,33 @@ +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.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.application.mgt.core.exception; + +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; + +/** + * Exception thrown during the ApplicationDTO Management DAO operations. + */ +public class SubscriptionManagementDAOException extends ApplicationManagementException { + + public SubscriptionManagementDAOException(String message, Throwable throwable) { + super(message, throwable); + } + + public SubscriptionManagementDAOException(String message) { + super(message, new Exception()); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index 7a1f683ba61..95a5a17807a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -39,6 +39,7 @@ import org.wso2.carbon.device.application.mgt.common.ApplicationSubscriptionType import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.dto.CategoryDTO; import org.wso2.carbon.device.application.mgt.common.Filter; +import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; import org.wso2.carbon.device.application.mgt.common.dto.TagDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; @@ -59,6 +60,7 @@ import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO; import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; +import org.wso2.carbon.device.application.mgt.core.dao.SubscriptionDAO; import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; @@ -87,6 +89,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -108,6 +111,7 @@ public class ApplicationManagerImpl implements ApplicationManager { private ApplicationDAO applicationDAO; private ApplicationReleaseDAO applicationReleaseDAO; private LifecycleStateDAO lifecycleStateDAO; + private SubscriptionDAO subscriptionDAO; private LifecycleStateManager lifecycleStateManager; public ApplicationManagerImpl() { @@ -120,6 +124,7 @@ public class ApplicationManagerImpl implements ApplicationManager { this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); + this.subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); } /*** @@ -135,79 +140,126 @@ public class ApplicationManagerImpl implements ApplicationManager { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - ApplicationDTO applicationDTO; - Application application; if (log.isDebugEnabled()) { log.debug("Application create request is received for the tenant : " + tenantId + " From" + " the user : " + userName); } - try { - applicationDTO = appWrapperToAppDTO(applicationWrapper); - ApplicationReleaseDTO initialApplicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); - applicationDTO.getApplicationReleaseDTOs().clear(); - ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), - applicationWrapper.getDeviceType(), initialApplicationReleaseDTO, applicationArtifact); - applicationDTO.getApplicationReleaseDTOs().add(addImageArtifacts(applicationReleaseDTO, applicationArtifact)); - } catch (UnexpectedServerErrorException e) { - String msg = "Error occurred when getting Device Type data."; - log.error(msg); - throw new ApplicationManagementException(msg, e); - } catch (ResourceManagementException e) { - String msg = "Error Occured when uploading artifacts of the application: " + applicationWrapper.getName(); - log.error(msg); - throw new ApplicationManagementException(msg, e); - } + ApplicationDTO applicationDTO; + List unrestrictedRoles; + Optional category; + List tags; + //validating and verifying application data try { - List applicationReleaseEntities = new ArrayList<>(); - ApplicationReleaseDTO applicationReleaseDTO; + ConnectionManagerUtil.openDBConnection(); + applicationDTO = appWrapperToAppDTO(applicationWrapper); + unrestrictedRoles = applicationWrapper.getUnrestrictedRoles(); + tags = applicationWrapper.getTags(); + + if (unrestrictedRoles != null && !unrestrictedRoles.isEmpty()) { + if (!isValidRestrictedRole(unrestrictedRoles)) { + String msg = "Unrestricted role list contain role/roles which are not in the user store."; + log.error(msg); + throw new ApplicationManagementException(msg); + } + if (!hasUserRole(unrestrictedRoles, userName)) { + String msg = "You are trying to restrict the visibility of the application for a role set, but " + + "in order to perform the action at least one role should be assigned to user: " + + userName; + log.error(msg); + throw new BadRequestException(msg); + } + } Filter filter = new Filter(); filter.setFullMatch(true); filter.setAppName(applicationDTO.getName().trim()); filter.setOffset(0); filter.setLimit(1); - - ConnectionManagerUtil.beginDBTransaction(); List applicationList = applicationDAO .getApplications(filter, applicationDTO.getDeviceTypeId(), tenantId); if (!applicationList.isEmpty()) { - String msg = - "Already an application registered with same name - " + applicationList.get(0) - .getName(); + String msg = "Already an application registered with same name - " + applicationList.get(0).getName() + + " for the device type " + applicationWrapper.getDeviceType(); log.error(msg); throw new RequestValidatingException(msg); } + List registeredCategories = this.applicationDAO.getAllCategories(tenantId); + String categoryName = applicationWrapper.getAppCategory(); + + if (registeredCategories.isEmpty()) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Registered application category set is empty. Since it is mandatory to add application " + + "category when adding new application, registered application category list shouldn't be null."; + log.error(msg); + throw new ApplicationManagementException(msg); + } + category = registeredCategories.stream().filter(obj -> obj.getCategoryName().equals(categoryName)) + .findAny(); + if (!category.isPresent()) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Request contains invalid category: " + categoryName; + log.error(msg); + throw new ApplicationManagementException(msg); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while getting database connection."; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (UnexpectedServerErrorException e) { + String msg = "Error occurred when getting Device Type data."; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred while getting data which is related to application. application name: " + + applicationWrapper.getName() + " and application type: " + applicationWrapper.getType(); + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (UserStoreException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred when validating the unrestricted roles given for the application"; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + + //uploading application artifacts + try { + ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); + applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), + applicationWrapper.getDeviceType(), applicationReleaseDTO, applicationArtifact, false); + applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact); + applicationDTO.getApplicationReleaseDTOs().clear(); + applicationDTO.getApplicationReleaseDTOs().add(applicationReleaseDTO); + } catch (ResourceManagementException e) { + String msg = "Error Occured when uploading artifacts of the application: " + applicationWrapper.getName(); + log.error(msg); + throw new ApplicationManagementException(msg, e); + } + + //insert application data into databse + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); + try { + List applicationReleaseEntities = new ArrayList<>(); + + ConnectionManagerUtil.beginDBTransaction(); // Insert to application table int appId = this.applicationDAO.createApplication(applicationDTO, tenantId); if (appId == -1) { - log.error("ApplicationDTO creation is Failed"); + log.error("Application data storing is Failed."); ConnectionManagerUtil.rollbackDBTransaction(); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); return null; } else { if (log.isDebugEnabled()) { log.debug("New ApplicationDTO entry added to AP_APP table. App Id:" + appId); } //adding application unrestricted roles - List unrestrictedRoles = applicationWrapper.getUnrestrictedRoles(); - if (!unrestrictedRoles.isEmpty()) { - if (!isValidRestrictedRole(unrestrictedRoles)) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Unrestricted role list contain role/roles which are not in the user store."; - log.error(msg); - throw new ApplicationManagementException(msg); - } - - if (!hasUserRole(unrestrictedRoles, userName)){ - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = - "You are trying to restrict the visibility of the application for a role set, but in order to perform the action at least one role should be assigned to user: " - + userName; - log.error(msg); - throw new BadRequestException(msg); - } + if (unrestrictedRoles != null && !unrestrictedRoles.isEmpty()) { this.visibilityDAO.addUnrestrictedRoles(unrestrictedRoles, appId, tenantId); if (log.isDebugEnabled()) { log.debug("New restricted roles to app ID mapping added to AP_UNRESTRICTED_ROLE table." @@ -215,25 +267,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - List registeredCategories = this.applicationDAO.getAllCategories(tenantId); - String categoryName = applicationWrapper.getAppCategory(); - Optional category = registeredCategories.stream() - .filter(obj -> obj.getCategoryName().equals(categoryName)).findAny(); - - if (registeredCategories.isEmpty()) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Registered application category set is empty category: " + categoryName; - log.error(msg); - throw new ApplicationManagementException(msg); - } - if (!category.isPresent()){ - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Request contains invalid category: " + categoryName; - log.error(msg); - throw new ApplicationManagementException(msg); - - } - /* In current flow, allow to add one category for an application. If it is required to add multiple categories DAO layer is implemented to match with that requirement. Hence logic is also implemented @@ -241,12 +274,10 @@ public class ApplicationManagerImpl implements ApplicationManager { */ List categoryIds = new ArrayList<>(); categoryIds.add(category.get().getId()); - this.applicationDAO.addCategoryMapping(categoryIds,appId,tenantId); - + this.applicationDAO.addCategoryMapping(categoryIds, appId, tenantId); //adding application tags - List tags = applicationWrapper.getTags(); - if (!tags.isEmpty()) { + if (tags != null && !tags.isEmpty()) { List registeredTags = applicationDAO.getAllTags(tenantId); List registeredTagNames = new ArrayList<>(); List tagIds = new ArrayList<>(); @@ -278,63 +309,86 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("Creating a new release. App Id:" + appId); } String initialLifecycleState = lifecycleStateManager.getInitialState(); - applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0); applicationReleaseDTO.setCurrentState(initialLifecycleState); - applicationReleaseDTO = this.applicationReleaseDAO.createRelease(applicationReleaseDTO, appId, tenantId); + applicationReleaseDTO = this.applicationReleaseDAO + .createRelease(applicationReleaseDTO, appId, tenantId); LifecycleStateDTO lifecycleStateDTO = getLifecycleStateInstance(initialLifecycleState, initialLifecycleState); this.lifecycleStateDAO .addLifecycleState(lifecycleStateDTO, appId, applicationReleaseDTO.getUuid(), tenantId); applicationReleaseEntities.add(applicationReleaseDTO); applicationDTO.setApplicationReleaseDTOs(applicationReleaseEntities); - application = appDtoToAppResponse(applicationDTO); + Application application = appDtoToAppResponse(applicationDTO); ConnectionManagerUtil.commitDBTransaction(); + return application; } - return application; } catch (LifeCycleManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred while adding lifecycle state. application name: " + applicationWrapper.getName() - + " application type: is " + applicationWrapper.getType(); + String msg = + "Error occurred while adding lifecycle state. application name: " + applicationWrapper.getName() + + " application type: is " + applicationWrapper.getType(); log.error(msg); + try { + applicationStorageManager.deleteAllApplicationReleaseArtifacts( + Collections.singletonList(applicationReleaseDTO.getAppHashValue())); + } 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); + throw new ApplicationManagementException(errorLog, e); + } throw new ApplicationManagementException(msg, e); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred while adding application or application release. application name: " + applicationWrapper.getName() + " application type: " + applicationWrapper.getType(); log.error(msg); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); throw new ApplicationManagementException(msg, e); - } catch(LifecycleManagementException e){ + } catch (LifecycleManagementException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred when getting initial lifecycle state. application name: " + applicationWrapper .getName() + " application type: is " + applicationWrapper.getType(); log.error(msg); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); throw new ApplicationManagementException(msg, e); - }catch (DBConnectionException e) { + } catch (DBConnectionException e) { String msg = "Error occurred while getting database connection."; log.error(msg); throw new ApplicationManagementException(msg, e); } catch (VisibilityManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred while adding unrestricted roles. application name: " + applicationWrapper.getName() - + " application type: " + applicationWrapper.getType(); + String msg = + "Error occurred while adding unrestricted roles. application name: " + applicationWrapper.getName() + + " application type: " + applicationWrapper.getType(); log.error(msg); + deleteApplicationArtifacts(Collections.singletonList(applicationReleaseDTO.getAppHashValue())); throw new ApplicationManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while disabling AutoCommit."; log.error(msg); throw new ApplicationManagementException(msg, e); - } catch (UserStoreException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred when validating the unrestricted roles given for the application"; - log.error(msg); - throw new ApplicationManagementException(msg, e); } finally { ConnectionManagerUtil.closeDBConnection(); } } + private void deleteApplicationArtifacts(List directoryPaths) throws ApplicationManagementException { + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + + try { + applicationStorageManager.deleteAllApplicationReleaseArtifacts(directoryPaths); + } catch (ApplicationStorageManagementException e) { + String errorLog = "Error occurred when deleting application artifacts. directory paths: ." + directoryPaths + .toString(); + log.error(errorLog); + throw new ApplicationManagementException(errorLog, e); + } + } + private ApplicationReleaseDTO addApplicationReleaseArtifacts(String applicationType, String deviceType, - ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact) + ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact, boolean isNewRelease) throws ResourceManagementException, ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); @@ -355,7 +409,7 @@ public class ApplicationManagerImpl implements ApplicationManager { String packagename = applicationInstaller.getPackageName(); ConnectionManagerUtil.getDBConnection(); - if (applicationReleaseDAO.isActiveReleaseExisitForPackageName(packagename, tenantId, + if (!isNewRelease && applicationReleaseDAO.isActiveReleaseExisitForPackageName(packagename, tenantId, lifecycleStateManager.getEndState())) { String msg = "Application release is already exist for the package name: " + packagename + ". Either you can delete all application releases for package " + packagename + " or " @@ -378,7 +432,7 @@ public class ApplicationManagerImpl implements ApplicationManager { .verifyReleaseExistenceByHash(md5OfApp, tenantId)) { throw new BadRequestException( "Application release exists for the uploaded binary file. Application Type: " - + applicationType + " Device Tyep: " + deviceType); + + applicationType + " Device Type: " + deviceType); } applicationReleaseDTO.setAppHashValue(md5OfApp); @@ -416,6 +470,92 @@ public class ApplicationManagerImpl implements ApplicationManager { return applicationReleaseDTO; } + private ApplicationReleaseDTO updateApplicationReleaseArtifacts(String applicationType, String deviceType, + ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact) + throws ResourceManagementException, ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + + // The application executable artifacts such as apks are uploaded. + if (ApplicationType.ENTERPRISE.toString().equals(applicationType)) { + try { + byte[] content = IOUtils.toByteArray(applicationArtifact.getInstallerStream()); + + try(ByteArrayInputStream binaryClone = new ByteArrayInputStream(content)){ + String md5OfApp = StorageManagementUtil.getMD5(binaryClone); + + if (md5OfApp == null) { + String msg = "Error occurred while md5sum value retrieving process: application UUID " + + applicationReleaseDTO.getUuid(); + log.error(msg); + throw new ApplicationStorageManagementException(msg); + } + if (!applicationReleaseDTO.getAppHashValue().equals(md5OfApp)){ + applicationReleaseDTO.setInstallerName(applicationArtifact.getInstallerName()); + + try (ByteArrayInputStream binary = new ByteArrayInputStream(content)) { + ApplicationInstaller applicationInstaller = applicationStorageManager + .getAppInstallerData(binary, deviceType); + String packagename = applicationInstaller.getPackageName(); + + ConnectionManagerUtil.getDBConnection(); + if (applicationReleaseDAO.isActiveReleaseExisitForPackageName(packagename, tenantId, + lifecycleStateManager.getEndState())) { + String msg = "Application release is already exist for the package name: " + packagename + + ". Either you can delete all application releases for package " + packagename + " or " + + "you can add this app release as an new application release, under the existing " + + "application."; + log.error(msg); + throw new ApplicationManagementException(msg); + } + applicationReleaseDTO.setVersion(applicationInstaller.getVersion()); + applicationReleaseDTO.setPackageName(packagename); + + if (this.applicationReleaseDAO + .verifyReleaseExistenceByHash(md5OfApp, tenantId)) { + throw new BadRequestException( + "Application release exists for the uploaded binary file. Application Type: " + + applicationType + " Device Tyep: " + deviceType); + } + + applicationReleaseDTO.setAppHashValue(md5OfApp); + String deletingAppHashValue = applicationReleaseDTO.getAppHashValue(); + try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) { + applicationReleaseDTO = applicationStorageManager + .uploadReleaseArtifact(applicationReleaseDTO,applicationType, + deviceType, binaryDuplicate); + applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, + applicationReleaseDTO); + } + } + } + + } + } catch (IOException e) { + String msg = + "Error occurred when getting byte array of binary file. Installer name: " + applicationArtifact + .getInstallerName(); + log.error(msg); + throw new ApplicationStorageManagementException(msg); + } catch (DBConnectionException e) { + String msg = "Error occurred when getting database connection for verifying application package existence."; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when executing the query for verifying application release existence for " + + "the package."; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } else if (ApplicationType.WEB_CLIP.toString().equals(applicationType)) { + applicationReleaseDTO.setVersion(Constants.DEFAULT_VERSION); + applicationReleaseDTO.setInstallerName(applicationReleaseDTO.getUrl()); + } + return applicationReleaseDTO; + } + private ApplicationReleaseDTO addImageArtifacts(ApplicationReleaseDTO applicationReleaseDTO, ApplicationArtifact applicationArtifact) throws ResourceManagementException { ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); @@ -432,7 +572,40 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationReleaseDTO.setScreenshotName1(scName); } else if (counter == 2) { applicationReleaseDTO.setScreenshotName2(scName); + } else if (counter == 3) { + applicationReleaseDTO.setScreenshotName3(scName); + } + counter++; + } + + // Upload images + applicationReleaseDTO = applicationStorageManager + .uploadImageArtifacts(applicationReleaseDTO, applicationArtifact.getIconStream(), + applicationArtifact.getBannerStream(), new ArrayList<>(screenshots.values())); + return applicationReleaseDTO; + } + + private ApplicationReleaseDTO updateImageArtifacts(ApplicationReleaseDTO applicationReleaseDTO, + ApplicationArtifact applicationArtifact) throws ResourceManagementException{ + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); + + applicationStorageManager.deleteImageArtifacts(applicationReleaseDTO); + applicationReleaseDTO.setIconName(applicationArtifact.getIconName()); + applicationReleaseDTO.setBannerName(applicationArtifact.getBannerName()); + applicationReleaseDTO.setScreenshotName1(null); + applicationReleaseDTO.setScreenshotName2(null); + applicationReleaseDTO.setScreenshotName3(null); + + Map screenshots = applicationArtifact.getScreenshots(); + List screenshotNames = new ArrayList<>(screenshots.keySet()); + + int counter = 1; + for (String scName : screenshotNames) { + if (counter == 1) { + applicationReleaseDTO.setScreenshotName1(scName); + } else if (counter == 2) { + applicationReleaseDTO.setScreenshotName2(scName); } else if (counter == 3) { applicationReleaseDTO.setScreenshotName3(scName); } @@ -458,9 +631,6 @@ public class ApplicationManagerImpl implements ApplicationManager { try { //set default values - if (StringUtils.isEmpty(filter.getSortBy())) { - filter.setSortBy("ASC"); - } if (filter.getLimit() == 0) { filter.setLimit(20); } @@ -468,59 +638,54 @@ public class ApplicationManagerImpl implements ApplicationManager { if (!StringUtils.isEmpty(deviceTypename)) { deviceType = getDeviceTypeData(deviceTypename); } - - ConnectionManagerUtil.openDBConnection(); - - validateFilter(filter); if (deviceType == null) { - appDTOs = applicationDAO.getApplications(filter, 0, tenantId); - } else { - appDTOs = applicationDAO.getApplications(filter, deviceType.getId(), tenantId); + deviceType = new DeviceType(); + deviceType.setId(-1); } - for (ApplicationDTO app : appDTOs) { + ConnectionManagerUtil.openDBConnection(); + validateFilter(filter); + appDTOs = applicationDAO.getApplications(filter, deviceType.getId(), tenantId); + //todo as a performance improvement get these data from DB. Consider where in clause. + for (ApplicationDTO applicationDTO : appDTOs) { boolean isSearchingApp = true; List filteringTags = filter.getTags(); List filteringCategories = filter.getAppCategories(); List filteringUnrestrictedRoles = filter.getUnrestrictedRoles(); - if (!lifecycleStateManager.getEndState().equals(app.getStatus())) { - List appUnrestrictedRoles = visibilityDAO.getUnrestrictedRoles(app.getId(), tenantId); + if (!lifecycleStateManager.getEndState().equals(applicationDTO.getStatus())) { + List appUnrestrictedRoles = visibilityDAO.getUnrestrictedRoles(applicationDTO.getId(), tenantId); if ((appUnrestrictedRoles.isEmpty() || hasUserRole(appUnrestrictedRoles, userName)) && ( filteringUnrestrictedRoles == null || filteringUnrestrictedRoles.isEmpty() || hasAppUnrestrictedRole(appUnrestrictedRoles, filteringUnrestrictedRoles, userName))) { if (filteringCategories != null && !filteringCategories.isEmpty()) { - List appTagList = applicationDAO.getAppCategories(app.getId(), tenantId); - boolean isAppCategory = false; - for (String category : filteringCategories) { - if (appTagList.contains(category)) { - isAppCategory = true; - break; - } - } + List appTagList = applicationDAO.getAppCategories(applicationDTO.getId(), tenantId); + boolean isAppCategory = filteringCategories.stream().anyMatch(appTagList::contains); if (!isAppCategory) { isSearchingApp = false; } } if (filteringTags != null && !filteringTags.isEmpty()) { - List appTagList = applicationDAO.getAppTags(app.getId(), tenantId); - boolean isAppTag = false; - for (String tag : filteringTags) { - if (appTagList.contains(tag)) { - isAppTag = true; - break; - } - } + List appTagList = applicationDAO.getAppTags(applicationDTO.getId(), tenantId); + boolean isAppTag = filteringTags.stream().anyMatch(appTagList::contains); if (!isAppTag) { isSearchingApp = false; } } if (isSearchingApp) { - filteredApplications.add(app); + filteredApplications.add(applicationDTO); } } } + + List filteredApplicationReleaseDTOs = new ArrayList<>(); + for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) { + if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState())) { + filteredApplicationReleaseDTOs.add(applicationReleaseDTO); + } + } + applicationDTO.setApplicationReleaseDTOs(filteredApplicationReleaseDTOs); } for(ApplicationDTO appDTO : filteredApplications){ @@ -566,36 +731,25 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public ApplicationRelease createRelease(int applicationId, - ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) - throws ApplicationManagementException { + public ApplicationRelease createRelease(int applicationId, ApplicationReleaseWrapper applicationReleaseWrapper, + ApplicationArtifact applicationArtifact) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationRelease applicationRelease; if (log.isDebugEnabled()) { log.debug("ApplicationDTO release request is received for the application id: " + applicationId); } - try { + ApplicationDTO applicationDTO = getApplication(applicationId); + ApplicationReleaseDTO applicationReleaseDTO = uploadReleaseArtifacts(applicationReleaseWrapper, + applicationDTO, applicationArtifact); ConnectionManagerUtil.beginDBTransaction(); - ApplicationDTO applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId); - if (applicationDTO == null) { - String msg = "Couldn't find application for the application Id: " + applicationId; - log.error(msg); - throw new NotFoundException(msg); - } - DeviceType deviceType = getDeviceTypeData(applicationDTO.getDeviceTypeId()); - ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), - deviceType.getName(), releaseWrapperToReleaseDTO(applicationReleaseWrapper), - applicationArtifact); - applicationReleaseDTO = addImageArtifacts(applicationReleaseDTO, applicationArtifact); - String initialstate = lifecycleStateManager.getInitialState(); applicationReleaseDTO.setCurrentState(initialstate); LifecycleStateDTO lifecycleState = getLifecycleStateInstance(initialstate, initialstate); - this.lifecycleStateDAO - .addLifecycleState(lifecycleState, applicationId, applicationReleaseDTO.getUuid(), tenantId); applicationReleaseDTO = this.applicationReleaseDAO .createRelease(applicationReleaseDTO, applicationDTO.getId(), tenantId); + this.lifecycleStateDAO + .addLifecycleState(lifecycleState, applicationId, applicationReleaseDTO.getUuid(), tenantId); applicationRelease = releaseDtoToRelease(applicationReleaseDTO); ConnectionManagerUtil.commitDBTransaction(); return applicationRelease; @@ -617,13 +771,48 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( "Error occurred while adding new application release for application " + applicationId, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + private ApplicationDTO getApplication(int applicationId) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationDTO applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId); + if (applicationDTO == null) { + String msg = "Couldn't find application for the application Id: " + applicationId; + log.error(msg); + throw new NotFoundException(msg); + } + return applicationDTO; + } catch (DBConnectionException e) { + throw new ApplicationManagementException( + "Error occurred while obtaining the database connection for getting application for the application ID: " + + applicationId, e); + } catch (ApplicationManagementDAOException e) { + throw new ApplicationManagementException( + "Error occurred while getting application data for application ID: " + applicationId, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + private ApplicationReleaseDTO uploadReleaseArtifacts(ApplicationReleaseWrapper applicationReleaseWrapper, + ApplicationDTO applicationDTO, ApplicationArtifact applicationArtifact) + throws ApplicationManagementException { + try { + DeviceType deviceType = getDeviceTypeData(applicationDTO.getDeviceTypeId()); + ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts(applicationDTO.getType(), + deviceType.getName(), releaseWrapperToReleaseDTO(applicationReleaseWrapper), applicationArtifact, + true); + return addImageArtifacts(applicationReleaseDTO, applicationArtifact); } catch (ResourceManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); String msg = - "Error occurred while uploading application release artifacts. Application ID: " + applicationId; + "Error occurred while uploading application release artifacts. Application ID: " + applicationDTO + .getId(); throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } } @@ -634,14 +823,9 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationDTO applicationDTO; boolean isVisibleApp = false; try { - ConnectionManagerUtil.openDBConnection(); - applicationDTO = this.applicationDAO.getApplicationById(appId, tenantId); - if (applicationDTO == null) { - String msg = "Couldn't find an application for application Id: " + appId; - log.error(msg); - throw new NotFoundException(msg); - } + applicationDTO = getApplication(appId); + ConnectionManagerUtil.openDBConnection(); List filteredApplicationReleaseDTOs = new ArrayList<>(); for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) { if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState()) && ( @@ -694,6 +878,53 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + @Override + public ApplicationRelease getApplicationReleaseByUUID(String uuid) throws ApplicationManagementException{ + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + boolean isVisibleAppRelease = false; + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for the UUID: " + uuid; + log.error(msg); + throw new NotFoundException(msg); + } + if (applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState())) { + return null; + } + + List unrestrictedRoles = this.visibilityDAO.getUnrestrictedRolesByUUID(uuid, tenantId); + if (!unrestrictedRoles.isEmpty()) { + if (hasUserRole(unrestrictedRoles, userName)) { + isVisibleAppRelease = true; + } + } else { + isVisibleAppRelease = true; + } + + if (!isVisibleAppRelease) { + String msg = "You are trying to access release of visibility restricted application. You don't have " + + "required roles to view this application,"; + log.error(msg); + throw new ForbiddenException(msg); + } + return releaseDtoToRelease(applicationReleaseDTO); + } catch (LifecycleManagementException e) { + String msg = "Error occurred when getting the end state of the application lifecycle flow"; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (UserStoreException e) { + String msg = "User-store exception while getting application with the application release UUID: " + uuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override public ApplicationDTO getApplicationByUuid(String uuid, String state) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -768,6 +999,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } private String[] getRoleNames() throws UserStoreException { + //todo check role by role UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); if (userRealm != null) { return userRealm.getUserStoreManager().getRoleNames(); @@ -906,80 +1138,52 @@ public class ApplicationManagerImpl implements ApplicationManager { // return applicationReleases; // } - @Override public List deleteApplication(int applicationId) throws ApplicationManagementException { - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + @Override + public void deleteApplication(int applicationId) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); List storedLocations = new ArrayList<>(); - ApplicationDTO application; - + ApplicationDTO applicationDTO; + if (log.isDebugEnabled()) { + log.debug("Request is received to delete applications which are related with the application id " + + applicationId); + } try { ConnectionManagerUtil.beginDBTransaction(); - application = this.applicationDAO.getApplicationById(applicationId, tenantId); + applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId); - if (application == null) { - throw new NotFoundException("Couldn't found an application for ApplicationDTO ID: " + applicationId); + if (applicationDTO == null) { + throw new NotFoundException("Couldn't found an application for Application ID: " + applicationId); } - - if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION) && !application - .getUnrestrictedRoles().isEmpty() && hasUserRole(application.getUnrestrictedRoles(), userName)) { - throw new ForbiddenException( - "You don't have permission to delete this application. In order to delete an application you " - + "need to have required permission. ApplicationDTO ID: " + applicationId); - } - List applicationReleases = getReleases(application, null); - if (log.isDebugEnabled()) { - log.debug("Request is received to delete applications which are related with the application id " - + applicationId); - } - for (ApplicationReleaseDTO applicationRelease : applicationReleases) { - LifecycleStateDTO appLifecycleState = this.lifecycleStateDAO - .getLatestLifeCycleState(applicationId, applicationRelease.getUuid()); - LifecycleStateDTO newAppLifecycleState = getLifecycleStateInstance(AppLifecycleState.REMOVED.toString(), - appLifecycleState.getCurrentState()); - if (lifecycleStateManager.isValidStateChange(newAppLifecycleState.getPreviousState(), - newAppLifecycleState.getCurrentState(), userName, tenantId)) { - this.lifecycleStateDAO - .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), - tenantId); - } else { - String currentState = appLifecycleState.getCurrentState(); - List lifecycleFlow = searchLifecycleStateFlow(currentState, - AppLifecycleState.REMOVED.toString()); - for (String nextState : lifecycleFlow) { - LifecycleStateDTO lifecycleState = getLifecycleStateInstance(nextState, currentState); - if (lifecycleStateManager.isValidStateChange(currentState, nextState, userName, tenantId)) { - this.lifecycleStateDAO - .addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), - tenantId); - } else { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Can't delete application release which has the UUID:" + applicationRelease - .getUuid() - + " and its belongs to the application which has application ID:" - + applicationId + " You have to move the lifecycle state from " - + currentState + " to acceptable state"); - } - currentState = nextState; - } + List applicationReleaseDTOs = applicationDTO.getApplicationReleaseDTOs(); + List activeApplicationReleaseDTOs = new ArrayList<>(); + for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) { + if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState())){ + activeApplicationReleaseDTOs.add(applicationReleaseDTO); } - storedLocations.add(applicationRelease.getAppHashValue()); + storedLocations.add(applicationReleaseDTO.getAppHashValue()); + } + if (!activeApplicationReleaseDTOs.isEmpty()) { + String msg = "There are application releases which are not in the state " + lifecycleStateManager + .getEndState() + ". Hence you are not allowed to delete the application"; + log.error(msg); + throw new ForbiddenException(msg); } this.applicationDAO.deleteApplication(applicationId); ConnectionManagerUtil.commitDBTransaction(); - } catch (UserStoreException e) { - String msg = "Error occured while check whether current user has the permission to delete an application"; + applicationStorageManager.deleteAllApplicationReleaseArtifacts(storedLocations); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when getting application data for application id: " + applicationId; log.error(msg); throw new ApplicationManagementException(msg, e); - } catch (LifeCycleManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occured while changing the application lifecycle state into REMOVED state."; + } catch (ApplicationStorageManagementException e) { + String msg = "Error occurred when deleting application artifacts in the file system. Application id: " + + applicationId; log.error(msg); - throw new ApplicationManagementException(msg, e); + throw new ApplicationManagementException(msg); } finally { ConnectionManagerUtil.closeDBConnection(); } - return storedLocations; } private List searchLifecycleStateFlow(String start, String finish) throws ApplicationManagementException { @@ -1030,86 +1234,91 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public String deleteApplicationRelease(int applicationId, String releaseUuid) + public void deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - ApplicationDTO application; + ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); try { ConnectionManagerUtil.beginDBTransaction(); - application = this.applicationDAO.getApplicationById(applicationId, tenantId); - if (application == null) { - throw new NotFoundException("Couldn't find an application for application ID: " + applicationId); + ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO + .getReleaseByUUID(releaseUuid, tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + releaseUuid; + log.error(msg); + throw new NotFoundException(msg); } - if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION) && !application - .getUnrestrictedRoles().isEmpty() && hasUserRole(application.getUnrestrictedRoles(), userName)) { - throw new ForbiddenException( - "You don't have permission for deleting application release. ApplicationDTO id: " + applicationId - + " and release UUID: " + releaseUuid); - } - - ApplicationReleaseDTO applicationRelease = this.applicationReleaseDAO - .getReleaseByIds(applicationId, releaseUuid, tenantId); - if (applicationRelease == null) { - throw new NotFoundException("Couldn't find an application release for application ID: " + applicationId - + " and release UUID: " + releaseUuid); - } - LifecycleStateDTO appLifecycleState = this.lifecycleStateDAO - .getLatestLifeCycleState(applicationId, releaseUuid); - if (appLifecycleState == null) { - throw new NotFoundException( - "Couldn't find an lifecycle sate for application ID: " + applicationId + " and UUID: " - + releaseUuid); - } - String currentState = appLifecycleState.getCurrentState(); - if (AppLifecycleState.DEPRECATED.toString().equals(currentState) || AppLifecycleState.REJECTED.toString() - .equals(currentState) || AppLifecycleState.UNPUBLISHED.toString().equals(currentState)) { - LifecycleStateDTO newAppLifecycleState = getLifecycleStateInstance(AppLifecycleState.REMOVED.toString(), - appLifecycleState.getCurrentState()); - if (lifecycleStateManager.isValidStateChange(newAppLifecycleState.getPreviousState(), - newAppLifecycleState.getCurrentState(), userName, tenantId)) { - this.lifecycleStateDAO - .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), - tenantId); - ConnectionManagerUtil.commitDBTransaction(); - } else { - List lifecycleFlow = searchLifecycleStateFlow(currentState, - AppLifecycleState.REMOVED.toString()); - for (String nextState : lifecycleFlow) { - LifecycleStateDTO lifecycleState = getLifecycleStateInstance(nextState, currentState); - if (lifecycleStateManager.isValidStateChange(currentState, nextState, userName, tenantId)) { - this.lifecycleStateDAO - .addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), - tenantId); - } else { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Can't delete the application release, You have to move the " - + "lifecycle state from " + currentState + " to " + nextState); - } - currentState = nextState; - } + + if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getInitialState())) { + String msg = "Application state is not in the initial state: " + lifecycleStateManager.getInitialState() + + ". Therefore you are not permitted to delete the application release."; + log.error(msg); + throw new ForbiddenException(msg); + } + List deviceSubscriptionDTOS = subscriptionDAO + .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId); + for (DeviceSubscriptionDTO deviceSubscriptionDTO : deviceSubscriptionDTOS) { + if (!deviceSubscriptionDTO.isUnsubscribed()) { + String msg = "This application is subscribed to device/s. Therefore you are not permitted to delete " + + "the application release."; + log.error(msg); + throw new ForbiddenException(msg); } - } else { - throw new ApplicationManagementException( - "Can't delete the application release, You have to move the " + "lifecycle state from " - + currentState + " to acceptable " + "state"); } - return applicationRelease.getAppHashValue(); + //todo delete application release data ON delete cascade + applicationStorageManager.deleteApplicationReleaseArtifacts(applicationReleaseDTO.getAppHashValue()); + + // LifecycleStateDTO appLifecycleState = this.lifecycleStateDAO +// .getLatestLifeCycleState(applicationId, releaseUuid); +// if (appLifecycleState == null) { +// throw new NotFoundException( +// "Couldn't find an lifecycle sate for application ID: " + applicationId + " and UUID: " +// + releaseUuid); +// } +// String currentState = appLifecycleState.getCurrentState(); +// if (AppLifecycleState.DEPRECATED.toString().equals(currentState) || AppLifecycleState.REJECTED.toString() +// .equals(currentState) || AppLifecycleState.UNPUBLISHED.toString().equals(currentState)) { +// LifecycleStateDTO newAppLifecycleState = getLifecycleStateInstance(AppLifecycleState.REMOVED.toString(), +// appLifecycleState.getCurrentState()); +// if (lifecycleStateManager.isValidStateChange(newAppLifecycleState.getPreviousState(), +// newAppLifecycleState.getCurrentState(), userName, tenantId)) { +// this.lifecycleStateDAO +// .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), +// tenantId); +// ConnectionManagerUtil.commitDBTransaction(); +// } else { +// List lifecycleFlow = searchLifecycleStateFlow(currentState, +// AppLifecycleState.REMOVED.toString()); +// for (String nextState : lifecycleFlow) { +// LifecycleStateDTO lifecycleState = getLifecycleStateInstance(nextState, currentState); +// if (lifecycleStateManager.isValidStateChange(currentState, nextState, userName, tenantId)) { +// this.lifecycleStateDAO +// .addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), +// tenantId); +// } else { +// ConnectionManagerUtil.rollbackDBTransaction(); +// throw new ApplicationManagementException( +// "Can't delete the application release, You have to move the " +// + "lifecycle state from " + currentState + " to " + nextState); +// } +// currentState = nextState; +// } +// } +// } else { +// throw new ApplicationManagementException( +// "Can't delete the application release, You have to move the " + "lifecycle state from " +// + currentState + " to acceptable " + "state"); +// } +// return applicationReleaseDTO.getAppHashValue(); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementDAOException( - "Error ocured when getting application data or application release data for application id of " - + applicationId + " application release UUID of the " + releaseUuid); - } catch (LifeCycleManagementDAOException e) { + String msg = "Error ocured when getting application data or application release data for application id of " + + applicationId + " application release UUID of the " + releaseUuid; + throw new ApplicationManagementDAOException(msg, e); + } catch (ApplicationStorageManagementException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Error occured when deleting application release for application ID of " + applicationId - + " and application release UUID of " + releaseUuid, e); - } catch (UserStoreException e) { - throw new ApplicationManagementException( - "Error occured when checking permission for executing application release update. ApplicationDTO ID: " - + applicationId + " and ApplicationDTO UUID: " + releaseUuid); + String msg = "Error occured when deleteing the application release artifact from the file system. Application release UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -1149,12 +1358,9 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - //todo with this implementation user has to provide all image artifacts in order to update one image artifact as - // well. Therefore enhance this later. public void updateApplicationImageArtifact(String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); ApplicationReleaseDTO applicationReleaseDTO; try { @@ -1174,8 +1380,6 @@ public class ApplicationManagerImpl implements ApplicationManager { + " and it is not an release updatable state. Hence please move the application release" + " into updatable state and retry the operation."); } - applicationStorageManager.deleteImageArtifacts(applicationReleaseDTO); - applicationReleaseDTO = this.applicationReleaseDAO .updateRelease(addImageArtifacts(applicationReleaseDTO, applicationArtifact), tenantId); if (applicationReleaseDTO == null) { @@ -1212,7 +1416,6 @@ public class ApplicationManagerImpl implements ApplicationManager { public void updateApplicationArtifact(String deviceType, String appType, String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); boolean isValidDeviceType = false; List deviceTypes; try { @@ -1245,10 +1448,8 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new NotFoundException(msg); } - String deletingAppHashValue = applicationReleaseDTO.getAppHashValue(); - applicationReleaseDTO = addApplicationReleaseArtifacts(appType, deviceType, applicationReleaseDTO, + applicationReleaseDTO = updateApplicationReleaseArtifacts(appType, deviceType, applicationReleaseDTO, applicationArtifact); - applicationStorageManager.copyImageArtifactsAndDeleteInstaller(deletingAppHashValue, applicationReleaseDTO); applicationReleaseDTO = this.applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId); if (applicationReleaseDTO == null) { ConnectionManagerUtil.rollbackDBTransaction(); @@ -1683,132 +1884,91 @@ public class ApplicationManagerImpl implements ApplicationManager { return lifecycleState; } - //todo check whether package names are same @Override - public boolean updateRelease(int applicationId, String releaseUuid, String deviceType, - ApplicationReleaseDTO updateRelease, InputStream binaryFileStram, InputStream iconFileStream, - InputStream bannerFileStream, List attachments) throws ApplicationManagementException { + public boolean updateRelease(String deviceType, String applicationType, String releaseUuid, + ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) + throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - ApplicationReleaseDTO release; - ApplicationDTO app = null; - ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager(); - DeviceType deviceTypeObj; - boolean isAdminUser; - try { - // Getting the device type details to get device type ID for internal mappings - deviceTypeObj = Util.getDeviceManagementService().getDeviceType(deviceType); - isAdminUser = isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION); - ConnectionManagerUtil.beginDBTransaction(); - app = this.applicationDAO.getApplicationById(applicationId, tenantId); - release = this.applicationReleaseDAO.getReleaseByIds(applicationId, releaseUuid, tenantId); + ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO + .getReleaseByUUID(releaseUuid, tenantId); - if (app == null) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new NotFoundException( - "Couldn't found an application for updating. ApplicationDTO id: " + applicationId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't found an application release for updating. Application release UUID: " + releaseUuid; + log.error(msg); + throw new NotFoundException(msg); } - if (deviceTypeObj == null || deviceTypeObj.getId() != app.getDeviceTypeId()) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new BadRequestException( - "Request to update application release for Invalid device type. Device type: " + deviceType - + " application ID " + applicationId + " ApplicationDTO Release UUID " + releaseUuid); - } - if (release == null) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new NotFoundException( - "Couldn't found an application realise for updating. ApplicationDTO id: " + applicationId - + " and application release UUID: " + releaseUuid); + if (!lifecycleStateManager.isUpdatableState(applicationReleaseDTO.getCurrentState())) { + String msg = "Application release in " + applicationReleaseDTO.getCurrentState() + + " state. Therefore you are not allowed to update the application release. Hence, " + + "please move application release from " + applicationReleaseDTO.getCurrentState() + + " to updatable state."; + log.error(msg); + throw new ForbiddenException(msg); } - String releaseType = updateRelease.getReleaseType(); - Double price = updateRelease.getPrice(); - String metaData = updateRelease.getMetaData(); + DeviceType deviceTypeObj = getDeviceTypeData(deviceType); + Double price = applicationReleaseWrapper.getPrice(); - if (price < 0.0 || (price == 0.0 && ApplicationSubscriptionType.PAID.toString().equals(app.getSubType())) - || (price > 0.0 && ApplicationSubscriptionType.FREE.toString().equals(app.getSubType()))) { - ConnectionManagerUtil.rollbackDBTransaction(); + String applicationSubType = this.applicationDAO.getApplicationSubTypeByUUID(releaseUuid, tenantId); + if (applicationSubType == null) { + String msg = "Couldn't find an application subscription type for the application release UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg); + } + + if (price < 0.0 || (price == 0.0 && ApplicationSubscriptionType.PAID.toString().equals(applicationSubType)) + || (price > 0.0 && ApplicationSubscriptionType.FREE.toString().equals(applicationSubType))) { throw new BadRequestException( "Invalid app release payload for updating application release. ApplicationDTO price is " + price - + " for " + app.getSubType() + " application. ApplicationDTO ID: " + applicationId - + ", ApplicationDTO Release UUID " + releaseUuid + " and supported device type is " - + deviceType); + + " for " + applicationSubType + " application., Application Release UUID " + + releaseUuid + " and supported device type is " + deviceType); } - release.setPrice(price); - if (releaseType != null) { - release.setReleaseType(releaseType); + applicationReleaseDTO.setPrice(price); + applicationReleaseDTO.setIsSharedWithAllTenants(applicationReleaseDTO.getIsSharedWithAllTenants()); + + String supportedOSVersions = applicationReleaseWrapper.getSupportedOsVersions(); + if (!StringUtils.isEmpty(supportedOSVersions)) { + //todo check OS versions are supported or not + applicationReleaseDTO.setSupportedOsVersions(supportedOSVersions); } - if (metaData != null) { - release.setMetaData(metaData); + if (!StringUtils.isEmpty(applicationReleaseWrapper.getDescription())) { + applicationReleaseDTO.setDescription(applicationReleaseWrapper.getDescription()); } - - List unrestrictedRoles = app.getUnrestrictedRoles(); - - String applicationReleaseCreatedUser = lifecycleStateDAO - .getAppReleaseCreatedUsername(applicationId, releaseUuid, tenantId); - - if (!isAdminUser && !(!unrestrictedRoles.isEmpty() && hasUserRole(unrestrictedRoles, userName)) - && !userName.equals(applicationReleaseCreatedUser)) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ForbiddenException("You are not authorized user to update application"); + if (!StringUtils.isEmpty(applicationReleaseWrapper.getReleaseType())) { + applicationReleaseDTO.setReleaseType(applicationReleaseWrapper.getReleaseType()); } + if (!StringUtils.isEmpty(applicationReleaseWrapper.getMetaData())) { + applicationReleaseDTO.setMetaData(applicationReleaseWrapper.getMetaData()); + } + + applicationReleaseDTO = updateApplicationReleaseArtifacts(applicationType, deviceTypeObj.getName(), + applicationReleaseDTO, applicationArtifact); + applicationReleaseDTO = updateImageArtifacts(applicationReleaseDTO, applicationArtifact); - //todo try to remove this DB call and get it when getting application release - LifecycleStateDTO lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid); - if (!AppLifecycleState.CREATED.toString().equals(lifecycleState.getCurrentState()) - && !AppLifecycleState.IN_REVIEW.toString().equals(lifecycleState.getCurrentState()) - && !AppLifecycleState.REJECTED.toString().equals(lifecycleState.getCurrentState())) { + boolean updateStatus = applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId) != null; + if (!updateStatus) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new ForbiddenException( - "You can't update application release which is in " + lifecycleState.getCurrentState() - + " State"); } - -// release = applicationStorageManager -// .deleteImageArtifacts(release, iconFileStream, bannerFileStream, attachments); -// release = applicationStorageManager -// .copyImageArtifactsAndDeleteInstaller(release, app.getType(), deviceType, binaryFileStram); - return applicationReleaseDAO.updateRelease(release, tenantId) != null; - } catch (DeviceManagementException e) { - throw new ApplicationManagementException("Error occured when validating the device type " + deviceType, e); - } catch (UserStoreException e) { - throw new ApplicationManagementException( - "Error occured while verifying whether user is admin user or not. Username " + userName - + " tenant id " + tenantId, e); - } catch (LifeCycleManagementDAOException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Error Occured when getting lifecycle state of the application release of application UUID: " - + releaseUuid, e); + ConnectionManagerUtil.commitDBTransaction(); + return updateStatus; } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Error occured when updating ApplicationDTO release. ApplicationDTO ID " + applicationId - + " ApplicationDTO Release UUID: " + releaseUuid, e); - } -// catch (ApplicationStorageManagementException e) { -// ConnectionManagerUtil.rollbackDBTransaction(); -// throw new ApplicationManagementException( -// "Error occured when updating application release artifact. ApplicationDTO ID " + applicationId -// + " ApplicationDTO release UUID: " + releaseUuid, e); -// } -// catch (ResourceManagementException e) { -// // updating images -// ConnectionManagerUtil.rollbackDBTransaction(); -// throw new ApplicationManagementException( -// "Error occured when updating image artifact of the application release. ApplicationDTO ID: " -// + applicationId + " ApplicationDTO release UUID: " + releaseUuid, e); -// } catch (RequestValidatingException e) { -// ConnectionManagerUtil.rollbackDBTransaction(); -// throw new ApplicationManagementException( -// "Error occured when validating application release artifact for device type " + deviceType -// + " And application type " + app.getType() + ". Applicationn ID: " + applicationId -// + " ApplicationDTO release UUID: " + releaseUuid); -// } + String msg = "Error occured when updating Application release. ApplicationDTO ID ApplicationDTO Release " + + "UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (ResourceManagementException e) { + String msg = "Error occured when updating application release artifact in the file system. Application " + + "release UUID:" + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } @@ -1986,6 +2146,7 @@ public class ApplicationManagerImpl implements ApplicationManager { String basePath = artifactDownloadEndpoint + Constants.FORWARD_SLASH + applicationReleaseDTO.getUuid(); ApplicationRelease applicationRelease = new ApplicationRelease(); applicationRelease.setDescription(applicationReleaseDTO.getDescription()); + applicationRelease.setVersion(applicationReleaseDTO.getVersion()); applicationRelease.setUuid(applicationReleaseDTO.getUuid()); applicationRelease.setReleaseType(applicationReleaseDTO.getReleaseType()); applicationRelease.setPrice(applicationReleaseDTO.getPrice()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index b695405bc10..053aa3bf73c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -78,7 +78,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager public ApplicationReleaseDTO uploadImageArtifacts(ApplicationReleaseDTO applicationRelease, InputStream iconFileStream, InputStream bannerFileStream, List screenShotStreams) throws ResourceManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String artifactDirectoryPath; String iconStoredLocation; String bannerStoredLocation; @@ -122,10 +121,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } catch (IOException e) { throw new ApplicationStorageManagementException("IO Exception while saving the screens hots for " + "the application " + applicationRelease.getUuid(), e); - } catch (ApplicationStorageManagementException e) { - throw new ApplicationStorageManagementException("ApplicationDTO Management DAO exception while trying to " - + "update the screen-shot count for the application " + applicationRelease.getUuid() + - " for the tenant id " + tenantId, e); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java index 9a0aee2efe3..9ea7d644f7a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java @@ -20,8 +20,10 @@ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.application.mgt.common.State; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; @@ -30,19 +32,26 @@ import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagem import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; +import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; +import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.Constants; import java.io.InputStream; +import java.util.Map; public class AppmDataHandlerImpl implements AppmDataHandler { - private UIConfiguration uiConfiguration; private static final Log log = LogFactory.getLog(AppmDataHandlerImpl.class); + private UIConfiguration uiConfiguration; + private LifecycleStateManager lifecycleStateManager; + public AppmDataHandlerImpl(UIConfiguration config) { this.uiConfiguration = config; + lifecycleStateManager = DataHolder.getInstance().getLifecycleStateManager(); + } @Override @@ -50,6 +59,11 @@ public class AppmDataHandlerImpl implements AppmDataHandler { return this.uiConfiguration; } + @Override + public Map getLifecycleConfiguration() throws LifecycleManagementException { + return lifecycleStateManager.getLifecycleConfig(); + } + @Override public InputStream getArtifactStream(String uuid, String artifactName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java index ecf8280eb02..15810529f9a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ApplicationManagementServiceComponent.java @@ -32,7 +32,7 @@ import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.impl.AppmDataHandlerImpl; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.ndatasource.core.DataSourceService; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java index 03598a7f43f..078fce37e49 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java @@ -19,9 +19,10 @@ package org.wso2.carbon.device.application.mgt.core.lifecycle; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.State; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; -import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException; import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils; import org.wso2.carbon.device.mgt.core.search.mgt.Constants; @@ -62,6 +63,15 @@ public class LifecycleStateManager { } } + public Map getLifecycleConfig() throws LifecycleManagementException { + if (lifecycleStates == null){ + String msg = "Lifecycle configuration in not initialized."; + log.error(msg); + throw new LifecycleManagementException(msg); + } + return lifecycleStates; + } + public Set getNextLifecycleStates(String currentLifecycleState) { return lifecycleStates.get(currentLifecycleState.toUpperCase()).getProceedingStates(); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java index cd6803a1cbf..8d982de6163 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java @@ -21,7 +21,7 @@ import org.junit.Assert; import org.testng.annotations.Test; import org.wso2.carbon.device.application.mgt.core.config.Configuration; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; -import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import java.util.List; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifeCycleStateManagerTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifeCycleStateManagerTest.java index 7dbbfe33043..95069938afc 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifeCycleStateManagerTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifeCycleStateManagerTest.java @@ -1,8 +1,8 @@ package org.wso2.carbon.device.application.mgt.core; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.core.lifecycle.State; -import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.State; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import java.util.HashMap; import java.util.List; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java index 4d610fe62a1..496b2318188 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java @@ -7,7 +7,7 @@ import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManageme import org.wso2.carbon.device.application.mgt.core.config.Configuration; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleState; import java.util.List; import java.util.Set; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java index 5ffaa35752b..47f2f64c1a4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementPublisherAPI.java @@ -80,7 +80,7 @@ import javax.ws.rs.core.Response; name = "Get ApplicationDTO Details", description = "Get application details", key = "perm:app:publisher:view", - permissions = {"/app-mgt/publisher/application/update"} + permissions = {"/app-mgt/publisher/application/view"} ), @Scope( name = "Update an ApplicationDTO", @@ -91,8 +91,7 @@ import javax.ws.rs.core.Response; } ) @Path("/applications") -@Api(value = "ApplicationDTO Management", description = "This API carries all application management related operations " + - "such as get all the applications, add application, etc.") +@Api(value = "ApplicationDTO Management") @Produces(MediaType.APPLICATION_JSON) public interface ApplicationManagementPublisherAPI { @@ -181,6 +180,48 @@ public interface ApplicationManagementPublisherAPI { @QueryParam("state") String state ); + @GET + @Path("/release/{uuid}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get the application release of requesting application UUID and state", + notes = "This will get the application release identified by the application release uuid and state.", + tags = "ApplicationDTO Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved relevant application release.", + response = ApplicationDTO.class), + @ApiResponse( + code = 403, + message = "Don't have permission to access the application release"), + @ApiResponse( + code = 404, + message = "Application release not found"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting relevant application release.", + response = ErrorResponse.class) + }) + Response getApplicationRelease( + @ApiParam( + name = "uuid", + value = "application release uuid", + required = true) + @PathParam("uuid") String uuid + ); + @PUT @Path("/{appId}") @Produces(MediaType.APPLICATION_JSON) @@ -377,7 +418,7 @@ public interface ApplicationManagementPublisherAPI { @DELETE @Consumes("application/json") - @Path("/{appid}") + @Path("/{appId}") @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, @@ -400,14 +441,21 @@ public interface ApplicationManagementPublisherAPI { @ApiResponse( code = 500, message = "Internal Server Error. \n Error occurred while deleting the application.", - response = ErrorResponse.class) + response = ErrorResponse.class), + @ApiResponse( + code = 403, + message = "Don't have permission to delete the application"), + @ApiResponse( + code = 404, + message = "Application not found"), }) + //todo add new scope and permission Response deleteApplication( @ApiParam( name = "UUID", value = "Unique identifier of the ApplicationDTO", required = true) - @PathParam("appid") int applicationId + @PathParam("appId") int applicationId ); @PUT @@ -535,7 +583,7 @@ public interface ApplicationManagementPublisherAPI { ); @PUT - @Path("/{deviceType}/{appId}/{uuid}") + @Path("/app-release/{deviceType}/{appType}/{uuid}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.MULTIPART_FORM_DATA) @ApiOperation( @@ -567,21 +615,56 @@ public interface ApplicationManagementPublisherAPI { response = ErrorResponse.class) }) Response updateApplicationRelease( - @ApiParam(name = "deviceType", value = "Supported device type of the application", required = true) + @ApiParam( + name = "deviceType", + value = "Supported device type of the application", + required = true) @PathParam("deviceType") String deviceType, - @ApiParam(name = "appId", value = "Identifier of the ApplicationDTO", required = true) - @PathParam("appId") int applicationId, - @ApiParam(name = "UUID", value = "Unique identifier of the ApplicationDTO Release", required = true) + @ApiParam( + name = "appType", + value = "Type of the application", + required = true) + @PathParam("appType") String appType, + @ApiParam( + name = "UUID", + value = "Unique identifier of the ApplicationDTO Release", + required = true) @PathParam("uuid") String applicationUUID, - @Multipart(value = "applicationRelease", required = false, type = "application/json") ApplicationReleaseDTO applicationRelease, - @Multipart(value = "binaryFile", required = false) Attachment binaryFile, - @Multipart(value = "icon", required = false) Attachment iconFile, - @Multipart(value = "banner", required = false) Attachment bannerFile, - @ApiParam(name = "screenshot1", value = "Screen Shots of the uploading application", required = true) + @ApiParam( + name = "applicationReleaseWrapper", + value = "Application release wrapper which is going to update.", + required = true) + @Multipart( + value = "applicationReleaseWrapper", + type = "application/json") + ApplicationReleaseWrapper applicationReleaseWrapper, + @ApiParam( + name = "binaryFile", + value = "Application installer file.", + required = true) + @Multipart(value = "binaryFile") Attachment binaryFile, + @ApiParam( + name = "icon", + value = "Icon file of the application release.", + required = true) + @Multipart(value = "icon") Attachment iconFile, + @ApiParam( + name = "banner", + value = "banner file of the application release.", + required = true) + @Multipart(value = "banner") Attachment bannerFile, + @ApiParam( + name = "screenshot1", + value = "First screenshot of the uploading application", + required = true) @Multipart(value = "screenshot1") Attachment screenshot1, - @ApiParam(name = "screenshot2", value = "Screen Shots of the uploading application") + @ApiParam( + name = "screenshot2", + value = "Second screenshot 2 of the uploading application") @Multipart(value = "screenshot2") Attachment screenshot2, - @ApiParam(name = "screenshot3", value = "Screen Shots of the uploading application") + @ApiParam( + name = "screenshot3", + value = "Third screenshot of the uploading application") @Multipart(value = "screenshot3") Attachment screenshot3); @GET diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ApplicationManagementPublisherAdminAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ApplicationManagementPublisherAdminAPI.java new file mode 100644 index 00000000000..2f2979d39ea --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/admin/ApplicationManagementPublisherAdminAPI.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.publisher.api.services.admin; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.Extension; +import io.swagger.annotations.ExtensionProperty; +import io.swagger.annotations.Info; +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Tag; +import org.wso2.carbon.apimgt.annotations.api.Scope; +import org.wso2.carbon.apimgt.annotations.api.Scopes; +import org.wso2.carbon.device.application.mgt.common.ApplicationList; +import org.wso2.carbon.device.application.mgt.common.ErrorResponse; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +/** + * APIs to handle application management related tasks. + */ +@SwaggerDefinition( + info = @Info( + version = "1.0.0", + title = "ApplicationDTO Management Publisher Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = "name", value = "ApplicationManagementPublisherAdminService"), + @ExtensionProperty(name = "context", value = "/api/application-mgt-publisher/v1.0/admin/applications"), + }) + } + ), + tags = { + @Tag(name = "application_management, device_management", description = "App publisher related Admin APIs") + } +) +@Scopes( + scopes = { + @Scope( + name = "Delete Application Release", + description = "Delete Application Release", + key = "perm:admin:app:publisher:update", + permissions = {"/app-mgt/publisher/admin/application/update"} + ) + } +) +@Path("/admin/applications") +@Api(value = "ApplicationDTO Management") +@Produces(MediaType.APPLICATION_JSON) +public interface ApplicationManagementPublisherAdminAPI { + + String SCOPE = "scope"; + + @DELETE + @Path("/release/{appId}/{uuid}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "DELETE", + value = "Delete application release.", + notes = "This will delete application release for given UUID", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:admin:app:publisher:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully delete application release.", + response = ApplicationList.class), + @ApiResponse( + code = 404, + message = "Not Found. There doesn't have an application release for UUID" + + "query."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while deleting application release.", + response = ErrorResponse.class) + }) Response deleteApplicationRelease( + @ApiParam( + name = "appId", + value = "application Id", + required = true) + @PathParam("appId") int applicationId, + @ApiParam( + name = "uuid", + value = "application release UUID", + required = true) + @PathParam("uuid") String releaseUuid); +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java index be37e9e3b2c..a38e0880ba7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java @@ -23,7 +23,6 @@ import org.apache.commons.logging.LogFactory; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Multipart; import org.wso2.carbon.device.application.mgt.common.*; -import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; @@ -85,7 +84,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } return Response.status(Response.Status.OK).entity(applications).build(); } catch(BadRequestException e){ - String msg = "Incompatible request payload is found. Please try with valid reuest payload."; + String msg = "Incompatible request payload is found. Please try with valid request payload."; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); }catch (ApplicationManagementException e) { @@ -126,6 +125,37 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } + @GET + @Consumes("application/json") + @Path("/release/{uuid}") + public Response getApplicationRelease( + @PathParam("uuid") String uuid) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + ApplicationRelease applicationRelease = applicationManager.getApplicationReleaseByUUID(uuid); + if (applicationRelease == null){ + String msg = "Application release is in the end state of the application lifecycle flow."; + log.error(msg); + return Response.status(Response.Status.OK).entity(msg).build(); + } + return Response.status(Response.Status.OK).entity(applicationRelease).build(); + } catch (NotFoundException e) { + String msg = "Application Release with UUID: " + uuid + " is not found"; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch(ForbiddenException e){ + String msg = "You don't have permission to access the application release. application release UUID: : " + + uuid; + log.error(msg); + return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); + } + catch (ApplicationManagementException e) { + String msg = "Error occurred while getting application release for UUID: " + uuid; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + @POST @Consumes("multipart/mixed") public Response createApplication( @@ -162,7 +192,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem if (application != null) { return Response.status(Response.Status.CREATED).entity(application).build(); } else { - String msg = "ApplicationDTO creation is failed"; + String msg = "Application creation is failed"; log.error(msg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } @@ -286,8 +316,6 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Multipart("binaryFile") Attachment binaryFile) { try { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - if (!ApplicationType.ENTERPRISE.toString().equals(appType)) { String msg = "If ApplicationDTO type is " + appType + ", therefore you don't have application release artifact to update for application release UUID: " @@ -295,6 +323,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem log.error(msg); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } + ApplicationManager applicationManager = APIUtil.getApplicationManager(); applicationManager.validateBinaryArtifact(binaryFile, appType); applicationManager.updateApplicationArtifact(deviceType, appType, applicationReleaseUuid, constructApplicationArtifact(binaryFile, null, null, null)); @@ -341,18 +370,14 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } - /* - //todo ---------------------- - */ - @Override @PUT - @Path("/{deviceType}/{appId}/{uuid}") + @Path("/app-release/{deviceType}/{appType}/{uuid}") public Response updateApplicationRelease( @PathParam("deviceType") String deviceType, - @PathParam("appId") int applicationId, + @PathParam("appType") String appType, @PathParam("uuid") String applicationUUID, - @Multipart("applicationRelease") ApplicationReleaseDTO applicationRelease, + @Multipart("applicationRelease") ApplicationReleaseWrapper applicationReleaseWrapper, @Multipart("binaryFile") Attachment binaryFile, @Multipart("icon") Attachment iconFile, @Multipart("banner") Attachment bannerFile, @@ -360,50 +385,35 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem @Multipart("screenshot2") Attachment screenshot2, @Multipart("screenshot3") Attachment screenshot3) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - InputStream iconFileStream; - InputStream bannerFileStream; - InputStream binaryFileStram; - List attachments = new ArrayList<>(); - List attachmentList = new ArrayList<>(); + List screenshots = new ArrayList<>(); if (screenshot1 != null){ - attachmentList.add(screenshot1); + screenshots.add(screenshot1); } if (screenshot2 != null) { - attachmentList.add(screenshot2); + screenshots.add(screenshot2); } if (screenshot3 != null) { - attachmentList.add(screenshot3); + screenshots.add(screenshot3); } - if (iconFile == null || bannerFile == null || binaryFile == null || attachmentList.isEmpty()){ - String msg = "Invalid data is received for application release updating. application id: " + applicationId - + " and application release UUID: " + applicationUUID; - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); - } - try { - binaryFileStram = binaryFile.getDataHandler().getInputStream(); - iconFileStream = iconFile.getDataHandler().getInputStream(); - bannerFileStream = bannerFile.getDataHandler().getInputStream(); - for (Attachment screenshot : attachmentList) { - attachments.add(screenshot.getDataHandler().getInputStream()); - } - boolean status = applicationManager - .updateRelease(applicationId, applicationUUID, deviceType, applicationRelease, binaryFileStram, - iconFileStream, bannerFileStream, attachments); - if (!status){ - log.error("ApplicationDTO release updating is failed. Please contact the administrator. ApplicationDTO id: " - + applicationId + ", ApplicationDTO release UUID: " + applicationUUID + ", Supported device type: " - + deviceType); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(applicationRelease).build(); + applicationManager.validateBinaryArtifact(binaryFile, appType); + applicationManager.validateImageArtifacts(iconFile, bannerFile, screenshots); + if (!applicationManager.updateRelease(deviceType, appType, applicationUUID, applicationReleaseWrapper, + constructApplicationArtifact(binaryFile, iconFile, bannerFile, screenshots))) { + String msg ="Application release updating is failed. Please contact the administrator. " + + "ApplicationDTO release UUID: " + applicationUUID + ", Supported device type: " + deviceType; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - return Response.status(Response.Status.OK).entity("ApplicationDTO release is successfully updated.").build(); - } catch(BadRequestException e){ - String msg = "Invalid request to update application release for application release UUID " + applicationUUID; + return Response.status(Response.Status.OK).entity("Application release is successfully updated.").build(); + } catch (BadRequestException e) { + String msg = + "Invalid request to update application release for application release UUID " + applicationUUID; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (NotFoundException e) { - String msg = "Couldn't found application or application release for application id: " + applicationId - + " and application release UUID " + applicationUUID; + String msg = + "Couldn't found application or application release for application release UUID " + applicationUUID; log.error(msg, e); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } catch (ForbiddenException e) { @@ -411,29 +421,26 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem + applicationUUID; log.error(msg, e); return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); - } - catch (ApplicationManagementException e) { + } catch (ApplicationManagementException e) { String msg = "Error while updating the application release of the application with UUID " + applicationUUID; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (IOException e) { - String msg = "Error while updating the release artifacts of the application with UUID " + applicationUUID; - log.error(msg, e); + } catch (RequestValidatingException e) { + String msg = "Error occurred while updating the application release in the file system"; + log.error(msg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } + @DELETE - @Path("/{appid}") - public Response deleteApplication( - @PathParam("appid") int applicationId) { + @Path("/{appId}") + public Response deleteApplication(@PathParam("appId") int applicationId) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { - List storedLocations = applicationManager.deleteApplication(applicationId); - applicationStorageManager.deleteAllApplicationReleaseArtifacts(storedLocations); - String responseMsg = "Successfully deleted the application and application releases: " + applicationId; - return Response.status(Response.Status.OK).entity(responseMsg).build(); + applicationManager.deleteApplication(applicationId); + return Response.status(Response.Status.OK) + .entity("Successfully deleted the application for application ID: " + applicationId).build(); } catch (NotFoundException e) { String msg = "Couldn't found application for application id: " + applicationId + " to delete the application"; @@ -447,47 +454,13 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem String msg = "Error occurred while deleting the application: " + applicationId; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (ApplicationStorageManagementException e) { - String msg = "Error occurred while deleting the application storage: " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } - @DELETE - @Path("/{appid}/{uuid}") - public Response deleteApplicationRelease( - @PathParam("appid") int applicationId, - @PathParam("uuid") String releaseUuid) { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); - try { - String storedLocation = applicationManager.deleteApplicationRelease(applicationId, releaseUuid); - applicationStorageManager.deleteApplicationReleaseArtifacts(storedLocation); - String responseMsg = "Successfully deleted the application release of: " + applicationId + ""; - return Response.status(Response.Status.OK).entity(responseMsg).build(); - } catch (NotFoundException e) { - String msg = "Couldn't found application release which is having application id: " + applicationId - + " and application release UUID:" + releaseUuid; - log.error(msg, e); - return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); - } catch (ForbiddenException e) { - String msg = - "You don't have require permission to delete the application release which has UUID " + releaseUuid - + " and application ID " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); - }catch (ApplicationManagementException e) { - String msg = "Error occurred while deleting the application: " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (ApplicationStorageManagementException e) { - String msg = "Error occurred while deleting the application storage: " + applicationId; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } + /* +//todo ---------------------- +*/ @GET @Path("/lifecycle/{appId}/{uuid}") public Response getLifecycleState( diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java new file mode 100644 index 00000000000..56270db7e96 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/admin/ApplicationManagementPublisherAdminAPIImpl.java @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.publisher.api.services.impl.admin; + +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 org.wso2.carbon.device.application.mgt.common.ApplicationArtifact; +import org.wso2.carbon.device.application.mgt.common.ApplicationList; +import org.wso2.carbon.device.application.mgt.common.ApplicationType; +import org.wso2.carbon.device.application.mgt.common.Filter; +import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; +import org.wso2.carbon.device.application.mgt.common.response.Application; +import org.wso2.carbon.device.application.mgt.common.response.ApplicationRelease; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; +import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseWrapper; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationUpdateWrapper; +import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper; +import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; +import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; +import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; +import org.wso2.carbon.device.application.mgt.core.util.APIUtil; +import org.wso2.carbon.device.application.mgt.publisher.api.services.ApplicationManagementPublisherAPI; +import org.wso2.carbon.device.application.mgt.publisher.api.services.admin.ApplicationManagementPublisherAdminAPI; + +import javax.activation.DataHandler; +import javax.validation.Valid; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Implementation of Application Management related APIs. + */ +@Produces({"application/json"}) +@Path("/applications") +public class ApplicationManagementPublisherAdminAPIImpl implements ApplicationManagementPublisherAdminAPI { + + private static Log log = LogFactory.getLog(ApplicationManagementPublisherAdminAPIImpl.class); + + @DELETE + @Path("/release/{appId}/{uuid}") + public Response deleteApplicationRelease( + @PathParam("appId") int applicationId, + @PathParam("uuid") String releaseUuid) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + applicationManager.deleteApplicationRelease(applicationId, releaseUuid); + String responseMsg = "Successfully deleted the application release for uuid: " + releaseUuid + ""; + return Response.status(Response.Status.OK).entity(responseMsg).build(); + } catch (NotFoundException e) { + String msg = "Couldn't found application release which is having application id: " + applicationId + + " and application release UUID:" + releaseUuid; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch (ForbiddenException e) { + String msg = + "You don't have require permission to delete the application release which has UUID " + releaseUuid + + " and application ID " + applicationId; + log.error(msg, e); + return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); + }catch (ApplicationManagementException e) { + String msg = "Error occurred while deleting the application: " + applicationId; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + +}