diff options
-rw-r--r-- | ms/sliboot/src/main/java/org/onap/ccsdk/apps/ms/sliboot/controllers/RestconfApiController.java | 2 | ||||
-rw-r--r-- | services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java | 89 |
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; + } + } + + } + + +} |