Search in sources :

Example 1 with ConfigValidationException

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);
    }
}
Also used : ConfigValidationMessage(org.eclipse.smarthome.config.core.validation.ConfigValidationMessage) ArrayList(java.util.ArrayList) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ArrayList(java.util.ArrayList) List(java.util.List) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException)

Example 2 with ConfigValidationException

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);
}
Also used : Locale(java.util.Locale) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException) Thing(org.eclipse.smarthome.core.thing.Thing) URISyntaxException(java.net.URISyntaxException) BadRequestException(javax.ws.rs.BadRequestException) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException) IOException(java.io.IOException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ConfigValidationException (org.eclipse.smarthome.config.core.validation.ConfigValidationException)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 BadRequestException (javax.ws.rs.BadRequestException)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 ConfigDescription (org.eclipse.smarthome.config.core.ConfigDescription)1 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 ConfigValidationMessage (org.eclipse.smarthome.config.core.validation.ConfigValidationMessage)1 Thing (org.eclipse.smarthome.core.thing.Thing)1 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)1