1 package org.onap.ccsdk.apps.filters;
3 import java.io.IOException;
4 import java.util.Collections;
5 import java.util.Enumeration;
6 import java.util.Iterator;
7 import java.util.LinkedList;
10 import jakarta.servlet.Filter;
11 import jakarta.servlet.FilterChain;
12 import jakarta.servlet.FilterConfig;
13 import jakarta.servlet.ServletException;
14 import jakarta.servlet.ServletRequest;
15 import jakarta.servlet.ServletResponse;
16 import jakarta.servlet.http.HttpServletRequest;
17 import jakarta.servlet.http.HttpServletRequestWrapper;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.core.Ordered;
22 import org.springframework.core.annotation.Order;
23 import org.springframework.stereotype.Component;
26 @Order(Ordered.HIGHEST_PRECEDENCE)
27 public class ContentTypeFilter implements Filter {
29 String DEFAULT_CONTENT_TYPE = "application/json";
33 public void doFilter(ServletRequest httpReq, ServletResponse httpResp, FilterChain chain)
34 throws IOException, ServletException {
35 String defaultContentType = System.getProperty("ccsdk.defaults.content-type", DEFAULT_CONTENT_TYPE);
36 chain.doFilter(new DefaultContentTypeHttpRequest((HttpServletRequest) httpReq, defaultContentType), httpResp);
40 private class DefaultContentTypeHttpRequest extends HttpServletRequestWrapper {
41 HttpServletRequest httpRequest;
43 List<String> acceptList;
45 List<String> headerNames;
47 public DefaultContentTypeHttpRequest(HttpServletRequest httpRequest, String defaultContentType) {
50 this.httpRequest = httpRequest;
51 this.contentType = defaultContentType;
52 this.acceptList = null;
54 boolean hasContentType = false;
55 headerNames = new LinkedList<String>();
56 Enumeration<String> headerNameEnum = httpRequest.getHeaderNames();
57 while (headerNameEnum.hasMoreElements()) {
58 String curHeaderName = headerNameEnum.nextElement();
59 if ("Content-Type".equalsIgnoreCase(curHeaderName)) {
60 hasContentType = true;
61 contentType = super.getContentType();
62 if ("application/yang-data+json".equalsIgnoreCase(contentType)) {
63 contentType = "application/json";
64 } else if ("application/yang-data+xml".equalsIgnoreCase(contentType)) {
65 contentType = "application/xml";
66 } else if (contentType.startsWith("text/plain") || contentType.startsWith("*/*")) {
67 // Use Accept header, if present, to determine content type.
68 boolean acceptsXml = false;
69 boolean acceptsJson = false;
70 for (Enumeration<String> e = getHeaders("Accept") ; e.hasMoreElements() ;) {
71 String curAcceptValue = e.nextElement();
72 if ("application/json".equalsIgnoreCase(curAcceptValue)) {
74 } else if ("application/yang-data+json".equalsIgnoreCase(curAcceptValue)) {
76 } else if ("application/xml".equalsIgnoreCase(curAcceptValue)) {
78 } else if ("application/yang-data+xml".equalsIgnoreCase(curAcceptValue)) {
83 contentType = "application/json";
84 } else if (acceptsXml) {
85 contentType = "application/xml";
87 // If Accept does not specify XML or JSON (could be Accept is missing), use default content type
88 contentType = defaultContentType;
91 } else if ("Accept".equalsIgnoreCase(curHeaderName)) {
92 acceptList = new LinkedList<String>();
93 for (Enumeration<String> e = getHeaders("Accept") ; e.hasMoreElements() ;) {
94 String acceptValue = e.nextElement();
95 if ("application/yang-data+json".equalsIgnoreCase(acceptValue)) {
96 if (!acceptList.contains("application/json")) {
97 acceptList.add("application/json");
99 } else if ("application/yang-data+xml".equalsIgnoreCase(acceptValue)) {
100 if (!acceptList.contains("application/xml")) {
101 acceptList.add("application/xml");
104 if (!acceptList.contains(acceptValue)) {
105 acceptList.add(acceptValue);
110 headerNames.add(curHeaderName);
112 if (!hasContentType) {
113 headerNames.add("Content-Type");
119 public String getHeader(String name) {
120 if (name.equalsIgnoreCase("Content-Type")) {
121 return getContentType();
123 return super.getHeader(name);
130 public Enumeration<String> getHeaders(String name) {
131 if ("Accept".equalsIgnoreCase(name) && (acceptList != null)) {
132 return Collections.enumeration(acceptList);
134 return super.getHeaders(name);
139 public Enumeration<String> getHeaderNames() {
140 return(Collections.enumeration(headerNames));
144 public String getContentType() {
151 public void init(FilterConfig filterConfig) throws ServletException {
155 public void destroy() {