Java Code Examples for org.jboss.dmr.ModelNode#toJSONString()

The following examples show how to use org.jboss.dmr.ModelNode#toJSONString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Address.java    From hawkular-agent with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains the address from the given ModelNode which is assumed to be a property list that
 * contains all the address parts and only the address parts.
 *
 * @param node address node
 * @return the address
 */
public static Address fromModelNode(ModelNode node) {
    // Rather than just store node as this.addressNode, we want to make sure it can be used as a valid address.
    // This also builds our own instance of ModelNode rather than use the one the caller gave us.
    Address address = Address.root();

    // if the node is not defined, this simply represents the root address
    if (!node.isDefined()) {
        return address;
    }

    try {
        List<Property> addressList = node.asPropertyList();
        for (Property addressProperty : addressList) {
            String resourceType = addressProperty.getName();
            String resourceName = addressProperty.getValue().asString();
            address.add(resourceType, resourceName);
        }
        return address;
    } catch (Exception e) {
        throw new IllegalArgumentException("Node cannot be used as an address: " + node.toJSONString(true));
    }
}
 
Example 2
Source File: LegacyTypeConverterUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testJsonObject() throws Exception {
    ModelNode description = createDescription(ModelType.OBJECT);

    TypeConverter converter = getConverter(description);

    Assert.assertEquals(SimpleType.STRING, converter.getOpenType());

    ModelNode node = new ModelNode();
    node.get("long").set(5L);
    node.get("string").set("Value");
    node.get("a", "b").set(true);
    node.get("c", "d").set(40);

    String json = node.toJSONString(false);
    String data = assertCast(String.class, converter.fromModelNode(node));
    Assert.assertEquals(json, data);

    Assert.assertEquals(ModelNode.fromJSONString(json), converter.toModelNode(data));
}
 
Example 3
Source File: ExpressionTypeConverterUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testJsonObject() throws Exception {
    ModelNode description = createDescription(ModelType.OBJECT);

    TypeConverter converter = getConverter(description);

    Assert.assertEquals(SimpleType.STRING, converter.getOpenType());

    ModelNode node = new ModelNode();
    node.get("long").set(5L);
    node.get("string").set("Value");
    node.get("a", "b").set(true);
    node.get("c", "d").set(40);

    String json = node.toJSONString(false);
    String data = assertCast(String.class, converter.fromModelNode(node));
    Assert.assertEquals(json, data);

    Assert.assertEquals(ModelNode.fromJSONString(json), converter.toModelNode(data));
}
 
Example 4
Source File: TypeConverters.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public Object fromModelNode(ModelNode node) {
    if (node == null || !node.isDefined()) {
        return null;
    }

    final OpenType<?> openType = getOpenType();
    if (openType instanceof CompositeType) {
        final CompositeType compositeType = (CompositeType)openType;
        //Create a composite
        final Map<String, Object> items = new HashMap<String, Object>();
        for (String attrName : compositeType.keySet()) {
            TypeConverter converter = getConverter(typeNode.get(attrName, TYPE), typeNode.get(attrName, VALUE_TYPE));
            items.put(attrName, converter.fromModelNode(node.get(attrName)));
        }

        try {
            return new CompositeDataSupport(compositeType, items);
        } catch (OpenDataException e) {
            throw new RuntimeException(e);
        }
    } else {
        return node.toJSONString(false);
    }
}
 
Example 5
Source File: XCorrelationIdTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testPost() throws Exception {
    ModelNode op = Util.getReadAttributeOperation(PathAddress.EMPTY_ADDRESS, "server-state");
    HttpPost post = new HttpPost(url.toURI());

    String cmdStr = op.toJSONString(true);
    StringEntity entity = new StringEntity(cmdStr);
    entity.setContentType(APPLICATION_JSON);
    post.setEntity(entity);

    assertNull(getCorrelationHeader(post));

    post.addHeader(HEADER, VALUE1);
    assertEquals(VALUE1, getCorrelationHeader(post));

    post.setHeader(HEADER, VALUE2);
    assertEquals(VALUE2, getCorrelationHeader(post));

    post.setHeader(HEADER, VALUE1);
    assertEquals(VALUE1, getCorrelationHeader(post));

    post.removeHeaders(HEADER);
    assertNull(getCorrelationHeader(post));
}
 
Example 6
Source File: ReadConfigAsFeaturesTestBase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected ModelNode getListElement(ModelNode list, String spec, ModelNode id) {
    for (ModelNode element : list.asList()) {
        if (element.get(SPEC).asString().equals(spec) &&
                (id == null || id.equals(element.get(ID)))) {
            return element;
        }
    }

    throw new IllegalArgumentException("no element for spec " + spec + " and id " + ((id == null) ? "null" : id.toJSONString(true)));
}
 
Example 7
Source File: ReadConfigAsFeaturesTestBase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected int getListElementIndex(ModelNode list, String spec, ModelNode id) {
    for (int i = 0; i < list.asList().size(); i++) {
        if (list.get(i).get(SPEC).asString().equals(spec) &&
                (id == null || id.equals(list.get(i).get(ID)))) {
            return i;
        }
    }
    throw new IllegalArgumentException("no element for spec " + spec + " and id " + ((id == null) ? "null" : id.toJSONString(true)));
}
 
Example 8
Source File: KeycloakAdapterConfigService.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public String getJSON(String deploymentName) {
    ModelNode deployment = this.secureDeployments.get(deploymentName);
    String realmName = deployment.get(RealmDefinition.TAG_NAME).asString();
    ModelNode realm = this.realms.get(realmName);

    ModelNode json = new ModelNode();
    json.get(RealmDefinition.TAG_NAME).set(realmName);

    // Realm values set first.  Some can be overridden by deployment values.
    if (realm != null) setJSONValues(json, realm);
    setJSONValues(json, deployment);
    return json.toJSONString(true);
}
 
Example 9
Source File: HttpMgmtProxy.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ModelNode sendPostCommand(ModelNode cmd, boolean ignoreFailure) throws Exception {
    String cmdStr = cmd.toJSONString(true);
    HttpPost post = new HttpPost(url.toURI());
    StringEntity entity = new StringEntity(cmdStr);
    entity.setContentType(APPLICATION_JSON);
    post.setEntity(entity);

    HttpResponse response = httpClient.execute(post, httpContext);
    String str = EntityUtils.toString(response.getEntity());
    if (response.getStatusLine().getStatusCode() == StatusCodes.OK || ignoreFailure) {
        return ModelNode.fromJSONString(str);
    }
    throw new Exception("Could not execute command: " + str);
}
 
Example 10
Source File: ResponseAttachmentTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private HttpPost getHttpPost(URL url) throws URISyntaxException, UnsupportedEncodingException {
    // For POST we are using the custom op instead read-attribute that we use for GET
    // but this is just a convenient way to exercise the op (GET can't call custom ops),
    // and isn't some limitation of POST
    ModelNode cmd = Util.createEmptyOperation(LogStreamExtension.STREAM_LOG_FILE, PathAddress.pathAddress(SUBSYSTEM, LogStreamExtension.SUBSYSTEM_NAME));
    String cmdStr = cmd.toJSONString(true);
    HttpPost post = new HttpPost(url.toURI());
    StringEntity entity = new StringEntity(cmdStr);
    entity.setContentType(APPLICATION_JSON);
    post.setEntity(entity);

    return post;
}
 
Example 11
Source File: DomainUtil.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static byte[] getResponseBytes(final ModelNode modelNode, final OperationParameter operationParameter) throws IOException {
    if (operationParameter.isEncode()) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedOutputStream out = new BufferedOutputStream(baos);
        modelNode.writeBase64(out);
        out.flush();
        return baos.toByteArray();
    } else {
        String json = modelNode.toJSONString(!operationParameter.isPretty());
        return json.getBytes(StandardCharsets.UTF_8);
    }
}
 
Example 12
Source File: LegacyTypeConverterUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testJsonObjectInList() throws Exception {
    ModelNode description = createDescription(ModelType.LIST, ModelType.OBJECT);

    TypeConverter converter = getConverter(description);

    ArrayType<String> arrayType = assertCast(ArrayType.class, converter.getOpenType());
    Assert.assertEquals(SimpleType.STRING, assertCast(SimpleType.class, arrayType.getElementOpenType()));

    ModelNode list = new ModelNode();
    ModelNode value1 = new ModelNode();
    value1.get("long").set(5L);
    value1.get("string").set("Value");
    value1.get("a", "b").set(true);
    value1.get("c", "d").set(40);
    list.add(value1);
    ModelNode value2 = new ModelNode();
    value2.get("long").set(10L);
    list.add(value2);

    String json1 = value1.toJSONString(false);
    String json2 = value2.toJSONString(false);
    String[] data = assertCast(String[].class, converter.fromModelNode(list));
    Assert.assertEquals(2, data.length);
    Assert.assertEquals(json1, data[0]);
    Assert.assertEquals(json2, data[1]);

    Assert.assertEquals(ModelNode.fromJSONString(list.toJSONString(false)), converter.toModelNode(data));
}
 
Example 13
Source File: KeycloakAdapterConfigService.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public String getJSON(DeploymentUnit deploymentUnit) {
    ModelNode deployment = getSecureDeployment(deploymentUnit);
    String realmName = deployment.get(RealmDefinition.TAG_NAME).asString();
    ModelNode realm = this.realms.get(realmName);

    ModelNode json = new ModelNode();
    json.get(RealmDefinition.TAG_NAME).set(realmName);

    // Realm values set first.  Some can be overridden by deployment values.
    if (realm != null) setJSONValues(json, realm);
    setJSONValues(json, deployment);
    return json.toJSONString(true);
}
 
Example 14
Source File: CachedDcDomainTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param hostname the hostname to query
 * @param requiredState the {@link ControlledProcessState.State} expected, or null for any state}
 * @return true if the host is present in the queried hosts's model (in the requiredState, if present), false otherwise.
 */
private boolean isHostPresentInModel(final String hostname, final ControlledProcessState.State requiredState) throws IOException {
    final ModelNode operation = new ModelNode();
    operation.get(OP).set(READ_ATTRIBUTE_OPERATION);
    operation.get(OP_ADDR).add(HOST, hostname);
    operation.get(NAME).set(HOST_STATE);

    ModelNode result;

    try (DomainClient client = getDomainClient(domainConfig.getMasterConfiguration())) {
        result = client.execute(operation);
        if (result.get(ModelDescriptionConstants.OUTCOME).asString().equals(ModelDescriptionConstants.SUCCESS)) {
            final ModelNode model = result.require(RESULT);
            if (requiredState == null) {
                return true;
            }
            return model.asString().equalsIgnoreCase(requiredState.toString());
        } else if (result.get(ModelDescriptionConstants.OUTCOME).asString().equals(FAILED)) {
            // make sure we get WFLYCTL0030: No resource definition is registered for address so we don't mistakenly hide other problems.
            if (result.require(FAILURE_DESCRIPTION).asString().contains("WFLYCTL0030")) {
                return false;
            }
            // otherwise we got a failure, but the host is present (perhaps still shutting down?), so return true
            return true;
        }
        // otherwise, something bad happened
        throw new RuntimeException(result != null ? result.toJSONString(false) : "Unknown error in determining host state.");
    }
}
 
Example 15
Source File: OpenshiftStartedEnvironment.java    From pnc with Apache License 2.0 5 votes vote down vote up
private String describeService(Service resultService) {
    if (resultService == null)
        return null;

    ModelNode node = resultService.getNode();
    return "Service[" + "name = " + resultService.getName() + ", node= '"
            + (node == null ? null : node.toJSONString(false)) + "]";
}
 
Example 16
Source File: ExpressionTypeConverterUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testJsonObjectInList() throws Exception {
    ModelNode description = createDescription(ModelType.LIST, ModelType.OBJECT);

    TypeConverter converter = getConverter(description);

    ArrayType<String> arrayType = assertCast(ArrayType.class, converter.getOpenType());
    Assert.assertEquals(SimpleType.STRING, assertCast(SimpleType.class, arrayType.getElementOpenType()));

    ModelNode list = new ModelNode();
    ModelNode value1 = new ModelNode();
    value1.get("long").set(5L);
    value1.get("string").set("Value");
    value1.get("a", "b").set(true);
    value1.get("c", "d").set(40);
    list.add(value1);
    ModelNode value2 = new ModelNode();
    value2.get("long").set(10L);
    list.add(value2);

    String json1 = value1.toJSONString(false);
    String json2 = value2.toJSONString(false);
    String[] data = assertCast(String[].class, converter.fromModelNode(list));
    Assert.assertEquals(2, data.length);
    Assert.assertEquals(json1, data[0]);
    Assert.assertEquals(json2, data[1]);

    Assert.assertEquals(ModelNode.fromJSONString(list.toJSONString(false)), converter.toModelNode(data));
}
 
Example 17
Source File: KeycloakAdapterConfigService.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public String getJSON(DeploymentUnit deploymentUnit) {
    ModelNode deployment = getSecureDeployment(deploymentUnit);
    String realmName = deployment.get(RealmDefinition.TAG_NAME).asString();
    ModelNode realm = this.realms.get(realmName);

    ModelNode json = new ModelNode();
    json.get(RealmDefinition.TAG_NAME).set(realmName);

    // Realm values set first.  Some can be overridden by deployment values.
    if (realm != null) setJSONValues(json, realm);
    setJSONValues(json, deployment);
    return json.toJSONString(true);
}
 
Example 18
Source File: HttpDeploymentUploadUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private HttpEntity createAddEntity(final byte[] hash) throws IOException {
    final ModelNode op = Operations.createAddOperation(deploymentAddress);
    op.get("content").get(0).get("hash").set(hash);
    op.get("enabled").set(true);
    return new StringEntity(op.toJSONString(true));
}
 
Example 19
Source File: TypeConverters.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
Object fromModelNode(final ModelNode node) {
    return node.toJSONString(false);
}
 
Example 20
Source File: Address.java    From hawkular-agent with Apache License 2.0 3 votes vote down vote up
/**
 * Obtains an address property list from the given ModelNode wrapper. The given haystack must have a
 * key whose value is the same as needle. The value of that named property must itself be a property list
 * containing all address parts (and only address parts).
 *
 * @param haystack the wrapper ModelNode that contains a property whose value is an address property list.
 * @param needle the name of the property in the given wrapper ModelNode whose value is the address property list.
 * @return the found address
 *
 * @throws IllegalArgumentException there is no address property list in the wrapper node with the given name.
 *
 * @see #fromModelNode(ModelNode)
 */

public static Address fromModelNodeWrapper(ModelNode haystack, String needle) {
    if (haystack.hasDefined(needle)) {
        return fromModelNode(haystack.get(needle));
    }

    throw new IllegalArgumentException("There is no address under the key [" + needle + "] in the node: "
            + haystack.toJSONString(true));
}