aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTIMONEY, DANIEL <dtimoney@att.com>2021-07-21 12:22:44 +0000
committerDan Timoney <dtimoney@att.com>2021-07-21 09:39:16 -0400
commit972daedd277fb9ef83f9b6d78b91da87675d3f76 (patch)
tree17c5fe7c244d757465cc2f3f745854a2f2134559
parent0c256aa80033780cf1e0a7c24663b80091e203ac (diff)
Add code to handle content type plain/text
Add code to handle content type plain/text Issue-ID: CCSDK-3378 Signed-off-by: lalena.aria <lalena.aria@att.com> Change-Id: Id4a30ad5ca81db0e7e3a328cc5d2496c42c9e150 Signed-off-by: Dan Timoney <dtimoney@att.com>
-rw-r--r--services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java81
1 files changed, 71 insertions, 10 deletions
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
index 41c71a45..20f9ec47 100644
--- a/services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java
+++ b/services/src/main/java/org/onap/ccsdk/apps/filters/ContentTypeFilter.java
@@ -3,6 +3,7 @@ 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;
@@ -14,6 +15,8 @@ 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;
@@ -24,33 +27,84 @@ 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;
+ String contentType;
+ List<String> acceptList;
List<String> headerNames;
public DefaultContentTypeHttpRequest(HttpServletRequest httpRequest, String defaultContentType) {
super(httpRequest);
+
this.httpRequest = httpRequest;
- this.defaultContentType = defaultContentType;
+ this.contentType = defaultContentType;
+ this.acceptList = null;
+
+ boolean hasContentType = false;
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;
+ 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")) {
+ // 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);
}
@@ -69,6 +123,17 @@ public class ContentTypeFilter implements Filter {
}
}
+
+
+ @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));
@@ -76,11 +141,7 @@ public class ContentTypeFilter implements Filter {
@Override
public String getContentType() {
- if (hasContentType) {
- return super.getContentType();
- } else {
- return defaultContentType;
- }
+ return contentType;
}
}