Skip to content

Commit

Permalink
properly manage Accept header with multiple options
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas loubrieu committed Aug 21, 2024
1 parent d88c0aa commit 5fa6e19
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gov.nasa.pds.api.registry.configuration;

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 +17,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 +46,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 +155,24 @@ 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 {

String[] acceptOrderedValues = acceptHeaderValue.split(",", 0);

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 5fa6e19

Please sign in to comment.