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 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;
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;
25 @Order(Ordered.HIGHEST_PRECEDENCE)
26 public class ContentTypeFilter implements Filter {
28 String DEFAULT_CONTENT_TYPE = "application/json";
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);
39 private class DefaultContentTypeHttpRequest extends HttpServletRequestWrapper {
40 HttpServletRequest httpRequest;
42 List<String> acceptList;
44 List<String> headerNames;
46 public DefaultContentTypeHttpRequest(HttpServletRequest httpRequest, String defaultContentType) {
49 this.httpRequest = httpRequest;
50 this.contentType = defaultContentType;
51 this.acceptList = null;
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)) {
73 } else if ("application/yang-data+json".equalsIgnoreCase(curAcceptValue)) {
75 } else if ("application/xml".equalsIgnoreCase(curAcceptValue)) {
77 } else if ("application/yang-data+xml".equalsIgnoreCase(curAcceptValue)) {
82 contentType = "application/json";
83 } else if (acceptsXml) {
84 contentType = "application/xml";
86 // If Accept does not specify XML or JSON (could be Accept is missing), use default content type
87 contentType = defaultContentType;
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");
98 } else if ("application/yang-data+xml".equalsIgnoreCase(acceptValue)) {
99 if (!acceptList.contains("application/xml")) {
100 acceptList.add("application/xml");
103 if (!acceptList.contains(acceptValue)) {
104 acceptList.add(acceptValue);
109 headerNames.add(curHeaderName);
111 if (!hasContentType) {
112 headerNames.add("Content-Type");
118 public String getHeader(String name) {
119 if (name.equalsIgnoreCase("Content-Type")) {
120 return getContentType();
122 return super.getHeader(name);
129 public Enumeration<String> getHeaders(String name) {
130 if ("Accept".equalsIgnoreCase(name) && (acceptList != null)) {
131 return Collections.enumeration(acceptList);
133 return super.getHeaders(name);
138 public Enumeration<String> getHeaderNames() {
139 return(Collections.enumeration(headerNames));
143 public String getContentType() {