How to Execute a Composite Operation with the JBoss AS Management API

Java

import static java.lang.System.out;

import java.net.InetAddress;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.sasl.RealmCallback;

import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.dmr.ModelNode;

public class JBossAsSample {

  public static void main(String[] args) throws Exception {
    ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("host"), 9999, new CallbackHandler() {

      public void handle(Callback[] callbacks) throws UnsupportedCallbackException {

        for (int i = 0; i < callbacks.length; i++) {

          if (callbacks[i] instanceof NameCallback) {
            NameCallback ncb = (NameCallback) callbacks[i];

            ncb.setName("user"); // must be in the ManagementRealm
          } else if (callbacks[i] instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) callbacks[i];

            pcb.setPassword("password".toCharArray());
          } else if (callbacks[i] instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) callbacks[i];

            rcb.setText(rcb.getDefaultText()); // "ManagementRealm"
          } else {
            throw new UnsupportedCallbackException(callbacks[i]);
          }

        }

      }

    });

    ModelNode operation = new ModelNode();

    operation.get("operation").set("composite");

    operation.get("address").setEmptyList();

    ModelNode steps = operation.get("steps");

    ModelNode step1 = new ModelNode();

    step1.get("operation").set("name");

    ModelNode address1 = step1.get("address");

    address1.add("name", "value");

    ModelNode step2 = new ModelNode();

    step2.get("operation").set("name");

    ModelNode address2 = step2.get("address");

    address2.add("name", "value");

    steps.add(step2);

    out.println(client.execute(operation));

    client.close();
  }

}

REST

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@WebServlet(name = "SampleJBossAsServlet", value = "/")
public class SampleJBossAsServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.digest("user", "password"));

    Response res = client.target("http://host:9990/management").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json("{\"operation\":\"composite\",\"steps\":[{\"operation\":\"name\",\"address\":[{\"name\":\"value\"}]},{\"operation\":\"name\",\"address\":[{\"name\":\"value\"}]}]}"));

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

Published by:

Fernando Ribeiro

Experienced tech executive with a 24-year track record in enterprise computing. Leading AWS's professional services application modernization division in Brazil. He's held diverse roles including management, solutions architecture, sales consultancy, and full-stack development at major players like Oracle, Red Hat, and IBM. Fernando also contributes to open source and writes about emerging technologies. The views expressed here are his own and do not necessarily reflect the views of AWS.

Categories SoftwareTags , , , , , , , Leave a comment

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.