How to Unset an Attribute with the JBoss AS Management API

Java

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 final class JBossAsSample {

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

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

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

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

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

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

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

        }

      }

    });

    final ModelNode operation = new ModelNode();

    operation.get("operation").set("unset-attribute");

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

    final ModelNode address = operation.get("address");

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

    client.execute(operation);

    client.close();
  }

}

REST

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 org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
 
@SuppressWarnings("serial")
@WebServlet(name = "SampleJBossAsServlet", value = "/")
public final class SampleJBossAsServlet extends HttpServlet {

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

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

    client.target("http://localhost:9990/management").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json("{\"operation\":\"unset-attribute\",\"name\":\"name\",\"address\":[{\"name\",\"value\"}]}"));

    client.close();
  }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s