I have written a web service and gets the request in form of HttpServletRequest Object , i then send all the headers for basic validation like checking for Blanks or null etc.
public void validateHeader(HttpServletRequest request)
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
genericHeaderValidator(key, value);
}
}
As you can see the request.getHeaderNames(); returns Enumeration<String>
.
But in some webservices i want to skip the validation as the above code is written in some other class, i want to keep it intact and want the change to be done at callers side.
For example
if i want to skip validation of header named "content-type" so when i do request.removeAttribute("content-type");
It doesn't remove the header attribute, so i went further and thought to remove it from Enumeration<String> headerNames
But enumeration doesn't allow us to remove any element.
Does anyone has any idea how to remove an element from the header parameters of HttpServletRequest Object.
Attribute, Parameter and Header, all three are different in context of HttpRequest. So, request.removeAttribute("xyz"); will remove an attribute named xyz in the request not the header. As far as I know, you can't remove or modify headers just like that.
Following are the different ways which I could think of to serve the purpose.
Option 1: When header to ignore is constant
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
if(!key.equals("content-type") {
String value = request.getHeader(key);
genericHeaderValidator(key, value);
}
}
}
Option 2: When headers to be ignored for validation are determined by the caller (your case). Pass the set of headers which are not to be validated like this:
public void validateHeader(HttpServletRequest request, Set<String> headersToBeIgnored) {
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
if(!headersToBeIgnored.contains(key){
String value = request.getHeader(key);
genericHeaderValidator(key, value);
}
}
}
So, here you can pass the set of headers from the callers side, which are not to be validated. And while looping over the headers, just check if the header is contained in headersToBeIgnored, then do not validate it.
Option 3: Extend HttpServletRequestWrapper
As you said that you need to remove the header from caller side, the accepted answer here (Adding an HTTP Header to the request in a servlet filter) serves exactly your purpose. You can extend HttpServletRequestWrapper and override getHeaderNames(); in which you can return another Enumeration with only required headers added.
However, for each different set of headers to be returned from a different caller, you will need to have a separate implementation. So, this should be fine if you do not have to many different callers.
In the above answer, you need to remove instead of add.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With