use of io.jans.scim.model.scim2.SearchRequest in project jans by JanssenProject.
the class SearchResourcesWebService method search.
@POST
@Consumes({ MEDIA_TYPE_SCIM_JSON, MediaType.APPLICATION_JSON })
@Produces({ MEDIA_TYPE_SCIM_JSON + UTF8_CHARSET_FRAGMENT, MediaType.APPLICATION_JSON + UTF8_CHARSET_FRAGMENT })
@HeaderParam("Accept")
@DefaultValue(MEDIA_TYPE_SCIM_JSON)
@ProtectedApi(scopes = { "https://jans.io/scim/all-resources.search" })
@RefAdjusted
public Response search(SearchRequest searchRequest) {
SearchRequest searchReq = new SearchRequest();
Response response = prepareSearchRequest(searchRequest.getSchemas(), searchRequest.getFilter(), searchRequest.getSortBy(), searchRequest.getSortOrder(), searchRequest.getStartIndex(), searchRequest.getCount(), searchRequest.getAttributesStr(), searchRequest.getExcludedAttributesStr(), searchReq);
if (response == null) {
try {
List<JsonNode> resources = new ArrayList<>();
Pair<Integer, Integer> totals = computeResults(searchReq, resources);
ListResponseJsonSerializer custSerializer = new ListResponseJsonSerializer(resourceSerializer, searchReq.getAttributesStr(), searchReq.getExcludedAttributesStr(), searchReq.getCount() == 0);
if (resources.size() > 0)
custSerializer.setJsonResources(resources);
ObjectMapper objmapper = new ObjectMapper();
SimpleModule module = new SimpleModule("ListResponseModule", Version.unknownVersion());
module.addSerializer(ListResponse.class, custSerializer);
objmapper.registerModule(module);
// Provide to constructor original start index, and totals calculated in computeResults call
ListResponse listResponse = new ListResponse(searchReq.getStartIndex(), totals.getFirst(), totals.getSecond());
String json = objmapper.writeValueAsString(listResponse);
response = Response.ok(json).location(new URI(endpointUrl)).build();
} catch (Exception e) {
log.error("Failure at search method", e);
response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
}
}
return response;
}
use of io.jans.scim.model.scim2.SearchRequest in project jans by JanssenProject.
the class SearchResourcesWebService method getListResponseTree.
/**
* Returns a JsonNode with the response obtained from sending a POST to a search method given the SearchRequest passed
* @param index Determines the concrete search method to be executed: (0 - user; 1 - group; 2 - fido device)
* @param searchRequest
* @return
*/
private JsonNode getListResponseTree(int index, SearchRequest searchRequest) {
try {
log.debug("getListResponseTree. Resource type is: {}", ScimResourceUtil.getType(resourceClasses[index]));
Response r = null;
switch(index) {
case 0:
r = userWS.searchUsersPost(searchRequest);
break;
case 1:
r = groupWS.searchGroupsPost(searchRequest);
break;
case 2:
r = fidoWS.searchDevicesPost(searchRequest, null);
break;
case 3:
r = fido2WS.searchF2DevicesPost(searchRequest, null);
break;
}
if (r.getStatus() != OK.getStatusCode())
throw new Exception("Intermediate POST search returned " + r.getStatus());
// readEntity does not work here since data is not backed by an input stream, so we just get the raw entity
String jsonStr = r.getEntity().toString();
return mapper.readTree(jsonStr);
} catch (Exception e) {
log.error("Error in getListResponseTree {}", e.getMessage());
log.error(e.getMessage(), e);
return null;
}
}
use of io.jans.scim.model.scim2.SearchRequest in project jans by JanssenProject.
the class SpecialCharsTest method containabilityAll.
@SkipTest(databases = { "spanner", "couchbase" })
@Test
public void containabilityAll() {
// Builds a long "and" based clause
String filter = specialFilterLdapChars.stream().reduce("", (partial, next) -> partial + String.format(" and userName co \"%s\"", next));
SearchRequest sr = new SearchRequest();
// Drop beginning (namely " and ")
sr.setFilter(filter.substring(4));
sr.setAttributes("userName");
// Search users whose usernames contain ALL the chars
Response response = client.searchUsersPost(sr);
assertEquals(response.getStatus(), OK.getStatusCode());
List<UserResource> resources = response.readEntity(ListResponse.class).getResources().stream().map(UserResource.class::cast).collect(Collectors.toList());
String userName = resources.get(0).getUserName();
assertEquals(resources.size(), 1);
assertTrue(Stream.of(SPECIAL_CHARS).allMatch(userName::contains));
}
use of io.jans.scim.model.scim2.SearchRequest in project jans by JanssenProject.
the class GroupAssignUserTest method getAdminId.
private String getAdminId() {
// Search the id of the admin user
SearchRequest sr = new SearchRequest();
sr.setFilter("userName eq \"admin\"");
Response response = client.searchUsersPost(sr);
assertEquals(response.getStatus(), OK.getStatusCode());
ListResponse lr = response.readEntity(ListResponse.class);
assertTrue(lr.getResources().size() > 0);
return lr.getResources().get(0).getId();
}
use of io.jans.scim.model.scim2.SearchRequest in project jans by JanssenProject.
the class GroupAssignUserTest method verifyGroupsAttribute.
@Test(dependsOnMethods = "assignToSecondGroup")
public void verifyGroupsAttribute() {
// Refresh the user instances so getGroups() can be called
// builds a filter string
StringBuilder filter = new StringBuilder();
friends.forEach(buddy -> filter.append(String.format(" or id eq \"%s\"", buddy.getId())));
// builds a search request
SearchRequest sr = new SearchRequest();
sr.setFilter(filter.substring(4));
// Retrieve only the first 3
sr.setCount(3);
// Performs the query
logger.info("Issuing query with filter: {}", sr.getFilter());
Response response = client.searchUsersPost(sr);
assertEquals(response.getStatus(), OK.getStatusCode());
logger.info("Verifying groups and users consistency...");
List<BaseScimResource> buddies = response.readEntity(ListResponse.class).getResources();
assertEquals(buddies.size(), 3);
// Verify all mad belong to group, and one of them, additionally to group2
buddies.stream().map(usrClass::cast).forEach(buddy -> {
Set<String> groupIds = buddy.getGroups().stream().map(Group::getValue).collect(Collectors.toSet());
assertTrue(groupIds.contains(group.getId()));
});
Optional<UserResource> usrOpt = buddies.stream().map(usrClass::cast).filter(buddy -> buddy.getGroups().size() > 1).findFirst();
assertTrue(usrOpt.isPresent());
user = usrOpt.get();
assertTrue(user.getGroups().stream().map(Group::getValue).collect(Collectors.toSet()).contains(group2.getId()));
}
Aggregations