From 50e60b662545a9ea7e120431c1ba52743b2d9aa7 Mon Sep 17 00:00:00 2001 From: gsergiu <4517853+gsergiu@users.noreply.github.com> Date: Tue, 13 Aug 2024 17:36:42 +0200 Subject: [PATCH] improve toString for profiles, fix multiple profiles extraction from request --- .../model/vocabulary/LdProfiles.java | 2 +- .../set/web/service/controller/BaseRest.java | 53 +++++++------------ .../web/service/controller/BaseRestTest.java | 6 +-- 3 files changed, 22 insertions(+), 39 deletions(-) diff --git a/set-definitions/src/main/java/eu/europeana/set/definitions/model/vocabulary/LdProfiles.java b/set-definitions/src/main/java/eu/europeana/set/definitions/model/vocabulary/LdProfiles.java index e3295164..2aab583a 100644 --- a/set-definitions/src/main/java/eu/europeana/set/definitions/model/vocabulary/LdProfiles.java +++ b/set-definitions/src/main/java/eu/europeana/set/definitions/model/vocabulary/LdProfiles.java @@ -76,7 +76,7 @@ public String getHeaderValue() { @Override public String toString() { - return getHeaderValue(); + return getRequestParamValue() + " (" + getHeaderValue() + ")"; } public String getPreferHeaderValue() { diff --git a/set-web/src/main/java/eu/europeana/set/web/service/controller/BaseRest.java b/set-web/src/main/java/eu/europeana/set/web/service/controller/BaseRest.java index 6c236264..c1df391c 100644 --- a/set-web/src/main/java/eu/europeana/set/web/service/controller/BaseRest.java +++ b/set-web/src/main/java/eu/europeana/set/web/service/controller/BaseRest.java @@ -116,25 +116,29 @@ public String toResourceId(String collection, String object) { /** * This method takes profile string and validates the profiles * and return the List of LdProfiles - * NOTE : Multiple profiles are only supported in profile param - * string only for serach + * NOTE : Multiple profiles (excluding debug) are only supported in profile param + * string only for search * - * @param profileStr - * @param request - * @return - * @throws HttpException + * @param profileStr the profiles indicated in request params + * @param request used to extract the prefer header + * @return list of extracted profiles, exclusing debug + * @throws HttpException if multiple profiles are incompatible */ public List getProfiles(String profileStr, HttpServletRequest request) throws HttpException { List ldProfiles = new ArrayList<>(); - // if multiple profiles present seperated by comma - if (profileStr.contains(WebUserSetFields.COMMA)) { + LdProfiles headerProfile = getHeaderProfile(request); + if(headerProfile != null) { + ldProfiles.add(headerProfile); + } + + //multiple profiles can be present seperated by comma + if (StringUtils.isNotEmpty(profileStr)) { for(String profile : Arrays.asList(StringUtils.split(profileStr, WebUserSetFields.COMMA))) { ldProfiles.add(getProfileFromParam(profile)); - } - validateMultipleProfile(ldProfiles, profileStr); - } else { - ldProfiles.add(getProfile(profileStr, request)); - } + } + } + + validateMultipleProfiles(ldProfiles, profileStr); return ldProfiles; } @@ -145,7 +149,7 @@ public List getProfiles(String profileStr, HttpServletRequest reques * @return * @throws HttpException */ - private void validateMultipleProfile(List ldProfiles, String profileStr) throws HttpException { + private void validateMultipleProfiles(List ldProfiles, String profileStr) throws HttpException { // remove profile 'debug' as it's only used for stackTrace purpose if (ldProfiles.contains(LdProfiles.DEBUG)) { ldProfiles.remove(LdProfiles.DEBUG); @@ -163,27 +167,6 @@ private void validateMultipleProfile(List ldProfiles, String profile } } - /** - * This method takes profile from a HTTP header if it exists or from the passed - * request parameter. - * - * @param paramProfile The HTTP request parameter - * @param request The HTTP request with headers - * @return profile value - * @throws HttpException - * @throws UserSetProfileValidationException - */ - // TODO: consider moving to api-commons - public LdProfiles getProfile(String paramProfile, HttpServletRequest request) throws HttpException { - LdProfiles profile = null; - profile = getHeaderProfile(request); - if (profile == null) { - // get profile from param - profile = getProfileFromParam(paramProfile); - } - return profile; - } - private LdProfiles getProfileFromParam(String paramProfile) throws HttpException { try { return LdProfiles.getByName(paramProfile); diff --git a/set-web/src/test/java/eu/europeana/set/web/service/controller/BaseRestTest.java b/set-web/src/test/java/eu/europeana/set/web/service/controller/BaseRestTest.java index 9b94463d..bc6cd758 100644 --- a/set-web/src/test/java/eu/europeana/set/web/service/controller/BaseRestTest.java +++ b/set-web/src/test/java/eu/europeana/set/web/service/controller/BaseRestTest.java @@ -101,10 +101,10 @@ public void testGetProfile() throws HttpException { String preferHeader = "include=" + ProfileConstants.VALUE_LD_ITEM_DESCRIPTIONS + "; wait=100"; Mockito.when(request.getHeader(Mockito.any())).thenReturn(preferHeader); - LdProfiles profile = baseRest.getProfile(LdProfiles.ITEMDESCRIPTIONS.getHeaderValue(), request); + List profiles = baseRest.getProfiles(LdProfiles.ITEMDESCRIPTIONS.getHeaderValue(), request); - assertNotNull(profile); - assertTrue(StringUtils.equals(ProfileConstants.VALUE_LD_ITEM_DESCRIPTIONS, profile.getHeaderValue())); + assertNotNull(profiles); + assertTrue(StringUtils.equals(ProfileConstants.VALUE_LD_ITEM_DESCRIPTIONS, profiles.get(0).getHeaderValue())); } @Test