RSSAdapterApplication extends MFPJAXRSApplication and is a good place to trigger any initialization required by your application.
RSSAdapterResource handles the requests to your adapter.
@Path("/") means that the resources are available at this URL: http(s)://host:port/ProjectName/adapters/AdapterName/
Because each request to your resource creates a new instance of RSSAdapterResource, it is important to reuse objects that might impact performance. In this example, the Http client is a static object and is initialized in a static init method, which gets called by the init method of RSSAdapterApplication, as described above.
This adapter exposes just one resource URL which allows you to retrieve the RSS feed from the back-end service.
@GET means that this procedure responds only to HTTP GET requests.
@Produces("application/json") specifies the Content Type of the response to send back. The adapter sends the response as a JSON object to make it easier on the client side.
Use @Context HttpServletResponse response to write to the response output stream. This enables more granularity than returning a simple string.
@QueryParam("topic") The topic String parameter enables the procedure to receive a parameter. The choice of QueryParam means that the parameter is to be passed in the query (/RSSAdapter/?topic=technology). Other options include @PathParam, @HeaderParam, @CookieParam, @FormParam, etc.
throws ClientProtocolException, ... means that any exception is forwarded back to the client. The client code is responsible for handling potential exceptions, which are received as HTTP 500 errors. Another solution (more likely in production code) is to handle exceptions in the server Java code and decide what to send to the client, based on the exact error.
execute(new HttpGet("/rss/edition.rss"), response). The actual HTTP request to the back-end service is handled by another method, which is defined later.
Depending on whether you pass a topic parameter, the execute method builds a different path and retrieve a different RSS file.
HttpResponse RSSResponse = client.execute(host, req). The static HTTP client is used to execute the HTTP request and store the response.
ServletOutputStream os = resultResponse.getOutputStream(). This is the output stream to write a response to the client.
resultResponse.addHeader("Content-Type", "application/json"). As mentioned before, the response is sent in JSON format.
String json = XML.toJson(RSSResponse.getEntity().getContent()). The adapter uses org.apache.wink.json4j.utils.XML to convert the XML RSS to a JSON string.
os.write(json.getBytes(Charset.forName("UTF-8"))) the resulting JSON string is written to the output stream.
The output stream is then flushed and closed.
If RSSResponse is not 200 OK, the status code and reason are written in the response instead.
Use the testing techniques as described in the Java adapter tutorial to test your work.
The adapter should return the RSS feed converted to JSON.
Sample adapter and application
The attached sample includes an adapter, called RSSAdapter, and a hybrid application, called RSSReader, to test the adapter inside an application.