Port to java 17
[ccsdk/apps.git] / services / src / main / java / org / onap / ccsdk / apps / filters / ContentTypeFilter.java
1 package org.onap.ccsdk.apps.filters;
2
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;
8 import java.util.List;
9
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;
18
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;
24
25 @Component
26 @Order(Ordered.HIGHEST_PRECEDENCE)
27 public class ContentTypeFilter implements Filter {
28
29     String DEFAULT_CONTENT_TYPE = "application/json";
30
31
32     @Override
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);
37
38     }
39
40     private class DefaultContentTypeHttpRequest extends HttpServletRequestWrapper {
41         HttpServletRequest httpRequest;
42         String contentType;
43         List<String> acceptList;
44
45         List<String> headerNames; 
46
47         public DefaultContentTypeHttpRequest(HttpServletRequest httpRequest, String defaultContentType) {
48             super(httpRequest);
49
50             this.httpRequest = httpRequest;
51             this.contentType = defaultContentType;
52             this.acceptList = null;
53
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)) {
73                                 acceptsJson = true;
74                             } else if ("application/yang-data+json".equalsIgnoreCase(curAcceptValue)) {
75                                 acceptsJson = true;
76                             } else if ("application/xml".equalsIgnoreCase(curAcceptValue)) {
77                                 acceptsXml = true;
78                             } else if ("application/yang-data+xml".equalsIgnoreCase(curAcceptValue)) {
79                                 acceptsXml = true;
80                             }
81                         }
82                         if (acceptsJson) {
83                             contentType = "application/json";
84                         } else if (acceptsXml) {
85                             contentType = "application/xml";
86                         } else {
87                             // If Accept does not specify XML or JSON (could be Accept is missing), use default content type
88                             contentType = defaultContentType;
89                         }
90                     }
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");
98                             }
99                         } else if ("application/yang-data+xml".equalsIgnoreCase(acceptValue)) {
100                             if (!acceptList.contains("application/xml")) {
101                                 acceptList.add("application/xml");
102                             }
103                         } else {
104                             if (!acceptList.contains(acceptValue)) {
105                                 acceptList.add(acceptValue);
106                             }
107                         }
108                     }
109                 }
110                 headerNames.add(curHeaderName);
111             }
112             if (!hasContentType) {
113                 headerNames.add("Content-Type");
114             }
115
116         }
117
118         @Override
119         public String getHeader(String name) {
120             if (name.equalsIgnoreCase("Content-Type")) {
121                 return getContentType();
122             } else {
123                 return super.getHeader(name);
124             }
125         }
126
127         
128
129         @Override
130         public Enumeration<String> getHeaders(String name) {
131             if ("Accept".equalsIgnoreCase(name) && (acceptList != null)) {
132                 return Collections.enumeration(acceptList);
133             } else {
134                 return super.getHeaders(name);
135             }
136         }
137
138         @Override
139         public Enumeration<String> getHeaderNames() {
140             return(Collections.enumeration(headerNames));
141         }
142
143         @Override
144         public String getContentType() {
145            return contentType;
146         }
147         
148     }
149
150     @Override
151     public void init(FilterConfig filterConfig) throws ServletException {
152     }
153
154     @Override
155     public void destroy() {
156
157     }
158     
159     
160 }