Search in sources :

Example 6 with TypeExtensionTO

use of org.apache.syncope.common.lib.to.TypeExtensionTO in project syncope by apache.

the class SchemaResource method newResourceResponse.

@Override
protected AbstractResource.ResourceResponse newResourceResponse(final IResource.Attributes attributes) {
    LOG.debug("Search all {} any type kind related schemas", AnyTypeKind.USER.name());
    ResourceResponse response = new AbstractResource.ResourceResponse();
    response.setContentType(MediaType.APPLICATION_JSON);
    try {
        HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
        if (!xsrfCheck(request)) {
            LOG.error("XSRF TOKEN does not match");
            response.setError(Response.Status.BAD_REQUEST.getStatusCode(), "XSRF TOKEN does not match");
            return response;
        }
        List<String> classes = Collections.emptyList();
        String group = attributes.getParameters().get("group").toString();
        if (group != null) {
            try {
                TypeExtensionTO typeExt = SyncopeEnduserSession.get().getService(SyncopeService.class).readUserTypeExtension(group);
                classes = typeExt.getAuxClasses();
            } catch (Exception e) {
                LOG.error("Could not read User type extension for Group {}", group);
            }
        } else {
            String anyTypeClass = attributes.getParameters().get("anyTypeClass").toString();
            if (anyTypeClass != null) {
                classes = Collections.singletonList(anyTypeClass);
            } else {
                classes = SyncopeEnduserSession.get().getService(SyncopeService.class).platform().getUserClasses();
            }
        }
        // USER from customization, if empty or null ignore it, use it to filter attributes otherwise
        Map<String, CustomAttributesInfo> customForm = SyncopeEnduserApplication.get().getCustomForm();
        SchemaService schemaService = SyncopeEnduserSession.get().getService(SchemaService.class);
        final List<SchemaTO> plainSchemas = classes.isEmpty() ? Collections.<SchemaTO>emptyList() : customForm == null || customForm.isEmpty() || customForm.get(SchemaType.PLAIN.name()) == null ? schemaService.search(new SchemaQuery.Builder().type(SchemaType.PLAIN).anyTypeClasses(classes).build()) : customForm.get(SchemaType.PLAIN.name()).isShow() ? customizeSchemas(schemaService.search(new SchemaQuery.Builder().type(SchemaType.PLAIN).anyTypeClasses(classes).build()), group, customForm.get(SchemaType.PLAIN.name()).getAttributes()) : Collections.<SchemaTO>emptyList();
        final List<SchemaTO> derSchemas = classes.isEmpty() ? Collections.<SchemaTO>emptyList() : customForm == null || customForm.isEmpty() || customForm.get(SchemaType.DERIVED.name()) == null ? schemaService.search(new SchemaQuery.Builder().type(SchemaType.DERIVED).anyTypeClasses(classes).build()) : customForm.get(SchemaType.DERIVED.name()).isShow() ? customizeSchemas(schemaService.search(new SchemaQuery.Builder().type(SchemaType.DERIVED).anyTypeClasses(classes).build()), group, customForm.get(SchemaType.DERIVED.name()).getAttributes()) : Collections.<SchemaTO>emptyList();
        final List<SchemaTO> virSchemas = classes.isEmpty() ? Collections.<SchemaTO>emptyList() : customForm == null || customForm.isEmpty() || customForm.get(SchemaType.VIRTUAL.name()) == null ? schemaService.search(new SchemaQuery.Builder().type(SchemaType.VIRTUAL).anyTypeClasses(classes).build()) : customForm.get(SchemaType.VIRTUAL.name()).isShow() ? customizeSchemas(schemaService.search(new SchemaQuery.Builder().type(SchemaType.VIRTUAL).anyTypeClasses(classes).build()), group, customForm.get(SchemaType.VIRTUAL.name()).getAttributes()) : Collections.<SchemaTO>emptyList();
        if (group != null) {
            plainSchemas.forEach(schema -> {
                schema.setKey(compositeSchemaKey(group, schema.getKey()));
            });
            derSchemas.forEach(schema -> {
                schema.setKey(compositeSchemaKey(group, schema.getKey()));
            });
            virSchemas.forEach(schema -> {
                schema.setKey(compositeSchemaKey(group, schema.getKey()));
            });
        }
        response.setTextEncoding(StandardCharsets.UTF_8.name());
        response.setWriteCallback(new AbstractResource.WriteCallback() {

            @Override
            public void writeData(final IResource.Attributes attributes) throws IOException {
                attributes.getResponse().write(MAPPER.writeValueAsString(new SchemaResponse().plainSchemas(plainSchemas).derSchemas(derSchemas).virSchemas(virSchemas)));
            }
        });
        response.setStatusCode(Response.Status.OK.getStatusCode());
    } catch (Exception e) {
        LOG.error("Error retrieving {} any type kind related schemas", AnyTypeKind.USER.name(), e);
        response.setError(Response.Status.BAD_REQUEST.getStatusCode(), new StringBuilder().append("ErrorMessage{{ ").append(e.getMessage()).append(" }}").toString());
    }
    return response;
}
Also used : AbstractResource(org.apache.wicket.request.resource.AbstractResource) IOException(java.io.IOException) TypeExtensionTO(org.apache.syncope.common.lib.to.TypeExtensionTO) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) SyncopeService(org.apache.syncope.common.rest.api.service.SyncopeService) SchemaService(org.apache.syncope.common.rest.api.service.SchemaService) SchemaTO(org.apache.syncope.common.lib.to.SchemaTO) SchemaResponse(org.apache.syncope.client.enduser.model.SchemaResponse) CustomAttributesInfo(org.apache.syncope.client.enduser.model.CustomAttributesInfo) IResource(org.apache.wicket.request.resource.IResource)

Aggregations

TypeExtensionTO (org.apache.syncope.common.lib.to.TypeExtensionTO)6 GroupTO (org.apache.syncope.common.lib.to.GroupTO)3 GroupPatch (org.apache.syncope.common.lib.patch.GroupPatch)2 Entity (org.apache.syncope.core.persistence.api.entity.Entity)2 IOException (java.io.IOException)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 StringUtils (org.apache.commons.lang3.StringUtils)1 CustomAttributesInfo (org.apache.syncope.client.enduser.model.CustomAttributesInfo)1 SchemaResponse (org.apache.syncope.client.enduser.model.SchemaResponse)1 SyncopeClientCompositeException (org.apache.syncope.common.lib.SyncopeClientCompositeException)1 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)1 MembershipTO (org.apache.syncope.common.lib.to.MembershipTO)1 SchemaTO (org.apache.syncope.common.lib.to.SchemaTO)1