Merge branch 'bluetooth' into 'master'

Bluetooth beacon

See merge request entgra/carbon-device-mgt!543
4.x.x
Dharmakeerthi Lasantha 5 years ago
commit 18656792bb

@ -0,0 +1,41 @@
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.jaxrs.beans.analytics.reporting;
import java.util.Map;
public class Stream {
String deviceIdentifier;
Map<String, String> events;
public String getDeviceIdentifier() {
return deviceIdentifier;
}
public void setDeviceIdentifier(String deviceIdentifier) {
this.deviceIdentifier = deviceIdentifier;
}
public Map<String, String> getEvents() {
return events;
}
public void setEvents(Map<String, String> events) {
this.events = events;
}
}

@ -361,35 +361,30 @@ public class RequestValidationUtil {
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build()); .setMessage(msg).build());
} catch (InstantiationException e) { } catch (InstantiationException e) {
if (log.isDebugEnabled()) {
String msg = "Error when creating an instance of validator related to deviceType " + deviceType; String msg = "Error when creating an instance of validator related to deviceType " + deviceType;
log.error(msg, e); log.debug(msg, e);
throw new InputValidationException( }
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build());
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
if (log.isDebugEnabled()) {
String msg = "Error when accessing an instance of validator related to deviceType " + deviceType; String msg = "Error when accessing an instance of validator related to deviceType " + deviceType;
log.error(msg, e); log.debug(msg, e);
throw new InputValidationException( }
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build());
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
if (log.isDebugEnabled()) {
String msg = "Error when loading an instance of validator related to deviceType " + deviceType; String msg = "Error when loading an instance of validator related to deviceType " + deviceType;
log.error(msg, e); log.debug(msg, e);
throw new InputValidationException( }
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build());
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
if (log.isDebugEnabled()) {
String msg = "Error occurred while constructing validator related to deviceType " + deviceType; String msg = "Error occurred while constructing validator related to deviceType " + deviceType;
log.error(msg, e); log.debug(msg, e);
throw new InputValidationException( }
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build());
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
if (log.isDebugEnabled()) {
String msg = "Error occurred while instantiating validator related to deviceType " + deviceType; String msg = "Error occurred while instantiating validator related to deviceType " + deviceType;
log.error(msg, e); log.debug(msg, e);
throw new InputValidationException( }
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build());
} }
return features; return features;
} }

@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import java.util.List; import java.util.List;
import java.util.Map;
public class DeviceDetailsWrapper { public class DeviceDetailsWrapper {
@ -30,6 +31,7 @@ public class DeviceDetailsWrapper {
Device device; Device device;
List<Application> applications; List<Application> applications;
DeviceLocation location; DeviceLocation location;
String events;
int tenantId; int tenantId;
List<DeviceGroup> groups; List<DeviceGroup> groups;
@ -91,6 +93,14 @@ public class DeviceDetailsWrapper {
this.location = location; this.location = location;
} }
public String getEvents() {
return events;
}
public void setEvents(String events) {
this.events = events;
}
public String getJSONString() { public String getJSONString() {
Gson gson = new Gson(); Gson gson = new Gson();
return gson.toJson(this).toString(); return gson.toJson(this).toString();

@ -43,6 +43,7 @@ public final class DeviceManagementConstants {
public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER"; public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER";
public static final String DEVICE_CACHE = "DEVICE_CACHE"; public static final String DEVICE_CACHE = "DEVICE_CACHE";
public static final String ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification"; public static final String ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification";
public static final String URL_SEPERATOR = "/";
public static final class ConfigurationManagement { public static final class ConfigurationManagement {
private ConfigurationManagement(){ private ConfigurationManagement(){
@ -164,8 +165,8 @@ public final class DeviceManagementConstants {
throw new AssertionError(); throw new AssertionError();
} }
public static final String REPORTING_EVENT_HOST = "iot.reporting.event.host"; public static final String REPORTING_EVENT_HOST = "iot.reporting.event.host";
public static final String REPORTING_CONTEXT = "/event"; public static final String REPORTING_CONTEXT = "/reporting/api/analyticsadmin/v1.0/event";
public static final String DEVICE_INFO_ENDPOINT = REPORTING_CONTEXT + "/device-info"; public static final String DEVICE_INFO_PARAM = "device-info";
public static final String APP_USAGE_ENDPOINT = REPORTING_CONTEXT + "/app-usage"; public static final String APP_USAGE_ENDPOINT = REPORTING_CONTEXT + "/app-usage";
} }
} }

@ -96,6 +96,18 @@ public interface DeviceInformationManager {
*/ */
List<DeviceLocation> getDeviceLocations(List<DeviceIdentifier> deviceIdentifiers) throws DeviceDetailsMgtException; List<DeviceLocation> getDeviceLocations(List<DeviceIdentifier> deviceIdentifiers) throws DeviceDetailsMgtException;
/**
* Send events to reporting backend
* @param deviceId device identifier of the reporting device
* @param deviceType device type of an device
* @param payload payload of the event
* @param eventType Event type being sent
* @return Http status code if a call is made and if failed to make a call 0
* @throws DeviceDetailsMgtException
*/
int publishEvents(String deviceId, String deviceType, String payload, String eventType)
throws DeviceDetailsMgtException;
// /** // /**
// * This method will manage the storing of device application list. // * This method will manage the storing of device application list.
// * @param deviceApplication - Device application list. // * @param deviceApplication - Device application list.

@ -27,6 +27,7 @@ import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublish
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService;
import org.wso2.carbon.device.mgt.common.device.details.DeviceData;
import org.wso2.carbon.device.mgt.common.device.details.DeviceDetailsWrapper; import org.wso2.carbon.device.mgt.common.device.details.DeviceDetailsWrapper;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException; import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException;
@ -91,7 +92,10 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
public void addDeviceInfo(Device device, DeviceInfo deviceInfo) throws DeviceDetailsMgtException { public void addDeviceInfo(Device device, DeviceInfo deviceInfo) throws DeviceDetailsMgtException {
try { try {
publishEvents(device, deviceInfo); DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper();
deviceDetailsWrapper.setDeviceInfo(deviceInfo);
publishEvents(device, deviceDetailsWrapper, DeviceManagementConstants.Report.DEVICE_INFO_PARAM);
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceInfo newDeviceInfo; DeviceInfo newDeviceInfo;
DeviceInfo previousDeviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(), DeviceInfo previousDeviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(),
@ -180,14 +184,37 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
} }
} }
private void publishEvents(Device device, DeviceInfo deviceInfo) { public int publishEvents(String deviceId, String deviceType, String payload, String eventType)
throws DeviceDetailsMgtException {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
try {
Device device = DeviceManagementDataHolder.getInstance().
getDeviceManagementProvider().getDevice(deviceIdentifier, false);
DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper();
deviceDetailsWrapper.setEvents(payload);
return publishEvents(device, deviceDetailsWrapper, eventType);
} catch (DeviceManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Event publishing error. Could not get device " + deviceId;
log.error(msg, e);
throw new DeviceDetailsMgtException(msg, e);
}
}
/**
* Send device details from core to reporting backend
* @param device Device that is sending event
* @param deviceDetailsWrapper Payload to send(example, deviceinfo, applist, raw events)
*/
private int publishEvents(Device device, DeviceDetailsWrapper deviceDetailsWrapper, String
eventType) {
String reportingHost = HttpReportingUtil.getReportingHost(); String reportingHost = HttpReportingUtil.getReportingHost();
if (!StringUtils.isBlank(reportingHost) if (!StringUtils.isBlank(reportingHost)
&& HttpReportingUtil.isPublishingEnabledForTenant()) { && HttpReportingUtil.isPublishingEnabledForTenant()) {
try { try {
DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper();
deviceDetailsWrapper.setDevice(device); deviceDetailsWrapper.setDevice(device);
deviceDetailsWrapper.setDeviceInfo(deviceInfo);
deviceDetailsWrapper.setTenantId(DeviceManagerUtil.getTenantId()); deviceDetailsWrapper.setTenantId(DeviceManagerUtil.getTenantId());
GroupManagementProviderService groupManagementService = DeviceManagementDataHolder GroupManagementProviderService groupManagementService = DeviceManagementDataHolder
.getInstance().getGroupManagementProviderService(); .getInstance().getGroupManagementProviderService();
@ -197,14 +224,16 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
deviceDetailsWrapper.setGroups(groups); deviceDetailsWrapper.setGroups(groups);
} }
String[] rolesOfUser = getRolesOfUser(CarbonContext.getThreadLocalCarbonContext() String[] rolesOfUser = DeviceManagerUtil.getRolesOfUser(CarbonContext
.getThreadLocalCarbonContext()
.getUsername()); .getUsername());
if (rolesOfUser != null && rolesOfUser.length > 0) { if (rolesOfUser != null && rolesOfUser.length > 0) {
deviceDetailsWrapper.setRole(rolesOfUser); deviceDetailsWrapper.setRole(rolesOfUser);
} }
HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), String eventUrl = reportingHost + DeviceManagementConstants.Report
reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT); .REPORTING_CONTEXT + DeviceManagementConstants.URL_SEPERATOR + eventType;
return HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), eventUrl);
} catch (EventPublishingException e) { } catch (EventPublishingException e) {
log.error("Error occurred while sending events", e); log.error("Error occurred while sending events", e);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
@ -218,6 +247,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
+ DeviceManagerUtil.getTenantId()); + DeviceManagerUtil.getTenantId());
} }
} }
return 0;
} }
@Override @Override
@ -524,19 +554,5 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
} }
} }
private String[] getRolesOfUser(String userName) throws UserStoreException {
UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm();
String[] roleList;
if (userRealm != null) {
userRealm.getUserStoreManager().getRoleNames();
roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName);
} else {
String msg = "User realm is not initiated. Logged in user: " + userName;
log.error(msg);
throw new UserStoreException(msg);
}
return roleList;
}
} }

@ -32,6 +32,7 @@ import org.apache.http.protocol.HTTP;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.caching.impl.CacheImpl; import org.wso2.carbon.caching.impl.CacheImpl;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService; import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService;
import org.wso2.carbon.device.mgt.common.AppRegistrationCredentials; import org.wso2.carbon.device.mgt.common.AppRegistrationCredentials;
@ -76,6 +77,7 @@ import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
import org.wso2.carbon.user.api.TenantManager; import org.wso2.carbon.user.api.TenantManager;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.ConfigurationContextService; import org.wso2.carbon.utils.ConfigurationContextService;
@ -1005,4 +1007,18 @@ public final class DeviceManagerUtil {
} }
} }
public static String[] getRolesOfUser(String userName) throws UserStoreException {
UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm();
String[] roleList;
if (userRealm != null) {
userRealm.getUserStoreManager().getRoleNames();
roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName);
} else {
String msg = "User realm is not initiated. Logged in user: " + userName;
log.error(msg);
throw new UserStoreException(msg);
}
return roleList;
}
} }

Loading…
Cancel
Save