Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config-api): multiple pattern search in attribute api #2491

Merged
merged 10 commits into from
Sep 28, 2022
28 changes: 14 additions & 14 deletions jans-config-api/docs/jans-config-api-swagger-auto.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2945,19 +2945,19 @@ components:
$ref: '#/components/schemas/AttributeValidation'
tooltip:
type: string
adminCanEdit:
type: boolean
userCanEdit:
whitePagesCanView:
type: boolean
adminCanAccess:
userCanAccess:
type: boolean
userCanView:
type: boolean
adminCanAccess:
type: boolean
adminCanView:
type: boolean
userCanAccess:
userCanEdit:
type: boolean
whitePagesCanView:
adminCanEdit:
type: boolean
baseDn:
type: string
Expand Down Expand Up @@ -3386,24 +3386,24 @@ components:
type: array
items:
type: string
value:
type: string
displayValue:
type: string
value:
type: string
LocalizedString:
type: object
properties:
values:
type: object
additionalProperties:
type: string
value:
type: string
languageTags:
uniqueItems: true
type: array
items:
type: string
value:
type: string
AppConfiguration:
type: object
properties:
Expand Down Expand Up @@ -4088,8 +4088,6 @@ components:
type: string
agamaConfiguration:
$ref: '#/components/schemas/EngineConfig'
fapi:
type: boolean
enabledFeatureFlags:
uniqueItems: true
type: array
Expand Down Expand Up @@ -4125,6 +4123,8 @@ components:
- code
- token
- id_token
fapi:
type: boolean
AuthenticationFilter:
required:
- baseDn
Expand Down Expand Up @@ -4749,10 +4749,10 @@ components:
type: array
items:
type: object
value:
type: object
displayValue:
type: string
value:
type: object
SessionId:
type: object
properties:
Expand Down
4 changes: 2 additions & 2 deletions jans-config-api/plugins/docs/user-mgt-plugin-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ components:
type: array
items:
type: object
value:
type: object
displayValue:
type: string
value:
type: object
CustomUser:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public Response getScopeById(@NotNull @PathParam(ApiConstants.INUM) String inum,
public Response getScopeByClientId(@NotNull @PathParam(ApiConstants.CREATORID) String creatorId) {
log.debug("SCOPES to be fetched by creatorId:{}", creatorId);
SearchRequest searchReq = new SearchRequest();
searchReq.setFilterAttributes(Arrays.asList("creatorId"));
searchReq.setFilterAttributeName(Arrays.asList("creatorId"));
searchReq.setFilter(creatorId);
List<CustomScope> scopes = scopeService.searchScope(searchReq);
return Response.ok(scopes).build();
Expand All @@ -149,7 +149,7 @@ public Response getScopeByClientId(@NotNull @PathParam(ApiConstants.CREATORID) S
public Response getScopeByType(@NotNull @PathParam(ApiConstants.TYPE) String type) {
log.debug("SCOPES to be fetched by type:{}", type);
SearchRequest searchReq = new SearchRequest();
searchReq.setFilterAttributes(Arrays.asList("jansScopeTyp"));
searchReq.setFilterAttributeName(Arrays.asList("jansScopeTyp"));
searchReq.setFilter(type);
List<CustomScope> scopes = scopeService.searchScope(searchReq);
return Response.ok(scopes).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import io.jans.orm.model.SortOrder;
import io.jans.orm.search.filter.Filter;
import jakarta.enterprise.context.ApplicationScoped;
import java.util.ArrayList;
import java.util.List;

/**
* @author Yuriy Zabrovarnyy
Expand All @@ -24,25 +26,33 @@ protected boolean isUseLocalCache() {
public PagedResult<GluuAttribute> searchGluuAttributes(SearchRequest searchRequest, String status) {
log.debug("Search GluuAttributes with searchRequest:{}, status:{}", searchRequest, status);

String[] targetArray = new String[] { searchRequest.getFilter() };

Filter activeFilter = null;
if (ApiConstants.ACTIVE.equalsIgnoreCase(status)) {
activeFilter = Filter.createEqualityFilter(AttributeConstants.JANS_STATUS, "active");
} else if (ApiConstants.INACTIVE.equalsIgnoreCase(status)) {
activeFilter = Filter.createEqualityFilter(AttributeConstants.JANS_STATUS, "inactive");
}

Filter displayNameFilter = Filter.createSubstringFilter(AttributeConstants.DISPLAY_NAME, null, targetArray,
null);
Filter descriptionFilter = Filter.createSubstringFilter(AttributeConstants.DESCRIPTION, null, targetArray,
null);
Filter nameFilter = Filter.createSubstringFilter(AttributeConstants.JANS_ATTR_NAME, null, targetArray, null);
Filter searchFilter = Filter.createORFilter(displayNameFilter, descriptionFilter, nameFilter);
Filter searchFilter = null;
List<Filter> filters = new ArrayList<>();
if (searchRequest.getFilterAssertionValue() != null && !searchRequest.getFilterAssertionValue().isEmpty()) {

for (String assertionValue : searchRequest.getFilterAssertionValue()) {
String[] targetArray = new String[] { assertionValue };
Filter displayNameFilter = Filter.createSubstringFilter(AttributeConstants.DISPLAY_NAME, null,
targetArray, null);
Filter descriptionFilter = Filter.createSubstringFilter(AttributeConstants.DESCRIPTION, null,
targetArray, null);
Filter nameFilter = Filter.createSubstringFilter(AttributeConstants.JANS_ATTR_NAME, null, targetArray,
null);
filters.add(Filter.createORFilter(displayNameFilter, descriptionFilter, nameFilter));
}
searchFilter = Filter.createORFilter(filters);
}


if (activeFilter != null) {
searchFilter = Filter.createANDFilter(
Filter.createORFilter(displayNameFilter, descriptionFilter, nameFilter), activeFilter);
searchFilter = Filter.createANDFilter(Filter.createORFilter(filters), activeFilter);
}

log.debug("GluuAttributes to be fetched with searchFilter:{}", searchFilter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ public List<CustomScope> getAllScopesList(int size, String scopeType, boolean wi
public List<CustomScope> searchScope(SearchRequest searchRequest) {
logger.debug("Search Scope with searchRequest:{}", searchRequest);

if (searchRequest != null && searchRequest.getFilterAttributes() != null
&& !searchRequest.getFilterAttributes().isEmpty()) {
if (searchRequest != null && searchRequest.getFilterAttributeName() != null
&& !searchRequest.getFilterAttributeName().isEmpty()) {

ArrayList<Filter> searchFilters = new ArrayList<>();

for (String filterAttribute : searchRequest.getFilterAttributes()) {
for (String filterAttribute : searchRequest.getFilterAttributeName()) {
Filter filter = Filter.createEqualityFilter(filterAttribute, searchRequest.getFilter());
searchFilters.add(filter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class SearchRequest {
private Integer startIndex;
private Integer count;
private int maxCount;
private List<String> filterAttributes;
private List<String> filterAttributeName;
private List<String> filterAssertionValue;

@JsonIgnore
private String attributesStr;
Expand Down Expand Up @@ -123,21 +124,28 @@ public void setMaxCount(int maxCount) {
this.maxCount = maxCount;
}

public List<String> getFilterAttributes() {
return filterAttributes;
public List<String> getFilterAttributeName() {
return filterAttributeName;
}

public void setFilterAttributes(List<String> filterAttributes) {
this.filterAttributes = filterAttributes;
public void setFilterAttributeName(List<String> filterAttributeName) {
this.filterAttributeName = filterAttributeName;
}

public List<String> getFilterAssertionValue() {
return filterAssertionValue;
}

public void setFilterAssertionValue(List<String> filterAssertionValue) {
this.filterAssertionValue = filterAssertionValue;
}

@Override
public String toString() {
return "SearchRequest [schemas=" + schemas + ", attributes=" + attributes + ", excludedAttributes="
+ excludedAttributes + ", filter=" + filter + ", sortBy=" + sortBy + ", sortOrder=" + sortOrder
+ ", startIndex=" + startIndex + ", count=" + count + ", maxCount=" + maxCount + ", filterAttributes="
+ filterAttributes + ", attributesStr=" + attributesStr + ", excludedAttributesStr="
+ excludedAttributesStr + "]";
+ ", startIndex=" + startIndex + ", count=" + count + ", maxCount=" + maxCount
+ ", filterAttributeName=" + filterAttributeName + ", filterAssertionValue=" + filterAssertionValue
+ ", attributesStr=" + attributesStr + ", excludedAttributesStr=" + excludedAttributesStr + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import static io.jans.as.model.util.Util.escapeLog;
import io.jans.configapi.core.model.ApiError;
import io.jans.configapi.core.model.SearchRequest;
import io.jans.configapi.core.util.Util;
import io.jans.orm.model.SortOrder;

import jakarta.inject.Inject;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.InternalServerErrorException;
import jakarta.ws.rs.NotFoundException;
Expand All @@ -26,12 +28,16 @@
import org.slf4j.LoggerFactory;

public class BaseResource {

@Inject
Util util;

private static Logger log = LoggerFactory.getLogger(BaseResource.class);
// Custom CODE

public static final String MISSING_ATTRIBUTE_CODE = "OCA001";
public static final String MISSING_ATTRIBUTE_MESSAGE = "A required attribute is missing.";

public static final String TOKEN_DELIMITER = ",";

public static <T> void checkResourceNotNull(T resource, String objectName) {
if (resource == null) {
throw new NotFoundException(getNotFoundError(objectName));
Expand Down Expand Up @@ -167,7 +173,7 @@ protected SearchRequest createSearchRequest(String schemas, String filter, Strin
if (StringUtils.isEmpty(sortOrder) || !sortOrder.equals(SortOrder.DESCENDING.getValue())) {
sortOrder = SortOrder.ASCENDING.getValue();
}

log.debug(" util.getTokens(filter,TOKEN_DELIMITER):{} ", util.getTokens(filter,TOKEN_DELIMITER));
searchRequest.setSchemas(schemas);
searchRequest.setAttributes(attrsList);
searchRequest.setExcludedAttributes(excludedAttrsList);
Expand All @@ -177,7 +183,7 @@ protected SearchRequest createSearchRequest(String schemas, String filter, Strin
searchRequest.setStartIndex(startIndex);
searchRequest.setCount(count);
searchRequest.setMaxCount(maximumRecCount);

searchRequest.setFilterAssertionValue(util.getTokens(filter,TOKEN_DELIMITER));
return searchRequest;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,46 @@

package io.jans.configapi.core.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

@ApplicationScoped
public class Util {

private Util() { }

@Inject
Logger log;

public static String escapeLog(Object param) {
if (param == null)
return "";
return param.toString().replaceAll("[\n\r\t]", "_");
}


public List<String> getTokens(String str, String format) {
log.debug(" String to get tokens - str:{}, format:{}", str, format);
if (StringUtils.isBlank(str)) {
return Collections.emptyList();
}

log.debug("str.contains(format):{}", str.contains(format));
if( !str.contains(format)) {
ArrayList<String> list = new ArrayList<>();
list.add(str);
log.debug(" Not tokenized - list:{}", list);
return (list);
}

log.debug("final tokenized list:{}", Collections.list(new StringTokenizer(str, format)).stream().map(token -> (String) token)
.collect(Collectors.toList()));
return Collections.list(new StringTokenizer(str, format)).stream().map(token -> (String) token)
.collect(Collectors.toList());
}
}