Skip to content

Commit

Permalink
map json extension to JSONLD media type in content negotiation
Browse files Browse the repository at this point in the history
  • Loading branch information
gsergiu committed Oct 2, 2024
1 parent d1b6f2b commit 8f0edde
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
73 changes: 45 additions & 28 deletions set-web/src/main/java/eu/europeana/set/web/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,69 @@ public class WebMvcConfig implements WebMvcConfigurer {

List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
Map<String, MediaType> mediaTypesMaping = new HashMap<String, MediaType>();

private static final MediaType APPLICATION_JSONLD = new MediaType("application", "ld+json");
private static final String EXTENSION_JSONLD = "jsonld";
private static final String EXTENSION_JSON = "json";

/**
* Setup CORS for all GET, HEAD and OPTIONS, requests.
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/v3/api-docs").allowedOrigins("*").allowedMethods("GET")
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS)
.allowCredentials(false).maxAge(600L); // in seconds
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,
HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS)
.allowCredentials(false).maxAge(600L); // in seconds

registry.addMapping("/v3/api-docs/**").allowedOrigins("*").allowedMethods("GET")
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS)
.allowCredentials(false).maxAge(600L); // in seconds
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,
HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS)
.allowCredentials(false).maxAge(600L); // in seconds


registry.addMapping("/actuator/**").allowedOrigins("*").allowedMethods("GET")
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS)
.allowCredentials(false).maxAge(600L); // in seconds
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,
HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS)
.allowCredentials(false).maxAge(600L); // in seconds

// create method
// delete user's sets by admin, delete by user's sets by
// delete user's sets by admin, delete by user's sets by
registry.addMapping("/set/").allowedOrigins("*").allowedMethods("POST", "DELETE")
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, HttpHeaders.ALLOW, HttpHeaders.LINK, HttpHeaders.ETAG, HttpHeaders.VARY,
HttpHeaders.CACHE_CONTROL, HttpHeaders.PREFERENCE_APPLIED)
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS,
HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, HttpHeaders.ALLOW, HttpHeaders.LINK,
HttpHeaders.ETAG, HttpHeaders.VARY, HttpHeaders.CACHE_CONTROL,
HttpHeaders.PREFERENCE_APPLIED)
.allowCredentials(false).maxAge(600L); // in seconds

// get, delete, update
registry.addMapping("/set/*").allowedOrigins("*").allowedMethods("GET", "PUT", "DELETE")
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaders.ALLOW,
HttpHeaders.LINK, HttpHeaders.ETAG, HttpHeaders.VARY, HttpHeaders.PREFERENCE_APPLIED)
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,
HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaders.ALLOW, HttpHeaders.LINK,
HttpHeaders.ETAG, HttpHeaders.VARY, HttpHeaders.PREFERENCE_APPLIED)
.allowCredentials(false).maxAge(600L); // in seconds

//lock/unlock
// lock/unlock
registry.addMapping("/set/admin/lock").allowedOrigins("*").allowedMethods("POST", "DELETE")
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaders.ALLOW)
.allowCredentials(false).maxAge(600L); // in seconds
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,
HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaders.ALLOW)
.allowCredentials(false).maxAge(600L); // in seconds



// publish/unpublish
registry.addMapping("/set/*/*").allowedOrigins("*").allowedMethods("PUT")
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaders.ALLOW,
HttpHeaders.ETAG, HttpHeaders.VARY, HttpHeaders.PREFERENCE_APPLIED)
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,
HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaders.ALLOW, HttpHeaders.ETAG,
HttpHeaders.VARY, HttpHeaders.PREFERENCE_APPLIED)
.allowCredentials(false).maxAge(600L); // in seconds

//add,remove,exists item in set
registry.addMapping("/set/*/*/*").allowedOrigins("*").allowedMethods("GET", "HEAD", "PUT", "DELETE")
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaders.ALLOW, HttpHeaders.PREFERENCE_APPLIED)
.allowCredentials(false).maxAge(600L); // in seconds
// add,remove,exists item in set
registry.addMapping("/set/*/*/*").allowedOrigins("*")

Check warning

Code scanning / SonarCloud

The program defines an overly permissive Cross-Origin Resource Sharing (CORS) policy See more on SonarCloud Warning

The program defines an overly permissive Cross-Origin Resource Sharing (CORS) policy See more on SonarCloud
.allowedMethods("GET", "HEAD", "PUT", "DELETE")
.exposedHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,
HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaders.ALLOW,
HttpHeaders.PREFERENCE_APPLIED)
.allowCredentials(false).maxAge(600L); // in seconds
}

/*
Expand All @@ -88,25 +100,30 @@ public void configureContentNegotiation(ContentNegotiationConfigurer configurer)
// work in future
// releases
configurer.favorPathExtension(true);

// use registered extensions instead of defaults
configurer.useRegisteredExtensionsOnly(true);

configurer.mediaTypes(getMediaTypesMapping());


// use application/ld+json if no Content-Type is specified
configurer.defaultContentType(APPLICATION_JSONLD, MediaType.APPLICATION_JSON);

configurer.mediaTypes(getMediaTypesMapping());
}

private Map<String, MediaType> getMediaTypesMapping() {
if (mediaTypesMaping.isEmpty()) {
for (MediaType mediaType : supportedMediaTypes) {
if(APPLICATION_JSONLD.equals(mediaType)){
for (MediaType mediaType : getSupportedMediaTypes()) {
if (APPLICATION_JSONLD.equals(mediaType)) {
// map .jsonld extention to jsonld media type
mediaTypesMaping.put(EXTENSION_JSONLD, mediaType);
// map .json extention to jsonld output
mediaTypesMaping.put(EXTENSION_JSON, mediaType);
continue;
} else if (MediaType.APPLICATION_JSON.equals(mediaType)) {
//.json mappins is already mapped to APPLICATION_JSONLD
continue;
}

mediaTypesMaping.put(mediaType.getSubtype(), mediaType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ protected ResponseEntity<String> buildGetResponse(UserSet userSet, LdProfiles pr

// build response
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(7);
headers.add(HttpHeaders.CONTENT_TYPE, HttpHeaders.CONTENT_TYPE_JSONLD_UTF8);
headers.add(LINK, UserSetHttpHeaders.VALUE_BASIC_CONTAINER);
headers.add(LINK, UserSetHttpHeaders.VALUE_BASIC_RESOURCE);
headers.add(ALLOW, UserSetHttpHeaders.ALLOW_GPD);
Expand Down

0 comments on commit 8f0edde

Please sign in to comment.