/** * Queries the results with the given {@link ManagementOperationNotificationQuery} parameter. * * @param scopeId The {@link ScopeId} in which to search results. * @param operationId The id of the {@link Device} in which to search results * @param query The {@link ManagementOperationNotificationQuery} to use to filter results. * @return The {@link ManagementOperationNotificationListResult} of all the result matching the given {@link ManagementOperationNotificationQuery} parameter. * @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions. * @since 1.0.0 */ @ApiOperation(nickname = "ManagementOperationNotificationQuery", value = "Queries the ManagementOperationNotifications", notes = "Queries the ManagementOperationNotifications with the given ManagementOperationNotifications parameter returning all matching ManagementOperationNotifications", response = ManagementOperationNotificationListResult.class) @POST @Path("_query") @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public ManagementOperationNotificationListResult query( @ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId, @ApiParam(value = "The device id to filter results.") @PathParam("deviceId") EntityId deviceId, @ApiParam(value = "The id of the Device in which to search results") @PathParam("operationId") EntityId operationId, @ApiParam(value = "The ManagementOperationNotificationQuery to use to filter results.", required = true) ManagementOperationNotificationQuery query) throws Exception { query.setScopeId(scopeId); AndPredicateImpl andPredicate = new AndPredicateImpl(); andPredicate.and(new AttributePredicateImpl<>(ManagementOperationNotificationAttributes.OPERATION_ID, operationId)); query.setPredicate(andPredicate); return managementOperationNotificationService.query(query); }
/** * Queries the results with the given {@link DeviceManagementOperationQuery} parameter. * * @param scopeId The {@link ScopeId} in which to search results. * @param deviceId The id of the {@link Device} in which to search results * @param query The {@link DeviceManagementOperationQuery} to use to filter results. * @return The {@link DeviceManagementOperationListResult} of all the result matching the given {@link DeviceManagementOperationQuery} parameter. * @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions. * @since 1.0.0 */ @ApiOperation(nickname = "deviceManagementOperationQuery", value = "Queries the DeviceManagementOperations", notes = "Queries the DeviceManagementOperations with the given DeviceManagementOperations parameter returning all matching DeviceManagementOperations", response = DeviceManagementOperationListResult.class) @POST @Path("_query") @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public DeviceManagementOperationListResult query( @ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId, @ApiParam(value = "The id of the Device in which to search results") @PathParam("deviceId") EntityId deviceId, @ApiParam(value = "The DeviceManagementOperationQuery to use to filter results.", required = true) DeviceManagementOperationQuery query) throws Exception { query.setScopeId(scopeId); AndPredicateImpl andPredicate = new AndPredicateImpl(); andPredicate.and(new AttributePredicateImpl<>(DeviceManagementOperationAttributes.DEVICE_ID, deviceId)); query.setPredicate(andPredicate); return deviceManagementOperationRegistryService.query(query); }
/** * Gets the {@link ManagementOperationNotification} list in the scope. * * @param scopeId The {@link ScopeId} in which to search results. * @param operationId The id of the {@link Device} in which to search results * @param resource The resource of the {@link ManagementOperationNotification} in which to search results * @param offset The result set offset. * @param limit The result set limit. * @return The {@link ManagementOperationNotificationListResult} of all the ManagementOperationNotifications associated to the current selected scope. * @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions. * @since 1.0.0 */ @ApiOperation(nickname = "ManagementOperationNotificationSimpleQuery", value = "Gets the ManagementOperationNotification list in the scope", notes = "Returns the list of all the ManagementOperationNotifications associated to the current selected scope.", response = ManagementOperationNotificationListResult.class) @GET @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public ManagementOperationNotificationListResult simpleQuery( @ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId, @ApiParam(value = "The device id to filter results.") @PathParam("deviceId") EntityId deviceId, @ApiParam(value = "The operation id to filter results.") @PathParam("operationId") EntityId operationId, @ApiParam(value = "The resource of the ManagementOperationNotification in which to search results") @QueryParam("resource") String resource, @ApiParam(value = "The result set offset.", defaultValue = "0") @QueryParam("offset") @DefaultValue("0") int offset, @ApiParam(value = "The result set limit.", defaultValue = "50") @QueryParam("limit") @DefaultValue("50") int limit) throws Exception { ManagementOperationNotificationQuery query = managementOperationNotificationFactory.newQuery(scopeId); AndPredicateImpl andPredicate = new AndPredicateImpl(); andPredicate.and(new AttributePredicateImpl<>(ManagementOperationNotificationAttributes.OPERATION_ID, operationId)); query.setPredicate(andPredicate); query.setOffset(offset); query.setLimit(limit); return query(scopeId, deviceId, operationId, query); }
/** * Gets the {@link Domain} list in the scope. * * @param scopeId The {@link ScopeId} in which to search results. * @param name The {@link Domain} name in which to search results. * @param offset The result set offset. * @param limit The result set limit. * @return The {@link DomainListResult} of all the domains associated to the current selected scope. * @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions. * @since 1.0.0 */ @ApiOperation(nickname = "domainSimpleQuery", value = "Gets the Domain list in the scope", notes = "Returns the list of all the domains associated to the current selected scope.", response = DomainListResult.class) @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public DomainListResult simpleQuery( @ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId, @ApiParam(value = "The domain name to filter results.") @QueryParam("name") String name, @ApiParam(value = "The result set offset.", defaultValue = "0") @QueryParam("offset") @DefaultValue("0") int offset, @ApiParam(value = "The result set limit.", defaultValue = "50") @QueryParam("limit") @DefaultValue("50") int limit) throws Exception { DomainQuery query = domainFactory.newQuery(null); AndPredicateImpl andPredicate = new AndPredicateImpl(); if (!Strings.isNullOrEmpty(name)) { andPredicate.and(new AttributePredicateImpl<>(DomainAttributes.NAME, name)); } query.setPredicate(andPredicate); query.setOffset(offset); query.setLimit(limit); return query(scopeId, query); }
/** * Queries the results with the given {@link RolePermissionQuery} parameter. * * @param scopeId The {@link ScopeId} in which to search results. * @param roleId The {@link Role} id in which to search results. * @param query The {@link RolePermissionQuery} to use to filter results. * @return The {@link RolePermissionListResult} of all the result matching the given {@link RolePermissionQuery} parameter. * @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions. * @since 1.0.0 */ @ApiOperation(nickname = "rolePermissionQuery", value = "Queries the RolePermissions", notes = "Queries the RolePermissions with the given RolePermissionQuery parameter returning all matching RolePermissions", response = RolePermissionListResult.class) @POST @Path("_query") @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public RolePermissionListResult query( @ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId, @ApiParam(value = "The Role id in which to search results.") @PathParam("roleId") EntityId roleId, @ApiParam(value = "The RolePermissionQuery to use to filter results.", required = true) RolePermissionQuery query) throws Exception { query.setScopeId(scopeId); AndPredicateImpl andPredicate = new AndPredicateImpl(); andPredicate.and(new AttributePredicateImpl<>(RolePermissionAttributes.ROLE_ID, roleId)); andPredicate.and(query.getPredicate()); query.setPredicate(andPredicate); return rolePermissionService.query(query); }
DeviceManagementOperationQuery query = deviceManagementOperationFactory.newQuery(scopeId); AndPredicateImpl andPredicate = new AndPredicateImpl(); andPredicate.and(new AttributePredicateImpl<>(DeviceManagementOperationAttributes.DEVICE_ID, deviceId)); if (!Strings.isNullOrEmpty(resource)) {
/** * Queries the results with the given {@link DeviceEventQuery} parameter. * * @param scopeId * The {@link ScopeId} in which to search results. * @param deviceId * The id of the {@link Device} in which to search results * @param query * The {@link DeviceEventQuery} to use to filter results. * @return The {@link DeviceEventListResult} of all the result matching the given {@link DeviceEventQuery} parameter. * @throws Exception * Whenever something bad happens. See specific {@link KapuaService} exceptions. * @since 1.0.0 */ @ApiOperation(nickname = "deviceEventQuery", value = "Queries the DeviceEvents", notes = "Queries the DeviceEvents with the given DeviceEvents parameter returning all matching DeviceEvents", response = DeviceEventListResult.class) @POST @Path("_query") @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public DeviceEventListResult query( @ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId, @ApiParam(value = "The id of the Device in which to search results") @PathParam("deviceId") EntityId deviceId, @ApiParam(value = "The DeviceEventQuery to use to filter results.", required = true) DeviceEventQuery query) throws Exception { query.setScopeId(scopeId); AndPredicateImpl andPredicate = new AndPredicateImpl(); andPredicate.and(new AttributePredicateImpl<>(DeviceEventAttributes.DEVICE_ID, deviceId)); andPredicate.and(query.getPredicate()); query.setPredicate(andPredicate); return deviceEventService.query(query); }
/** * Check the connection count for a specific reserved user id<br> * <b>Utility method used by the connection logic</b> * * @param scopeId * @param userId * @param count * @throws KapuaException */ protected void checkConnectionCountByReservedUserId(KapuaId scopeId, KapuaId userId, long count) throws KapuaException { // check that no devices have this user as strict user DeviceConnectionOptionQuery query = deviceConnectionOptionFactory.newQuery(scopeId); AndPredicateImpl andPredicate = new AndPredicateImpl(); andPredicate.and(new AttributePredicateImpl<>(DeviceConnectionOptionAttributes.RESERVED_USER_ID, userId)); query.setPredicate(andPredicate); query.setLimit(1); Long connectionCountByReservedUserId = KapuaSecurityUtils.doPrivileged(() -> deviceConnectionOptionService.count(query)); if (connectionCountByReservedUserId != null && connectionCountByReservedUserId > count) { throw new SecurityException("User not authorized!"); // TODO manage the error message. is it better to throw a more specific exception or keep it obfuscated for security reason? } }
private EventStoreRecordListResult getUnsentEvents(String serviceName, EventsProcessType eventsProcessType) throws KapuaException { EventStoreRecordQuery query = new EventStoreFactoryImpl().newQuery(null); AndPredicateImpl andPredicate = new AndPredicateImpl(); andPredicate.and(new AttributePredicateImpl<>(EventStoreRecordAttributes.SERVICE_NAME, serviceName)); if (EventsProcessType.SEND_ERROR.equals(eventsProcessType)) { LOGGER.trace("Looking for SENT_ERROR events. Add EventStatus=SENT_ERROR query predicate."); andPredicate.and(new AttributePredicateImpl<>(EventStoreRecordAttributes.EVENT_STATUS, EventStatus.SEND_ERROR)); } else { LOGGER.trace("Looking for OLD events. Add EventStatus=RAISED query predicate."); andPredicate.and(new AttributePredicateImpl<>(EventStoreRecordAttributes.EVENT_STATUS, EventStatus.TRIGGERED)); //add timestamp predicate Date eventDateBound = Date.from(KapuaDateUtils.getKapuaSysDate().minusMillis(OLD_MESSAGES_TIME_WINDOW)); LOGGER.trace("Looking for OLD events. Add timestamp condition query predicate. Date before {}", eventDateBound); andPredicate.and(new AttributePredicateImpl<>(EventStoreRecordAttributes.MODIFIED_ON, eventDateBound, Operator.LESS_THAN_OR_EQUAL)); } query.setPredicate(andPredicate); query.setLimit(EVENT_SCAN_WINDOW); return kapuaEventService.query(query); }
private EventStoreRecordListResult getUnsentEvents(String serviceName, EventsProcessType eventsProcessType) throws KapuaException { EventStoreRecordQuery query = new EventStoreFactoryImpl().newQuery(null); AndPredicateImpl andPredicate = new AndPredicateImpl(); andPredicate.and(new AttributePredicateImpl<>(EventStoreRecordAttributes.SERVICE_NAME, serviceName)); if (EventsProcessType.SEND_ERROR.equals(eventsProcessType)) { LOGGER.trace("Looking for SENT_ERROR events. Add EventStatus=SENT_ERROR query predicate."); andPredicate.and(new AttributePredicateImpl<>(EventStoreRecordAttributes.EVENT_STATUS, EventStatus.SEND_ERROR)); } else { LOGGER.trace("Looking for OLD events. Add EventStatus=RAISED query predicate."); andPredicate.and(new AttributePredicateImpl<>(EventStoreRecordAttributes.EVENT_STATUS, EventStatus.TRIGGERED)); //add timestamp predicate Date eventDateBound = Date.from(KapuaDateUtils.getKapuaSysDate().minusMillis(OLD_MESSAGES_TIME_WINDOW)); LOGGER.trace("Looking for OLD events. Add timestamp condition query predicate. Date before {}", eventDateBound); andPredicate.and(new AttributePredicateImpl<>(EventStoreRecordAttributes.MODIFIED_ON, eventDateBound, Operator.LESS_THAN_OR_EQUAL)); } query.setPredicate(andPredicate); query.setLimit(EVENT_SCAN_WINDOW); return kapuaEventService.query(query); }
private long countExistingCredentials(CredentialType credentialType, KapuaId scopeId, KapuaId userId) throws KapuaException { KapuaLocator locator = KapuaLocator.getInstance(); CredentialFactory credentialFactory = locator.getFactory(CredentialFactory.class); KapuaQuery<Credential> credentialQuery = credentialFactory.newQuery(scopeId); CredentialType ct = credentialType; QueryPredicate credentialTypePredicate = new AttributePredicateImpl<>(CredentialAttributes.CREDENTIAL_TYPE, ct); QueryPredicate userIdPredicate = new AttributePredicateImpl<>(CredentialAttributes.USER_ID, userId); QueryPredicate andPredicate = new AndPredicateImpl().and(credentialTypePredicate).and(userIdPredicate); credentialQuery.setPredicate(andPredicate); return count(credentialQuery); }
private long countExistingCredentials(CredentialType credentialType, KapuaId scopeId, KapuaId userId) throws KapuaException { KapuaLocator locator = KapuaLocator.getInstance(); CredentialFactory credentialFactory = locator.getFactory(CredentialFactory.class); KapuaQuery<Credential> credentialQuery = credentialFactory.newQuery(scopeId); CredentialType ct = credentialType; QueryPredicate credentialTypePredicate = new AttributePredicateImpl<>(CredentialAttributes.CREDENTIAL_TYPE, ct); QueryPredicate userIdPredicate = new AttributePredicateImpl<>(CredentialAttributes.USER_ID, userId); QueryPredicate andPredicate = new AndPredicateImpl().and(credentialTypePredicate).and(userIdPredicate); credentialQuery.setPredicate(andPredicate); return count(credentialQuery); }
@Override public void open(Serializable arg0) throws Exception { JobContextWrapper jobContextWrapper = new JobContextWrapper(jobContext); StepContextWrapper stepContextWrapper = new StepContextWrapper(stepContext); LOG.info("JOB {} - Opening cursor...", jobContextWrapper.getJobId()); // // Job Id and JobTarget status filtering AndPredicateImpl andPredicate = new AndPredicateImpl( new AttributePredicateImpl<>(JobTargetAttributes.JOB_ID, jobContextWrapper.getJobId()) ); // // Step index filtering stepIndexFiltering(jobContextWrapper, stepContextWrapper, andPredicate); // // Filter selected target targetSublistFiltering(jobContextWrapper, andPredicate); // // Query the targets JobTargetQuery query = jobTargetFactory.newQuery(jobContextWrapper.getScopeId()); query.setPredicate(andPredicate); jobTargets = KapuaSecurityUtils.doPrivileged(() -> jobTargetService.query(query)); LOG.info("JOB {} - Opening cursor... Done!", jobContextWrapper.getJobId()); }
@Override public void open(Serializable arg0) throws Exception { JobContextWrapper jobContextWrapper = new JobContextWrapper(jobContext); StepContextWrapper stepContextWrapper = new StepContextWrapper(stepContext); LOG.info("JOB {} - Opening cursor...", jobContextWrapper.getJobId()); // // Job Id and JobTarget status filtering AndPredicateImpl andPredicate = new AndPredicateImpl( new AttributePredicateImpl<>(JobTargetAttributes.JOB_ID, jobContextWrapper.getJobId()) ); // // Step index filtering stepIndexFiltering(jobContextWrapper, stepContextWrapper, andPredicate); // // Filter selected target targetSublistFiltering(jobContextWrapper, andPredicate); // // Query the targets JobTargetQuery query = jobTargetFactory.newQuery(jobContextWrapper.getScopeId()); query.setPredicate(andPredicate); jobTargets = KapuaSecurityUtils.doPrivileged(() -> jobTargetService.query(query)); LOG.info("JOB {} - Opening cursor... Done!", jobContextWrapper.getJobId()); }
protected void closeManagementOperation(KapuaId scopeId, KapuaId deviceId, KapuaId operationId, KapuaResponseMessage<?, ?> responseMessageMessage) throws KapuaException { DeviceManagementOperationQuery query = DEVICE_MANAGEMENT_OPERATION_FACTORY.newQuery(scopeId); query.setPredicate( new AndPredicateImpl( new AttributePredicateImpl<>(DeviceManagementOperationAttributes.DEVICE_ID, deviceId), new AttributePredicateImpl<>(DeviceManagementOperationAttributes.OPERATION_ID, operationId) ) ); DeviceManagementOperation deviceManagementOperation = DEVICE_MANAGEMENT_OPERATION_REGISTRY_SERVICE.query(query).getFirstItem(); if (deviceManagementOperation == null) { throw new KapuaEntityNotFoundException(DeviceManagementOperation.TYPE, operationId); } deviceManagementOperation.setStatus(responseMessageMessage.getResponseCode().isAccepted() ? OperationStatus.COMPLETED : OperationStatus.FAILED); deviceManagementOperation.setEndedOn(responseMessageMessage.getReceivedOn()); KapuaSecurityUtils.doPrivileged(() -> DEVICE_MANAGEMENT_OPERATION_REGISTRY_SERVICE.update(deviceManagementOperation)); }
@Override public Map<String, Object> getConfigValues(KapuaId scopeId) throws KapuaException { KapuaLocator locator = KapuaLocator.getInstance(); AuthorizationService authorizationService = locator.getService(AuthorizationService.class); PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class); authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.read, scopeId)); AndPredicateImpl predicate = new AndPredicateImpl() .and(new AttributePredicateImpl<>(ServiceConfigAttributes.SERVICE_ID, pid, Operator.EQUAL)) .and(new AttributePredicateImpl<>(KapuaEntityAttributes.SCOPE_ID, scopeId, Operator.EQUAL)); ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId); query.setPredicate(predicate); ServiceConfigListResult result = entityManagerSession.onResult(em -> ServiceDAO.query(em, ServiceConfig.class, ServiceConfigImpl.class, new ServiceConfigListResultImpl(), query)); Properties properties = null; if (result != null && !result.isEmpty()) { properties = result.getFirstItem().getConfigurations(); } KapuaTocd ocd = getConfigMetadata(scopeId); return toValues(ocd, properties); }
@Override public Map<String, Object> getConfigValues(KapuaId scopeId) throws KapuaException { KapuaLocator locator = KapuaLocator.getInstance(); AuthorizationService authorizationService = locator.getService(AuthorizationService.class); PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class); authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.read, scopeId)); AndPredicateImpl predicate = new AndPredicateImpl() .and(new AttributePredicateImpl<>(ServiceConfigAttributes.SERVICE_ID, pid, Operator.EQUAL)) .and(new AttributePredicateImpl<>(KapuaEntityAttributes.SCOPE_ID, scopeId, Operator.EQUAL)); ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId); query.setPredicate(predicate); ServiceConfigListResult result = entityManagerSession.onResult(em -> ServiceDAO.query(em, ServiceConfig.class, ServiceConfigImpl.class, new ServiceConfigListResultImpl(), query)); Properties properties = null; if (result != null && !result.isEmpty()) { properties = result.getFirstItem().getConfigurations(); } KapuaTocd ocd = getConfigMetadata(scopeId); return toValues(ocd, properties); }
new AndPredicateImpl( new AttributePredicateImpl<>(GroupAttributes.NAME, group.getName()), new AttributePredicateImpl<>(GroupAttributes.ENTITY_ID, group.getId(), Operator.NOT_EQUAL)
new AndPredicateImpl( new AttributePredicateImpl<>(RoleAttributes.NAME, role.getName()), new AttributePredicateImpl<>(RoleAttributes.ENTITY_ID, role.getId(), Operator.NOT_EQUAL)
new AndPredicateImpl( new AttributePredicateImpl<>(TagAttributes.NAME, tag.getName()), new AttributePredicateImpl<>(TagAttributes.ENTITY_ID, tag.getId(), Operator.NOT_EQUAL)