Why are com.sun.net.HttpExchange attributes shared between instances?

Julien Royer eldebaran at gmail.com
Tue Dec 10 11:10:00 UTC 2019


Hi,

I’m having some troubles understanding how com.sun.net.HttpExchange attributes (setAttribute and getAttribute methods) work. Looking at the Javadoc, I understood that these attributes were specific to a given HttpExchange instance:

https://hg.openjdk.java.net/jdk/jdk/file/a4fb32538898/src/jdk.httpserver/share/classes/com/sun/net/httpserver/HttpExchange.java#l219 <https://hg.openjdk.java.net/jdk/jdk/file/a4fb32538898/src/jdk.httpserver/share/classes/com/sun/net/httpserver/HttpExchange.java#l219>
Filter modules may store arbitrary objects with HttpExchange instances as an out-of-band communication mechanism.

This was also my intuition since these methods are defined on HttpExchange and HttpContext (similar to javax.servlet.ServletRequest#setAttribute and javax.servlet.ServletContext#setAttribute).

When using them on a project, I realised that my intuition was wrong though. The source code is very explicit:

https://hg.openjdk.java.net/jdk/jdk/file/a4fb32538898/src/jdk.httpserver/share/classes/sun/net/httpserver/ExchangeImpl.java#l366 <https://hg.openjdk.java.net/jdk/jdk/file/a4fb32538898/src/jdk.httpserver/share/classes/sun/net/httpserver/ExchangeImpl.java#l366>
if (attributes == null) {
  attributes = getHttpContext().getAttributes();
}

Is the code wrong or is my understanding of the Javadoc incorrect? If so, shouldn’t it be clarified somehow?

Thank you,
Julien

P.S. this question was also asked on stackoverflow: https://stackoverflow.com/questions/59178977/are-com-sun-net-httpserver-httpexchange-attributes-broken <https://stackoverflow.com/questions/59178977/are-com-sun-net-httpserver-httpexchange-attributes-broken>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/net-dev/attachments/20191210/86cf5b4e/attachment.htm>


More information about the net-dev mailing list