Search in sources :

Example 11 with InvalidAttributeValueException

use of javax.management.InvalidAttributeValueException in project oxTrust by GluuFederation.

the class Scim2PatchService method applyPatchOperationWithValueFilter.

private BaseScimResource applyPatchOperationWithValueFilter(BaseScimResource resource, PatchOperation operation, String valSelFilter, String attribute, String subAttribute) throws SCIMException, InvalidAttributeValueException {
    String path = operation.getPath();
    ObjectMapper mapper = new ObjectMapper();
    Class<? extends BaseScimResource> cls = resource.getClass();
    Map<String, Object> resourceAsMap = mapper.convertValue(resource, new TypeReference<Map<String, Object>>() {
    });
    List<Map<String, Object>> list;
    Attribute attrAnnot = IntrospectUtil.getFieldAnnotation(attribute, cls, Attribute.class);
    if (attrAnnot != null) {
        if (!attrAnnot.multiValueClass().equals(NullType.class) && attrAnnot.type().equals(AttributeDefinition.Type.COMPLEX)) {
            Object colObject = resourceAsMap.get(attribute);
            list = colObject == null ? null : new ArrayList<Map<String, Object>>((Collection<Map<String, Object>>) colObject);
        } else
            throw new SCIMException(String.format("Attribute '%s' expected to be complex multi-valued", attribute));
    } else
        throw new SCIMException(String.format("Attribute '%s' not recognized or expected to be complex multi-valued", attribute));
    if (list == null)
        log.info("applyPatchOperationWithValueFilter. List of values for {} is empty. Operation has no effect", attribute);
    else {
        try {
            valSelFilter = FilterUtil.preprocess(valSelFilter, cls);
            ParseTree parseTree = filterService.getParseTree(valSelFilter);
            List<Integer> matchingIndexes = new ArrayList<Integer>();
            for (int i = 0; i < list.size(); i++) {
                if (filterService.complexAttributeMatch(parseTree, list.get(i), attribute, cls))
                    // Important: add so that resulting list is reverse-ordered
                    matchingIndexes.add(0, i);
            }
            if (subAttribute.length() > 0 && matchingIndexes.size() > 0 && operation.getType().equals(PatchOperationType.REMOVE)) {
                // per spec (section 3.5.2.2 RFC 7644) subAttribute must not be required or read-only
                Attribute subAttrAnnot = IntrospectUtil.getFieldAnnotation(attribute + "." + subAttribute, cls, Attribute.class);
                if (subAttrAnnot != null && (subAttrAnnot.mutability().equals(READ_ONLY) || subAttrAnnot.isRequired()))
                    throw new InvalidAttributeValueException("Cannot remove read-only or required attribute " + attribute + "." + subAttribute);
            }
            /*
                Here we differ from spec (see section 3.5.2.3/4 of RFC7644. If no record match is made, we are supposed to
                return error 400 with scimType of noTarget. But this is clearly inconvenient
                */
            log.info("There are {} entries matching the filter '{}'", matchingIndexes.size(), path);
            for (Integer index : matchingIndexes) {
                if (operation.getType().equals(PatchOperationType.REMOVE)) {
                    if (// Remove the whole item
                    subAttribute.length() == 0)
                        // If intValue is not used, the remove(Object) method is called!
                        list.remove(index.intValue());
                    else
                        // remove subattribute only
                        list.get(index).remove(subAttribute);
                } else
                    applyPartialUpdate(attribute, subAttribute, list, index, operation.getValue(), cls);
            }
            log.trace("New {} list is:\n{}", attribute, mapper.writeValueAsString(list));
            resourceAsMap.put(attribute, list.size() == 0 ? null : list);
            resource = mapper.convertValue(resourceAsMap, cls);
        } catch (InvalidAttributeValueException ei) {
            throw ei;
        } catch (Exception e) {
            log.info("Error processing Patch operation with value selection path '{}'", path);
            log.error(e.getMessage(), e);
            throw new SCIMException(e.getMessage(), e);
        }
    }
    return resource;
}
Also used : Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 12 with InvalidAttributeValueException

use of javax.management.InvalidAttributeValueException in project oxTrust by GluuFederation.

the class ScimResourceUtil method transferToResource.

private static BaseScimResource transferToResource(BaseScimResource origin, final BaseScimResource destination, List<Extension> extensions, boolean replacing) throws InvalidAttributeValueException {
    log.debug("transferToResource. Processing {} operation", replacing ? "replace" : "add");
    Map<String, Object> fromMap = mapper.convertValue(origin, new TypeReference<Map<String, Object>>() {
    });
    Map<String, Object> toMap = mapper.convertValue(destination, new TypeReference<Map<String, Object>>() {
    });
    log.debug("transferToResource. Recursive traversal of resource is taking place");
    traversalClass tclass = new traversalClass(origin.getClass());
    tclass.traverse("", fromMap, toMap, replacing);
    attachExtensionInfo(fromMap, toMap, extensions, replacing);
    if (tclass.error == null)
        return mapper.convertValue(toMap, origin.getClass());
    else
        throw new InvalidAttributeValueException(tclass.error);
}
Also used : InvalidAttributeValueException(javax.management.InvalidAttributeValueException)

Example 13 with InvalidAttributeValueException

use of javax.management.InvalidAttributeValueException in project spf4j by zolyfarkas.

the class ExportedValuesMBean method setAttributes.

/**
 * {@inheritDoc}
 */
@Override
public AttributeList setAttributes(final AttributeList list) {
    AttributeList result = new AttributeList(list.size());
    for (Attribute attr : list.asList()) {
        ExportedValue<Object> eval = (ExportedValue<Object>) exportedValues.get(attr.getName());
        if (eval != null) {
            try {
                eval.set(attr.getValue());
                result.add(attr);
            } catch (InvalidAttributeValueException | InvalidObjectException | RuntimeException ex) {
                LOG.warn("Exception while setting attr {}", attr, ex);
                throw new JMRuntimeException("Exception while setting attributes " + list + ", detail:\n" + Throwables.toString(ex));
            }
        }
    }
    return result;
}
Also used : JMRuntimeException(javax.management.JMRuntimeException) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) InvalidObjectException(java.io.InvalidObjectException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) JMRuntimeException(javax.management.JMRuntimeException)

Example 14 with InvalidAttributeValueException

use of javax.management.InvalidAttributeValueException in project spf4j by zolyfarkas.

the class RegistryTest method testRegistry.

@Test
public void testRegistry() throws InterruptedException, IOException, InstanceNotFoundException, MBeanException, AttributeNotFoundException, ReflectionException, InvalidAttributeValueException {
    JmxTest testObj = new JmxTest();
    Properties props = new Properties();
    props.setProperty("propKey", "propvalue");
    Registry.export("caca", "maca", props);
    Registry.export("test", "Test", props, testObj);
    Registry.registerMBean("test2", "TestClassic", new org.spf4j.jmx.Test());
    Map<String, Object> map = new HashMap<>();
    map.put("isCrap", Boolean.TRUE);
    map.put("a.crap", Boolean.FALSE);
    map.put("isNonsense", "bla");
    map.put("", "bla");
    Registry.export("testMap", "map", map, testObj);
    // Thread.sleep(300000);
    Client.setAttribute("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi", "test", "Test", "booleanFlag", Boolean.TRUE);
    Client.setAttribute("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi", "test", "Test", "propKey", "caca");
    Object ret = Client.getAttribute("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi", "test", "Test", "booleanFlag");
    Assert.assertEquals(Boolean.TRUE, ret);
    String prop = (String) Client.getAttribute("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi", "test", "Test", "propKey");
    Assert.assertEquals("caca", prop);
    Assert.assertEquals("caca", props.get("propKey"));
    CompositeData cd = (CompositeData) Client.getAttribute("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi", "test", "Test", "columnDef");
    LOG.debug("CD={}", cd);
    Assert.assertEquals("bla", cd.get("name"));
    CompositeData cd2 = (CompositeData) Client.callOperation("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi", "test", "Test", "getColumnDef", "bubu");
    LOG.debug("CD2={}", cd2);
    Assert.assertEquals("bubu", cd2.get("name"));
    CompositeData cd3 = (CompositeData) Client.callOperation("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi", "test", "Test", "echo", cd2);
    LOG.debug("CD3={}", cd3);
    Assert.assertEquals("bubu", cd3.get("name"));
    Client.callOperation("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi", "test", "Test", "print", 3, Boolean.TRUE, "caca", cd2);
    Client.setAttribute("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi", "test", "Test", "stringVal", "bla bla");
    Object ret2 = Client.getAttribute("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi", "test", "Test", "stringVal");
    Assert.assertEquals("bla bla", ret2);
    try {
        Client.setAttribute("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi", "test", "Test", "doubleVal", 0.0);
        Assert.fail();
    } catch (InvalidAttributeValueException e) {
        Throwables.writeTo(e, System.err, Throwables.PackageDetail.SHORT);
    }
// Thread.sleep(1000000000);
}
Also used : HashMap(java.util.HashMap) CompositeData(javax.management.openmbean.CompositeData) Properties(java.util.Properties) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) Test(org.junit.Test)

Example 15 with InvalidAttributeValueException

use of javax.management.InvalidAttributeValueException in project jspwiki by apache.

the class SimpleMBean method setAttribute.

public void setAttribute(Attribute attr) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
    Method m;
    String mname = "set" + StringUtils.capitalize(attr.getName());
    m = findGetterSetter(getClass(), mname, attr.getValue().getClass());
    if (m == null)
        throw new AttributeNotFoundException(attr.getName());
    Object[] args = { attr.getValue() };
    try {
        m.invoke(this, args);
    } catch (IllegalArgumentException e) {
        throw new InvalidAttributeValueException("Faulty argument: " + e.getMessage());
    } catch (IllegalAccessException e) {
        throw new ReflectionException(e, "Cannot access attribute " + e.getMessage());
    } catch (InvocationTargetException e) {
        throw new ReflectionException(e, "Cannot invoke attribute " + e.getMessage());
    }
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) Method(java.lang.reflect.Method) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

InvalidAttributeValueException (javax.management.InvalidAttributeValueException)39 AttributeNotFoundException (javax.management.AttributeNotFoundException)26 ReflectionException (javax.management.ReflectionException)24 MBeanException (javax.management.MBeanException)23 Attribute (javax.management.Attribute)19 InstanceNotFoundException (javax.management.InstanceNotFoundException)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 RuntimeOperationsException (javax.management.RuntimeOperationsException)8 AttributeList (javax.management.AttributeList)6 IntrospectionException (javax.management.IntrospectionException)6 SCIMException (org.gluu.oxtrust.model.exception.SCIMException)6 Test (org.testng.annotations.Test)6 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 Method (java.lang.reflect.Method)5 URI (java.net.URI)5 ListenerNotFoundException (javax.management.ListenerNotFoundException)5 RuntimeErrorException (javax.management.RuntimeErrorException)5 Consumes (javax.ws.rs.Consumes)5 DefaultValue (javax.ws.rs.DefaultValue)5 HeaderParam (javax.ws.rs.HeaderParam)5