6.1 The Decorator ApproachOne way to enhance the MIDP standard HTTP I/O is to provide a decorator class (CustomConnection) that wraps around the default MIDP HttpConnection implementation but overrides some methods to handle custom headers. Since the decorator class also implements the HttpConnection interface, it is transparent to existing MIDP applications that use HttpConnection. 6.1.1 The CustomConnector Factory ClassIn order to instantiate the CustomConnection decorator, we need to write a new connection factory class CustomConnector (Listing 6.1). The custom request headers are set in the CustomConnector.open() method when a new connection is established. Listing 6.1. The CustomConnector factory class
6.1.2 The CustomConnection ClassNow, let's have a closer look at class CustomConnection (Listing 6.2). It overrides only two methods, openInputStream() and openDataInputStream(), which process custom headers when the response data is retrieved. Listing 6.2. The CustomConnection class
6.1.3 Decorator Pros and ConsThe decorator solution is elegant and transparent to existing applications. However, it has several weaknesses.It is not scalable. For each task involving custom HTTP headers, we need to write a pair of decorator and connector factory classes.The decorator solution does not work correctly with HTTP tasks that require automatic header resubmission from the client side. An example of such tasks is the HTTP Digest Authentication (see Section 6.5). For general-purpose HTTP headers handling, we need a new framework that is more powerful than simple decorators. |