diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index ea493d6fa71..8f281832edc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -75,7 +75,10 @@ org.w3c.dom, org.wso2.carbon.governance.api.exception, org.wso2.carbon.governance.api.generic, - org.wso2.carbon.governance.api.generic.dataobjects + org.wso2.carbon.governance.api.generic.dataobjects, + org.wso2.carbon.apimgt.api, + org.wso2.carbon.apimgt.api.model, + org.wso2.carbon.apimgt.impl !org.wso2.carbon.device.mgt.core.internal, @@ -176,6 +179,14 @@ org.apache.ws.commons.axiom.wso2 axiom + + org.wso2.carbon.apimgt + org.wso2.carbon.apimgt.api + + + org.wso2.carbon.apimgt + org.wso2.carbon.apimgt.impl + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIConfig.java new file mode 100644 index 00000000000..faf50e85e78 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIConfig.java @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.core.api.mgt; + +import org.wso2.carbon.apimgt.api.APIProvider; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlTransient; + +public class APIConfig { + + private String name; + private String owner; + private String context; + private String endpoint; + private String version; + private String transports; + private APIProvider provider; + + public void init(APIProvider provider) { + this.provider = provider; + } + + @XmlTransient + public APIProvider getProvider() { + return provider; + } + + @XmlElement(name = "Name", nillable = false) + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlElement(name = "Owner", nillable = false) + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + @XmlElement(name = "Context", nillable = false) + public String getContext() { + return context; + } + + public void setContext(String context) { + this.context = context; + } + + @XmlElement(name = "Endpoint", nillable = false) + public String getEndpoint() { + return endpoint; + } + + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + + @XmlElement(name = "Version", nillable = false) + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + @XmlElement(name = "Transports", nillable = false) + public String getTransports() { + return transports; + } + + public void setTransports(String transports) { + this.transports = transports; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIPublisherService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIPublisherService.java new file mode 100644 index 00000000000..908a2ae32c4 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIPublisherService.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.core.api.mgt; + +import org.wso2.carbon.apimgt.api.APIManagementException; +import org.wso2.carbon.apimgt.api.model.API; +import org.wso2.carbon.apimgt.api.model.APIIdentifier; + +import java.util.List; + +public interface APIPublisherService { + + void publishAPI(API api) throws APIManagementException; + + void removeAPI(APIIdentifier id) throws APIManagementException; + + void publishAPIs(List apis) throws APIManagementException; + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIPublisherServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIPublisherServiceImpl.java new file mode 100644 index 00000000000..eae11d973f2 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIPublisherServiceImpl.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.core.api.mgt; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.apimgt.api.APIManagementException; +import org.wso2.carbon.apimgt.api.APIProvider; +import org.wso2.carbon.apimgt.api.model.API; +import org.wso2.carbon.apimgt.api.model.APIIdentifier; +import org.wso2.carbon.apimgt.impl.APIManagerFactory; + +import java.util.List; + +public class APIPublisherServiceImpl implements APIPublisherService { + + private static final Log log = LogFactory.getLog(APIPublisherServiceImpl.class); + + @Override + public void publishAPI(API api) throws APIManagementException { + if (log.isDebugEnabled()) { + log.debug("Publishing API '" + api.getId() + "'"); + } + APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(api.getApiOwner()); + provider.addAPI(api); + if (log.isDebugEnabled()) { + log.debug("Successfully published API '" + api.getId() + "' with the context '" + + api.getContext() + "'"); + } + } + + @Override + public void removeAPI(APIIdentifier id) throws APIManagementException { + if (log.isDebugEnabled()) { + log.debug("Removing API '" + id.getApiName() + "'"); + } + APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(id.getProviderName()); + provider.deleteAPI(id); + if (log.isDebugEnabled()) { + log.debug("API '" + id.getApiName() + "' has been successfully removed"); + } + } + + @Override + public void publishAPIs(List apis) throws APIManagementException { + if (log.isDebugEnabled()) { + log.debug("Publishing a batch of APIs"); + } + for (API api : apis) { + this.publishAPI(api); + } + if (log.isDebugEnabled()) { + log.debug("Successfully published the batch of APIs"); + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIRegistrationStartupObserver.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIRegistrationStartupObserver.java new file mode 100644 index 00000000000..9bce7ef4273 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIRegistrationStartupObserver.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.core.api.mgt; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.apimgt.api.APIManagementException; +import org.wso2.carbon.apimgt.api.APIProvider; +import org.wso2.carbon.apimgt.impl.APIManagerFactory; +import org.wso2.carbon.core.ServerStartupObserver; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.api.mgt.config.APIPublisherConfig; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; + +import java.util.List; + +public class APIRegistrationStartupObserver implements ServerStartupObserver { + + private static final Log log = LogFactory.getLog(APIRegistrationStartupObserver.class); + + @Override + public void completingServerStartup() { + + } + + @Override + public void completedServerStartup() { + try { + this.initAPIConfigs(); + /* Publish all mobile device management related JAX-RS services as APIs */ + this.publishAPIs(); + } catch (DeviceManagementException e) { + log.error("Error occurred while publishing Mobile Device Management related APIs", e); + } + } + + private void initAPIConfigs() throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Initializing Mobile Device Management related APIs"); + } + List apiConfigs = APIPublisherConfig.getInstance().getApiConfigs(); + for (APIConfig apiConfig : apiConfigs) { + try { + APIProvider provider = + APIManagerFactory.getInstance().getAPIProvider(apiConfig.getOwner()); + apiConfig.init(provider); + } catch (APIManagementException e) { + throw new DeviceManagementException("Error occurred while initializing API Config '" + + apiConfig.getName() + "'", e); + } + } + } + + private void publishAPIs() throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Publishing Mobile Device Management related APIs"); + } + List apiConfigs = APIPublisherConfig.getInstance().getApiConfigs(); + for (APIConfig apiConfig : apiConfigs) { + DeviceManagerUtil.publishAPI(apiConfig); + if (log.isDebugEnabled()) { + log.debug("Successfully published API '" + apiConfig.getName() + "' with the context '" + + apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'"); + } + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/config/APIPublisherConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/config/APIPublisherConfig.java new file mode 100644 index 00000000000..96f220fc70e --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/config/APIPublisherConfig.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.core.api.mgt.config; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Document; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.api.mgt.APIConfig; +import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import org.wso2.carbon.utils.CarbonUtils; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import java.io.File; +import java.util.List; + +@XmlRootElement(name = "APIPublisherConfig") +public class APIPublisherConfig { + + private List apiConfigs; + private static APIPublisherConfig config; + + private static final Log log = LogFactory.getLog(APIPublisherConfig.class); + private static final String USER_DEFINED_API_CONFIG_PATH = + CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "user-api-publisher-config.xml"; + + private static final Object LOCK = new Object(); + + public static APIPublisherConfig getInstance() { + if (config == null) { + synchronized (LOCK) { + try { + init(); + } catch (DeviceManagementException e) { + log.error("Error occurred while initializing API Publisher Config", e); + } + } + } + return config; + } + + @XmlElementWrapper(name = "APIs", required = true) + @XmlElement(name = "API", required = true) + public List getApiConfigs() { + return apiConfigs; + } + + public void setApiConfig(List apiConfigs) { + this.apiConfigs = apiConfigs; + } + + private static void init() throws DeviceManagementException { + try { + File publisherConfig = new File(APIPublisherConfig.USER_DEFINED_API_CONFIG_PATH); + Document doc = DeviceManagerUtil.convertToDocument(publisherConfig); + + /* Un-marshaling Device Management configuration */ + JAXBContext ctx = JAXBContext.newInstance(DeviceManagementConfig.class); + Unmarshaller unmarshaller = ctx.createUnmarshaller(); + config = (APIPublisherConfig) unmarshaller.unmarshal(doc); + } catch (JAXBException e) { + throw new DeviceManagementException("Error occurred while un-marshalling API Publisher Config", e); + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java index 9eb45bde55a..38a99f45a20 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java @@ -59,7 +59,6 @@ public class DeviceConfigurationManager { public synchronized void initConfig() throws DeviceManagementException { - //catch generic exception.if any exception occurs wrap and throw DeviceManagementException try { File deviceMgtConfig = new File(deviceMgtConfigXMLPath); Document doc = DeviceManagerUtil.convertToDocument(deviceMgtConfig); @@ -68,8 +67,8 @@ public class DeviceConfigurationManager { JAXBContext cdmContext = JAXBContext.newInstance(DeviceManagementConfig.class); Unmarshaller unmarshaller = cdmContext.createUnmarshaller(); this.currentDeviceConfig = (DeviceManagementConfig) unmarshaller.unmarshal(doc); - } catch (JAXBException jaxbEx) { - throw new DeviceManagementException("Error occurred while initializing Data Source config", jaxbEx); + } catch (JAXBException e) { + throw new DeviceManagementException("Error occurred while initializing Data Source config", e); } try { @@ -80,9 +79,8 @@ public class DeviceConfigurationManager { JAXBContext notificationContext = JAXBContext.newInstance(NotificationMessagesConfig.class); Unmarshaller unmarshaller = notificationContext.createUnmarshaller(); this.notificationMessagesConfig = (NotificationMessagesConfig) unmarshaller.unmarshal(doc); - }catch(JAXBException jaxbEx){ - throw new DeviceManagementException("Error occurred while initializing Notification settings config", - jaxbEx); + } catch(JAXBException e){ + throw new DeviceManagementException("Error occurred while initializing Notification settings config", e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index 5a0f9965830..813a3e5a7a0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.internal; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; +import org.wso2.carbon.device.mgt.core.api.mgt.APIPublisherService; import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.registry.core.service.RegistryService; @@ -34,6 +35,7 @@ public class DeviceManagementDataHolder { private LicenseManager licenseManager; private RegistryService registryService; private LicenseConfig licenseConfig; + private APIPublisherService apiPublisherService; private static DeviceManagementDataHolder thisInstance = new DeviceManagementDataHolder(); @@ -96,4 +98,12 @@ public class DeviceManagementDataHolder { this.licenseConfig = licenseConfig; } + public APIPublisherService getApiPublisherService() { + return apiPublisherService; + } + + public void setApiPublisherService(APIPublisherService apiPublisherService) { + this.apiPublisherService = apiPublisherService; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 69f7f7c27d0..1a83004c299 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -21,6 +21,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService; +import org.wso2.carbon.core.ServerStartupObserver; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; @@ -29,6 +31,9 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManager; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementRepository; import org.wso2.carbon.device.mgt.core.DeviceManagementServiceProviderImpl; +import org.wso2.carbon.device.mgt.core.api.mgt.APIPublisherService; +import org.wso2.carbon.device.mgt.core.api.mgt.APIPublisherServiceImpl; +import org.wso2.carbon.device.mgt.core.api.mgt.APIRegistrationStartupObserver; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; @@ -63,6 +68,12 @@ import org.wso2.carbon.user.core.service.RealmService; * policy="dynamic" * bind="setRegistryService" * unbind="unsetRegistryService" + * @scr.reference name="api.manager.config.service" + * interface="org.wso2.carbon.apimgt.impl.APIManagerConfigurationService" + * cardinality="1..1" + * policy="dynamic" + * bind="setAPIManagerConfigurationService" + * unbind="unsetAPIManagerConfigurationService" */ public class DeviceManagementServiceComponent { @@ -129,6 +140,12 @@ public class DeviceManagementServiceComponent { BundleContext bundleContext = componentContext.getBundleContext(); bundleContext.registerService(DeviceManagementService.class.getName(), new DeviceManagementServiceImpl(), null); + + APIPublisherService publisher = new APIPublisherServiceImpl(); + DeviceManagementDataHolder.getInstance().setApiPublisherService(publisher); + bundleContext.registerService(APIPublisherService.class, publisher, null); + + bundleContext.registerService(ServerStartupObserver.class, new APIRegistrationStartupObserver(), null); } private void setupDeviceManagementSchema(DataSourceConfig config) @@ -246,4 +263,12 @@ public class DeviceManagementServiceComponent { return pluginRepository; } + protected void setAPIManagerConfigurationService(APIManagerConfigurationService service) { + //do nothing + } + + protected void unsetAPIManagerConfigurationService(APIManagerConfigurationService service) { + //do nothing + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index a619a3e4e33..5595b1c953d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -20,9 +20,17 @@ package org.wso2.carbon.device.mgt.core.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; +import org.wso2.carbon.apimgt.api.APIManagementException; +import org.wso2.carbon.apimgt.api.APIProvider; +import org.wso2.carbon.apimgt.api.model.API; +import org.wso2.carbon.apimgt.api.model.APIIdentifier; +import org.wso2.carbon.apimgt.api.model.APIStatus; +import org.wso2.carbon.apimgt.api.model.URITemplate; +import org.wso2.carbon.apimgt.impl.APIConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.api.mgt.APIConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; @@ -35,15 +43,27 @@ import javax.sql.DataSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; -import java.util.HashMap; -import java.util.Hashtable; -import java.util.List; -import java.util.Map; +import java.util.*; public final class DeviceManagerUtil { private static final Log log = LogFactory.getLog(DeviceManagerUtil.class); + enum HTTPMethod { + GET, POST, DELETE, PUT, OPTIONS + } + + private static List httpMethods; + + static { + httpMethods = new ArrayList(); + httpMethods.add(HTTPMethod.GET); + httpMethods.add(HTTPMethod.POST); + httpMethods.add(HTTPMethod.DELETE); + httpMethods.add(HTTPMethod.PUT); + httpMethods.add(HTTPMethod.OPTIONS); + } + public static Document convertToDocument(File file) throws DeviceManagementException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); @@ -147,4 +167,63 @@ public final class DeviceManagerUtil { return ctx.getTenantId(); } + public static void publishAPI(APIConfig config) throws DeviceManagementException { + APIProvider provider = config.getProvider(); + APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion()); + API api = new API(id); + try { + api.setContext(config.getContext()); + api.setUrl(config.getVersion()); + api.setUriTemplates(getURITemplates(config.getEndpoint(), + APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN)); + api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY); + api.addAvailableTiers(provider.getTiers()); + api.setEndpointSecured(false); + api.setStatus(APIStatus.PUBLISHED); + api.setTransports(config.getTransports()); + + provider.addAPI(api); + } catch (APIManagementException e) { + throw new DeviceManagementException("Error occurred while registering the API", e); + } + } + + public static void removeAPI(APIConfig config) throws DeviceManagementException { + try { + APIProvider provider = config.getProvider(); + APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion()); + provider.deleteAPI(id); + } catch (APIManagementException e) { + throw new DeviceManagementException("Error occurred while removing API", e); + } + } + + private static Set getURITemplates(String endpoint, String authType) { + Set uriTemplates = new LinkedHashSet(); + if (APIConstants.AUTH_NO_AUTHENTICATION.equals(authType)) { + for (HTTPMethod method : httpMethods) { + URITemplate template = new URITemplate(); + template.setAuthType(APIConstants.AUTH_NO_AUTHENTICATION); + template.setHTTPVerb(method.toString()); + template.setResourceURI(endpoint); + template.setUriTemplate("/*"); + uriTemplates.add(template); + } + } else { + for (HTTPMethod method : httpMethods) { + URITemplate template = new URITemplate(); + if (HTTPMethod.OPTIONS.equals(method)) { + template.setAuthType(APIConstants.AUTH_NO_AUTHENTICATION); + } else { + template.setAuthType(APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN); + } + template.setHTTPVerb(method.toString()); + template.setResourceURI(endpoint); + template.setUriTemplate("/*"); + uriTemplates.add(template); + } + } + return uriTemplates; + } + } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml index 331d2e076de..e248b3af861 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml @@ -109,6 +109,7 @@ org.wso2.carbon.core.server:${carbon.kernel.version} org.wso2.carbon.governance.metadata:${carbon.governance.version} + org.wso2.carbon.apimgt.core:${carbon.api.mgt.version} diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml index edfc7a924f5..2bb3dae4b83 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml @@ -1,5 +1,4 @@ - + + + + appmanager + admin + enrollment + 1.0.0 + http://localhost:9763/ + http,https + + + diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf index 51eb8e435ae..36ac4e31efe 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf @@ -2,5 +2,6 @@ instructions.configure = \ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/cdm-config.xml,target:${installFolder}/../../conf/cdm-config.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/notification-messages.xml,target:${installFolder}/../../conf/notification-messages.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/license-config.xml,target:${installFolder}/../../conf/etc/license-config.xml,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/user-api-publisher-config.xml,target:${installFolder}/../../conf/etc/user-api-publisher-config.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/dbscripts/cdm,target:${installFolder}/../../../dbscripts/cdm,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/rxts/license.rxt,target:${installFolder}/../../../repository/resources/rxts/license.rxt,overwrite:true);\ \ No newline at end of file diff --git a/pom.xml b/pom.xml index 149573171c9..52d24f291b8 100644 --- a/pom.xml +++ b/pom.xml @@ -416,6 +416,135 @@ ${carbon.commons.version} + + + org.wso2.carbon.apimgt + org.wso2.carbon.apimgt.api + ${carbon.api.mgt.version} + + + com.googlecode.json-simple.wso2 + json-simple + + + + + org.wso2.carbon.apimgt + org.wso2.carbon.apimgt.impl + ${carbon.api.mgt.version} + + + org.wso2.carbon.mediation + org.wso2.carbon.mediation.initializer + + + org.apache.woden.wso2 + woden + + + org.wso2.carbon + org.wso2.carbon.user.core + + + org.wso2.carbon.governance + org.wso2.carbon.governance.api + + + org.wso2.carbon + org.wso2.carbon.registry.ws.client + + + org.wso2.carbon + org.wso2.carbon.identity.core + + + org.wso2.carbon + org.wso2.carbon.identity.oauth + + + org.wso2.carbon + org.wso2.carbon.apimgt.keymgt.client + + + org.apache.poi + poi-ooxml + + + org.wso2.carbon + org.wso2.carbon.rest.api.stub + + + org.json.wso2 + json + + + com.h2database.wso2 + h2-database-engine + + + org.wso2.carbon + org.wso2.carbon.apimgt.handlers.security.stub + + + org.wso2.carbon + org.wso2.carbon.user.mgt.stub + + + org.wso2.carbon + org.wso2.carbon.um.ws.api + + + org.wso2.carbon + org.wso2.carbon.mediation.dependency.mgt + + + com.google.code.gson + gson + + + org.wso2.carbon + org.wso2.carbon.mediation.registry + + + org.apache.httpcomponents + httpmime + + + org.wso2.carbon + org.wso2.carbon.event.core + + + org.wso2.carbon + org.wso2.carbon.sequences.stub + + + org.wso2.carbon + org.wso2.carbon.mediation.security.stub + + + org.wso2.carbon + org.wso2.carbon.registry.indexing + + + org.wso2.carbon + org.wso2.carbon.apimgt.keymgt.stub + + + org.wso2.carbon + org.wso2.carbon.securevault + + + com.googlecode.json-simple.wso2 + json-simple + + + org.apache.bsf + bsf-all + + + + + @@ -660,6 +789,9 @@ 0.9.2-SNAPSHOT 4.3.6 + + 1.3.1 +