Example #1
0
  private synchronized void init() {
    if (initialised) {
      return;
    }
    attributeNameToResourceName = getAttributeNameToResourceName(schema);
    hiddenAttributeNames = getHiddenAttributeNames();

    for (Object attributeName : schema.getAttributeSchemaNames()) {
      AttributeSchemaConverter attributeSchemaConverter;

      final AttributeSchema attributeSchema =
          this.schema.getAttributeSchema((String) attributeName);
      final AttributeSchema.Syntax syntax = attributeSchema.getSyntax();

      attributeSchemaConverter = getAttributeSchemaValue(syntax);

      final String resourceName = attributeSchema.getResourceName();
      if (resourceName == null) {
        attributeSchemaConverters.put((String) attributeName, attributeSchemaConverter);
      } else {
        attributeSchemaConverters.put(resourceName, attributeSchemaConverter);
      }
    }

    resourceNameToAttributeName = attributeNameToResourceName.inverse();
    attributeNameToSection = getAttributeNameToSection();

    initialised = true;
  }
Example #2
0
  /**
   * Discards attributes from the builder.
   *
   * @param discard Set of attribute names to be discarded.
   */
  public void discardAttribute(Set discard) {
    for (Iterator i = mapTypeToAttributeSchema.keySet().iterator(); i.hasNext(); ) {
      SchemaType type = (SchemaType) i.next();
      Set attributeSchema = (Set) mapTypeToAttributeSchema.get(type);

      if ((attributeSchema != null) && !attributeSchema.isEmpty()) {
        for (Iterator j = attributeSchema.iterator(); j.hasNext(); ) {
          AttributeSchema as = (AttributeSchema) j.next();
          if (discard.contains(as.getName())) {
            j.remove();
          }
        }
      }
    }
  }
Example #3
0
  private Set getAttributeSchemas(SchemaType type) throws SMSException {
    Set results = null;
    ServiceSchema schema = svcSchemaManager.getSchema(type);

    if (schema != null) {
      Set attributes = schema.getAttributeSchemas();

      if ((attributes != null) && !attributes.isEmpty()) {
        results = new HashSet(attributes.size() * 2);
        for (Iterator iter = attributes.iterator(); iter.hasNext(); ) {
          AttributeSchema as = (AttributeSchema) iter.next();
          String i18nKey = as.getI18NKey();

          if ((i18nKey != null) && (i18nKey.trim().length() > 0)) {
            results.add(as);
          }
        }
      }
    }

    return results;
  }
Example #4
0
 private boolean shouldBeIgnored(String attributeName) {
   final AttributeSchema attributeSchema = schema.getAttributeSchema(attributeName);
   return attributeSchema == null
       || StringUtils.isBlank(attributeSchema.getI18NKey())
       || hiddenAttributeNames.contains(attributeName);
 }
Example #5
0
  /**
   * Will validate the Map representation of the service configuration against the serviceSchema and
   * return a corresponding JSON representation
   *
   * @param attributeValuePairs The schema attribute values.
   * @param realm The realm, or null if global.
   * @return Json representation of attributeValuePairs
   */
  public JsonValue toJson(String realm, Map<String, Set<String>> attributeValuePairs) {
    if (!initialised) {
      init();
    }
    final boolean validAttributes;
    try {
      if (realm == null) {
        validAttributes = schema.validateAttributes(attributeValuePairs);
      } else {
        validAttributes = schema.validateAttributes(attributeValuePairs, realm);
      }
    } catch (SMSException e) {
      debug.error(
          "schema validation threw an exception while validating the attributes: realm="
              + realm
              + " attributes: "
              + attributeValuePairs,
          e);
      throw new JsonException("Unable to validate attributes", e);
    }

    JsonValue parentJson = json(new HashMap<String, Object>());

    if (validAttributes) {
      for (String attributeName : attributeValuePairs.keySet()) {
        String jsonResourceName = attributeNameToResourceName.get(attributeName);

        String name;
        if (jsonResourceName != null) {
          name = jsonResourceName;
        } else {
          name = attributeName;
        }

        AttributeSchema attributeSchema = schema.getAttributeSchema(attributeName);

        if (shouldBeIgnored(attributeName)) {
          continue;
        }

        AttributeSchema.Type type = attributeSchema.getType();
        final Set<String> object = attributeValuePairs.get(attributeName);

        Object jsonAttributeValue = null;

        if (type == null) {
          throw new JsonException("Type not defined.");
        }

        AttributeSchemaConverter attributeSchemaConverter = attributeSchemaConverters.get(name);

        if (isASingleValue(type)) {
          if (!object.isEmpty()) {
            jsonAttributeValue = attributeSchemaConverter.toJson(object.iterator().next());
          }
        } else if (containsMultipleValues(type)) {
          if (isAMap(attributeSchema.getUIType())) {
            Map<String, Object> map = new HashMap<String, Object>();

            Iterator<String> itr = object.iterator();
            while (itr.hasNext()) {
              Pair<String, String> entry = nameValueParser.parse(itr.next());
              map.put(entry.getFirst(), attributeSchemaConverter.toJson(entry.getSecond()));
            }
            jsonAttributeValue = map;
          } else {
            List<Object> list = new ArrayList<Object>();

            Iterator<String> itr = object.iterator();
            while (itr.hasNext()) {
              list.add(attributeSchemaConverter.toJson(itr.next()));
            }
            jsonAttributeValue = list;
          }
        }

        String sectionName = attributeNameToSection.get(attributeName);
        if (sectionName != null) {
          parentJson.putPermissive(
              new JsonPointer("/" + sectionName + "/" + name), jsonAttributeValue);
        } else {
          parentJson.put(name, jsonAttributeValue);
        }
      }
    } else {
      throw new JsonException("Invalid attributes");
    }
    return parentJson;
  }