AdamBien / stateful

A RESTful statemachine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

stateful

A RESTful statemachine using the scxml w3c for configuration. Stateful uses: Apache Commons SCXML v2 as state machine.

requirements

Drop the statefulapp.war into a Java EE 7 application server on Java 8 (tested on WildFly, GlassFish, Payara)

usage

create a state machine from SCXML file

curl -i -H"Content-type: application/xml" -XPUT --data-binary @"src/test/resources/state.xml" http://localhost:8080/statefulapp/resources/machines/duke

Sample file:

<scxml xmlns="http://www.w3.org/2005/07/scxml"
       version="1.0"
       datamodel="ecmascript"
       initial="indexpage">

    <state id="indexpage">
        <transition event="login" cond="user === 'hacker'" target="isolated"/>
        <transition event="login" cond="user === 'valid'" target="authenticated"/>
    </state>

    <state id="authenticated">
        <transition event="browse"   target="browsing"/>
        <transition event="logout"    target="unauthenticated"/>
    </state>

    <state id="browsing">
        <transition event="logout" target="unauthenticated"/>
    </state>

    <state id="unauthenticated">
        <transition event="authenticate"   target="indexpage"/>
    </state>
    <state id="isolated">
        <transition event="authenticate"   target="indexpage"/>
    </state>
</scxml>

Response:

HTTP/1.1 201 Created
Location: http://localhost:8080/statefulapp/resources/machines/duke
(…)

retrieve SXML definition for a particular machine

curl http://localhost:8080/statefulapp/resources/machines/duke

Response:

HTTP/1.1 200 OK
Content-Type: application/xml
(…)
<?xml version=‘1.0' encoding=‘UTF-8'?><scxml xmlns="http://www.w3.org/2005/07/scxml" xmlns:cs="http://commons.apache.org/scxml" version="1.0" initial="index page"><!—http://commons.apache.org/scxml--><state id="index page"><transition event="login" target="authenticated"/></state><state id="authenticated"><transition event="browse" target="browsing"/><transition event="logout" target="unauthenticated"/></state><state id="browsing"><transition event="logout" target="unauthenticated"/></state><state id="unauthenticated"><transition event="authenticate" target="index page"/></state></scxml>

list all installed state machine names

curl http://localhost:8080/statefulapp/resources/machines/

Response:

HTTP/1.1 200 OK
Content-Type: application/json
["duke"]

retrieve the current event and next transitions

curl http://localhost:8080/statefulapp/resources/machines/duke/states

Response:

HTTP/1.1 200 OK
Content-Type: application/json
(…)
{"current-state":["index page"],"next-transitions":[{"login":["authenticated"]}]}

retrieve attachments associated with the state machine

curl -i  http://localhost:8080/statefulapp/resources/machines/duke/attachments

Response:

HTTP/1.1 204 No Content

add an attachment

curl -i -XPUT -H'Content-type: application/json' -d'{"answer":42}'  http://localhost:8080/statefulapp/resources/machines/duke/attachments

Response:

HTTP/1.1 201 Created
(...)
Location: http://localhost:8080/statefulapp/resources/machines/duke/attachments

delete an attachment

curl -i -XDELETE http://localhost:8080/statefulapp/resources/machines/duke/attachments

Response:

HTTP/1.1 204 No Content

trigger a transition

curl -i -XPUT -H'Content-type: application/json' -d'{"event":"login"}' http://localhost:8080/statefulapp/resources/machines/duke/states

Response:

HTTP/1.1 200 OK
Content-Type: application/json
(…)
{"current-state":["authenticated"],"next-transitions":[{"browse":["browsing"],"logout":["unauthenticated"]}]}

trigger a transition with condition

curl -i -XPUT -H'Content-type: application/json' -d'{"event":"login","conditions":{"user":"hacker"}}' http://localhost:8080/statefulapp/resources/machines/duke/states

Response:

HTTP/1.1 200 OK
Content-Type: application/json
(…)
{"current-state":["isolated"],"next-transitions":[{"authenticate":["indexpage"]}]}

reset state machine

curl -i -XDELETE http://localhost:8080/statefulapp/resources/machines/duke/states

Response:

HTTP/1.1 200 OK
{"current-state":["index page"],
"next-transitions":[{"login":["authenticated"]}]}

destroy a state machine

curl -i -XDELETE http://localhost:8080/statefulapp/resources/machines/duke/

Response:

HTTP/1.1 204 No Content
(…)

About

A RESTful statemachine

License:Apache License 2.0


Languages

Language:JavaScript 91.7%Language:CSS 5.0%Language:Java 3.0%Language:HTML 0.3%Language:Shell 0.0%