diff --git a/pom.xml b/pom.xml index 59ccc4a793..7d00d00335 100644 --- a/pom.xml +++ b/pom.xml @@ -235,11 +235,6 @@ org.wso2.carbon.device.mgt.extensions provided - - org.wso2.carbon.devicemgt-plugins - org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin - provided - diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java index c00dd6ab79..f5bb1d84b1 100644 --- a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java @@ -18,7 +18,6 @@ package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl; -import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -35,18 +34,15 @@ import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.constants.VirtualFireAlarmConstants; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.exception.VirtualFirealarmDeviceMgtPluginException; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.VirtualFirealarmSecurityManager; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppAccount; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppConfig; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppServerClient; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.constants.VirtualFireAlarmConstants; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dto.SensorRecord; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.exception.VirtualFireAlarmException; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.APIUtil; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.VirtualFireAlarmServiceUtils; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.ZipArchive; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.ZipUtil; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.xmpp.VirtualFirealarmXMPPException; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.xmpp.XmppAccount; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.xmpp.XmppConfig; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.xmpp.XmppServerClient; import org.wso2.carbon.identity.jwt.client.extension.JWTClient; import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; @@ -76,11 +72,8 @@ import java.util.UUID; public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { - private static final String XMPP_PROTOCOL = "XMPP"; - private static final String MQTT_PROTOCOL = "MQTT"; private static final String KEY_TYPE = "PRODUCTION"; private static ApiApplicationKey apiApplicationKey; - private static final String DEVICE_MGT_SCOPE_IDENTIFIER = "device-mgt"; private static Log log = LogFactory.getLog(VirtualFireAlarmServiceImpl.class); @POST @@ -103,10 +96,7 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); } String resource = VirtualFireAlarmConstants.BULB_CONTEXT.replace("/", ""); - PrivateKey serverPrivateKey = VirtualFirealarmSecurityManager.getServerPrivateKey(); String actualMessage = resource + ":" + switchToState; - String encryptedMsg = VirtualFireAlarmServiceUtils.prepareSecurePayLoad(actualMessage, - serverPrivateKey); String publishTopic = APIUtil.getTenantDomainOftheUser() + "/" + VirtualFireAlarmConstants.DEVICE_TYPE + "/" + deviceId; @@ -114,7 +104,7 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { commandOp.setCode("buzz"); commandOp.setType(Operation.Type.COMMAND); commandOp.setEnabled(true); - commandOp.setPayLoad(encryptedMsg); + commandOp.setPayLoad(actualMessage); Properties props = new Properties(); props.setProperty(VirtualFireAlarmConstants.MQTT_ADAPTER_TOPIC_PROPERTY_NAME, publishTopic); @@ -137,10 +127,6 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { } catch (DeviceAccessAuthorizationException e) { log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (VirtualFireAlarmException e) { - String errorMsg = "Preparing Secure payload failed for device - [" + deviceId + "]"; - log.error(errorMsg); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } catch (OperationManagementException e) { String msg = "Error occurred while executing command operation upon ringing the buzzer"; log.error(msg, e); @@ -162,37 +148,22 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { DeviceGroupConstants.Permissions.DEFAULT_MANAGE_POLICIES_PERMISSIONS)) { return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); } - PrivateKey serverPrivateKey = VirtualFirealarmSecurityManager.getServerPrivateKey(); String actualMessage = VirtualFireAlarmConstants.POLICY_CONTEXT + ":" + policy; - String encryptedMsg = VirtualFireAlarmServiceUtils.prepareSecurePayLoad(actualMessage, - serverPrivateKey); Map dynamicProperties = new HashMap<>(); - switch (protocolString) { - case XMPP_PROTOCOL: - dynamicProperties.put(VirtualFireAlarmConstants.JID_PROPERTY_KEY, - deviceId + "@" + XmppConfig.getInstance().getServerName()); - dynamicProperties.put(VirtualFireAlarmConstants.SUBJECT_PROPERTY_KEY, "POLICTY-REQUEST"); - dynamicProperties.put(VirtualFireAlarmConstants.MESSAGE_TYPE_PROPERTY_KEY, - VirtualFireAlarmConstants.CHAT_PROPERTY_KEY); - APIUtil.getOutputEventAdapterService().publish(VirtualFireAlarmConstants.XMPP_ADAPTER_NAME, - dynamicProperties, encryptedMsg); - break; - default: - - String publishTopic = APIUtil.getTenantDomainOftheUser() + "/" - + VirtualFireAlarmConstants.DEVICE_TYPE + "/" + deviceId; - dynamicProperties.put(VirtualFireAlarmConstants.ADAPTER_TOPIC_PROPERTY, publishTopic); - APIUtil.getOutputEventAdapterService().publish(VirtualFireAlarmConstants.MQTT_ADAPTER_NAME, - dynamicProperties, encryptedMsg); - break; - } + String publishTopic = APIUtil.getTenantDomainOftheUser() + "/" + + VirtualFireAlarmConstants.DEVICE_TYPE + "/" + deviceId; + dynamicProperties.put(VirtualFireAlarmConstants.ADAPTER_TOPIC_PROPERTY, publishTopic); + dynamicProperties.put(VirtualFireAlarmConstants.JID_PROPERTY_KEY, + deviceId + "@" + XmppConfig.getInstance().getServerName()); + dynamicProperties.put(VirtualFireAlarmConstants.SUBJECT_PROPERTY_KEY, "POLICTY-REQUEST"); + dynamicProperties.put(VirtualFireAlarmConstants.MESSAGE_TYPE_PROPERTY_KEY, + VirtualFireAlarmConstants.CHAT_PROPERTY_KEY); + APIUtil.getOutputEventAdapterService().publish(VirtualFireAlarmConstants.XMPP_ADAPTER_NAME, + dynamicProperties, actualMessage); return Response.ok().build(); } catch (DeviceAccessAuthorizationException e) { log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (VirtualFireAlarmException e) { - log.error(e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } } @@ -260,7 +231,7 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { } catch (UserStoreException ex) { log.error(ex.getMessage(), ex); return Response.status(500).entity(ex.getMessage()).build(); - } catch (VirtualFirealarmDeviceMgtPluginException ex) { + } catch (VirtualFirealarmXMPPException ex) { log.error(ex.getMessage(), ex); return Response.status(500).entity(ex.getMessage()).build(); } @@ -294,7 +265,7 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { private ZipArchive createDownloadFile(String owner, String deviceName, String sketchType) throws DeviceManagementException, APIManagerException, JWTClientException, - UserStoreException, VirtualFirealarmDeviceMgtPluginException { + UserStoreException, VirtualFirealarmXMPPException { //create new device id String deviceId = shortUUID(); boolean status = register(deviceId, deviceName); diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/constants/VirtualFireAlarmConstants.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/constants/VirtualFireAlarmConstants.java new file mode 100644 index 0000000000..204ede0040 --- /dev/null +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/constants/VirtualFireAlarmConstants.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.iot.virtualfirealarm.service.impl.constants; + +public class VirtualFireAlarmConstants { + public final static String DEVICE_TYPE = "virtual_firealarm"; + public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME"; + public final static String DEVICE_PLUGIN_DEVICE_ID = "VIRTUAL_FIREALARM_DEVICE_ID"; + public final static String STATE_ON = "ON"; + public final static String STATE_OFF = "OFF"; + + public static final String URL_PREFIX = "http://"; + public static final String BULB_CONTEXT = "BULB"; + public static final String POLICY_CONTEXT = "POLICY"; + + //sensor events sumerized table name for temperature + public static final String TEMPERATURE_EVENT_TABLE = "DEVICE_TEMPERATURE_SUMMARY"; + public final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super"; + + //mqtt tranport related constants + public static final String MQTT_ADAPTER_NAME = "virtual_firealarm_mqtt"; + public static final String MQTT_ADAPTER_TYPE = "oauth-mqtt"; + public static final String ADAPTER_TOPIC_PROPERTY = "topic"; + public static final String MQTT_PORT = "\\$\\{mqtt.broker.port\\}"; + public static final String MQTT_BROKER_HOST = "\\$\\{mqtt.broker.host\\}"; + public static final String CARBON_CONFIG_PORT_OFFSET = "Ports.Offset"; + public static final String DEFAULT_CARBON_LOCAL_IP_PROPERTY = "carbon.local.ip"; + public static final int CARBON_DEFAULT_PORT_OFFSET = 0; + public static final int DEFAULT_MQTT_PORT = 1886; + + //xmpp transport related constants + public static final String XMPP_ADAPTER_NAME = "virtual_firealarm_xmpp"; + public static final String XMPP_ADAPTER_TYPE = "xmpp"; + public static final String PASSWORD_PROPERTY_KEY = "password"; + public static final String JID_PROPERTY_KEY = "jid"; + public static final String CLIENT_JID_PROPERTY_KEY = "xmpp.client.jid"; + public static final String SUBJECT_PROPERTY_KEY = "xmpp.client.subject"; + public static final String MESSAGE_TYPE_PROPERTY_KEY = "xmpp.client.messageType"; + public static final String CHAT_PROPERTY_KEY = "chat"; + + public static final String USERNAME_PROPERTY_KEY = "username"; + public static final String DCR_PROPERTY_KEY = "dcrUrl"; + public static final String BROKER_URL_PROPERTY_KEY = "url"; + public static final String SCOPES_PROPERTY_KEY = "scopes"; + public static final String QOS_PROPERTY_KEY = "qos"; + public static final String CLIENT_ID_PROPERTY_KEY = "qos"; + public static final String CLEAR_SESSION_PROPERTY_KEY = "clearSession"; + public static final String TOPIC = "topic"; + public static final String SUBSCRIBED_TOPIC = "carbon.super/virtual_firealarm/+/publisher"; + + public static final String CONTENT_VALIDATION = "contentValidator"; + public static final String CONTENT_TRANSFORMATION = "contentTransformer"; + public static final String RESOURCE = "resource"; + + public static final String JSON_SERIAL_KEY = "SerialNumber"; + public static final String TEMPERATURE_STREAM_DEFINITION = "org.wso2.iot.devices.temperature"; + public static final String JSON_MESSAGE_KEY = "Msg"; + public static final String JSON_SIGNATURE_KEY = "Sig"; + + public static final String HOST_KEY = "host"; + public static final String PORT_KEY = "port"; + + public static final String SERVER_NAME = "serverName"; + + public static final String MQTT_ADAPTER_TOPIC_PROPERTY_NAME = "mqtt.adapter.topic"; + + public static final String APIM_APPLICATION_TOKEN_VALIDITY_PERIOD = "3600"; +} diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/VirtualFireAlarmServiceUtils.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/VirtualFireAlarmServiceUtils.java deleted file mode 100644 index 277000e0f0..0000000000 --- a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/VirtualFireAlarmServiceUtils.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2016, 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.iot.virtualfirealarm.service.impl.util; - -import org.apache.commons.codec.binary.Base64; -import org.json.JSONObject; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.exception.VirtualFirealarmDeviceMgtPluginException; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.VirtualFirealarmSecurityManager; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.exception.VirtualFireAlarmException; - -import java.lang.*; -import java.security.PrivateKey; - -/** - * - */ -public class VirtualFireAlarmServiceUtils { - - private static final String JSON_MESSAGE_KEY = "Msg"; - private static final String JSON_SIGNATURE_KEY = "Sig"; - - public static String prepareSecurePayLoad(String message, PrivateKey signatureKey) throws VirtualFireAlarmException { - try { - message = Base64.encodeBase64String(message.getBytes()); - String signedPayload = VirtualFirealarmSecurityManager.signMessage(message, signatureKey); - JSONObject jsonPayload = new JSONObject(); - jsonPayload.put(JSON_MESSAGE_KEY, message); - jsonPayload.put(JSON_SIGNATURE_KEY, signedPayload); - return jsonPayload.toString(); - } catch (VirtualFirealarmDeviceMgtPluginException e) { - throw new VirtualFireAlarmException(e); - } - - } -} diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/VirtualFireAlarmUtilConstants.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/VirtualFireAlarmUtilConstants.java index fae96848d3..a255b9537f 100644 --- a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/VirtualFireAlarmUtilConstants.java +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/VirtualFireAlarmUtilConstants.java @@ -1,6 +1,5 @@ package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppConfig; public class VirtualFireAlarmUtilConstants { diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/ZipUtil.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/ZipUtil.java index 2c6cd28407..39e938f2b8 100644 --- a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/ZipUtil.java +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/ZipUtil.java @@ -30,7 +30,7 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppConfig; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.xmpp.XmppConfig; import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.NetworkUtils; diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/VirtualFirealarmXMPPException.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/VirtualFirealarmXMPPException.java new file mode 100644 index 0000000000..57d0b8a0bd --- /dev/null +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/VirtualFirealarmXMPPException.java @@ -0,0 +1,56 @@ +/* + * 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.iot.virtualfirealarm.service.impl.xmpp; + + +public class VirtualFirealarmXMPPException extends Exception{ + + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public VirtualFirealarmXMPPException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public VirtualFirealarmXMPPException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public VirtualFirealarmXMPPException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public VirtualFirealarmXMPPException() { + super(); + } + + public VirtualFirealarmXMPPException(Throwable cause) { + super(cause); + } + +} diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/XmppAccount.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/XmppAccount.java new file mode 100644 index 0000000000..162ea957f9 --- /dev/null +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/XmppAccount.java @@ -0,0 +1,63 @@ +/* + * 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.iot.virtualfirealarm.service.impl.xmpp; + +/** + * holds the information related to account that needs to be created on xmpp server. + */ +public class XmppAccount { + + private String username; + private String password; + private String accountName; + private String email; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getAccountName() { + return accountName; + } + + public void setAccountName(String accountName) { + this.accountName = accountName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + +} diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/XmppConfig.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/XmppConfig.java new file mode 100644 index 0000000000..d96f14c6e5 --- /dev/null +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/XmppConfig.java @@ -0,0 +1,124 @@ +/* + * 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.iot.virtualfirealarm.service.impl.xmpp; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class XmppConfig { + + private String host; + private int port; + private String username; + private String password; + private String serverName; + private boolean enabled; + private String jid; + private static XmppConfig xmppConfig = new XmppConfig(); + private static final Log log = LogFactory.getLog(XmppConfig.class); + private static final String ENABLED = "enabled"; + private static final String USERNAME = "username"; + private static final String PASSWORD = "password"; + private static final String HOST = "host"; + private static final String PORT = "port"; + private static final String SERVERNAME = "serverName"; + private static final String JID = "jid"; + + private XmppConfig() { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + InputStream input = classLoader.getResourceAsStream("../xmpp.properties"); + Properties properties = new Properties(); + try { + properties.load(input); + enabled = Boolean.parseBoolean(properties.getProperty(ENABLED, "false")); + host = properties.getProperty(HOST); + port = Integer.parseInt(properties.getProperty(PORT)); + username = properties.getProperty(USERNAME); + password = properties.getProperty(PASSWORD); + serverName = properties.getProperty(SERVERNAME); + jid = properties.getProperty(JID); + } catch (IOException e) { + log.error("Failed to load xmpp config properties."); + } + } + + public static XmppConfig getInstance() { + return xmppConfig; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getServerName() { + return serverName; + } + + public void setServerName(String serverName) { + this.serverName = serverName; + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public String getJid() { + return jid; + } + + public void setJid(String jid) { + this.jid = jid; + } +} diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/XmppServerClient.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/XmppServerClient.java new file mode 100644 index 0000000000..7369f90a4f --- /dev/null +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/xmpp/XmppServerClient.java @@ -0,0 +1,66 @@ +/* + * 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.iot.virtualfirealarm.service.impl.xmpp; + +import org.jivesoftware.smack.AccountManager; +import org.jivesoftware.smack.ConnectionConfiguration; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException; + +import java.util.HashMap; +import java.util.Map; + +public class XmppServerClient { + + public static boolean createAccount(XmppAccount xmppAccount) throws VirtualFirealarmXMPPException { + if (XmppConfig.getInstance().isEnabled()) { + if (xmppAccount != null) { + try { + ConnectionConfiguration config = new ConnectionConfiguration(XmppConfig.getInstance().getHost(), + XmppConfig.getInstance().getPort(), + "Accounts"); + XMPPConnection xmppConnection = new XMPPConnection(config); + xmppConnection.connect(); + xmppConnection.login(XmppConfig.getInstance().getUsername(), XmppConfig.getInstance().getPassword()); + AccountManager accountManager = xmppConnection.getAccountManager(); + Map attributes = new HashMap<>(); + attributes.put("username", xmppAccount.getUsername()); + attributes.put("password", xmppAccount.getPassword()); + attributes.put("email", xmppAccount.getEmail()); + attributes.put("name", xmppAccount.getAccountName()); + accountManager.createAccount(xmppAccount.getUsername(), xmppAccount.getPassword(), attributes); + xmppConnection.disconnect(); + return true; + } catch (XMPPException e) { + if (e.getXMPPError().getCode() == 409) { + //AccountAlreadyExist + return true; + } else { + throw new VirtualFirealarmXMPPException( + "XMPP account creation failed. Error: " + e.getLocalizedMessage(), e); + } + } + } else { + throw new VirtualFirealarmXMPPException("Invalid XMPP attributes"); + } + } else { + return true; + } + } +} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index d250c67de7..eb4a397bf7 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -35,4 +35,6 @@ true + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/xmpp.properties b/src/main/webapp/WEB-INF/xmpp.properties new file mode 100644 index 0000000000..3a173cb920 --- /dev/null +++ b/src/main/webapp/WEB-INF/xmpp.properties @@ -0,0 +1,25 @@ +# +# Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +# +# Licensed 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. +# +# + +#[XMPP-Configurations] +enabled=false +username=admin +password=admin +host=localhost +port=5222 +serverName=localhost +jid=admin@localhost \ No newline at end of file