forked from community/device-mgt-plugins
parent
fddf834ee7
commit
0aae89e864
@ -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";
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<String, String> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
Loading…
Reference in new issue