Skip navigation links

Package com.sap.smp.client.httpc

httpc - HTTP conversation library

See: Description

Package com.sap.smp.client.httpc Description

httpc - HTTP conversation library

This library is built on top of the HttpURLConnection and HttpsURLConnection APIs provided by the Android system. The main purpose is to facilitate the sending and receiving of HTTP request and responses. The main power of this library that the whole communication is done inside a well-defined conversation flow that can be instrumented with request and response filters. This mechanism allows interleaving operations to be configured for HTTP conversations for common purposes such as authentication, authorization, tracing, etc..

The center point of the library is the HttpConversationManager which has to be created and initialized with the necessary configuration to customize the flow. The IHttpConversation obtained from the manager can then be started to actually start the flow. The speciality is that a listener-based pattern is used for both sending and receiving (via IRequestListener and IResponseListener). This pattern is for the sake of low memory consumption.

In general, the usage of the library follows a simple pattern, as depicted by the below example:

        HttpConversationManager manager = new HttpConversationManager(context).setExecutor(Executors.newCachedThreadPool());
        IHttpConversation conversation = manager.create(new URL("http://www.example.com"));

        conversation.setRequestListener(new IRequestListener() {
                public Object onRequestHeaderSending(ISendEvent event) {
                        Map headers = event.getRequestHeaders();
                        headers.put("Content-Type", "application/octet-stream");
                        return null;
                }

                public Object onRequestBodySending(ITransmitEvent event) {
                        OutputStream stream = event.getStream();
                        stream.write(...);
                        return null;
                }
        });

        conversation.setResponseListener(new IResponseListener() {
                public Object onResponseReceived(IReceiveEvent event) {
                        InputStream stream = event.getStream();
                        stream.read(...);

                        return null;
                }
        });
        conversation.start();
 

Filters and other configuration options of more complex use cases are grouped into IManagerConfigurator implementations. These can be used to simply configure a given manager to a particular scenario. For further simplicity, the setDefault()/getDefault() methods of the manager can be used to set a VM-wide manager instance in case the application wishes to use a single one for all it's HTTP-based communication.

Putting it all together, developers can take advantage of the fluent API defined by the manager, the configurator and the conversation itself. For example:

 new SomeKindOfManagerConfigurator.configure(new HttpConversationManager(context))
                .create(new URL("http://www.example.com"))
                .setMethod(HttpMethod.GET)
                .setResponseListener(new IResponseListener() {
                        public Object onResponseReceived(IReceiveEvent event) {
                                InputStream stream = event.getStream();
                                stream.read(...);

                                return null;
                        }
                })
                .start();
 
..assuming that SomeKindOfManagerConfigurator implements IManagerConfigurator.

Skip navigation links