gaurav001
gaurav001

Reputation: 167

Worklight adapter response header cache control

I created a HTTP adapter.

That work fine.

This is the sample of rss getStories

function getStories(interest) {
    path = getPath(interest);

    var input = {
        method : 'get',
        returnedContentType : 'xml',
        path : path
    };


    return WL.Server.invokeHttp(input);
}

I get successful response with header information as follows:

Cache-Control →no-cache, no-store, must-revalidate
Content-Length →
Content-Length
The length of the response body in octets (8-bit bytes) 
9220
Content-Type →application/json; charset=UTF-8
Date →Thu, 19 Jun 2014 12:46:12 GMT
Expires →Sat, 26 Jul 1997 05:00:00 GMT
P3P →policyref="/w3c/p3p.xml", CP="CAO DSP COR CURa ADMa DEVa OUR IND PHY ONL UNI COM NAV INT DEM PRE"
X-Powered-By →Servlet/3.0

And the client app reads HTTP header information and therefore doesn't cache the response.

How can I enable cache? Also if you notice the "Expires" it is also of 1997, I don't know how and from where it is taking this date.

Upvotes: 0

Views: 532

Answers (1)

Jeroen
Jeroen

Reputation: 49

Worklight return the headers from the back-end services unchanged. If you nevertheless would like to change the headers, you can do so in the Worklight adapter. The inspiration from the solution below I got from this article: Handling Backend Responses in Adapters.

I created the standard Worklight cnn adapter (New -> Worklight Adapter -> HTTP Adapter) and gave it a name (changeHeadersAdapter).

Then in the changeHeaderAdapter-impl.js I changed the getStories procedure to:

function getStories(interest) {
path = getPath(interest);

var input = {
    method : 'get',
    returnedContentType : 'xml',
    path : path
};

var backendResponse = WL.Server.invokeHttp(input);

if(backendResponse.isSuccessful && backendResponse.statusCode == 200){ //For simplicity, considering only 200 as valid
    //Do something interesting with the data
    backendResponse.responseHeaders['Cache-Control'] = "public, max-age=0";
}
else{
    backendResponse.isSuccessful = false; //Overwrite to failure
}

return backendResponse;

}

As you see I changed the Cache-Control parameter to "public" (normally it would return "private").

I guess this answers your question. Please let me know.

Upvotes: 0

Related Questions