Skip to content

Commit

Permalink
Simplify attribute selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Coffield authored and fhanik committed Jun 28, 2016
1 parent 4b87e04 commit 2c9f91c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 69 deletions.
Expand Up @@ -12,18 +12,12 @@
*******************************************************************************/
package org.cloudfoundry.identity.uaa.resources;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import com.jayway.jsonpath.JsonPath;
import org.cloudfoundry.identity.uaa.util.JsonUtils;
import org.springframework.util.Assert;

import java.util.*;

public class SearchResultsFactory {
public static <T> SearchResults<Map<String, Object>> cropAndBuildSearchResultFrom(
List<T> input,
Expand Down Expand Up @@ -90,28 +84,18 @@ public static <T> SearchResults<Map<String, Object>> buildSearchResultFrom(

Assert.state(input.size() <= count,
"Cannot build search results from parent list. Use subList before you call this method.");
Map<String, Expression> expressions = buildExpressions(attributes, mapper);
StandardEvaluationContext context = new StandardEvaluationContext();
Collection<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (T object : input) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (String attribute : expressions.keySet()) {
map.put(attribute, expressions.get(attribute).getValue(context, object));
String serializedObject = JsonUtils.writeValueAsString(object);
for (String attribute : attributes) {
String jsonPath = "$." + mapper.mapToInternal(attribute);
Object value = JsonPath.read(serializedObject, jsonPath);
map.put(attribute, value);
}
results.add(map);
}

return new SearchResults<Map<String, Object>>(schemas, results, startIndex, count, total);
}

private static Map<String, Expression> buildExpressions(String[] attributes, AttributeNameMapper mapper) {
Map<String, Expression> expressions = new LinkedHashMap<String, Expression>();
for (String attribute : attributes) {
String spel = mapper != null ? mapper.mapToInternal(attribute) : attribute;
Expression expression = new SpelExpressionParser().parseExpression(spel);
expressions.put(attribute, expression);
}
return expressions;
return new SearchResults<>(schemas, results, startIndex, count, total);
}

}
Expand Up @@ -12,41 +12,16 @@
*******************************************************************************/
package org.cloudfoundry.identity.uaa.scim.endpoints;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jayway.jsonpath.JsonPathException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.cloudfoundry.identity.uaa.approval.Approval;
import org.cloudfoundry.identity.uaa.approval.ApprovalStore;
import org.cloudfoundry.identity.uaa.codestore.ExpiringCode;
import org.cloudfoundry.identity.uaa.codestore.ExpiringCodeStore;
import org.cloudfoundry.identity.uaa.constants.OriginKeys;
import org.cloudfoundry.identity.uaa.resources.AttributeNameMapper;
import org.cloudfoundry.identity.uaa.resources.ResourceMonitor;
import org.cloudfoundry.identity.uaa.resources.SearchResults;
import org.cloudfoundry.identity.uaa.resources.SearchResultsFactory;
import org.cloudfoundry.identity.uaa.resources.SimpleAttributeNameMapper;
import org.cloudfoundry.identity.uaa.scim.DisableInternalUserManagementFilter;
import org.cloudfoundry.identity.uaa.scim.DisableUserManagementSecurityFilter;
import org.cloudfoundry.identity.uaa.scim.InternalUserManagementDisabledException;
import org.cloudfoundry.identity.uaa.scim.ScimCore;
import org.cloudfoundry.identity.uaa.scim.ScimGroup;
import org.cloudfoundry.identity.uaa.scim.ScimGroupMembershipManager;
import org.cloudfoundry.identity.uaa.scim.ScimUser;
import org.cloudfoundry.identity.uaa.scim.ScimUserProvisioning;
import org.cloudfoundry.identity.uaa.resources.*;
import org.cloudfoundry.identity.uaa.scim.*;
import org.cloudfoundry.identity.uaa.scim.exception.ScimException;
import org.cloudfoundry.identity.uaa.scim.exception.ScimResourceConflictException;
import org.cloudfoundry.identity.uaa.scim.exception.UserAlreadyVerifiedException;
Expand All @@ -58,8 +33,6 @@
import org.cloudfoundry.identity.uaa.web.ExceptionReport;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelParseException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
Expand All @@ -73,18 +46,17 @@
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.View;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.SecureRandom;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import static org.cloudfoundry.identity.uaa.codestore.ExpiringCodeType.REGISTRATION;
import static org.springframework.util.StringUtils.isEmpty;

Expand Down Expand Up @@ -357,14 +329,12 @@ public SearchResults<?> findUsers(
}

AttributeNameMapper mapper = new SimpleAttributeNameMapper(Collections.<String, String> singletonMap(
"emails\\.(.*)", "emails.![$1]"));
"emails\\.(.*)", "emails[*].$1"));
String[] attributes = attributesCommaSeparated.split(",");
try {
return SearchResultsFactory.buildSearchResultFrom(input, startIndex, count, result.size(), attributes,
mapper, Arrays.asList(ScimCore.SCHEMAS));
} catch (SpelParseException e) {
throw new ScimException("Invalid attributes: [" + attributesCommaSeparated + "]", HttpStatus.BAD_REQUEST);
} catch (SpelEvaluationException e) {
} catch (JsonPathException e) {
throw new ScimException("Invalid attributes: [" + attributesCommaSeparated + "]", HttpStatus.BAD_REQUEST);
}
}
Expand Down

0 comments on commit 2c9f91c

Please sign in to comment.