Search code examples
javaservletscookiesodataolingo

How to set a cookie in OData response Olingo?


I'm using following piece of code to return response. I don't know where I'm missing things, but there is no discussion anywhere related to setting cookies in OData library.

ODataResponse resp = EntityProvider.writeFeed(contentType, entitySet, results, propertiesBuilder.build());

The returned response doesn't have any setters to set cookie/headers presumably it's due to builder pattern.

In ODataResponseBuilder, there is a way to set headers

ODataResponse resp = resp.header("Set-Cookie", "sampleCookie="+cookieValue).build()

But in this case, I'm not sure how to set entitySet to the builder as in the case of writeFeed.

When it comes to HttpServletRequest, I was able to obtain it by ODataContext#getParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT), but seems there is no equivalent way to get response object.

Any help would be highly appreciated.


Solution

  • You can either use ODataResponse.fromResponse([response from the writeFeed method]) to receive a ResponseBuilderInstance which already contains the content from the response you put in. There you can set custom headers and afterwards call the build() method to receive a final ODataResponse which you can return in your processor.

    Or you use a servletFilter to set them directly at the ServletResponse. I would recommend to use the fromResponse method within Olingo.