summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Timoney <dtimoney@att.com>2020-11-10 16:49:04 -0500
committerDan Timoney <dtimoney@att.com>2020-11-10 16:49:04 -0500
commit3504bad8d098ce09340a93e7916b20ae422d4497 (patch)
tree85d7e4df2f39bb82a6773b6fa028ee809af58c74
parent50d4d559247852ebb52aee1dc9f65a7bcf752ad4 (diff)
Add content-type header if missing
Added new filter that inserts a Content-Type header (default value application/json) if none is provided. Change-Id: I4e06913851816448576aad35deb9a809a0dbcda7 Issue-ID: CCSDK-2977 Signed-off-by: Dan Timoney <dtimoney@att.com>
-rw-r--r--ms/sliboot/src/main/java/org/onap/ccsdk/apps/ms/sliboot/controllers/RestconfApiController.java2
-rw-r--r--services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java89
2 files changed, 90 insertions, 1 deletions
diff --git a/ms/sliboot/src/main/java/org/onap/ccsdk/apps/ms/sliboot/controllers/RestconfApiController.java b/ms/sliboot/src/main/java/org/onap/ccsdk/apps/ms/sliboot/controllers/RestconfApiController.java
index f37fe137..44981845 100644
--- a/ms/sliboot/src/main/java/org/onap/ccsdk/apps/ms/sliboot/controllers/RestconfApiController.java
+++ b/ms/sliboot/src/main/java/org/onap/ccsdk/apps/ms/sliboot/controllers/RestconfApiController.java
@@ -54,7 +54,7 @@ import java.util.*;
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-02-20T12:50:11.207-05:00")
@RestController
-@ComponentScan(basePackages = {"org.onap.ccsdk.apps.ms.sliboot.*", "org.onap.ccsdk.apps.services"})
+@ComponentScan(basePackages = {"org.onap.ccsdk.apps.ms.sliboot.*", "org.onap.ccsdk.apps.services", "org.onap.ccsdk.apps.filters"})
@EntityScan("org.onap.ccsdk.apps.ms.sliboot.*")
public class RestconfApiController implements ConfigApi, OperationalApi, OperationsApi {
diff --git a/services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java b/services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java
new file mode 100644
index 00000000..41c71a45
--- /dev/null
+++ b/services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java
@@ -0,0 +1,89 @@
+package org.onap.ccsdk.apps.filters;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Enumeration;
+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.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 defaultContentType;
+ boolean hasContentType;
+
+ List<String> headerNames;
+
+ public DefaultContentTypeHttpRequest(HttpServletRequest httpRequest, String defaultContentType) {
+ super(httpRequest);
+ this.httpRequest = httpRequest;
+ this.defaultContentType = defaultContentType;
+ headerNames = new LinkedList<String>();
+ Enumeration<String> headerNameEnum = httpRequest.getHeaderNames();
+ hasContentType = false;
+ while (headerNameEnum.hasMoreElements()) {
+ String curHeaderName = headerNameEnum.nextElement();
+ if ("Content-Type".equalsIgnoreCase(curHeaderName)) {
+ hasContentType = true;
+ }
+ 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> getHeaderNames() {
+ return(Collections.enumeration(headerNames));
+ }
+
+ @Override
+ public String getContentType() {
+ if (hasContentType) {
+ return super.getContentType();
+ } else {
+ return defaultContentType;
+ }
+ }
+
+ }
+
+
+}