Search in sources :

Example 26 with ListResponse

use of io.jans.scim.model.scim2.ListResponse in project oxTrust by GluuFederation.

the class SchemaWebService method serve.

@GET
@Produces(MEDIA_TYPE_SCIM_JSON + UTF8_CHARSET_FRAGMENT)
@HeaderParam("Accept")
@DefaultValue(MEDIA_TYPE_SCIM_JSON)
@RejectFilterParam
public Response serve() {
    Response response;
    try {
        int total = resourceSchemas.size();
        ListResponse listResponse = new ListResponse(1, total, total);
        for (String urn : resourceSchemas.keySet()) {
            listResponse.addResource(getSchemaInstance(resourceSchemas.get(urn), urn));
        }
        String json = resourceSerializer.getListResponseMapper().writeValueAsString(listResponse);
        response = Response.ok(json).location(new URI(endpointUrl)).build();
    } catch (Exception e) {
        log.error("Failure at serve method", e);
        response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
    }
    return response;
}
Also used : ListResponse(org.gluu.oxtrust.model.scim2.ListResponse) Response(javax.ws.rs.core.Response) ListResponse(org.gluu.oxtrust.model.scim2.ListResponse) URI(java.net.URI) RejectFilterParam(org.gluu.oxtrust.service.scim2.interceptor.RejectFilterParam)

Example 27 with ListResponse

use of io.jans.scim.model.scim2.ListResponse in project oxTrust by GluuFederation.

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
@RefAdjusted
@ApiOperation(value = "General search POST /.search", notes = "Returns a list of resources (https://tools.ietf.org/html/rfc7644#section-3.4.3)", response = ListResponse.class)
public Response search(@ApiParam(value = "SearchRequest", required = true) 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<JsonNode>();
            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(org.gluu.oxtrust.model.scim2.SearchRequest) ListResponse(org.gluu.oxtrust.model.scim2.ListResponse) ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode) ListResponseJsonSerializer(org.gluu.oxtrust.service.scim2.serialization.ListResponseJsonSerializer) URI(java.net.URI) ListResponse(org.gluu.oxtrust.model.scim2.ListResponse) Response(javax.ws.rs.core.Response) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) SimpleModule(org.codehaus.jackson.map.module.SimpleModule) RefAdjusted(org.gluu.oxtrust.service.scim2.interceptor.RefAdjusted) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ProtectedApi(org.gluu.oxtrust.service.filter.ProtectedApi)

Example 28 with ListResponse

use of io.jans.scim.model.scim2.ListResponse in project jans by JanssenProject.

the class ComplexSearchUserTest method searchNoAttributesParam.

@Test
public void searchNoAttributesParam() {
    final String ims = "Skype";
    logger.debug("Searching users with attribute nickName existent or ims.value={} using POST verb", ims);
    SearchRequest sr = new SearchRequest();
    sr.setFilter("nickName pr or ims.value eq \"" + ims + "\"");
    Response response = client.searchUsersPost(sr);
    assertEquals(response.getStatus(), OK.getStatusCode());
    ListResponse listResponse = response.readEntity(ListResponse.class);
    if (listResponse.getResources() != null) {
        for (BaseScimResource resource : listResponse.getResources()) {
            UserResource other = (UserResource) resource;
            boolean c1 = other.getNickName() != null;
            boolean c2 = true;
            if (other.getIms() != null)
                c2 = other.getIms().stream().anyMatch(im -> im.getValue().toLowerCase().equals(ims.toLowerCase()));
            assertTrue(c1 || c2);
        }
    }
}
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) 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)

Example 29 with ListResponse

use of io.jans.scim.model.scim2.ListResponse in project jans by JanssenProject.

the class ComplexSearchUserTest method searchAttributesParam.

@Test
public void searchAttributesParam() throws Exception {
    int count = 3;
    List<String> attrList = Arrays.asList("name.familyName", "active");
    logger.debug("Searching at most {} users using POST verb", count);
    logger.debug("Sorted by family name descending");
    logger.debug("Retrieving only the attributes {}", attrList);
    SearchRequest sr = new SearchRequest();
    sr.setFilter("name.familyName pr");
    sr.setSortBy("name.familyName");
    sr.setSortOrder("descending");
    // Generate a string with the attributes desired to be returned separated by comma
    sr.setAttributes(attrList.toString().replaceFirst("\\[", "").replaceFirst("]", ""));
    sr.setCount(count);
    Response response = client.searchUsersPost(sr);
    assertEquals(response.getStatus(), OK.getStatusCode());
    ListResponse listResponse = response.readEntity(ListResponse.class);
    if (listResponse.getResources().size() < count)
        logger.warn("Less than {} users satisfying the criteria. TESTER please check manually", count);
    else {
        // Obtain an array of results
        UserResource[] users = listResponse.getResources().stream().map(usrClass::cast).collect(Collectors.toList()).toArray(new UserResource[0]);
        assertEquals(users.length, count);
        // Build a set of all attributes that should not appear in the response
        Set<String> check = new HashSet<>();
        check.addAll(IntrospectUtil.allAttrs.get(usrClass));
        // Remove from the ALL list, those requested plus its "parents"
        for (String attr : attrList) {
            String part = attr;
            for (int i = part.length(); i > 0; i = part.lastIndexOf(".")) {
                part = part.substring(0, i);
                check.remove(part);
            }
        }
        // Remove those that are ALWAYS present (per spec)
        check.removeAll(IntrospectUtil.alwaysCoreAttrs.get(usrClass).keySet());
        // Confirm for every user, those attributes are not there
        for (UserResource user : users) {
            for (String path : check) {
                String val = null;
                try {
                    val = BeanUtils.getProperty(user, path);
                } catch (NestedNullException nne) {
                // Intentionally left empty
                } finally {
                    assertNull(val);
                }
            }
        }
        boolean correctSorting = true;
        for (int i = 1; i < users.length && correctSorting; i++) {
            String familyName = users[i - 1].getName().getFamilyName();
            String familyName2 = users[i].getName().getFamilyName();
            // First string has to be greater than or equal second
            correctSorting = familyName.compareTo(familyName2) >= 0;
        }
        if (!correctSorting) {
            // LDAP may ignore case sensitivity, try again using lowercasing
            correctSorting = true;
            for (int i = 1; i < users.length && correctSorting; i++) {
                String familyName = users[i - 1].getName().getFamilyName().toLowerCase();
                String familyName2 = users[i].getName().getFamilyName().toLowerCase();
                // First string has to be greater than or equal second
                correctSorting = familyName.compareTo(familyName2) >= 0;
            }
        }
        assertTrue(correctSorting);
    }
}
Also used : SearchRequest(io.jans.scim.model.scim2.SearchRequest) ListResponse(io.jans.scim.model.scim2.ListResponse) UserResource(io.jans.scim.model.scim2.user.UserResource) Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) NestedNullException(org.apache.commons.beanutils.NestedNullException) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test)

Example 30 with ListResponse

use of io.jans.scim.model.scim2.ListResponse in project jans by JanssenProject.

the class GroupsBulkTest 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(), Status.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) BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) SearchRequest(io.jans.scim.model.scim2.SearchRequest) ListResponse(io.jans.scim.model.scim2.ListResponse)

Aggregations

Response (javax.ws.rs.core.Response)38 ListResponse (io.jans.scim.model.scim2.ListResponse)36 Test (org.testng.annotations.Test)26 UserBaseTest (io.jans.scim2.client.UserBaseTest)16 SearchRequest (io.jans.scim.model.scim2.SearchRequest)12 UserResource (io.jans.scim.model.scim2.user.UserResource)11 URI (java.net.URI)11 BaseTest (io.jans.scim2.client.BaseTest)10 ListResponse (org.gluu.oxtrust.model.scim2.ListResponse)9 DefaultValue (javax.ws.rs.DefaultValue)8 HeaderParam (javax.ws.rs.HeaderParam)8 Produces (javax.ws.rs.Produces)8 ArrayList (java.util.ArrayList)7 GET (javax.ws.rs.GET)7 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)4 BaseScimResource (io.jans.scim.model.scim2.BaseScimResource)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)2 RejectFilterParam (io.jans.scim.service.scim2.interceptor.RejectFilterParam)2