I'm writing a very simple proxy servlet by taking as a reference this one. I want o add caching capabilities where the key in the cache would be the URI.
Of course the issue is that I cannot cache the whole response since if I pass it throught the pipeline the input stream will be consumed and then the cached one is no more available.
What do u think is the best way to approach this? How can I copy an HTTPResponse (or just the HTTPEntity) withouth consuming it's content?
An
InputStream, unless otherwise stated, is single shot: you consume it once and that's it.If you want to read it many times, that isn't just a stream any more, it's a stream with a buffer. To cache the input stream you should write the response content into a file or into the memory, so you can re-read it again (multiple times).
The
HTTPEntitycan be re-readable but it depends on the type of the implementation. You can check this with.isRepeatable()for example. This is the original javadoc of apache.You could use the
FileEntitywhich is self-contained and therefore repeatable (re-readable).To archive this (cache into a file), you can read the content of
HTTPEntityand write it into aFile. After that you can create aFileEntitywith theFile, we created and wrote before. Finally you just need to replace theHTTPResponse's entity with the newFileEntity.Here is a simple example without context:
Now you can re-read the content from the file again and again in the future.
You just need to find the file based on the URI :)
To cache in-memory you could use the
ByteArrayEntity.This method just caches the body. Not the http headers.
Update: Alternative
Or you could use Apache HttpClient Cache.
https://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html