Search in sources :

Example 41 with ListResponse

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

the class ListResponseProvider method readFrom.

public ListResponse readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    InputStreamReader isr = new InputStreamReader(entityStream, Charset.forName("UTF-8"));
    List<BaseScimResource> resources = null;
    // we will get a LinkedHashMap here...
    Map<String, Object> map = mapper.readValue(isr, new TypeReference<Map<String, Object>>() {
    });
    // "remove" what came originally
    Object branch = map.remove("Resources");
    if (branch != null) {
        resources = new ArrayList<>();
        // Here we assume everything is coming from the server correctly (that is, following the spec) and conversions succeed
        for (Object resource : (Collection) branch) {
            Map<String, Object> resourceAsMap = IntrospectUtil.strObjMap(resource);
            List<String> schemas = (List<String>) resourceAsMap.get("schemas");
            // Guess the real class of the resource by inspecting the schemas in it
            for (String schema : schemas) {
                for (Class<? extends BaseScimResource> cls : IntrospectUtil.allAttrs.keySet()) {
                    if (ScimResourceUtil.getSchemaAnnotation(cls).id().equals(schema)) {
                        // Create the object with the proper class
                        resources.add(mapper.convertValue(resource, cls));
                        logger.trace("Found resource of class {} in ListResponse", cls.getSimpleName());
                        break;
                    }
                }
            }
        }
    }
    ListResponse response = mapper.convertValue(map, ListResponse.class);
    response.setResources(resources);
    return response;
}
Also used : InputStreamReader(java.io.InputStreamReader) ListResponse(io.jans.scim.model.scim2.ListResponse) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 42 with ListResponse

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

the class ListResponseJsonSerializer method serialize.

@Override
public void serialize(ListResponse listResponse, JsonGenerator jGen, SerializerProvider provider) throws IOException {
    try {
        jGen.writeStartObject();
        jGen.writeArrayFieldStart("schemas");
        for (String schema : listResponse.getSchemas()) jGen.writeString(schema);
        jGen.writeEndArray();
        jGen.writeNumberField("totalResults", listResponse.getTotalResults());
        if (!skipResults) {
            if (listResponse.getItemsPerPage() > 0) {
                // these two bits are "REQUIRED when partial results are returned due to pagination." (section 3.4.2 RFC 7644)
                jGen.writeNumberField("startIndex", listResponse.getStartIndex());
                jGen.writeNumberField("itemsPerPage", listResponse.getItemsPerPage());
            }
            // Section 3.4.2 RFC 7644: Resources [...] REQUIRED if "totalResults" is non-zero
            if (listResponse.getTotalResults() > 0) {
                jGen.writeArrayFieldStart("Resources");
                if (listResponse.getResources().size() > 0) {
                    for (BaseScimResource resource : listResponse.getResources()) {
                        JsonNode jsonResource = mapper.readTree(resourceSerializer.serialize(resource, attributes, excludeAttributes));
                        jGen.writeTree(jsonResource);
                    }
                } else if (jsonResources != null) {
                    for (JsonNode node : jsonResources) {
                        jGen.writeTree(node);
                    }
                }
                jGen.writeEndArray();
            }
        }
        jGen.writeEndObject();
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) IOException(java.io.IOException)

Example 43 with ListResponse

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

the class QueryParamRetrievalTest method multipleRetrieval.

@Test
public void multipleRetrieval() {
    logger.debug("Retrieving test users...");
    String include = "displayName, externalId";
    Response response = client.searchUsers("displayName co \"Test\"", null, null, null, null, include, null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    ListResponse listResponse = response.readEntity(ListResponse.class);
    List<UserResource> list = listResponse.getResources().stream().map(usrClass::cast).collect(Collectors.toList());
    // Verify users retrieved contain attributes of interest
    for (UserResource usr : list) {
        assertNotNull(usr.getDisplayName());
        assertTrue(usr.getDisplayName().toLowerCase().contains("test"));
    // assertNotNull(usr.getExternalId());
    }
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) ListResponse(io.jans.scim.model.scim2.ListResponse) UserResource(io.jans.scim.model.scim2.user.UserResource) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test)

Example 44 with ListResponse

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

the class QueryParamRetrievalTest method multipleRetrievalExcluding.

@Test(dependsOnMethods = "multipleRetrieval")
public void multipleRetrievalExcluding() {
    logger.debug("Retrieving test users...");
    String exclude = "displayName, externalId, name, addresses, emails";
    Response response = client.searchUsers("displayName co \"Test\"", null, null, null, null, null, exclude);
    assertEquals(response.getStatus(), OK.getStatusCode());
    ListResponse listResponse = response.readEntity(ListResponse.class);
    List<UserResource> list = listResponse.getResources().stream().map(UserResource.class::cast).collect(Collectors.toList());
    // Verify users retrieved do not contain attributes of interest
    for (UserResource usr : list) {
        assertNull(usr.getDisplayName());
        assertNull(usr.getExternalId());
        assertNull(usr.getName());
        assertNull(usr.getAddresses());
        assertNull(usr.getEmails());
        assertNotNull(usr.getSchemas());
        assertNotNull(usr.getId());
    }
    user = list.get(0);
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) ListResponse(io.jans.scim.model.scim2.ListResponse) UserResource(io.jans.scim.model.scim2.user.UserResource) UserBaseTest(io.jans.scim2.client.UserBaseTest) Test(org.testng.annotations.Test)

Example 45 with ListResponse

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

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 : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) ListResponse(io.jans.scim.model.scim2.ListResponse) URI(java.net.URI) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RejectFilterParam(io.jans.scim.service.scim2.interceptor.RejectFilterParam)

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