use of org.eclipse.smarthome.config.core.validation.ConfigValidationException in project smarthome by eclipse.
the class ConfigDescriptionValidatorImpl method validate.
/**
* Validates the given configuration parameters against the given configuration description having the given URI.
*
* @param configurationParameters the configuration parameters to be validated
* @param configDescriptionURI the URI of the configuration description against which the configuration parameters
* are to be validated
*
* @throws ConfigValidationException if one or more configuration parameters do not match with the configuration
* description having the given URI
* @throws NullPointerException if given config description URI or configuration parameters are null
*/
@Override
@SuppressWarnings("unchecked")
public void validate(Map<String, Object> configurationParameters, URI configDescriptionURI) {
Objects.requireNonNull(configurationParameters, "Configuration parameters must not be null");
Objects.requireNonNull(configDescriptionURI, "Config description URI must not be null");
ConfigDescription configDescription = getConfigDescription(configDescriptionURI);
if (configDescription == null) {
logger.warn("Skipping config description validation because no config description found for URI '{}'", configDescriptionURI);
return;
}
Map<String, ConfigDescriptionParameter> map = configDescription.toParametersMap();
Collection<ConfigValidationMessage> configDescriptionValidationMessages = new ArrayList<>();
for (String key : configurationParameters.keySet()) {
ConfigDescriptionParameter configDescriptionParameter = map.get(key);
if (configDescriptionParameter != null) {
// If the parameter supports multiple selection, then it may be provided as an array
if (configDescriptionParameter.isMultiple() && configurationParameters.get(key) instanceof List) {
// Perform validation on each value in the list separately
for (Object value : (List<Object>) configurationParameters.get(key)) {
ConfigValidationMessage message = validateParameter(configDescriptionParameter, value);
if (message != null) {
configDescriptionValidationMessages.add(message);
}
}
} else {
ConfigValidationMessage message = validateParameter(configDescriptionParameter, configurationParameters.get(key));
if (message != null) {
configDescriptionValidationMessages.add(message);
}
}
}
}
if (!configDescriptionValidationMessages.isEmpty()) {
throw new ConfigValidationException(bundleContext.getBundle(), translationProvider, configDescriptionValidationMessages);
}
}
use of org.eclipse.smarthome.config.core.validation.ConfigValidationException in project smarthome by eclipse.
the class ThingResource method updateConfiguration.
/**
* Updates Thing configuration.
*
* @param thingUID
* @param configurationParameters
* @return Response with the updated Thing or error information
* @throws IOException
*/
@PUT
@RolesAllowed({ Role.ADMIN })
@Path("/{thingUID}/config")
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Updates thing's configuration.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = Thing.class), @ApiResponse(code = 400, message = "Configuration of the thing is not valid."), @ApiResponse(code = 404, message = "Thing not found"), @ApiResponse(code = 409, message = "Thing could not be updated as it is not editable.") })
public Response updateConfiguration(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) String language, @PathParam("thingUID") @ApiParam(value = "thing") String thingUID, @ApiParam(value = "configuration parameters") Map<String, Object> configurationParameters) throws IOException {
final Locale locale = LocaleUtil.getLocale(language);
ThingUID thingUIDObject = new ThingUID(thingUID);
// ask whether the Thing exists at all, 404 otherwise
Thing thing = thingRegistry.get(thingUIDObject);
if (null == thing) {
logger.info("Received HTTP PUT request for update configuration at '{}' for the unknown thing '{}'.", uriInfo.getPath(), thingUID);
return getThingNotFoundResponse(thingUID);
}
// ask whether the Thing exists as a managed thing, so it can get
// updated, 409 otherwise
Thing managed = managedThingProvider.get(thingUIDObject);
if (null == managed) {
logger.info("Received HTTP PUT request for update configuration at '{}' for an unmanaged thing '{}'.", uriInfo.getPath(), thingUID);
return getThingResponse(Status.CONFLICT, thing, locale, "Cannot update Thing " + thingUID + " as it is not editable.");
}
// only move on if Thing is known to be managed, so it can get updated
try {
// note that we create a Configuration instance here in order to
// have normalized types
thingRegistry.updateConfiguration(thingUIDObject, new Configuration(normalizeConfiguration(configurationParameters, thing.getThingTypeUID(), thing.getUID())).getProperties());
} catch (ConfigValidationException ex) {
logger.debug("Config description validation exception occurred for thingUID {} - Messages: {}", thingUID, ex.getValidationMessages());
return Response.status(Status.BAD_REQUEST).entity(ex.getValidationMessages(locale)).build();
} catch (Exception ex) {
logger.error("Exception during HTTP PUT request for update config at '{}'", uriInfo.getPath(), ex);
return JSONResponse.createResponse(Status.INTERNAL_SERVER_ERROR, null, ex.getMessage());
}
return getThingResponse(Status.OK, thing, locale, null);
}
Aggregations