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;
}
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);
}
}
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());
}
}
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);
}
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;
}
Aggregations