Skip to content

Commit

Permalink
improve toString for profiles, fix multiple profiles extraction from
Browse files Browse the repository at this point in the history
request
  • Loading branch information
gsergiu committed Aug 13, 2024
1 parent ddc211c commit 50e60b6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public String getHeaderValue() {

@Override
public String toString() {
return getHeaderValue();
return getRequestParamValue() + " (" + getHeaderValue() + ")";
}

public String getPreferHeaderValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LdProfiles> getProfiles(String profileStr, HttpServletRequest request) throws HttpException {
List<LdProfiles> 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;
}

Expand All @@ -145,7 +149,7 @@ public List<LdProfiles> getProfiles(String profileStr, HttpServletRequest reques
* @return
* @throws HttpException
*/
private void validateMultipleProfile(List<LdProfiles> ldProfiles, String profileStr) throws HttpException {
private void validateMultipleProfiles(List<LdProfiles> 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);
Expand All @@ -163,27 +167,6 @@ private void validateMultipleProfile(List<LdProfiles> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LdProfiles> 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
Expand Down

0 comments on commit 50e60b6

Please sign in to comment.