Apama 10.15.1 | Connecting Apama Applications to External Components | Standard Connectivity Plug-ins | The HTTP Client Transport Connectivity Plug-in | Mapping events between EPL and HTTP client requests | Example mapping rules
 
Example mapping rules
A full example configuration can be found in the samples directory of your Apama installation. The monitoring sample, found in samples/connectivity_plugin/app/monitoring, can be run both with this pre-compiled HTTP client or with the simple HTTP client sample under samples/connectivity_plugin/cpp/httpclient.
Simple example
The following is a simple REST service with a single URL that is not interested in dealing with error cases:
event PutData {
integer requestId;
string requestString;
}
event PutDataResponse {
integer requestId;
string responseString;
}
Each PUT request contains a request string which performs an action on the server and returns another string in the response.
startChains:
simpleRestService:
- apama.eventMap:
# Channel that responses are delivered on
defaultChannel: SRS-response
- mapperCodec:
PutData: # requests
towardsTransport:
mapFrom:
- metadata.requestId: payload.requestId
- payload: payload.requestString
defaultValue:
- metadata.http.method: PUT
- metadata.http.path: /path/to/service
PutDataResponse:
towardsHost:
mapFrom:
- payload.responseString: payload
- payload.requestId: metadata.requestId
- classifierCodec:
rules:
- PutDataResponse:
- stringCodec
- HTTPClientTransport:
host: foo.com
CRUD service example
The following is a more complex service that implements a full CRUD (create, read, update, delete) service, with different types of request on different objects. There are several different request types with individual mapping rules. The create request is implemented with these events and mapping rules:
event CreateResource {
integer id;
string value;
}
event ResourceCreated {
integer id;
string resource;
}
There is one URL for adding new resources which returns the resource identifier which can be used to manipulate it in future via a redirection header.
- mapperCodec:
CreateResource: # requests
towardsTransport:
mapFrom:
- metadata.requestId: payload.id
- payload: payload.value
defaultValue:
- metadata.http.path: /newResource
- metadata.http.method: PUT
ResourceCreated:
towardsHost:
mapFrom:
# redirects us to the new resource
- payload.resource: metadata.http.headers.location
- payload.id: metadata.requestId
The full example is provided below:
event GetValue {
integer id;
string resource;
}
event CurrentValue {
integer id;
string value;
}
event UpdateValue {
integer id;
string resource;
string newValue;
}
event CreateResource {
integer id;
string value;
}
event ResourceCreated {
integer id;
string resource;
}
event DestroyResource {
integer id;
string resource;
}
event ResourceDestroyed {
integer id;
}
event ResourceNotFound {
integer id;
string resource;
}
event InternalError {
integer id;
string error;
}
 
startChains:
storageService:
- apama.eventMap:
# Channel that responses are delivered on
defaultChannel: storageResponses
- mapperCodec:
CreateResource: # requests
towardsTransport:
mapFrom:
- metadata.requestId: payload.id
- payload: payload.value
defaultValue:
- metadata.http.path: /newResource
- metadata.http.method: PUT
DestroyResource: # requests
towardsTransport:
mapFrom:
- metadata.requestId: payload.id
- metadata.path: payload.resource
defaultValue:
- metadata.http.method: DELETE
UpdateValue: # requests
towardsTransport:
mapFrom:
- metadata.requestId: payload.id
- metadata.path: payload.resource
- payload: payload.newValue
defaultValue:
- metadata.http.method: PUT
GetValue: # requests
towardsTransport:
mapFrom:
- metadata.requestId: payload.id
- metadata.path: payload.resource
defaultValue:
- metadata.http.method: GET
ResourceCreated:
towardsHost:
mapFrom:
# redirects us to the new resource
- payload.resource: metadata.http.headers.location
- payload.id: metadata.requestId
ResourceDestroyed:
towardsHost:
mapFrom:
- payload.id: metadata.requestId
CurrentValue:
towardsHost:
mapFrom:
- payload.value: payload
- payload.id: metadata.requestId
ResourceNotFound:
towardsHost:
mapFrom:
- payload.resource: metadata.http.path
- payload.id: metadata.requestId
InternalError:
towardsHost:
mapFrom:
- payload.error: metadata.statusReason
- payload.id: metadata.requestId
- classifierCodec:
rules:
- ResourceCreated:
- metadata.http.statusCode: 200
- metadata.http.path: /newResource
- CurrentValue:
- metadata.http.statusCode: 200
- metadata.http.method: GET
- ResourceDestroyed:
- metadata.http.statusCode: 200
- metadata.http.method: DELETE
- ResourceNotFound:
- metadata.http.statusCode: 404
- InternalError:
- metadata.http.statusCode:
- stringCodec
- HTTPClientTransport:
host: foo.com
Login example
An example with a login request that has to manage cookies might look like this when the service uses JSON:
event Command {
string command;
sequence<string> arguments;
}
event Login {
string username;
string password;
}
event LoginSuccess {
dictionary<string, string> sessionCookies;
}
event ExecuteCommand {
integer id;
Command command;
dictionary<string, string> sessionCookies;
}
event CommandResponse {
integer id;
string response;
}
The Login command sends a password and sets a cookie which must be set in all the following requests. In practice this may need to be repeated on startup, after some timeout period or certain errors.
startChains:
remoteAccessService:
- apama.eventMap:
# Channel that you send requests to
subscribeChannels: remoteAccess
# Channel that responses are delivered on
defaultChannel: remoteAccess
- mapperCodec:
Login: # requests
towardsTransport:
mapFrom:
# payload.user and payload.password will be converted
# into a JSON document
defaultValue:
- metadata.http.path: /login
- metadata.http.method: PUT
- metadata.requestId: "" # ignored
ExecuteCommand: # requests
towardsTransport:
mapFrom:
- metadata.requestId: payload.id
# set the whole map of any cookies set by the server
- metadata.http.cookies: payload.sessionCookies
# a JSON object made from this event
- payload: payload.command
defaultValue:
- metadata.http.method: PUT
- metadata.path: /execute
LoginSuccess:
towardsHost:
mapFrom:
# store all cookies set by the server,
# no matter what they are
- payload.sessionCookies: metadata.http.cookies
CommandResponse:
towardsHost:
mapFrom:
# payload.response already parsed from the JSON response
- payload.id: metadata.requestId
- classifierCodec:
rules:
- LoginSuccess:
- metadata.http.statusCode: 200
- metadata.http.cookies.session:
- CommandResponse:
- metadata.http.statusCode: 200
- jsonCodec
- stringCodec
- HTTPClientTransport:
host: foo.com
tls: true
Content-encoding example
The following example shows how to define content encoding for an HTTP request:
event HTTPRequest
{
integer id;
string path;
string data;
string method;
string contentEncoding;
}
You can use the Mapper codec to map the encoding method as follows:
- mapperCodec:
HTTPRequest:
towardsTransport:
mapFrom:
- metadata.http.path: payload.path
- metadata.requestId: payload.id
- metadata.http.method: payload.method
- metadata.http.headers.content-encoding: payload.contentEncoding
- payload: payload.data