Filed under: Best Practices
Comments: None
The NexJ framework allows you to create an interface between external web pages and NexJ servers through channels and services. You can create a channel to handle incoming HTTP messages, pass them to a service for work to be done, and then send responses back to the sender. You can also create a service to receive the message from the channel, parse out information from the message, change data in the local application, and then return information back to the channel to be sent externally.
For example, you can create a channel and a service to allow new Leads to be created with information gathered by a corporate website.
Define the channel as follows:
1 2 3 4 5 |
<HTTP contentType="text/plain"url="http://localhost:7080/nexj/finance/anon/channel/LeadCreation/"> <ServiceBindings> <ServiceBinding output="LeadCreation" service="LeadCreation"/> </ServiceBindings> </HTTP> |
The website gathers the information (name, email address) and formats it into an HTTP Post message addressed to the channel you created. The following is an example of the HTTP message being sent by the webpage to the channel:
Method | POST |
URL | http://localhost:7080/nexj/finance/anon/channel/LeadCreation/ |
Headers | Host: localhost:7080 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Referer: http://localhost:7080/nexj/LeadCreation.htm Content-Length: 1040 Connection: keep-alive Pragma: no-cache Cache-Control: no-cache |
Body | emailAddress=john.smith%40nexjtest.com&firstName=John&lastName=Smith |
The URL in the channel definition and the HTTP message is the address to which HTTP messages must be sent. It must follow the pattern shown above. For example, if you are deploying Finance, the pattern is “http://ServerAddress/nexj/finance/anon/channel/ChannelName”.
The channel is bound to a service, in this case LeadCreation, which will receive the HTTP message and process the request.
Once in the service, ‘this’ is set to the incoming HTTP request. The following is an example of the HTTP message as it is received in the service:
this | #<TO<HTTP, @1924383663>( method=”POST”, body=””, channel=”LeadCreation”, path=”/”, headers=TO<, @2012500221>( user-agent=”Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0″, host=”localhost:7080″, accept=”text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8″, referer=”http://localhost:7080/nexj/LeadCreation.htm”, content-type=”application/x-www-form-urlencoded; charset=UTF-8″, cache-control=”no-cache”, accept-encoding=”gzip, deflate”, content-length=1083, pragma=”no-cache”, accept-language=”en-US,en;q=0.5″, connection=”keep-alive” ), parameters=TO<, @1223774097( lastName=”Smith”, emailAddress=”john.smith@nexjtest.com”, firstName=”John” ), url=”http://localhost:7080/nexj/finance/anon/channel/LeadCreation/” )> |
The service uses a flow diagram to define the work that is done. In the parseArguments script, the information in the HTTP message is parsed out into variables as follows:
1 2 3 |
(set! email (string-trim ((this'parameters)'emailAddress))) (set! firstName (string-trim ((this'parameters)'firstName))) (set! lastName (string-trim ((this'parameters)'lastName))) |
The information is then used to create the Lead and send a response back through the channel. To create the response, a simple message format is used in the response script at the end of the service:
1 2 3 4 5 |
(message (: :class "HTTP") (: status 200) (: body returnMessage)) ) |
This will be formatted into an HTTP message to be received by the external website in response to the POST sent to the NexJ server. The following is a simple example with a plaintext body.
Status | 200 |
Body | Created a Lead with name John Smith and email address john.smith@nexjtest.com |
By using channels and services, you can integrate external web pages with the NexJ server. Information can be passed back and forth using simple HTTP messages. This toolset allows an anonymous user to interact with the NexJ server, which allows you to create functionality not requiring server login credentials to use.