HTTP resource adaptor package includes an example "Ping" service. This service simply responds with a page displaying the request contents.
Install and run the service
Below are basic instructions for deploying and testing the HTTP example service in your SLEE, followed by an excerpt of SBB code showing how a client sends and a server processes HTTP requests.
1 |
Install the service: $ cd examples $ ant |
---|---|
2 |
Navigate to |
HTTPS
To enable HTTPS:
1 |
Generate a keystore for the server: $ keytool -keystore http-ra.ks -storepass changeit -genkeypair
|
||
---|---|---|---|
2 |
Configure the resource adaptor entity and restart it: |
||
3 |
The URL |
Sample code
Below are excerpts of SBB code showing how servers can process HTTP requests, and clients can send them.
Server
Below is an excerpt of SBB code showing how incoming HTTP requests can be processed.
import com.opencloud.slee.resources.http.*;
...
// SBB event handler
public void onGET(HttpRequest event, ActivityContextInterface aci) {
IncomingHttpRequestActivity activity = (IncomingHttpRequestActivity) aci.getActivity();
// Send a redirect response...
HttpResponse response = activity.createResponse(302, "Moved Temporarily");
response.setHeader("Location", "http://anotherserver/index.html");
try {
activity.sendResponse(response);
} catch (IOException e) {
warn("unable to send response", e);
}
}
Client
Below is an excerpt of SBB code showing how outgoing HTTP requests can be made.
import com.opencloud.slee.resources.http.*;
...
public void setSbbContext(SbbContext sbbContext) {
this.sbbContext = sbbContext;
Context sbbEnv = (Context) new InitialContext().lookup("java:comp/env");
provider = (HttpProvider) sbbEnv.lookup("slee/resources/http/2.2/provider");
acif = (HttpActivityContextInterfaceFactory) sbbEnv.lookup("slee/resources/http/2.2/acifactory");
...
}
public void onSomeEvent(SomeEvent event, ActivityContextInterface aci) {
...
HttpRequest newRequest = provider.createRequest(HttpRequest.POST, new URL("http://someserver/service.jsp"));
newRequest.setContentAsString("text/plain; charset=\"utf-8\"", "test message");
OutgoingHttpRequestActivity activity = provider.sendRequest(newRequest);
ActivityContextInterface newACI = acif.getActivityContextInterface(activity);
// attach so we will receive response...
newACI.attach(sbbContext.getSbbLocalObject());
}
public void onHttpResponse(HttpResponse event, ActivityContextInterface aci) {
trace("Received " + event.getStatusCode() + " response");
byte[] data = event.getContent(); // Or getContentAsString() if appropriate
// process response content...
}
// instance vars
private SbbContext sbbContext;
private HttpProvider provider;
private HttpActivityContextInterfaceFactory acif;