/**
  * Tries to locate resource id attribute
  *
  * @param result an evaluation result
  * @return a resource id attribute
  */
 private String getResourceId(Result result) {
   if (log.isDebugEnabled()) {
     log.debug("Mapping result=\"{}\" to resourceId", result);
   }
   Category resource = result.getAttribute(Categories.RESOURCE);
   if (resource == null) {
     return null;
   }
   Collection<Attribute> attrs = resource.getEntity().getAttributes(RESOURCE_ID);
   if (attrs.size() == 1) {
     Attribute resourceId = Iterables.getOnlyElement(attrs);
     AttributeExp v = Iterables.getOnlyElement(resourceId.getValues());
     Optional<TypeToString> toString = TypeToString.Types.getIndex().get(v.getType());
     Preconditions.checkState(toString.isPresent());
     return toString.get().toString(v);
   }
   Collection<AttributeExp> values =
       resource.getEntity().getAttributeValues(CONTENT_SELECTOR, XacmlTypes.XPATH);
   if (values.isEmpty() || values.size() > 1) {
     return null;
   }
   AttributeExp v = Iterables.getOnlyElement(values);
   Optional<TypeToString> toString = TypeToString.Types.getIndex().get(v.getType());
   Preconditions.checkState(toString.isPresent());
   return toString.get().toString(v);
 }
 @Test
 public void testEntityMarshall() throws Exception {
   Entity entity =
       Entity.builder()
           .content(sampleContent1())
           .attribute(
               Attribute.builder("testId3")
                   .value(StringExp.of("aaa"))
                   .value(StringExp.of("bbbb"))
                   .value(StringExp.of("cccc"))
                   .build())
           .attribute(
               Attribute.builder("testId4")
                   .value(StringExp.of("zzzz"))
                   .value(StringExp.of("aaaa"))
                   .value(StringExp.of("cccc"))
                   .build())
           .build();
   Category a =
       Category.builder()
           .category(Categories.SUBJECT_ACCESS)
           .entity(
               Entity.builder()
                   .attribute(Attribute.builder("testId1").value(EntityExp.of(entity)).build())
                   .build())
           .build();
   JsonElement o = json.toJsonTree(a);
   Category b = json.fromJson(o, Category.class);
   assertEquals(a, b);
 }
Example #3
0
 @Override
 public JsonElement serialize(Attribute src, Type typeOfSrc, JsonSerializationContext context) {
   JsonObject o = new JsonObject();
   o.addProperty(ATTRIBUTE_ID_PROPERTY, src.getAttributeId());
   if (src.getIssuer() != null) {
     o.addProperty(ISSUER_PROPERTY, src.getIssuer());
   }
   Collection<AttributeExp> values = src.getValues();
   serializeValue(context, o, values);
   // OMIT property if value is "false"
   if (src.isIncludeInResult()) {
     o.addProperty(INCLUDE_IN_RESULT_PROPERTY, src.isIncludeInResult());
   }
   return o;
 }