@HiveWebsocketAuth @PreAuthorize("isAuthenticated() and hasPermission(null, 'GET_DEVICE_TYPE')") public void processDeviceTypeList(JsonObject request, WebSocketSession session) { HivePrincipal principal = (HivePrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); ListDeviceTypeRequest listDeviceTypeRequest = ListDeviceTypeRequest.createListDeviceTypeRequest(request); listDeviceTypeRequest.setPrincipal(Optional.ofNullable(principal)); String sortField = Optional.ofNullable(listDeviceTypeRequest.getSortField()).map(String::toLowerCase).orElse(null); if (sortField != null && !ID.equalsIgnoreCase(sortField) && !NAME.equalsIgnoreCase(sortField)) { logger.error("Unable to proceed device type list request. Invalid sortField"); throw new HiveException(Messages.INVALID_REQUEST_PARAMETERS, BAD_REQUEST.getStatusCode()); } WebSocketResponse response = new WebSocketResponse(); if (!principal.areAllDeviceTypesAvailable() && (principal.getDeviceTypeIds() == null || principal.getDeviceTypeIds().isEmpty())) { logger.warn("Unable to get list for empty device types"); response.addValue(DEVICE_TYPES, Collections.<DeviceTypeVO>emptyList(), DEVICE_TYPES_LISTED); webSocketClientHandler.sendMessage(request, response, session); } else { deviceTypeService.list(listDeviceTypeRequest) .thenAccept(deviceTypes -> { logger.debug("Device type list request proceed successfully."); response.addValue(DEVICE_TYPES, deviceTypes, DEVICE_TYPES_LISTED); webSocketClientHandler.sendMessage(request, response, session); }); } }
@Override public Response handle(Request request) { final ListDeviceTypeRequest req = (ListDeviceTypeRequest) request.getBody(); final List<DeviceTypeVO> deviceTypes = deviceTypeDao.list(req.getName(), req.getNamePattern(), req.getSortField(), req.isSortOrderAsc(), req.getTake(), req.getSkip(), req.getPrincipal()); return Response.newBuilder() .withBody(new ListDeviceTypeResponse(deviceTypes)) .buildSuccess(); } }