Пример #1
0
  private ToolStrip setupTableButtons() {
    ToolStrip tools = new ToolStrip();
    ToolButton addButton =
        new ToolButton(
            Console.CONSTANTS.common_label_add(),
            event -> {
              ModelNodeFormBuilder.FormAssets addFormAssets =
                  new ModelNodeFormBuilder()
                      .setResourceDescription(resourceDescription)
                      .setCreateMode(true)
                      .unsorted()
                      .exclude("permission-mappings")
                      .setCreateNameAttribute(false)
                      .setSecurityContext(securityContext)
                      .build();
              addFormAssets.getForm().setEnabled(true);

              DefaultWindow dialog =
                  new DefaultWindow(Console.MESSAGES.newTitle("Permission Mapping"));
              AddResourceDialog.Callback callback =
                  new AddResourceDialog.Callback() {
                    @Override
                    public void onAdd(ModelNode payload) {
                      circuit.dispatch(
                          new AddListAttribute(
                              ElytronStore.SIMPLE_PERMISSION_MAPPER_ADDRESS,
                              "permission-mappings",
                              permissionMapping,
                              payload));
                      dialog.hide();
                    }

                    @Override
                    public void onCancel() {
                      dialog.hide();
                    }
                  };
              AddResourceDialog addDialog =
                  new AddResourceDialog(addFormAssets, resourceDescription, callback);
              dialog.setWidth(480);
              dialog.setHeight(360);
              dialog.setWidget(addDialog);
              dialog.setGlassEnabled(true);
              dialog.center();
            });
    ToolButton removeButton =
        new ToolButton(
            Console.CONSTANTS.common_label_delete(),
            event -> {
              final ModelNode selection = selectionModel.getSelectedObject();
              if (selection != null) {
                Feedback.confirm(
                    "Filter",
                    Console.MESSAGES.deleteConfirm("Permission Mapping " + selection.asString()),
                    isConfirmed -> {
                      if (isConfirmed) {
                        circuit.dispatch(
                            new RemoveListAttribute(
                                ElytronStore.SIMPLE_PERMISSION_MAPPER_ADDRESS,
                                permissionMapping,
                                "permission-mappings",
                                selection));
                      }
                    });
              }
            });
    tools.addToolButtonRight(addButton);
    tools.addToolButtonRight(removeButton);
    return tools;
  }
Пример #2
0
  private void _showAddDialog(
      final ModelNode address,
      boolean isSingleton,
      SecurityContext securityContext,
      ModelNode description) {
    List<Property> tuples = address.asPropertyList();
    String type = "";
    if (tuples.size() > 0) {
      type = tuples.get(tuples.size() - 1).getName();
    }

    ModelNodeFormBuilder builder =
        new ModelNodeFormBuilder()
            .setCreateMode(true)
            .setSingleton(isSingleton)
            .setResourceDescription(description)
            .setSecurityContext(securityContext);

    ModelNodeFormBuilder.FormAssets assets = builder.build();

    final ModelNodeForm form = assets.getForm();
    form.setEnabled(true);

    if (form.hasWritableAttributes()) {
      final DefaultWindow window = new DefaultWindow(Console.MESSAGES.createResource(type));
      window.addStyleName("browser-view");

      DialogueOptions options =
          new DialogueOptions(
              new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                  // save
                  FormValidation validation = form.validate();
                  if (!validation.hasErrors()) {
                    presenter.onAddChildResource(address, form.getUpdatedEntity());
                    window.hide();
                  }
                }
              },
              new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                  // cancel
                  window.hide();
                }
              });

      VerticalPanel layout = new VerticalPanel();
      layout.setStyleName("fill-layout-width");
      ModelNode opDescription = description.get("operations").get("add").get("description");
      ContentDescription text = new ContentDescription(opDescription.asString());
      layout.add(text);
      layout.add(assets.asWidget());

      WindowContentBuilder content = new WindowContentBuilder(layout, options);
      window.trapWidget(content.build());
      window.setGlassEnabled(true);
      window.setWidth(480);
      window.setHeight(360);
      window.center();
    } else {
      // no writable attributes
      Feedback.alert(
          Console.CONSTANTS.cannotCreateChildResource(),
          Console.MESSAGES.noConfigurableAttributes(address.toString()));
    }
  }
  private ToolStrip mainTableTools() {
    ToolStrip tools = new ToolStrip();
    ToolButton addButton =
        new ToolButton(
            Console.CONSTANTS.common_label_add(),
            event -> {
              ModelNodeFormBuilder.FormAssets addFormAssets =
                  new ModelNodeFormBuilder()
                      .setResourceDescription(resourceDescription)
                      .setCreateMode(true)
                      .unsorted()
                      .setCreateNameAttribute(false)
                      .setSecurityContext(securityContext)
                      .build();
              addFormAssets.getForm().setEnabled(true);
              addFormAssets
                  .getForm()
                  .addFormValidator(
                      (formItems, formValidation) -> {

                        // at least one field is necessary to fill
                        boolean allEmpty = true;
                        for (FormItem formItem : formItems) {
                          if (!formItem.isUndefined()) {
                            allEmpty = false;
                            break;
                          }
                        }
                        if (allEmpty) {
                          formValidation.addError("match-abstract-type");
                          FormItem mechanismNameFormItem =
                              formItem(formItems, "match-abstract-type");
                          mechanismNameFormItem.setErrMessage(
                              "At least one field must contain valid values.");
                          mechanismNameFormItem.setErroneous(true);
                        }
                      });

              DefaultWindow dialog = new DefaultWindow(Console.MESSAGES.newTitle("Match Rule"));
              AddResourceDialog.Callback callback =
                  new AddResourceDialog.Callback() {
                    @Override
                    public void onAdd(ModelNode payload) {
                      for (Property node : payload.asPropertyList()) {
                        // remove undefined attributes
                        if (!node.getValue().isDefined()) {
                          payload.remove(node.getName());
                        }
                      }
                      circuit.dispatch(
                          new AddListAttribute(
                              ElytronStore.AUTHENTICATION_CONTEXT_ADDRESS,
                              MATCH_RULES,
                              authContextName,
                              payload));
                      dialog.hide();
                    }

                    @Override
                    public void onCancel() {
                      dialog.hide();
                    }
                  };
              AddResourceDialog addDialog =
                  new AddResourceDialog(addFormAssets, resourceDescription, callback);
              dialog.setWidth(480);
              dialog.setHeight(510);
              dialog.setWidget(addDialog);
              dialog.setGlassEnabled(true);
              dialog.center();
            });
    ToolButton removeButton =
        new ToolButton(
            Console.CONSTANTS.common_label_delete(),
            event -> {
              final ModelNode selection = selectionModel.getSelectedObject();
              if (selection != null) {
                Feedback.confirm(
                    "Match Rule",
                    Console.MESSAGES.deleteConfirm("Match Rule " + selection.asString()),
                    isConfirmed -> {
                      if (isConfirmed) {
                        circuit.dispatch(
                            new RemoveListAttribute(
                                ElytronStore.AUTHENTICATION_CONTEXT_ADDRESS,
                                authContextName,
                                MATCH_RULES,
                                selection));
                      }
                    });
              }
            });
    tools.addToolButtonRight(addButton);
    tools.addToolButtonRight(removeButton);
    return tools;
  }
Пример #4
0
  public static Object downCast(ModelNode value, ModelNode metadata) {
    Object result = null;
    ModelType targetType = resolveTypeFromMetaData(metadata);
    switch (targetType) {
      case STRING:
        result = value.asString();
        break;
      case INT:
        result = value.asInt();
        break;
      case LONG:
        result = value.asLong();
        break;
      case BOOLEAN:
        result = value.asBoolean();
        break;
      case BIG_DECIMAL:
        result = value.asBigDecimal();
        break;
      case BIG_INTEGER:
        result = value.asBigInteger();
        break;
      case DOUBLE:
        result = value.asDouble();
        break;
      case LIST:
        {
          try {

            List<ModelNode> items = value.asList();
            List<String> list = new ArrayList<String>(items.size());
            for (ModelNode item : items) list.add(item.asString());
            result = list;

          } catch (Throwable t) {
            t.printStackTrace();
            result = new ArrayList<>(); // syntax errors
          }
          break;
        }
      case PROPERTY:
        { // it's actually interpreted as a property list, but that ttype doesn'ty really exist
          // (yet)
          try {

            List<Property> properties = value.asPropertyList();
            Map<String, String> map = new HashMap<>();
            for (Property item : properties) map.put(item.getName(), item.getValue().asString());
            result = map;

          } catch (Throwable t) {
            t.printStackTrace();
            result = new HashMap(); // syntax errors
          }
          break;
        }
      case UNDEFINED:
        break;
      default:
        throw new RuntimeException("Unexpected targetType " + targetType);
    }
    return result;
  }
Пример #5
0
  public FormAssets build() {

    // pre-requisite
    if (createMode && !modelDescription.hasDefined("operations")) {
      throw new IllegalStateException("Operation descriptions not defined");
    }

    this.form = new ModelNodeForm(this.address, this.securityContext);
    this.form.setNumColumns(2);
    this.form.setEnabled(false);

    assert modelDescription.hasDefined("attributes")
        : "Invalid model description. Expected child 'attributes'";

    List<Property> attributeDescriptions = new ArrayList<Property>();
    if (createMode
        && modelDescription.get("operations").get("add").hasDefined("request-properties")) {
      attributeDescriptions =
          modelDescription.get("operations").get("add").get("request-properties").asPropertyList();
    } else if (!createMode) {
      attributeDescriptions = modelDescription.get("attributes").asPropertyList();
    }

    // sort fields
    if (!unsorted) {
      Collections.sort(
          attributeDescriptions,
          new Comparator<Property>() {
            @Override
            public int compare(Property property, Property property1) {
              return property.getName().compareTo(property1.getName());
            }
          });
    }

    // catch-all directive, if no explicit attributes given
    if (includes.isEmpty()) {
      for (Property attr : attributeDescriptions) {
        includes.add(attr.getName());
      }
    }
    // in any case remove attributes marked for exclusion
    includes.removeAll(excludes);

    LinkedList<FormItem> requiredItems = new LinkedList<FormItem>();
    LinkedList<FormItem> optionalItems = new LinkedList<FormItem>();

    SafeHtmlBuilder helpTexts = new SafeHtmlBuilder();
    helpTexts.appendHtmlConstant("<table class='help-attribute-descriptions'>");

    Map<String, ModelNode> defaultValues = new HashMap<String, ModelNode>();
    int numWritable = 0;

    boolean hasRequired = false;

    // for some decision below we need to know wether or not required attributes exist at all
    if (requiredOnly) {
      for (Property attr : attributeDescriptions) {
        ModelNode value = attr.getValue();
        boolean required = isRequired(value);
        boolean readOnly = value.get("access-type").asString().equals("read-only");
        if (required & !readOnly) {
          hasRequired = true;
          break;
        }
      }
    }

    Set<String[]> unsupportedTypes = new HashSet<>();
    for (String attribute : includes) {
      for (Property attr : attributeDescriptions) {

        boolean isRuntime = attr.getValue().get("storage").asString().equals("runtime");
        boolean isConfig =
            !attr.getValue().get("storage").asString().equals("runtime"); // TODO: verify statement

        if (runtimeAttributes == false && isRuntime) {
          continue;
        }

        if (configAttributes == false && isConfig) {
          continue;
        }

        if (!attr.getName().equals(attribute)) continue;

        // -------
        // Attribute meta data

        // name
        char[] attrName = attr.getName().toCharArray();
        attrName[0] = Character.toUpperCase(attrName[0]);

        // field label
        String label = new String(attrName).replace("-", " ");
        ModelNode attrDesc = attr.getValue();

        // skip deprecated attributes
        if (attrDesc.hasDefined("deprecated")) {
          // Log.error("Skip deprecated attribute '" + attr.getName() + "'");
          continue;
        }

        // type
        ModelType type = ModelType.valueOf(attrDesc.get("type").asString().toUpperCase());

        // default value
        if (attrDesc.hasDefined("default")) {
          ModelNode defaultValue = attrDesc.get("default");
          ModelNode value = new ModelNode();
          // value.set(type, ModelNodeForm.downCast(defaultValue));
          setValue(
              value,
              type,
              ModelNodeForm.downCast(defaultValue, attrDesc)); // workaround for numeric types

          defaultValues.put(attr.getName(), value);
        }

        // read-only
        final boolean readOnly =
            attrDesc.hasDefined("access-type")
                ? attrDesc.get("access-type").asString().equals("read-only")
                : false;

        // nillable
        boolean isRequired = isRequired(attrDesc);

        // createMode flag
        if ((createMode && readOnly)) continue;

        // requiredOnly flag
        if (requiredOnly && hasRequired && !isRequired) continue;

        // count writable attributes
        if (!readOnly && !isRuntime) numWritable++;

        // -------
        // help

        helpTexts.appendHtmlConstant("<tr class='help-field-row'>");
        helpTexts.appendHtmlConstant("<td class='help-field-name'>");
        helpTexts.appendEscaped(label).appendEscaped(": ");
        helpTexts.appendHtmlConstant("</td>");
        helpTexts.appendHtmlConstant("<td class='help-field-desc'>");
        try {
          String descWorkaround = attrDesc.get("description").asString();

          helpTexts.appendHtmlConstant(descWorkaround.equals("null") ? "n/a" : descWorkaround);
        } catch (Throwable e) {
          // ignore parse errors
          helpTexts.appendHtmlConstant("<i>Failed to parse description</i>");
        }
        helpTexts.appendHtmlConstant("</td>");
        helpTexts.appendHtmlConstant("</tr>");

        FormItem formItem = null;

        // explicitly created form items (monkey patching)
        if (itemFactories.containsKey(attr.getName())) {
          formItem = itemFactories.get(attr.getName()).create(attr);
        }

        // not created by explicit factory
        if (null == formItem) {
          switch (type) {
            case BOOLEAN:
              formItem = new CheckBoxItem(attr.getName(), label);
              formItem.setRequired(isRequired);
              formItem.setEnabled(!readOnly && !isRuntime);
              break;
            case DOUBLE:
              formItem = new DoubleFormItem(attr.getName(), label);
              formItem.setRequired(isRequired);
              formItem.setEnabled(!readOnly && !isRuntime);
              break;
            case LONG:
              boolean allowNegativeValues = false;
              if (attrDesc.hasDefined("default"))
                allowNegativeValues = attrDesc.get("default").asLong() < 0;

              formItem = new NumberBoxItem(attr.getName(), label, allowNegativeValues);
              formItem.setRequired(isRequired);
              formItem.setEnabled(!readOnly && !isRuntime);
              break;
            case BIG_DECIMAL:
              formItem = new NumberBoxItem(attr.getName(), label);
              formItem.setRequired(isRequired);
              formItem.setEnabled(!readOnly && !isRuntime);
              break;
            case INT:
              if (attrDesc.hasDefined("min") && attrDesc.hasDefined("max")) {
                formItem =
                    new NumberBoxItem(
                        attr.getName(),
                        label,
                        attrDesc.get("min").asLong(),
                        attrDesc.get("max").asLong());
              } else {
                formItem = new NumberBoxItem(attr.getName(), label);
              }

              formItem.setRequired(isRequired);
              formItem.setEnabled(!readOnly && !isRuntime);

              break;
            case LIST:
              formItem = new ListItem(attr.getName(), label);
              formItem.setRequired(isRequired);
              formItem.setEnabled(!readOnly && !isRuntime);
              break;
            case STRING:
              if (attrDesc.get("allowed").isDefined()) {
                List<ModelNode> allowed = attrDesc.get("allowed").asList();
                Set<String> allowedValues = new HashSet<String>(allowed.size());
                for (ModelNode value : allowed) allowedValues.add(value.asString());

                final boolean isNillable =
                    attrDesc.hasDefined(NILLABLE) && attrDesc.get(NILLABLE).asBoolean();
                ComboBoxItem combo = new ComboBoxItem(attr.getName(), label, isNillable);
                combo.setValueMap(allowedValues);
                combo.setEnabled(!readOnly && !isRuntime);
                combo.setRequired(isRequired);

                formItem = combo;
              } else {
                formItem = createSuggestBoxForCapabilityReference(attr, label, isRequired);
                if (formItem == null) {
                  // there is no capability-reference
                  TextBoxItem textBoxItem = new TextBoxItem(attr.getName(), label);
                  textBoxItem.setAllowWhiteSpace(true);

                  textBoxItem.setRequired(isRequired);
                  textBoxItem.setEnabled(!readOnly && !isRuntime);

                  formItem = textBoxItem;
                }
              }

              // TODO: Support for TextAreaItem

              break;
            case OBJECT:
              if (attrDesc.has("value-type")
                  && attrDesc.get("value-type").asString().equals("STRING")) {
                PropertyListItem propList = new PropertyListItem(attr.getName(), label);
                propList.setRequired(isRequired);
                propList.setEnabled(!readOnly && !isRuntime);

                formItem = propList;
                break;
              }
            default:
              {
                unsupportedTypes.add(new String[] {attr.getName(), type.toString()});
                Log.error("Unsupported ModelType " + type + ", attribute '" + attr.getName() + "'");
              }
          }
        }

        if (formItem != null) {
          if (createMode) {
            if (isRequired && includeOptionals) requiredItems.add(formItem);
            else optionalItems.add(formItem);
          } else {
            requiredItems.add(formItem);
          }

          // attribute meta data attached to form item
          formItem.setMetadata(attrDesc);
        }
      }
    }

    // some resources already contain a name attribute
    FormItem nameItem = null;
    if (createMode) {
      for (FormItem item : requiredItems) {
        if ("name".equals(item.getName())) {
          nameItem = item;
          break;
        }
      }
      for (FormItem item : optionalItems) {
        if ("name".equals(item.getName())) {
          nameItem = item;
          break;
        }
      }
    }

    // remove so it can be prepended
    if (nameItem != null) {
      requiredItems.remove(nameItem);
      optionalItems.remove(nameItem);
    }

    // distinguish required and optional fields (createMode)
    if (requiredItems.isEmpty()) {
      // no required fields explicitly given, treat all fields as required
      if (createMode) {
        optionalItems.addFirst(new TextBoxItem("name", "Name", true));
        numWritable++;
      }
      form.setFields(optionalItems.toArray(new FormItem[] {}));
    } else {
      if (createMode) {
        requiredItems.addFirst(new TextBoxItem("name", "Name", true));
        numWritable++;
      }

      form.setFields(requiredItems.toArray(new FormItem[] {}));

      if (optionalItems.size() > 0)
        form.setFieldsInGroup(
            "Optional Fields",
            new DisclosureGroupRenderer(),
            optionalItems.toArray(new FormItem[] {}));
    }

    // form meta data
    form.setDefaults(defaultValues);
    form.setHasWritableAttributes(numWritable > 0);

    FormAssets formAssets = new FormAssets(form, helpTexts.toSafeHtml());
    formAssets.setUnsupportedTypes(unsupportedTypes);
    return formAssets;
  }