I have a question regarding the header classes (CacheControl, EntityTag, ...) in JAX-RS. They all implement the pattern of having a static reference:
private static final HeaderDelegate<EntityTag> HEADER_DELEGATE =
RuntimeDelegate.getInstance().createHeaderDelegate(EntityTag.class);
and implementing valueOf/toString methods:
public static EntityTag valueOf(final String value) {
return HEADER_DELEGATE.fromString(value);
}
public String toString() {
return HEADER_DELEGATE.toString(this);
}
What is this actually for?
RestEasy implements adding a header in org.jboss.resteasy.plugins.server.servlet.HttpServletResponseHeaders:
protected void addResponseHeader(String key, Object value)
{
if (value == null)
{
return;
}
RuntimeDelegate.HeaderDelegate delegate = factory.getHeaderDelegate(value.getClass());
if (delegate != null)
{
//System.out.println("addResponseHeader: " + key + " " + delegate.toString(value));
response.addHeader(key, delegate.toString(value));
}
else
{
//System.out.println("addResponseHeader: " + key + " " + value.toString());
response.addHeader(key, value.toString());
}
}
Ans Jersey in org.glassfish.jersey.message.internal.HeaderUtils:
public static String asString(final Object headerValue, RuntimeDelegate rd) {
if (headerValue == null) {
return null;
}
if (headerValue instanceof String) {
return (String) headerValue;
}
if (rd == null) {
rd = RuntimeDelegate.getInstance();
}
final HeaderDelegate hp = rd.createHeaderDelegate(headerValue.getClass());
return (hp != null) ? hp.toString(headerValue) : headerValue.toString();
}
In both cases the toString() method of the header classes will not be used since createHeaderDelegate() is required to return a value for them.
And I don't see any use case for them as a user of the JAX-RS Api.
The static variable makes testing a lot harder and only causes problems e.g. when more than one implementation is on the classpath and is not a very nice design.