How to add "last modified" response header to REST API

We are storing the Resourcebundle in the HIPPO CMS, and publish it through REST API, using the library plugin option.

Now to enable REST api client caching, the REST response header received need to have
following header entries.

Cache-Control:
Last-Modified:

We are able add the header entries through

hst:responseheaders @ path /hst:hst/hst:hosts/dev-localhost/localhost

But how can set the Last-Modified header with value of the variable/node jcr:lastModified ?

Eg:-

Last-Modified: ${jcr:lastModified}

Do you use “Rest Services Setup tool” in Essentials to setup a new mount (e.g, “/api”) for resource bundle documents, creating a custom JAX-RS class like the author did [1]?
Or are you using the Generic Content REST API explained in [2]?

Regards,

Woonsan

[1] https://www.onehippo.org/labs/rest-resources-and-hippo-cms.html
[2] https://www.onehippo.org/trails/content-rest-api/introduction.html

Thanks for the quick response,

I am using Rest Services Setup tool.

Thanks

Actually the both (the two references) can be set up through Rest Services Setup tool. Is your REST API endpoint is from (a) Plain JAX-RS or (b) Generic Content REST API?
If you’re not sure, you can copy and paste the json output to show the structure (not data) here. Then we will know which one you’re using.

http://localhost:8080/site/api/documents/31336ce3-1236-41ee-a2bb-ae0912104937

message Body

{
“id”: “31336ce3-1236-41ee-a2bb-ae0912104937”,
“name”: “pdf-resources”,
“displayName”: “pdf-resources”,
“type”: “resourcebundle:resourcebundle”,
“pubState”: “published”,
“pubwfCreationDate”: “2018-10-11T11:22:58.267+01:00”,
“pubwfLastModificationDate”: “2018-10-11T11:27:03.426+01:00”,
“pubwfPublicationDate”: “2018-10-11T11:27:09.645+01:00”,
“bundleId”: “pdf-resources”,
“keys”: {
“MSG_VM_DOC_TITLE”: {
“description”: “”,
“default”: “ZZZZZ”
},
“MSG_VM_DOC_AUTHOR”: {
“description”: “”,
“default”: “ZZZ”
}
}
}

Header

Last-Modified → jcr:lastModified (value is not substituted)

Date →Thu, 11 Oct 2018 15:10:22 GMT

Content-Type →application/json;charset=UTF-8

Transfer-Encoding →chunked

OK. You are using “Content REST API” as it shows the same structure as shown in [1].
It provides neither the feature to set “Last-Modified” header nor dynamic resolution of the value.
According to its technical design document [2], it uses “RestApiPipeline”. So, I think it is technically possible to inject a custom valve to determine and set any extra response headers to the pipeline. See [3] for detail.

Regars,

Woonsan

[1] https://www.onehippo.org/library/concepts/rest/content-rest-api/document-detail-resource.html
[2] https://www.onehippo.org/library/concepts/rest/content-rest-api/technical-design.html
[3] https://www.onehippo.org/library/concepts/hst-spring/hst-orderable-valve-support.html

Thanks Woonsan,

I have managed to add a Valve to the RestApiPipeline, and I can add new response header using

context.getServletResponse().addHeader(“Last-Modified”, );

I tried various options to retrieve the last modified date for the current resource bundle , but failed!!.

I thought the Last-Modified information would be available in

context.getRequestContext().getContentBean() , but this method returned null!!

Regards,
Jijo

Great!

I don’t know the detail on how Content REST API is implemented and whether or not it’s supposed to set the requestContext’s content bean. It doesn’t look implemented to support that.

You can perhaps read the UUID from the URI by HstRequestContext#getServletRequest().getRequestURI which probably returns something like /site/api/documents/31336ce3-1236-41ee-a2bb-ae0912104937. Then you can get the document’s UUID from there.

Now, read the document node by HstRequestContext#getSession().getNodeByIdentifier() (e.g. getSession.getNodeByIdentifier("31336ce3-1236-41ee-a2bb-ae0912104937")), and reads its property somehow like org.hippoecm.repository.util.JcrUtils#getDateProperty(Node node, String propNameOfTheNode, Calendar defaultDate).

Regards,

Woonsan

Thanks,

But on that node with id, 31336ce3-1236-41ee-a2bb-ae0912104937 has only following properties,
[jcr:mixinTypes]
[jcr:primaryType]
[jcr:uuid]

That’s probably the handle node. You need one of the three possible subnodes representing the published/unpublished/draft variants.

Thanks to Both,

Now we picked the data/resource sub-node from the handle node. And this sub-node has the required property “hippostdpubwf:lastModificationDate”.

Regards,
Jijo

Hello everyone. I have one question a bit off the topic. The last modified date and published date are actually present in document info.
Does somebody know how to add these values to the RESOURCE API response?

For anyone viewing this later, please see How to add "last modified date" and "publish date" to Resourse API response for the solution.