use of org.gluu.oxtrust.model.scim2.Extension in project oxTrust by GluuFederation.
the class UserDeserializer method deserialize.
@Override
public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
log.info(" deserialize() ");
try {
JsonNode rootNode = jsonParser.readValueAsTree();
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
User user = mapper.readValue(rootNode.toString(), User.class);
if (user.getSchemas() == null) {
throw new IllegalArgumentException("Required field \"schemas\" is null or missing.");
} else if (!user.getSchemas().contains(Constants.USER_CORE_SCHEMA_ID)) {
throw new IllegalArgumentException("User Core schema is required.");
} else if (user.getSchemas().contains(Constants.USER_EXT_SCHEMA_ID)) {
JsonNode userExtensionNode = rootNode.get(Constants.USER_EXT_SCHEMA_ID);
if (userExtensionNode != null) {
ExtensionDeserializer deserializer = new ExtensionDeserializer();
deserializer.setId(Constants.USER_EXT_SCHEMA_ID);
SimpleModule deserializerModule = new SimpleModule("ExtensionDeserializerModule", new Version(1, 0, 0, ""));
deserializerModule.addDeserializer(Extension.class, deserializer);
mapper.registerModule(deserializerModule);
Extension extension = mapper.readValue(userExtensionNode.toString(), Extension.class);
user.addExtension(extension);
} else {
throw new IllegalArgumentException("User Extension schema is indicated, but value body is absent.");
}
}
return user;
} catch (Exception e) {
e.printStackTrace();
throw new IOException(INTERNAL_SERVER_ERROR_MESSAGE);
}
}
use of org.gluu.oxtrust.model.scim2.Extension in project oxTrust by GluuFederation.
the class UserSerializer method serializeUserExtension.
protected void serializeUserExtension(Map.Entry<String, JsonNode> rootNodeEntry, ObjectMapper mapper, User user, JsonGenerator jsonGenerator) throws Exception {
Extension extension = user.getExtension(rootNodeEntry.getKey());
Map<String, Object> list = new HashMap<String, Object>();
boolean enclosingWritten = false;
for (Map.Entry<String, Extension.Field> extEntry : extension.getFields().entrySet()) {
if (attributes != null && attributes.size() > 0) {
for (String attribute : attributes) {
attribute = FilterUtil.stripScim2Schema(attribute);
if (extEntry.getKey().equalsIgnoreCase(attribute)) {
if (!enclosingWritten) {
jsonGenerator.writeFieldName(rootNodeEntry.getKey());
enclosingWritten = true;
}
break;
}
}
} else {
if (!enclosingWritten) {
jsonGenerator.writeFieldName(rootNodeEntry.getKey());
enclosingWritten = true;
}
}
if (enclosingWritten) {
GluuAttribute gluuAttribute = attributeService.getAttributeByName(extEntry.getKey());
GluuAttributeDataType attributeDataType = gluuAttribute.getDataType();
if ((gluuAttribute.getOxMultivaluedAttribute() != null) && gluuAttribute.getOxMultivaluedAttribute().equals(OxMultivalued.TRUE)) {
if (attributeDataType.equals(GluuAttributeDataType.STRING) || attributeDataType.equals(GluuAttributeDataType.PHOTO)) {
List<String> stringList = Arrays.asList(mapper.readValue(extEntry.getValue().getValue(), String[].class));
list.put(extEntry.getKey(), stringList);
} else if (attributeDataType.equals(GluuAttributeDataType.DATE)) {
List<Date> dateList = Arrays.asList(mapper.readValue(extEntry.getValue().getValue(), Date[].class));
List<String> stringList = new ArrayList<String>();
DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime().withZoneUTC();
for (Date date : dateList) {
String dateString = dateTimeFormatter.print(date.getTime());
stringList.add(dateString);
}
list.put(extEntry.getKey(), stringList);
} else if (attributeDataType.equals(GluuAttributeDataType.NUMERIC)) {
List<BigDecimal> numberList = Arrays.asList(mapper.readValue(extEntry.getValue().getValue(), BigDecimal[].class));
list.put(extEntry.getKey(), numberList);
}
} else {
list.put(extEntry.getKey(), extEntry.getValue().getValue());
}
}
}
if (enclosingWritten) {
jsonGenerator.writeObject(list);
}
}
use of org.gluu.oxtrust.model.scim2.Extension in project oxTrust by GluuFederation.
the class ResourceTypeWS method getUserResourceType.
private ResourceType getUserResourceType() {
Class<? extends BaseScimResource> cls = UserResource.class;
List<Extension> usrExtensions = extService.getResourceExtensions(cls);
List<SchemaExtensionHolder> schemaExtensions = new ArrayList<SchemaExtensionHolder>();
for (Extension extension : usrExtensions) {
SchemaExtensionHolder userExtensionSchema = new SchemaExtensionHolder();
userExtensionSchema.setSchema(extension.getUrn());
userExtensionSchema.setRequired(false);
schemaExtensions.add(userExtensionSchema);
}
ResourceType usrRT = new ResourceType();
fillResourceType(usrRT, ScimResourceUtil.getSchemaAnnotation(cls), userService.getEndpointUrl(), getResourceLocation(USER_SUFFIX), schemaExtensions);
return usrRT;
}
use of org.gluu.oxtrust.model.scim2.Extension in project oxTrust by GluuFederation.
the class SchemaWebService method getSchemaInstance.
private SchemaResource getSchemaInstance(Class<? extends BaseScimResource> clazz, String urn) throws Exception {
if (ScimResourceUtil.getDefaultSchemaUrn(clazz).equals(urn))
// Process core attributes
return getSchemaInstance(clazz);
else {
// process extension attributes
SchemaResource resource = null;
Class<? extends BaseScimResource> schemaCls = SchemaResource.class;
// Find the appropriate extension
List<Extension> extensions = extService.getResourceExtensions(clazz);
for (Extension extension : extensions) {
if (extension.getUrn().equals(urn)) {
Meta meta = new Meta();
meta.setResourceType(ScimResourceUtil.getType(schemaCls));
meta.setLocation(endpointUrl + "/" + urn);
resource = new SchemaResource();
resource.setId(urn);
resource.setName(extension.getName());
resource.setDescription(extension.getDescription());
resource.setMeta(meta);
List<SchemaAttribute> attribs = new ArrayList<SchemaAttribute>();
for (ExtensionField field : extension.getFields().values()) {
SchemaAttribute schAttr = new SchemaAttribute();
schAttr.setName(field.getName());
schAttr.setMultiValued(field.isMultiValued());
schAttr.setDescription(field.getDescription());
schAttr.setRequired(false);
schAttr.setCanonicalValues(null);
schAttr.setCaseExact(false);
schAttr.setMutability(AttributeDefinition.Mutability.READ_WRITE.getName());
schAttr.setReturned(AttributeDefinition.Returned.DEFAULT.getName());
schAttr.setUniqueness(AttributeDefinition.Uniqueness.NONE.getName());
schAttr.setReferenceTypes(null);
AttributeDefinition.Type type = field.getAttributeDefinitionType();
schAttr.setType(type == null ? null : type.getName());
schAttr.setSubAttributes(null);
attribs.add(schAttr);
}
resource.setAttributes(attribs);
break;
}
}
return resource;
}
}
use of org.gluu.oxtrust.model.scim2.Extension in project oxTrust by GluuFederation.
the class ExtensionService method getResourceExtensions.
public List<Extension> getResourceExtensions(Class<? extends BaseScimResource> cls) {
List<Extension> list = new ArrayList<Extension>();
try {
// Currently support one extension only for User Resource
if (cls.equals(UserResource.class)) {
Map<String, ExtensionField> fields = new HashMap<String, ExtensionField>();
for (GluuAttribute attribute : attrService.getSCIMRelatedAttributes()) {
if (attribute.getOxSCIMCustomAttribute().equals(ScimCustomAtribute.TRUE)) {
// first non-null check is needed because certain entries do not have the multivalue attribute set
boolean multi = attribute.getOxMultivaluedAttribute() != null && attribute.getOxMultivaluedAttribute().equals(OxMultivalued.TRUE);
ExtensionField field = new ExtensionField();
field.setDescription(attribute.getDescription());
field.setType(attribute.getDataType());
field.setMultiValued(multi);
field.setName(attribute.getName());
fields.put(attribute.getName(), field);
}
}
Extension ext = new Extension(USER_EXT_SCHEMA_ID);
ext.setFields(fields);
ext.setName(USER_EXT_SCHEMA_NAME);
ext.setDescription(USER_EXT_SCHEMA_DESCRIPTION);
list.add(ext);
}
} catch (Exception e) {
log.error("An error ocurred when building extension for {}", cls.getName());
log.error(e.getMessage(), e);
}
return list;
}
Aggregations