aboutsummaryrefslogtreecommitdiffstats
path: root/services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java
blob: f2f638f66aae3a95e400f3e5fea432db9b239740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.onap.ccsdk.apps.filters;

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 contentType;
        List<String> acceptList;

        List<String> headerNames; 

        public DefaultContentTypeHttpRequest(HttpServletRequest httpRequest, String defaultContentType) {
            super(httpRequest);

            this.httpRequest = httpRequest;
            this.contentType = defaultContentType;
            this.acceptList = null;

            boolean hasContentType = false;
            headerNames = new LinkedList<String>();
            Enumeration<String> headerNameEnum = httpRequest.getHeaderNames();
            while (headerNameEnum.hasMoreElements()) {
                String curHeaderName = headerNameEnum.nextElement();
                if ("Content-Type".equalsIgnoreCase(curHeaderName)) {
                    hasContentType = true;
                    contentType = super.getContentType();
                    if ("application/yang-data+json".equalsIgnoreCase(contentType)) {
                        contentType = "application/json";
                    } else if ("application/yang-data+xml".equalsIgnoreCase(contentType)) {
                        contentType = "application/xml";
                    } else if (contentType.startsWith("text/plain") || contentType.startsWith("*/*")) {
                        // Use Accept header, if present, to determine content type.
                        boolean acceptsXml = false;
                        boolean acceptsJson = false;
                        for (Enumeration<String> e = getHeaders("Accept") ; e.hasMoreElements() ;) {
                            String curAcceptValue = e.nextElement();
                            if ("application/json".equalsIgnoreCase(curAcceptValue)) {
                                acceptsJson = true;
                            } else if ("application/yang-data+json".equalsIgnoreCase(curAcceptValue)) {
                                acceptsJson = true;
                            } else if ("application/xml".equalsIgnoreCase(curAcceptValue)) {
                                acceptsXml = true;
                            } else if ("application/yang-data+xml".equalsIgnoreCase(curAcceptValue)) {
                                acceptsXml = true;
                            }
                        }
                        if (acceptsJson) {
                            contentType = "application/json";
                        } else if (acceptsXml) {
                            contentType = "application/xml";
                        } else {
                            // If Accept does not specify XML or JSON (could be Accept is missing), use default content type
                            contentType = defaultContentType;
                        }
                    }
                } else if ("Accept".equalsIgnoreCase(curHeaderName)) {
                    acceptList = new LinkedList<String>();
                    for (Enumeration<String> e = getHeaders("Accept") ; e.hasMoreElements() ;) {
                        String acceptValue = e.nextElement();
                        if ("application/yang-data+json".equalsIgnoreCase(acceptValue)) {
                            if (!acceptList.contains("application/json")) {
                                acceptList.add("application/json");
                            }
                        } else if ("application/yang-data+xml".equalsIgnoreCase(acceptValue)) {
                            if (!acceptList.contains("application/xml")) {
                                acceptList.add("application/xml");
                            }
                        } else {
                            if (!acceptList.contains(acceptValue)) {
                                acceptList.add(acceptValue);
                            }
                        }
                    }
                }
                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> getHeaders(String name) {
            if ("Accept".equalsIgnoreCase(name) && (acceptList != null)) {
                return Collections.enumeration(acceptList);
            } else {
                return super.getHeaders(name);
            }
        }

        @Override
        public Enumeration<String> getHeaderNames() {
            return(Collections.enumeration(headerNames));
        }

        @Override
        public String getContentType() {
           return contentType;
        }
        
    }
    
    
}