Example usage for java.util Set spliterator

List of usage examples for java.util Set spliterator

Introduction

In this page you can find the example usage for java.util Set spliterator.

Prototype

@Override
default Spliterator<E> spliterator() 

Source Link

Document

Creates a Spliterator over the elements in this set.

Usage

From source file:com.devicehive.websockets.handlers.NotificationHandlers.java

@PreAuthorize("isAuthenticated() and hasPermission(null, 'CREATE_DEVICE_NOTIFICATION')")
public WebSocketResponse processNotificationInsert(JsonObject request, WebSocketSession session) {
    HivePrincipal principal = (HivePrincipal) SecurityContextHolder.getContext().getAuthentication()
            .getPrincipal();/* ww  w  . j a v a  2  s  .  c  o m*/
    final String deviceGuid = Optional.ofNullable(request.get(Constants.DEVICE_GUID))
            .map(JsonElement::getAsString).orElse(null);
    DeviceNotificationWrapper notificationSubmit = gson.fromJson(request.get(Constants.NOTIFICATION),
            DeviceNotificationWrapper.class);

    logger.debug("notification/insert requested. Session {}. Guid {}", session, deviceGuid);
    if (notificationSubmit == null || notificationSubmit.getNotification() == null) {
        logger.debug("notification/insert proceed with error. Bad notification: notification is required.");
        throw new HiveException(Messages.NOTIFICATION_REQUIRED, SC_BAD_REQUEST);
    }

    Set<DeviceVO> devices = new HashSet<>();
    if (deviceGuid == null) {
        for (String guid : principal.getDeviceGuids()) {
            devices.add(deviceService.findByGuidWithPermissionsCheck(guid, principal));
        }
    } else {
        devices.add(deviceService.findByGuidWithPermissionsCheck(deviceGuid, principal));
    }
    if (devices.isEmpty() || StreamSupport.stream(devices.spliterator(), true).allMatch(o -> o == null)) {
        logger.debug("notification/insert canceled for session: {}. Guid is not provided", session);
        throw new HiveException(Messages.DEVICE_GUID_REQUIRED, SC_FORBIDDEN);
    }

    WebSocketResponse response = new WebSocketResponse();

    for (DeviceVO device : devices) {
        if (device.getNetwork() == null) {
            logger.debug("notification/insert. No network specified for device with guid = {}", deviceGuid);
            throw new HiveException(String.format(Messages.DEVICE_IS_NOT_CONNECTED_TO_NETWORK, deviceGuid),
                    SC_FORBIDDEN);
        }
        DeviceNotification message = notificationService.convertWrapperToNotification(notificationSubmit,
                device);

        notificationService.insert(message, device).thenApply(notification -> {
            logger.debug("notification/insert proceed successfully. Session {}. Guid {}", session, deviceGuid);
            response.addValue(NOTIFICATION, new InsertNotification(message.getId(), message.getTimestamp()),
                    NOTIFICATION_TO_DEVICE);
            return response;
        }).exceptionally(ex -> {
            logger.warn("Unable to insert notification.", ex);
            throw new HiveException(Messages.INTERNAL_SERVER_ERROR, SC_INTERNAL_SERVER_ERROR);
        }).join();
    }

    return response;
}