Search in sources :

Example 6 with SearchRequest

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;
}
Also used : SearchRequest(io.jans.scim.model.scim2.SearchRequest) ListResponse(io.jans.scim.model.scim2.ListResponse) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ListResponseJsonSerializer(io.jans.scim.service.scim2.serialization.ListResponseJsonSerializer) URI(java.net.URI) Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) RefAdjusted(io.jans.scim.service.scim2.interceptor.RefAdjusted) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ProtectedApi(io.jans.scim.service.filter.ProtectedApi)

Example 7 with SearchRequest

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;
    }
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse)

Example 8 with SearchRequest

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));
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) SearchRequest(io.jans.scim.model.scim2.SearchRequest) UserResource(io.jans.scim.model.scim2.user.UserResource) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test) SkipTest(io.jans.scim2.listener.SkipTest) BeforeTest(org.testng.annotations.BeforeTest) SkipTest(io.jans.scim2.listener.SkipTest)

Example 9 with SearchRequest

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();
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) SearchRequest(io.jans.scim.model.scim2.SearchRequest) ListResponse(io.jans.scim.model.scim2.ListResponse)

Example 10 with SearchRequest

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()));
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) Member(io.jans.scim.model.scim2.group.Member) UserBaseTest(io.jans.scim2.client.UserBaseTest) java.util(java.util) UserResource(io.jans.scim.model.scim2.user.UserResource) Test(org.testng.annotations.Test) Collectors(java.util.stream.Collectors) GroupResource(io.jans.scim.model.scim2.group.GroupResource) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) SearchRequest(io.jans.scim.model.scim2.SearchRequest) Group(io.jans.scim.model.scim2.user.Group) Response(javax.ws.rs.core.Response) Assert(org.testng.Assert) ListResponse(io.jans.scim.model.scim2.ListResponse) Status(javax.ws.rs.core.Response.Status) SearchRequest(io.jans.scim.model.scim2.SearchRequest) ListResponse(io.jans.scim.model.scim2.ListResponse) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) UserResource(io.jans.scim.model.scim2.user.UserResource) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test)

Aggregations

Response (javax.ws.rs.core.Response)31 SearchRequest (io.jans.scim.model.scim2.SearchRequest)23 ListResponse (io.jans.scim.model.scim2.ListResponse)21 Test (org.testng.annotations.Test)15 UserResource (io.jans.scim.model.scim2.user.UserResource)13 UserBaseTest (io.jans.scim2.client.UserBaseTest)13 SearchRequest (com.google.cloud.retail.v2.SearchRequest)7 SearchResponse (com.google.cloud.retail.v2.SearchResponse)7 SearchServiceClient (com.google.cloud.retail.v2.SearchServiceClient)7 URI (java.net.URI)6 SearchRequest (org.gluu.oxtrust.model.scim2.SearchRequest)6 BaseScimResource (io.jans.scim.model.scim2.BaseScimResource)5 SkipTest (io.jans.scim2.listener.SkipTest)5 SCIMException (io.jans.scim.model.exception.SCIMException)4 URISyntaxException (java.net.URISyntaxException)4 BeforeTest (org.testng.annotations.BeforeTest)4 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)3 DuplicateEntryException (io.jans.orm.exception.operation.DuplicateEntryException)2 GroupResource (io.jans.scim.model.scim2.group.GroupResource)2 Annotation (java.lang.annotation.Annotation)2