From 1f25e7af91379448f1a7fa536043a95b936149af Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Mon, 15 Feb 2021 22:11:05 +0530 Subject: [PATCH] Implement SMS configuration API to update changes of the sms-config file --- .../sms-mgt/io.entgra.sms.mgt.api/pom.xml | 224 ++++++++++++++++++ .../ConfigurationManagementService.java | 118 +++++++++ .../api/service/addons/ApiOriginFilter.java | 48 ++++ .../addons/GsonMessageBodyHandler.java | 85 +++++++ .../ConfigurationManagementServiceImpl.java | 52 ++++ .../webapp/META-INF/webapp-classloading.xml | 35 +++ .../src/main/webapp/WEB-INF/cxf-servlet.xml | 55 +++++ .../src/main/webapp/WEB-INF/web.xml | 88 +++++++ 8 files changed, 705 insertions(+) create mode 100644 components/sms-mgt/io.entgra.sms.mgt.api/pom.xml create mode 100644 components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/ConfigurationManagementService.java create mode 100644 components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/addons/ApiOriginFilter.java create mode 100644 components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/addons/GsonMessageBodyHandler.java create mode 100644 components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/impl/ConfigurationManagementServiceImpl.java create mode 100644 components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml create mode 100644 components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml create mode 100644 components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/WEB-INF/web.xml diff --git a/components/sms-mgt/io.entgra.sms.mgt.api/pom.xml b/components/sms-mgt/io.entgra.sms.mgt.api/pom.xml new file mode 100644 index 0000000000..587153c06d --- /dev/null +++ b/components/sms-mgt/io.entgra.sms.mgt.api/pom.xml @@ -0,0 +1,224 @@ + + + + + + + org.wso2.carbon.devicemgt + sms-mgt + 4.1.12-SNAPSHOT + ../pom.xml + + + 4.0.0 + io.entgra.sms.mgt.api + war + Entgra IoT - SMS Management Core + Entgra IoT - SMS Management API + http://entgra.io + + + + + maven-compiler-plugin + + 1.8 + 1.8 + + + + maven-war-plugin + + WEB-INF/lib/*cxf*.jar + api#sms-mgt#v1.0 + + + + org.jacoco + jacoco-maven-plugin + + ${basedir}/target/coverage-reports/jacoco-unit.exec + + + + jacoco-initialize + + prepare-agent + + + + jacoco-site + test + + report + + + ${basedir}/target/coverage-reports/jacoco-unit.exec + ${basedir}/target/coverage-reports/site + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + file:src/test/resources/log4j.properties + + + src/test/resources/testng.xml + + + + + + + + + deploy + + compile + + + org.apache.maven.plugins + maven-antrun-plugin + 1.7 + + + compile + + run + + + + + + + + + + + + + + + + + + client + + test + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + test + + java + + + + + + + + + + + + org.apache.cxf + cxf-rt-frontend-jaxws + provided + + + org.apache.cxf + cxf-rt-frontend-jaxrs + provided + + + org.apache.cxf + cxf-rt-transports-http + provided + + + javax.servlet + javax.servlet-api + provided + + + javax.ws.rs + javax.ws.rs-api + provided + + + io.swagger + swagger-annotations + + + io.swagger + swagger-jaxrs + + + com.fasterxml.jackson.module + jackson-module-jaxb-annotations + + + org.slf4j + slf4j-api + + + org.wso2.orbit.com.fasterxml.jackson.core + jackson-core + + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.apimgt.annotations + provided + + + org.wso2.carbon + org.wso2.carbon.logging + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.common + provided + + + org.wso2.carbon.devicemgt + io.entgra.sms.mgt.core + provided + + + org.wso2.carbon.devicemgt + io.entgra.sms.mgt.common + provided + + + + diff --git a/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/ConfigurationManagementService.java b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/ConfigurationManagementService.java new file mode 100644 index 0000000000..d987b8149c --- /dev/null +++ b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/ConfigurationManagementService.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2021, 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 io.entgra.sms.mgt.api.service; + +import io.entgra.sms.mgt.common.SMSMgtConstants; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +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.ResponseHeader; +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 javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@SwaggerDefinition( + info = @Info( + version = "1.0.0", + title = "", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = "name", value = "ConfigurationManagementService"), + @ExtensionProperty(name = "context", value = "/api/sms-mgt/v1.0/configuration"), + }) + } + ), + tags = { + @Tag(name = "sms_management", description = "") + } +) +@Path("/configuration") +@Api(value = "Configuration Management", description = "The general SMS configuration management capabilities " + + "are exposed through this API.") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +@Scopes(scopes = { + @Scope( + name = "View configurations", + description = "", + key = "perm:sms-mgt:view-configuration", + permissions = {"/sms-mgt/platform-configurations/view"} + ) +}) +public interface ConfigurationManagementService { + + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Reload SMS Configuration", + notes = "Reload SMS Configuration in sms-mgt.xml file", + tags = "SMS Configuration Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SMSMgtConstants.SCOPE, value = "perm:sms-mgt:view-configuration") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully reloaded SMS configurations.", + responseContainer = "List", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + } + ), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client already has the latest " + + "version of the requested resource."), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the general " + + "platform configurations.") + }) + Response reloadConfiguration(); +} diff --git a/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/addons/ApiOriginFilter.java b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/addons/ApiOriginFilter.java new file mode 100644 index 0000000000..461a1574ef --- /dev/null +++ b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/addons/ApiOriginFilter.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021, 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 io.entgra.sms.mgt.api.service.addons; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class ApiOriginFilter implements Filter { + + public void doFilter(ServletRequest request, ServletResponse response, + FilterChain chain) throws IOException, ServletException { + HttpServletResponse res = (HttpServletResponse) response; + res.addHeader("Access-Control-Allow-Origin", "*"); + res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); + res.addHeader("Access-Control-Allow-Headers", "Content-Type"); + chain.doFilter(request, response); + } + + public void destroy() { + //do nothing + } + + public void init(FilterConfig filterConfig) throws ServletException { + //do nothing + } +} diff --git a/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/addons/GsonMessageBodyHandler.java b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/addons/GsonMessageBodyHandler.java new file mode 100644 index 0000000000..6a6a640b36 --- /dev/null +++ b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/addons/GsonMessageBodyHandler.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2021, 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 io.entgra.sms.mgt.api.service.addons; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +public class GsonMessageBodyHandler { + + public static final String DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z"; + private Gson gson; + private static final String UTF_8 = "UTF-8"; + + public boolean isReadable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType) { + return true; + } + + private Gson getGson() { + if (gson == null) { + final GsonBuilder gsonBuilder = new GsonBuilder(); + gson = gsonBuilder.setDateFormat(DATE_FORMAT).create(); + } + return gson; + } + + public Object readFrom(Class objectClass, Type type, Annotation[] annotations, MediaType mediaType, + MultivaluedMap stringStringMultivaluedMap, InputStream entityStream) + throws IOException, WebApplicationException { + + InputStreamReader reader = new InputStreamReader(entityStream, "UTF-8"); + + try { + return getGson().fromJson(reader, type); + } finally { + reader.close(); + } + } + + public boolean isWriteable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType) { + return true; + } + + public long getSize(Object o, Class aClass, Type type, Annotation[] annotations, MediaType mediaType) { + return -1; + } + + public void writeTo(Object object, Class aClass, Type type, Annotation[] annotations, MediaType mediaType, + MultivaluedMap stringObjectMultivaluedMap, OutputStream entityStream) + throws IOException, WebApplicationException { + + OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8); + try { + getGson().toJson(object, type, writer); + } finally { + writer.close(); + } + } +} diff --git a/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/impl/ConfigurationManagementServiceImpl.java b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/impl/ConfigurationManagementServiceImpl.java new file mode 100644 index 0000000000..38b46fba87 --- /dev/null +++ b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/java/io/entgra/sms/mgt/api/service/impl/ConfigurationManagementServiceImpl.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021, 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 io.entgra.sms.mgt.api.service.impl; + +import io.entgra.sms.mgt.api.service.ConfigurationManagementService; +import io.entgra.sms.mgt.core.config.SMSConfigurationManager; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; + +@Path("/configuration") +public class ConfigurationManagementServiceImpl implements ConfigurationManagementService { + + private static final Log log = LogFactory.getLog(ConfigurationManagementServiceImpl.class); + + @Path("/reload") + @GET + @Override + public Response reloadConfiguration() { + if (log.isDebugEnabled()) { + log.debug("Reloading SMS Configuration in file sms-config.xml"); + } + try { + SMSConfigurationManager.getInstance().initConfig(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while reloading SMS Configuration"; + log.error(msg, e); + return Response.serverError().entity(msg).build(); + } + return Response.ok().entity("Successfully reloaded SMS Configuration in file sms-config.xml ").build(); + } +} diff --git a/components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml new file mode 100644 index 0000000000..d0d7a01621 --- /dev/null +++ b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml @@ -0,0 +1,35 @@ + + + + + + + + + false + + + CXF,Carbon + diff --git a/components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml new file mode 100644 index 0000000000..5fb71d8484 --- /dev/null +++ b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/WEB-INF/web.xml b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..dda9411ac4 --- /dev/null +++ b/components/sms-mgt/io.entgra.sms.mgt.api/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,88 @@ + + + + Admin-Webapp + + JAX-WS/JAX-RS Device Management Endpoint + JAX-WS/JAX-RS Servlet + CXFServlet + + org.apache.cxf.transport.servlet.CXFServlet + + + + swagger.security.filter + ApiAuthorizationFilterImpl + + 1 + + + CXFServlet + /* + + + 60 + + + + doAuthentication + true + + + + nonSecuredEndPoints + + + + + + managed-api-enabled + true + + + managed-api-owner + admin + + + isSharedWithAllTenants + true + + + + + + SMSMgt-Admin + /* + + + CONFIDENTIAL + + + + + ApiOriginFilter + io.entgra.sms.mgt.api.service.addons.ApiOriginFilter + + + + ApiOriginFilter + /* + + +