protected JsonConverterException buildJsonFieldError(final String type, final String value, final Throwable cause) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.JSON_UNMARSHAL_ERROR); getLogger().error("{} - Error unmarshalling JSON payload. Wrong {} value: {}.", internalErrorCode, type, value, cause); final String errorMessage = String.format(UNMARSHAL_JSON_ERROR_TEMPLATE, internalErrorCode); final List<String> errorDetails = new ArrayList<String>(); errorDetails.add(String.format(UNMARSHAL_JSON_FIELD_ERROR_TEMPLATE, type, value)); return new JsonConverterException(errorMessage, errorDetails); }
protected JsonConverterException buildMarshalJsonException(final Object obj, final Throwable cause) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.JSON_MARSHAL_ERROR); getLogger().error("{} - Error marshalling object of type {} to JSON.", internalErrorCode, obj.getClass().getName(), cause); final String errorMessage = String.format(MARSHAL_JSON_ERROR_TEMPLATE, internalErrorCode); return new JsonConverterException(errorMessage); }
protected JsonConverterException buildUnmarshallJsonException(final Class<?> clazz, final Throwable cause) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.JSON_UNMARSHAL_ERROR); getLogger().error("{} - Error unmarshalling JSON payload to class {}.", internalErrorCode, clazz.getName(), cause); final String errorMessage = String.format(UNMARSHAL_JSON_ERROR_TEMPLATE, internalErrorCode); return new JsonConverterException(errorMessage, cause); }
@AfterThrowing(pointcut = "anyTemplateMethodPointcut())", throwing = "ex") public void doTranslationAction(final RuntimeException ex) { if (ex instanceof CatalogException) { throw ex; } else { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.CATALOG_GENERAL_ERROR); LOGGER.error("{} - Internal system error.", internalErrorCode, ex); throw new CatalogException(INTERNAL_SYSTEM_ERROR_KEY, new Object[] {internalErrorCode}, ex); } }
@RequestMapping(value = "/location", method = RequestMethod.PUT) @ResponseBody public CatalogResponseMessage updateMobileComponentsLocation(@RequestBody final CatalogInputMessage message) { LOGGER.debug("Catalog API: updating mobile component locations"); // message contains a list with N CatalogSensorElement instances. This list is ordered by // timestamp. try { final ApiConverterContext context = new ApiConverterContext(message, sensorService, componentService); final List<Component> components = ApiConverter.buildMobileComponentsFromSensorLocationElements(context); LOGGER.debug("Catalog API: build {} components that need update its locations ", components.size()); componentService.updateAll(components); } catch (final Exception ex) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.CATALOG_API_ERROR); LOGGER.error("{} - Error updating location for sensors. ", internalErrorCode, ex); final String errorMessage = String.format(SentiloConstants.INTERNAL_ERROR_MESSAGE_TEMPLATE, internalErrorCode); return new CatalogResponseMessage(errorMessage); } return new CatalogResponseMessage(); }
response.setAlerts(catalogAlerts); } catch (final Exception ex) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.CATALOG_ALERT_API_ERROR); LOGGER.error("{} - Error searching authorized alerts for entity {}. ", internalErrorCode, entityId, ex); final String errorMessage = String.format(SentiloConstants.INTERNAL_ERROR_MESSAGE_TEMPLATE, internalErrorCode);
@RequestMapping(value = "/authorized/provider/{entityId}", method = RequestMethod.GET) @ResponseBody public CatalogResponseMessage getAuthorizedProviders(@PathVariable final String entityId, @RequestParam(required = false) final Map<String, String> parameters) { LOGGER.debug("Catalog API: getting authorized sensors and providers for entity {} ", entityId); final List<AuthorizedProvider> authorizedProviders = new ArrayList<AuthorizedProvider>(); try { final List<Permission> permissions = permissionService.getActivePermissions(entityId); // For every permission object, we define a new AuthorizedProvider object with its list of // sensors. for (final Permission permission : permissions) { final List<CatalogSensor> catalogSensors = catalogSensorService.getSensorsByProvider(permission.getTarget(), parameters); if (!CollectionUtils.isEmpty(catalogSensors)) { authorizedProviders.add(new AuthorizedProvider(permission.getTarget(), permission.getType().toString(), catalogSensors)); } } } catch (final Exception ex) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.CATALOG_API_ERROR); LOGGER.error("{} - Error searching authorized providers. ", internalErrorCode, ex); final String errorMessage = String.format(SentiloConstants.INTERNAL_ERROR_MESSAGE_TEMPLATE, internalErrorCode); return new CatalogResponseMessage(errorMessage); } LOGGER.debug("Catalog API: found {} authorized providers ", authorizedProviders.size()); return new CatalogResponseMessage(authorizedProviders); }
@RequestMapping(value = "/entity/{entityId}/delete", method = RequestMethod.PUT) @ResponseBody public CatalogAlertResponseMessage deleteAlerts(@PathVariable final String entityId, @RequestBody(required = false) final CatalogAlertInputMessage message) { // Solo se pueden borrar alarmas externas de las cuales se es el propietario. // Las alarmas internas solo se pueden borrar via la web. LOGGER.debug("Catalog alert API: deleting alerts. Operation invoked by entity {} ", entityId); try { if (message == null || SentiloUtils.arrayIsEmpty(message.getAlertsIds())) { alertService.deleteOwnAlerts(entityId); LOGGER.debug("Catalog alert API: deleted all alerts from entity {}", entityId); } else if (!SentiloUtils.arrayIsEmpty(message.getAlertsIds())) { alertService.deleteOwnAlerts(message.getAlertsIds(), entityId); LOGGER.debug("Catalog alert API: deleted {} alerts", message.getAlertsIds().length); } } catch (final Exception ex) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.CATALOG_API_ERROR); LOGGER.error("{} - Error deleting alerts. ", internalErrorCode, ex); final String errorMessage = String.format(SentiloConstants.INTERNAL_ERROR_MESSAGE_TEMPLATE, internalErrorCode); return new CatalogAlertResponseMessage(errorMessage); } return new CatalogAlertResponseMessage(); } }
return new CatalogResponseMessage(CatalogResponseMessage.FORBIDDEN, SentiloConstants.RESTRICTED_TO_PROVIDERS_ERROR); } catch (final Exception ex) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.CATALOG_API_ERROR); LOGGER.error("{} - Error updating data into database. ", internalErrorCode, ex); final String errorMessage = String.format(SentiloConstants.INTERNAL_ERROR_MESSAGE_TEMPLATE, internalErrorCode);
return new CatalogResponseMessage(CatalogResponseMessage.FORBIDDEN, SentiloConstants.RESTRICTED_TO_PROVIDERS_ERROR); } catch (final Exception ex) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.CATALOG_API_ERROR); LOGGER.error("{} - Error inserting data into database.", internalErrorCode, ex); final String errorMessage = String.format(SentiloConstants.INTERNAL_ERROR_MESSAGE_TEMPLATE, internalErrorCode);
@RequestMapping(value = "/delete/provider/{providerId}", method = RequestMethod.PUT) @ResponseBody public CatalogResponseMessage deleteProviderChilds(@RequestBody(required = false) final CatalogDeleteInputMessage message, @PathVariable final String providerId) { LOGGER.debug("Catalog API: deleting {} resources ", providerId); try { // The first step must be to validate the <providerId> parameter to verify that actually it // represents a provider and not an application. checkProviderAccess(providerId); if (message == null || SentiloUtils.arrayIsEmpty(message.getSensorsIds()) && SentiloUtils.arrayIsEmpty(message.getComponentsIds())) { providerService.deleteChildren(new Provider(providerId)); LOGGER.debug("Catalog API: deleted all resources"); } else if (!SentiloUtils.arrayIsEmpty(message.getSensorsIds())) { sensorService.deleteSensors(providerId, message.getSensorsIds()); LOGGER.debug("Catalog API: deleted {} sensors", message.getSensorsIds().length); } else if (!SentiloUtils.arrayIsEmpty(message.getComponentsIds())) { componentService.deleteComponents(providerId, message.getComponentsIds()); LOGGER.debug("Catalog API: deleted {} components", message.getComponentsIds().length); } } catch (final NotAllowedActionException naae) { LOGGER.warn("Rejected operation to delete provider's resources because {} doesn't represents a provider.", providerId); return new CatalogResponseMessage(CatalogResponseMessage.FORBIDDEN, SentiloConstants.RESTRICTED_TO_PROVIDERS_ERROR); } catch (final Exception ex) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.CATALOG_API_ERROR); LOGGER.error("{} - Error deleting childs from provider {} . ", internalErrorCode, providerId, ex); final String errorMessage = String.format(SentiloConstants.INTERNAL_ERROR_MESSAGE_TEMPLATE, internalErrorCode); return new CatalogResponseMessage(errorMessage); } return new CatalogResponseMessage(); }
@RequestMapping(value = "/entity/{entityId}", method = RequestMethod.POST) @ResponseBody public CatalogAlertResponseMessage createAlerts(@PathVariable final String entityId, @RequestBody final CatalogAlertInputMessage message) { LOGGER.debug("Catalog alert API: registering new alerts. Operation invoked by entity {} ", entityId); final CatalogUserDetails catalogUser = userDetailsService.getCatalogUserDetails(); try { final ApiAlertConverterContext context = new ApiAlertConverterContext(message, entityId, applicationService, providerService, alertService); final List<Alert> alerts = ApiAlertConverter.buildAlertsFromCatalogAlerts(context, catalogUser.getUsername()); if (!context.getResults().hasErrors()) { validator.validate(alerts, context.getResults(), false); } if (context.getResults().hasErrors()) { final String errorMessage = "Bad request data. Alerts have not been inserted. Please review the following errors"; LOGGER.debug("Catalog alert API: alerts have not been inserted. Found {} errors after validate data. {}", context.getResults().getErrorsCount(), context.getResults().toString()); return new CatalogAlertResponseMessage(CatalogResponseMessage.BAD_REQUEST, errorMessage, context.getResults().getErrors()); } else { alertService.insertAll(alerts); LOGGER.debug("Catalog alert API: inserted {} alerts", alerts.size()); } } catch (final Exception ex) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.CATALOG_API_ERROR); LOGGER.error("{} - Error inserting alerts into database. ", internalErrorCode, ex); final String errorMessage = String.format(SentiloConstants.INTERNAL_ERROR_MESSAGE_TEMPLATE, internalErrorCode); return new CatalogAlertResponseMessage(errorMessage); } return new CatalogAlertResponseMessage(); }
@RequestMapping(value = "/entity/{entityId}", method = RequestMethod.PUT) @ResponseBody public CatalogAlertResponseMessage updateAlerts(@PathVariable final String entityId, @RequestBody final CatalogAlertInputMessage message) { LOGGER.debug("Catalog alert API: updating alerts. Operation invoked by entity {} ", entityId); final CatalogUserDetails catalogUser = userDetailsService.getCatalogUserDetails(); try { final ApiAlertConverterContext context = new ApiAlertConverterContext(message, entityId, applicationService, providerService, alertService, true); final List<Alert> alerts = ApiAlertConverter.buildAlertsFromCatalogAlerts(context, catalogUser.getUsername()); if (!context.getResults().hasErrors()) { validator.validate(alerts, context.getResults(), true); } if (context.getResults().hasErrors()) { final String errorMessage = "Bad request data. Alerts have not been updated. Please review the following errors"; LOGGER.debug("Catalog alert API: alerts have not been updated. Found {} errors after validate data. {}", context.getResults().getErrorsCount(), context.getResults().toString()); return new CatalogAlertResponseMessage(CatalogResponseMessage.BAD_REQUEST, errorMessage, context.getResults().getErrors()); } else { alertService.updateAll(alerts); LOGGER.debug("Catalog alert API: updated {} alerts", alerts.size()); } } catch (final Exception ex) { final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.CATALOG_API_ERROR); LOGGER.error("{} - Error updating alerts into database. ", internalErrorCode, ex); final String errorMessage = String.format(SentiloConstants.INTERNAL_ERROR_MESSAGE_TEMPLATE, internalErrorCode); return new CatalogAlertResponseMessage(errorMessage); } return new CatalogAlertResponseMessage(); }
final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.SENTILO_ACCESS_ERROR); LOGGER.error("{} - Internal access error.", internalErrorCode, e); final String errorMessage = String.format(SentiloConstants.INTERNAL_ERROR_MESSAGE_TEMPLATE, internalErrorCode); final String internalErrorCode = SentiloUtils.buildNewInternalErrorCode(SentiloConstants.SENTILO_UNKNOWN_ERROR); LOGGER.error("{} - Internal server error.", internalErrorCode, e); final String errorMessage = String.format(SentiloConstants.INTERNAL_ERROR_MESSAGE_TEMPLATE, internalErrorCode);