diff options
author | Dan Timoney <dtimoney@att.com> | 2020-11-10 16:49:04 -0500 |
---|---|---|
committer | Dan Timoney <dtimoney@att.com> | 2020-11-10 16:49:04 -0500 |
commit | 3504bad8d098ce09340a93e7916b20ae422d4497 (patch) | |
tree | 85d7e4df2f39bb82a6773b6fa028ee809af58c74 /services/src/main/java | |
parent | 50d4d559247852ebb52aee1dc9f65a7bcf752ad4 (diff) |
Add content-type header if missing
Added new filter that inserts a Content-Type header (default value
application/json) if none is provided.
Change-Id: I4e06913851816448576aad35deb9a809a0dbcda7
Issue-ID: CCSDK-2977
Signed-off-by: Dan Timoney <dtimoney@att.com>
Diffstat (limited to 'services/src/main/java')
-rw-r--r-- | services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java b/services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java new file mode 100644 index 00000000..41c71a45 --- /dev/null +++ b/services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java @@ -0,0 +1,89 @@ +package org.onap.ccsdk.apps.filters; + +import java.io.IOException; +import java.util.Collections; +import java.util.Enumeration; +import java.util.LinkedList; +import java.util.List; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; + +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +@Component +@Order(Ordered.HIGHEST_PRECEDENCE) +public class ContentTypeFilter implements Filter { + + String DEFAULT_CONTENT_TYPE = "application/json"; + + @Override + public void doFilter(ServletRequest httpReq, ServletResponse httpResp, FilterChain chain) + throws IOException, ServletException { + String defaultContentType = System.getProperty("ccsdk.defaults.content-type", DEFAULT_CONTENT_TYPE); + + chain.doFilter(new DefaultContentTypeHttpRequest((HttpServletRequest) httpReq, defaultContentType), httpResp); + + } + + private class DefaultContentTypeHttpRequest extends HttpServletRequestWrapper { + HttpServletRequest httpRequest; + String defaultContentType; + boolean hasContentType; + + List<String> headerNames; + + public DefaultContentTypeHttpRequest(HttpServletRequest httpRequest, String defaultContentType) { + super(httpRequest); + this.httpRequest = httpRequest; + this.defaultContentType = defaultContentType; + headerNames = new LinkedList<String>(); + Enumeration<String> headerNameEnum = httpRequest.getHeaderNames(); + hasContentType = false; + while (headerNameEnum.hasMoreElements()) { + String curHeaderName = headerNameEnum.nextElement(); + if ("Content-Type".equalsIgnoreCase(curHeaderName)) { + hasContentType = true; + } + headerNames.add(curHeaderName); + } + if (!hasContentType) { + headerNames.add("Content-Type"); + } + + } + + @Override + public String getHeader(String name) { + if (name.equalsIgnoreCase("Content-Type")) { + return getContentType(); + } else { + return super.getHeader(name); + } + } + + @Override + public Enumeration<String> getHeaderNames() { + return(Collections.enumeration(headerNames)); + } + + @Override + public String getContentType() { + if (hasContentType) { + return super.getContentType(); + } else { + return defaultContentType; + } + } + + } + + +} |