f2f638f66aae3a95e400f3e5fea432db9b239740
[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 javax.servlet.Filter;
11 import javax.servlet.FilterChain;
12 import javax.servlet.ServletException;
13 import javax.servlet.ServletRequest;
14 import javax.servlet.ServletResponse;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletRequestWrapper;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.springframework.core.Ordered;
21 import org.springframework.core.annotation.Order;
22 import org.springframework.stereotype.Component;
23
24 @Component
25 @Order(Ordered.HIGHEST_PRECEDENCE)
26 public class ContentTypeFilter implements Filter {
27
28     String DEFAULT_CONTENT_TYPE = "application/json";
29
30
31     @Override
32     public void doFilter(ServletRequest httpReq, ServletResponse httpResp, FilterChain chain)
33             throws IOException, ServletException {
34                 String defaultContentType = System.getProperty("ccsdk.defaults.content-type", DEFAULT_CONTENT_TYPE);
35                 chain.doFilter(new DefaultContentTypeHttpRequest((HttpServletRequest) httpReq, defaultContentType), httpResp);
36
37     }
38
39     private class DefaultContentTypeHttpRequest extends HttpServletRequestWrapper {
40         HttpServletRequest httpRequest;
41         String contentType;
42         List<String> acceptList;
43
44         List<String> headerNames; 
45
46         public DefaultContentTypeHttpRequest(HttpServletRequest httpRequest, String defaultContentType) {
47             super(httpRequest);
48
49             this.httpRequest = httpRequest;
50             this.contentType = defaultContentType;
51             this.acceptList = null;
52
53             boolean hasContentType = false;
54             headerNames = new LinkedList<String>();
55             Enumeration<String> headerNameEnum = httpRequest.getHeaderNames();
56             while (headerNameEnum.hasMoreElements()) {
57                 String curHeaderName = headerNameEnum.nextElement();
58                 if ("Content-Type".equalsIgnoreCase(curHeaderName)) {
59                     hasContentType = true;
60                     contentType = super.getContentType();
61                     if ("application/yang-data+json".equalsIgnoreCase(contentType)) {
62                         contentType = "application/json";
63                     } else if ("application/yang-data+xml".equalsIgnoreCase(contentType)) {
64                         contentType = "application/xml";
65                     } else if (contentType.startsWith("text/plain") || contentType.startsWith("*/*")) {
66                         // Use Accept header, if present, to determine content type.
67                         boolean acceptsXml = false;
68                         boolean acceptsJson = false;
69                         for (Enumeration<String> e = getHeaders("Accept") ; e.hasMoreElements() ;) {
70                             String curAcceptValue = e.nextElement();
71                             if ("application/json".equalsIgnoreCase(curAcceptValue)) {
72                                 acceptsJson = true;
73                             } else if ("application/yang-data+json".equalsIgnoreCase(curAcceptValue)) {
74                                 acceptsJson = true;
75                             } else if ("application/xml".equalsIgnoreCase(curAcceptValue)) {
76                                 acceptsXml = true;
77                             } else if ("application/yang-data+xml".equalsIgnoreCase(curAcceptValue)) {
78                                 acceptsXml = true;
79                             }
80                         }
81                         if (acceptsJson) {
82                             contentType = "application/json";
83                         } else if (acceptsXml) {
84                             contentType = "application/xml";
85                         } else {
86                             // If Accept does not specify XML or JSON (could be Accept is missing), use default content type
87                             contentType = defaultContentType;
88                         }
89                     }
90                 } else if ("Accept".equalsIgnoreCase(curHeaderName)) {
91                     acceptList = new LinkedList<String>();
92                     for (Enumeration<String> e = getHeaders("Accept") ; e.hasMoreElements() ;) {
93                         String acceptValue = e.nextElement();
94                         if ("application/yang-data+json".equalsIgnoreCase(acceptValue)) {
95                             if (!acceptList.contains("application/json")) {
96                                 acceptList.add("application/json");
97                             }
98                         } else if ("application/yang-data+xml".equalsIgnoreCase(acceptValue)) {
99                             if (!acceptList.contains("application/xml")) {
100                                 acceptList.add("application/xml");
101                             }
102                         } else {
103                             if (!acceptList.contains(acceptValue)) {
104                                 acceptList.add(acceptValue);
105                             }
106                         }
107                     }
108                 }
109                 headerNames.add(curHeaderName);
110             }
111             if (!hasContentType) {
112                 headerNames.add("Content-Type");
113             }
114
115         }
116
117         @Override
118         public String getHeader(String name) {
119             if (name.equalsIgnoreCase("Content-Type")) {
120                 return getContentType();
121             } else {
122                 return super.getHeader(name);
123             }
124         }
125
126         
127
128         @Override
129         public Enumeration<String> getHeaders(String name) {
130             if ("Accept".equalsIgnoreCase(name) && (acceptList != null)) {
131                 return Collections.enumeration(acceptList);
132             } else {
133                 return super.getHeaders(name);
134             }
135         }
136
137         @Override
138         public Enumeration<String> getHeaderNames() {
139             return(Collections.enumeration(headerNames));
140         }
141
142         @Override
143         public String getContentType() {
144            return contentType;
145         }
146         
147     }
148     
149     
150 }