From 0abbf55714c363794e9b8de703bc68ebe0b63b9d Mon Sep 17 00:00:00 2001 From: ayyoob Date: Tue, 9 May 2017 00:57:14 +0530 Subject: [PATCH] moved default input and output configurations to global properties --- .../adapter/http/HTTPMessageServlet.java | 5 ++ .../mqtt/util/MQTTAdapterListener.java | 6 +- .../MQTTBrokerConnectionConfiguration.java | 16 +++- .../output/adapter/mqtt/MQTTEventAdapter.java | 12 +-- .../MQTTBrokerConnectionConfiguration.java | 22 +++++- .../authorization/DeviceAuthorizer.java | 20 ----- .../authorization/PermissionUtil.java | 76 ------------------- ...DeviceAccessAuthorizationAdminService.java | 2 +- 8 files changed, 47 insertions(+), 112 deletions(-) delete mode 100644 components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/PermissionUtil.java diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.http/src/main/java/org/wso2/carbon/device/mgt/input/adapter/http/HTTPMessageServlet.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.http/src/main/java/org/wso2/carbon/device/mgt/input/adapter/http/HTTPMessageServlet.java index afda05edf..9ac531e6d 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.http/src/main/java/org/wso2/carbon/device/mgt/input/adapter/http/HTTPMessageServlet.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.http/src/main/java/org/wso2/carbon/device/mgt/input/adapter/http/HTTPMessageServlet.java @@ -69,9 +69,14 @@ public class HTTPMessageServlet extends HttpServlet { this.tenantId = tenantId; this.exposedTransports = eventAdapterConfiguration.getProperties().get( HTTPEventAdapterConstants.EXPOSED_TRANSPORTS); + String globalContentValidator = globalProperties.get(HTTPEventAdapterConstants. + ADAPTER_CONF_CONTENT_VALIDATOR_TYPE); String contentValidatorType = eventAdapterConfiguration.getProperties().get( HTTPEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE); + if (globalContentValidator != null && !globalContentValidator.isEmpty() ) { + contentValidatorType = globalContentValidator; + } if (contentValidatorType == null || HTTPEventAdapterConstants.DEFAULT.equals(contentValidatorType)) { contentValidator = InputAdapterServiceDataHolder.getInputAdapterExtensionService() .getDefaultContentValidator(); diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java index 7635155d3..12f362380 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java @@ -191,7 +191,11 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { } } } - mqttClient.connect(connectionOptions); + try { + mqttClient.connect(connectionOptions); + } catch (MqttException e) { + log.error("Broker is unreachable, Waiting....."); + } mqttClient.subscribe(topic); } diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java index 3685c245f..32669bd96 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java @@ -105,10 +105,18 @@ public class MQTTBrokerConnectionConfiguration { this.brokerUrl = PropertyUtils.replaceMqttProperty(url); this.dcrUrl = PropertyUtils .replaceMqttProperty(globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_DCR_URL)); - this.contentValidatorType = eventAdapterConfiguration.getProperties() - .get(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE); - this.cleanSession = Boolean.parseBoolean(eventAdapterConfiguration.getProperties() - .get(MQTTEventAdapterConstants.ADAPTER_CONF_CLEAN_SESSION)); + this.contentValidatorType = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE); + if (contentValidatorType == null || contentValidatorType.isEmpty()) { + this.contentValidatorType = eventAdapterConfiguration.getProperties() + .get(MQTTEventAdapterConstants.ADAPTER_CONF_CONTENT_VALIDATOR_TYPE); + } + String cleanSession = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_CLEAN_SESSION); + if (cleanSession == null || cleanSession.isEmpty()) { + this.cleanSession = Boolean.parseBoolean(eventAdapterConfiguration.getProperties() + .get(MQTTEventAdapterConstants.ADAPTER_CONF_CLEAN_SESSION)); + } else { + this.cleanSession = Boolean.parseBoolean(cleanSession); + } //If global properties are available those will be assigned else constant values will be assigned if (globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_KEEP_ALIVE) != null) { keepAlive = Integer.parseInt((globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_KEEP_ALIVE))); diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapter.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapter.java index fbc9c015d..658a124d2 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapter.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/MQTTEventAdapter.java @@ -44,10 +44,10 @@ public class MQTTEventAdapter implements OutputEventAdapter { private Map globalProperties; private MQTTAdapterPublisher mqttAdapterPublisher; private int connectionKeepAliveInterval; - private String qos; private static ThreadPoolExecutor threadPoolExecutor; private static final Log log = LogFactory.getLog(MQTTEventAdapter.class); private int tenantId; + private MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration; public MQTTEventAdapter(OutputEventAdapterConfiguration eventAdapterConfiguration, Map globalProperties) { @@ -117,11 +117,11 @@ public class MQTTEventAdapter implements OutputEventAdapter { @Override public void connect() { - MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration = + mqttBrokerConnectionConfiguration = new MQTTBrokerConnectionConfiguration(eventAdapterConfiguration, globalProperties); String clientId = eventAdapterConfiguration.getStaticProperties().get( MQTTEventAdapterConstants.ADAPTER_CONF_CLIENTID); - qos = eventAdapterConfiguration.getStaticProperties().get(MQTTEventAdapterConstants.ADAPTER_MESSAGE_QOS); + mqttAdapterPublisher = new MQTTAdapterPublisher(mqttBrokerConnectionConfiguration, clientId, tenantId); } @@ -179,11 +179,7 @@ public class MQTTEventAdapter implements OutputEventAdapter { } } } - if (qos == null || qos.trim().isEmpty()) { - mqttAdapterPublisher.publish(message.toString(), topic); - } else { - mqttAdapterPublisher.publish(Integer.parseInt(qos), message.toString(), topic); - } + mqttAdapterPublisher.publish(mqttBrokerConnectionConfiguration.getQos(), message.toString(), topic); } catch (Throwable t) { EventAdapterUtil.logAndDrop(eventAdapterConfiguration.getName(), message, null, t, log, tenantId); } diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java index 2bbd394ab..148ccb3d8 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/output/adapter/mqtt/util/MQTTBrokerConnectionConfiguration.java @@ -34,6 +34,7 @@ public class MQTTBrokerConnectionConfiguration { private boolean cleanSession = true; private int keepAlive; private boolean globalCredentailSet; + private int qos; public String getTokenUrl() { return tokenUrl; @@ -75,6 +76,9 @@ public class MQTTBrokerConnectionConfiguration { return globalCredentailSet; } + public int getQos() { + return qos; + } public MQTTBrokerConnectionConfiguration(OutputEventAdapterConfiguration eventAdapterConfiguration, Map globalProperties) { adapterName = eventAdapterConfiguration.getName(); @@ -98,14 +102,28 @@ public class MQTTBrokerConnectionConfiguration { if (scopes == null) { this.scopes = MQTTEventAdapterConstants.EMPTY_STRING; } - this.cleanSession = Boolean.parseBoolean(eventAdapterConfiguration.getStaticProperties() - .get(MQTTEventAdapterConstants.ADAPTER_CONF_CLEAN_SESSION)); + String cleanSession = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_CLEAN_SESSION); + if (cleanSession == null || cleanSession.isEmpty()) { + this.cleanSession = Boolean.parseBoolean(eventAdapterConfiguration.getStaticProperties() + .get(MQTTEventAdapterConstants.ADAPTER_CONF_CLEAN_SESSION)); + } else { + this.cleanSession = Boolean.parseBoolean(cleanSession); + } //If global properties are available those will be assigned else constant values will be assigned if (globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_KEEP_ALIVE) != null) { keepAlive = Integer.parseInt((globalProperties.get(MQTTEventAdapterConstants.ADAPTER_CONF_KEEP_ALIVE))); } else { keepAlive = MQTTEventAdapterConstants.ADAPTER_CONF_DEFAULT_KEEP_ALIVE; } + String qosVal = globalProperties.get(MQTTEventAdapterConstants.ADAPTER_MESSAGE_QOS); + if (qosVal == null || qosVal.isEmpty()) { + this.qos = Integer.parseInt(qosVal); + } else { + qosVal = eventAdapterConfiguration.getStaticProperties().get(MQTTEventAdapterConstants.ADAPTER_MESSAGE_QOS); + this.qos = Integer.parseInt(qosVal); + } + + } } diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/DeviceAuthorizer.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/DeviceAuthorizer.java index c4c40856e..775ea200f 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/DeviceAuthorizer.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/DeviceAuthorizer.java @@ -63,23 +63,14 @@ public class DeviceAuthorizer implements Authorizer { private static DeviceAccessAuthorizationAdminService deviceAccessAuthorizationAdminService; private static final String CDMF_SERVER_BASE_CONTEXT = "/api/device-mgt/v1.0"; private static final String DEVICE_MGT_SERVER_URL = "deviceMgtServerUrl"; - private static final String STAT_PERMISSION = "statsPermission"; private static final String DEVICE_ID = "deviceId"; private static final String DEVICE_TYPE = "deviceType"; private static Log log = LogFactory.getLog(DeviceAuthorizer.class); - private static List statPermissions; - public DeviceAuthorizer() { } @Override public void init(Map globalProperties) { - statPermissions = getPermissions(globalProperties); - if (statPermissions != null && !statPermissions.isEmpty()) { - for (String permission : statPermissions) { - PermissionUtil.putPermission(permission); - } - } try { deviceAccessAuthorizationAdminService = Feign.builder().client(getSSLClient()).logger(new Slf4jLogger()) .logLevel(Logger.Level.FULL).requestInterceptor(new OAuthRequestInterceptor(globalProperties)) @@ -102,9 +93,6 @@ public class DeviceAuthorizer implements Authorizer { AuthorizationRequest authorizationRequest = new AuthorizationRequest(); authorizationRequest.setTenantDomain(authenticationInfo.getTenantDomain()); - if (statPermissions != null && !statPermissions.isEmpty()) { - authorizationRequest.setPermissions(statPermissions); - } authorizationRequest.setUsername(authenticationInfo.getUsername()); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); @@ -137,14 +125,6 @@ public class DeviceAuthorizer implements Authorizer { return deviceMgtServerUrl; } - private List getPermissions(Map properties) { - String stats = properties.get(STAT_PERMISSION); - if (stats != null && !stats.isEmpty()) { - return Arrays.asList(stats.replace("\n", "").split(" ")); - } - return null; - } - private static Client getSSLClient() { return new Client.Default(getTrustedSSLSocketFactory(), new HostnameVerifier() { @Override diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/PermissionUtil.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/PermissionUtil.java deleted file mode 100644 index cab25a8c4..000000000 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/PermissionUtil.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.output.adapter.websocket.authorization; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.base.MultitenantConstants; -import org.wso2.carbon.device.mgt.output.adapter.websocket.internal.WebsocketEventAdaptorServiceDataHolder; -import org.wso2.carbon.registry.api.Resource; -import org.wso2.carbon.registry.core.Registry; -import org.wso2.carbon.registry.core.exceptions.RegistryException; - -import java.util.StringTokenizer; - -/** - * Utility class which holds necessary utility methods required for persisting permissions in - * registry. - */ -public class PermissionUtil { - - public static final String PERMISSION_PROPERTY_NAME = "name"; - private static Log log = LogFactory.getLog(DeviceAuthorizer.class); - - public static void putPermission(String permission) { - try { - StringTokenizer tokenizer = new StringTokenizer(permission, "/"); - String lastToken = "", currentToken, tempPath; - while (tokenizer.hasMoreTokens()) { - currentToken = tokenizer.nextToken(); - tempPath = lastToken + "/" + currentToken; - if (!checkResourceExists(tempPath)) { - createRegistryCollection(tempPath, currentToken); - - } - lastToken = tempPath; - } - } catch (org.wso2.carbon.registry.api.RegistryException e) { - log.error("Failed to creation permission in registry" + permission, e); - } - } - - public static void createRegistryCollection(String path, String resourceName) - throws org.wso2.carbon.registry.api.RegistryException { - Resource resource = getGovernanceRegistry().newCollection(); - resource.addProperty(PERMISSION_PROPERTY_NAME, resourceName); - getGovernanceRegistry().beginTransaction(); - getGovernanceRegistry().put(path, resource); - getGovernanceRegistry().commitTransaction(); - } - - public static boolean checkResourceExists(String path) - throws RegistryException { - return getGovernanceRegistry().resourceExists(path); - } - - public static Registry getGovernanceRegistry() throws RegistryException { - return WebsocketEventAdaptorServiceDataHolder.getRegistryService() - .getGovernanceSystemRegistry(MultitenantConstants.SUPER_TENANT_ID); - } - -} diff --git a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/client/dto/DeviceAccessAuthorizationAdminService.java b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/client/dto/DeviceAccessAuthorizationAdminService.java index 7ff42b115..440173bf3 100644 --- a/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/client/dto/DeviceAccessAuthorizationAdminService.java +++ b/components/extensions/cdmf-transport-adapters/output/org.wso2.carbon.device.mgt.output.adapter.websocket/src/main/java/org/wso2/carbon/device/mgt/output/adapter/websocket/authorization/client/dto/DeviceAccessAuthorizationAdminService.java @@ -28,7 +28,7 @@ import javax.ws.rs.core.MediaType; @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -@Path("/admin/authorization") +@Path("/admin/authorization/stat") /** * This interface provided the definition of the device - user access verification service. */