Skip to content

Commit

Permalink
Handle Accept header with multiple values (#529)
Browse files Browse the repository at this point in the history
* properly manage Accept header with multiple options

* move MVC function in appropriate class, add unit tests

---------

Co-authored-by: thomas loubrieu <[email protected]>
  • Loading branch information
tloubrieu-jpl and thomas loubrieu authored Aug 22, 2024
1 parent d88c0aa commit d26e447
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gov.nasa.pds.api.registry.configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -15,6 +18,11 @@
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import gov.nasa.pds.api.registry.controllers.ProductsController;
import gov.nasa.pds.api.registry.model.api_responses.PdsProductBusinessObject;
import gov.nasa.pds.api.registry.model.api_responses.ProductBusinessLogic;
import gov.nasa.pds.api.registry.model.api_responses.WyriwygBusinessObject;
import gov.nasa.pds.api.registry.model.exceptions.AcceptFormatNotSupportedException;
import gov.nasa.pds.api.registry.view.CsvErrorMessageSerializer;
import gov.nasa.pds.api.registry.view.CsvPluralSerializer;
import gov.nasa.pds.api.registry.view.CsvSingularSerializer;
Expand All @@ -39,9 +47,37 @@
public class WebMVCConfig implements WebMvcConfigurer {
private static final Logger log = LoggerFactory.getLogger(WebMVCConfig.class);



@Value("${server.contextPath}")
private String contextPath;

private static Map<String, Class<? extends ProductBusinessLogic>> formatters =
new HashMap<String, Class<? extends ProductBusinessLogic>>();

static public Map<String, Class<? extends ProductBusinessLogic>> getFormatters() {
return formatters;
}

static {
// TODO move that at a better place, it is not specific to this controller
formatters.put("*", PdsProductBusinessObject.class);
formatters.put("*/*", PdsProductBusinessObject.class);
formatters.put("application/csv", WyriwygBusinessObject.class);
formatters.put("application/json", PdsProductBusinessObject.class);
formatters.put("application/kvp+json", WyriwygBusinessObject.class);
// this.formatters.put("application/vnd.nasa.pds.pds4+json", new
// Pds4ProductBusinessObject(true));
// this.formatters.put("application/vnd.nasa.pds.pds4+xml", new
// Pds4ProductBusinessObject(false));
formatters.put("application/xml", PdsProductBusinessObject.class);
formatters.put("text/csv", WyriwygBusinessObject.class);
formatters.put("text/html", PdsProductBusinessObject.class);
formatters.put("text/xml", PdsProductBusinessObject.class);
}



@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String contextPath = this.contextPath.endsWith("/") ? this.contextPath : this.contextPath + "/";
Expand Down Expand Up @@ -120,4 +156,27 @@ public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
WebMVCConfig.log.info("Number of converters available after adding locals "
+ Integer.toString(converters.size()));
}



static public Class<? extends ProductBusinessLogic> selectFormatterClass(String acceptHeaderValue)
throws AcceptFormatNotSupportedException {


// split by , and remove extra spaces
String[] acceptOrderedValues =
Arrays.stream(acceptHeaderValue.split(",")).map(String::trim).toArray(String[]::new);

for (String acceptValue : acceptOrderedValues) {
if (WebMVCConfig.formatters.containsKey(acceptValue)) {
return WebMVCConfig.formatters.get(acceptValue);
}
}

// if none of the Accept format proposed matches
throw new AcceptFormatNotSupportedException(
"None of the format(s) " + acceptHeaderValue + " is supported.");

}

}
Loading

0 comments on commit d26e447

Please sign in to comment.