Skip to content

Commit

Permalink
Merge pull request #263 from europeana/EA-3913-3870-3872-3823-fix-sev…
Browse files Browse the repository at this point in the history
…eral-issues

fix: get content type (always jsonld), profile can be space separated,
  • Loading branch information
gsergiu authored Oct 3, 2024
2 parents ed57a73 + 8f0edde commit 5e6a93b
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 37 deletions.
3 changes: 2 additions & 1 deletion set-common/src/main/resources/set.user.properties.template
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ europeana.apikey.jwttoken.siganturekey=
#europeana.search.apikey=

#API key used for invocation of search api
#europeana.search.url=https://api.europeana.eu/api/v2/search.json?wskey=MY_KEY&query=
#europeana.search.url=https://api.europeana.eu/record/v2/search.json?wskey=MY_KEY&query=


# location for the elevation file
elevation.file.folder=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class WebUserSetFields extends WebUserSetModelFields {
public static final String SLASH = "/";
public static final String PAR_CHAR = "?";
public static final String AND = "&";
public static final String COMMA = ",";
public static final String COMMA = ",";
public static final String SPACE = " ";
public static final String EQUALS_PARAMETER = "=";
public static final String JSON_LD_REST = ".jsonld";
public static final String FORMAT_JSONLD = "jsonld";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,9 @@ public void searchFacetsMultipleValidProfileWithFacets_Debug() throws Exception

@Test
public void searchFacetsMultipleValidProfileWithFacetsDebug() throws Exception {
//profiles can also be space separated
String profile =
LdProfiles.DEBUG.name() + "," + LdProfiles.FACETS.name() + "," + LdProfiles.MINIMAL.name();
LdProfiles.DEBUG.name() + " " + LdProfiles.FACETS.name() + " " + LdProfiles.MINIMAL.name();
mockMvc
.perform(get(SEARCH_URL).param(CommonApiConstants.QUERY_PARAM_PROFILE, profile)
.queryParam(CommonApiConstants.PARAM_WSKEY, API_KEY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public void getUserSet_Success() throws Exception {
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andReturn().getResponse();

assertEquals(response.getHeader(HttpHeaders.CONTENT_TYPE), eu.europeana.api.commons.web.http.HttpHeaders.CONTENT_TYPE_JSONLD_UTF8);
String result = response.getContentAsString();
assertNotNull(result);
assertEquals(HttpStatus.OK.value(), response.getStatus());
Expand Down
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("*")
.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 @@ -36,6 +36,7 @@
import eu.europeana.api.commons.web.exception.ApplicationAuthenticationException;
import eu.europeana.api.commons.web.exception.HttpException;
import eu.europeana.api.commons.web.exception.ParamValidationException;
import eu.europeana.api.commons.web.http.HttpHeaders;
import eu.europeana.api.commons.web.model.vocabulary.Operations;
import eu.europeana.set.definitions.config.UserSetConfiguration;
import eu.europeana.set.definitions.exception.UserSetProfileValidationException;
Expand Down Expand Up @@ -131,11 +132,21 @@ public List<LdProfiles> getProfiles(String profileStr, HttpServletRequest reques
ldProfiles.add(headerProfile);
}

//multiple profiles can be present seperated by comma
//multiple profiles can be present seperated by comma or space
if (StringUtils.isNotEmpty(profileStr)) {
if(profileStr.contains(WebUserSetFields.COMMA)) {
for(String profile : Arrays.asList(StringUtils.split(profileStr, WebUserSetFields.COMMA))) {
ldProfiles.add(getProfileFromParam(profile));
}
}
else if(profileStr.contains(WebUserSetFields.SPACE)) {
for(String profile : Arrays.asList(StringUtils.split(profileStr, WebUserSetFields.SPACE))) {
ldProfiles.add(getProfileFromParam(profile));
}
}
else {
ldProfiles.add(getProfileFromParam(profileStr));
}
}

validateMultipleProfiles(ldProfiles, profileStr);
Expand Down Expand Up @@ -290,7 +301,7 @@ protected ResponseEntity<String> buildGetResponse(UserSet userSet, LdProfiles pr
String etag = generateETag(userSet.getModified(), WebFields.FORMAT_JSONLD, getApiVersion());

// build response
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(5);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(7);
headers.add(LINK, UserSetHttpHeaders.VALUE_BASIC_CONTAINER);
headers.add(LINK, UserSetHttpHeaders.VALUE_BASIC_RESOURCE);
headers.add(ALLOW, UserSetHttpHeaders.ALLOW_GPD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class AuxiliaryMethodsRest extends BaseRest {
@GetMapping(value = { "/set/elevation" }, produces = {MediaType.APPLICATION_XML_VALUE})
@Operation(description = "Generate Elevation file for best bets", summary = "Generate elevation file")
public ResponseEntity<String> generateElevationFile(
@RequestParam(value = CommonApiConstants.PARAM_WSKEY, required = true) String wsKey,
@RequestParam(value = CommonApiConstants.PARAM_WSKEY, required = false) String wsKey,
HttpServletRequest request) throws HttpException {
verifyReadAccess(request);
return generateElevation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ void mergeDescriptiveProperties(PersistentUserSet persistedSet, UserSet updates)
persistedSet.setType(updates.getType());
}

if (updates.getCollectionType() != null) {
persistedSet.setCollectionType(updates.getCollectionType());
}
persistedSet.setCollectionType(updates.getCollectionType());

if (updates.getVisibility() != null) {
persistedSet.setVisibility(updates.getVisibility());
Expand Down

0 comments on commit 5e6a93b

Please sign in to comment.