Issue
I am creating a simple and lightweight reverse HTTP proxy and using com.sun.net.httpserver.httpserver for it.
Once the proxy is done with its work it should add few headers in request headers and forward the request to actual service, however when i add anything in request headers i get UnsupportedOperationException because it is using UnmodifiableHeaders.
Can someone tell me how to configure com.sun.net.httpserver.httpserver to use modifiable headers?
Or is there any other better yet super lightweight HTTP server which supports this?
java.lang.UnsupportedOperationException: unsupported operation
at jdk.httpserver/sun.net.httpserver.UnmodifiableHeaders.add(UnmodifiableHeaders.java:65)
at com.example.SampleFilter.lambda$addValuesInRequestHeader$1(SampleFilter.java:80)
Solution
There is no way to add/edit headers but we can use httpExchange.setAttribute() and httpExchange.getAttribute() methods to pass additional information when hopping between filters.
For adding headers to the request that we want to forward to service, we create a new http request from the original one and include attributes in headers
Answered By - aatish rana
Answer Checked By - Cary Denson (JavaFixing Admin)