Search in sources :

Example 1 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class ServiceDetectorRegistryImpl method createDetector.

private static ServiceDetector createDetector(ServiceDetectorFactory<? extends ServiceDetector> factory, Map<String, String> properties) {
    if (factory == null) {
        return null;
    }
    final ServiceDetector detector = factory.createDetector();
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(detector);
    wrapper.setPropertyValues(properties);
    return detector;
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) ServiceDetector(org.opennms.netmgt.provision.ServiceDetector)

Example 2 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class MonitoringLocationsRestService method updateMonitoringLocation.

@PUT
@Path("{monitoringLocation}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Transactional
public Response updateMonitoringLocation(@PathParam("monitoringLocation") String monitoringLocation, MultivaluedMapImpl params) {
    writeLock();
    try {
        OnmsMonitoringLocation def = m_monitoringLocationDao.get(monitoringLocation);
        LOG.debug("updateMonitoringLocation: updating monitoring location {}", monitoringLocation);
        if (params.isEmpty())
            return Response.notModified().build();
        boolean modified = false;
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(def);
        wrapper.registerCustomEditor(Duration.class, new StringIntervalPropertyEditor());
        for (final String key : params.keySet()) {
            if (wrapper.isWritableProperty(key)) {
                Object value = null;
                String stringValue = params.getFirst(key);
                value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
                wrapper.setPropertyValue(key, value);
                modified = true;
            }
        }
        if (modified) {
            LOG.debug("updateMonitoringLocation: monitoring location {} updated", monitoringLocation);
            m_monitoringLocationDao.save(def);
            return Response.noContent().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) StringIntervalPropertyEditor(org.opennms.netmgt.provision.persist.StringIntervalPropertyEditor) OnmsMonitoringLocation(org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class NodeRestService method updateCategoryForNode.

@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/{nodeCriteria}/categories/{categoryName}")
public Response updateCategoryForNode(@PathParam("nodeCriteria") final String nodeCriteria, @PathParam("categoryName") final String categoryName, MultivaluedMapImpl params) {
    writeLock();
    try {
        OnmsNode node = m_nodeDao.get(nodeCriteria);
        if (node == null) {
            throw getException(Status.BAD_REQUEST, "Node {} was not found.", nodeCriteria);
        }
        OnmsCategory category = getCategory(node, categoryName);
        if (category == null) {
            throw getException(Status.BAD_REQUEST, "Category {} not found on node {}", categoryName, nodeCriteria);
        }
        LOG.debug("updateCategory: updating category {}", category);
        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(category);
        boolean updated = false;
        for (String key : params.keySet()) {
            if (wrapper.isWritableProperty(key)) {
                String stringValue = params.getFirst(key);
                Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
                wrapper.setPropertyValue(key, value);
                updated = true;
            }
        }
        if (updated) {
            LOG.debug("updateCategory: category {} updated", category);
            m_categoryDao.saveOrUpdate(category);
        } else {
            LOG.debug("updateCategory: no fields updated in category {}", category);
        }
        return Response.noContent().build();
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsCategory(org.opennms.netmgt.model.OnmsCategory) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 4 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class UserRestService method updateUser.

@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("{userCriteria}")
public Response updateUser(@Context final SecurityContext securityContext, @PathParam("userCriteria") final String userCriteria, final MultivaluedMapImpl params) {
    writeLock();
    try {
        if (!hasEditRights(securityContext)) {
            throw getException(Status.BAD_REQUEST, "User {} does not have write access to users!", securityContext.getUserPrincipal().getName());
        }
        final OnmsUser user = getOnmsUser(userCriteria);
        LOG.debug("updateUser: updating user {}", user);
        boolean modified = false;
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(user);
        for (final String key : params.keySet()) {
            if (wrapper.isWritableProperty(key)) {
                final String stringValue = params.getFirst(key);
                final Object value = wrapper.convertIfNecessary(stringValue, wrapper.getPropertyType(key));
                wrapper.setPropertyValue(key, value);
                modified = true;
            }
        }
        if (modified) {
            LOG.debug("updateUser: user {} updated", user);
            try {
                m_userManager.save(user);
            } catch (final Throwable t) {
                throw getException(Status.INTERNAL_SERVER_ERROR, t);
            }
            return Response.noContent().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsUser(org.opennms.netmgt.model.OnmsUser) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 5 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class CategoryRestService method updateCategory.

@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/{categoryName}")
public Response updateCategory(@PathParam("categoryName") final String categoryName, final MultivaluedMapImpl params) {
    writeLock();
    try {
        OnmsCategory category = m_categoryDao.findByName(categoryName);
        if (category == null) {
            throw getException(Status.BAD_REQUEST, "Category with name '{}' was not found.", categoryName);
        }
        LOG.debug("updateCategory: updating category {}", category);
        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(category);
        boolean modified = false;
        for (String key : params.keySet()) {
            if (wrapper.isWritableProperty(key)) {
                String stringValue = params.getFirst(key);
                Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
                wrapper.setPropertyValue(key, value);
                modified = true;
            }
        }
        LOG.debug("updateCategory: category {} updated", category);
        if (modified) {
            m_categoryDao.saveOrUpdate(category);
            return Response.noContent().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsCategory(org.opennms.netmgt.model.OnmsCategory) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Aggregations

BeanWrapper (org.springframework.beans.BeanWrapper)144 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)79 Test (org.junit.jupiter.api.Test)31 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)18 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)18 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 Consumes (javax.ws.rs.Consumes)16 PUT (javax.ws.rs.PUT)16 Path (javax.ws.rs.Path)16 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 HashSet (java.util.HashSet)12 BeansException (org.springframework.beans.BeansException)12 PropertyDescriptor (java.beans.PropertyDescriptor)11 OnmsNode (org.opennms.netmgt.model.OnmsNode)9 Test (org.junit.Test)6 BigDecimal (java.math.BigDecimal)5