Active item and collection examples

These projects adapt the active Java-Types examples from mams-guide. Read Active request handling for the request lifecycle and diagram. In MAMS 2.4.1, the inherited active handler accepts requests by default, so these examples explicitly cover acceptance and refusal.

A read-only item

Download the active item project, extract it and run mvn clean compile astra:deploy.

agent Main extends mams.javatypes.Active {
    module Strings strings;

    plan +!main(list args) {
        mams.MAMSAgent::!setup();
        mams.MAMSAgent::!created("base");
        mams.javatypes.Active::!itemResource("hello", "Hello");
    }

    plan $cartago.signal(string ArtifactName, httpEvent(int Index, string Method))
        : artifact(string Name, ArtifactName, cartago.ArtifactId Id) {
        if (Method == "GET" | Method == "OPTIONS") {
            mams.javatypes.Active::!acceptRequest(Index, ArtifactName);
        } else {
            mams.javatypes.Active::!refuseRequest(Index, ArtifactName);
        }
    }
}

The policy accepts GET and OPTIONS for supported requests reaching the handler, and refuses the others. GET /main/hello returns 200 OK with the initial name. A PUT is intercepted and refused:

curl -i -X PUT \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{"name":"Rem"}' \
  http://localhost:9000/main/hello

Expect 403 Forbidden. A subsequent GET still shows the original value. The refusal prevents the PUT handler from executing. Accepting a PUT instead would update the resource and still emit the replacement notification to the agent.

The general event plan is essential: a GET-only override would leave PUT to the inherited default acceptance handler.

A collection and its children

Stop the item server. Download the active collection project, extract it and run mvn clean compile astra:deploy.

agent Main extends mams.javatypes.Active {
    module Strings strings;

    plan +!main(list args) {
        mams.MAMSAgent::!setup();
        mams.MAMSAgent::!created("base");
        mams.javatypes.Active::!listResource("hello", "Hello");
    }

    plan $cartago.signal(string ArtifactName, httpEvent(int Index, string Method))
        : artifact(string Name, ArtifactName, cartago.ArtifactId Id) {
        if (Name == "hello" | strings.contains(ArtifactName, "hello-")) {
            mams.javatypes.Active::!acceptRequest(Index, ArtifactName);
        } else {
            mams.javatypes.Active::!refuseRequest(Index, ArtifactName);
        }
    }
}

The agent accepts supported requests for the hello collection and its children, identified by their artifact names. It refuses other requests that reach this handler. The standard Strings module replaces the older guide’s temporary string helper.

Try the collection HTTP sequence against this server. POST creates /main/hello/rem, GET reads it, PUT replaces its name, and DELETE removes it. Unlike the passive example, each supported request is queued and accepted by an agent decision before its handler runs.

The name-based policy demonstrates how to handle both the collection and dynamically created children; it is not an authentication policy. Adapt the decision to inspect headers, request data or agent state as appropriate. Unsupported methods still return 405 Method Not Allowed before the active decision path.