Search in sources :

Example 36 with PlainSchemaTO

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

the class SchemaRestClient method deletePlainSchema.

public PlainSchemaTO deletePlainSchema(final String name) {
    PlainSchemaTO response = getService(SchemaService.class).read(SchemaType.PLAIN, name);
    getService(SchemaService.class).delete(SchemaType.PLAIN, name);
    return response;
}
Also used : PlainSchemaTO(org.apache.syncope.common.lib.to.PlainSchemaTO) SchemaService(org.apache.syncope.common.rest.api.service.SchemaService)

Example 37 with PlainSchemaTO

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

the class UserSelfReadResource method newResourceResponse.

@Override
protected ResourceResponse newResourceResponse(final IResource.Attributes attributes) {
    LOG.debug("Requested user self information");
    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;
        }
        UserTO userTO = SerializationUtils.clone(SyncopeEnduserSession.get().getSelfTO());
        // 1. Date -> millis conversion for PLAIN MEMBERSHIPS attributes of USER
        for (PlainSchemaTO plainSchema : SyncopeEnduserSession.get().getDatePlainSchemas()) {
            for (MembershipTO membership : userTO.getMemberships()) {
                dateToMillis(membership.getPlainAttrs(), plainSchema);
            }
        }
        // 2. membership attributes management
        for (MembershipTO membership : userTO.getMemberships()) {
            String groupName = membership.getGroupName();
            membership.getPlainAttrs().stream().map(attr -> {
                attr.setSchema(groupName.concat(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR).concat(attr.getSchema()));
                return attr;
            }).forEachOrdered(attr -> {
                userTO.getPlainAttrs().add(attr);
            });
            membership.getPlainAttrs().clear();
            membership.getDerAttrs().stream().map(attr -> {
                attr.setSchema(groupName.concat(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR).concat(attr.getSchema()));
                return attr;
            }).forEachOrdered(attr -> {
                userTO.getDerAttrs().add(attr);
            });
            membership.getDerAttrs().clear();
            membership.getVirAttrs().stream().map((attr) -> {
                attr.setSchema(groupName.concat(SyncopeEnduserConstants.MEMBERSHIP_ATTR_SEPARATOR).concat(attr.getSchema()));
                return attr;
            }).forEachOrdered(attr -> {
                userTO.getVirAttrs().add(attr);
            });
            membership.getVirAttrs().clear();
        }
        // USER from customization, if empty or null ignore it, use it to filter attributes otherwise
        applyFromCustomization(userTO, SyncopeEnduserApplication.get().getCustomForm());
        // 1.1 Date -> millis conversion for PLAIN attributes of USER
        for (PlainSchemaTO plainSchema : SyncopeEnduserSession.get().getDatePlainSchemas()) {
            dateToMillis(userTO.getPlainAttrs(), plainSchema);
        }
        final String selfTOJson = MAPPER.writeValueAsString(userTO);
        response.setContentType(MediaType.APPLICATION_JSON);
        response.setTextEncoding(StandardCharsets.UTF_8.name());
        response.setWriteCallback(new WriteCallback() {

            @Override
            public void writeData(final Attributes attributes) throws IOException {
                attributes.getResponse().write(selfTOJson);
            }
        });
        response.setStatusCode(Response.Status.OK.getStatusCode());
    } catch (Exception e) {
        LOG.error("Error retrieving selfTO", e);
        response.setError(Response.Status.BAD_REQUEST.getStatusCode(), new StringBuilder().append("ErrorMessage{{ ").append(e.getMessage()).append(" }}").toString());
    }
    return response;
}
Also used : AttrTO(org.apache.syncope.common.lib.to.AttrTO) Set(java.util.Set) AbstractResource(org.apache.wicket.request.resource.AbstractResource) IOException(java.io.IOException) SerializationUtils(org.apache.commons.lang3.SerializationUtils) SchemaType(org.apache.syncope.common.lib.types.SchemaType) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) SyncopeEnduserConstants(org.apache.syncope.client.enduser.SyncopeEnduserConstants) SyncopeEnduserSession(org.apache.syncope.client.enduser.SyncopeEnduserSession) PlainSchemaTO(org.apache.syncope.common.lib.to.PlainSchemaTO) IResource(org.apache.wicket.request.resource.IResource) Response(javax.ws.rs.core.Response) Map(java.util.Map) SyncopeEnduserApplication(org.apache.syncope.client.enduser.SyncopeEnduserApplication) CustomAttributesInfo(org.apache.syncope.client.enduser.model.CustomAttributesInfo) Resource(org.apache.syncope.client.enduser.annotations.Resource) UserTO(org.apache.syncope.common.lib.to.UserTO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) IOException(java.io.IOException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) PlainSchemaTO(org.apache.syncope.common.lib.to.PlainSchemaTO) UserTO(org.apache.syncope.common.lib.to.UserTO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO)

Example 38 with PlainSchemaTO

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

the class BaseUserSelfResource method dateToMillis.

protected void dateToMillis(final Set<AttrTO> attrs, final PlainSchemaTO plainSchema) throws ParseException {
    final FastDateFormat fmt = FastDateFormat.getInstance(plainSchema.getConversionPattern());
    attrs.stream().filter(attr -> (attr.getSchema().equals(plainSchema.getKey()))).forEachOrdered(attr -> {
        for (ListIterator<String> itor = attr.getValues().listIterator(); itor.hasNext(); ) {
            String value = itor.next();
            try {
                itor.set(String.valueOf(fmt.parse(value).getTime()));
            } catch (ParseException ex) {
                LOG.error("Unable to parse date {}", value);
            }
        }
    });
}
Also used : PlainSchemaTO(org.apache.syncope.common.lib.to.PlainSchemaTO) ListIterator(java.util.ListIterator) AttrTO(org.apache.syncope.common.lib.to.AttrTO) Set(java.util.Set) IOException(java.io.IOException) ParseException(java.text.ParseException) StandardCharsets(java.nio.charset.StandardCharsets) FastDateFormat(org.apache.commons.lang3.time.FastDateFormat) FastDateFormat(org.apache.commons.lang3.time.FastDateFormat) ParseException(java.text.ParseException)

Example 39 with PlainSchemaTO

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

the class SchemaLogic method create.

@PreAuthorize("hasRole('" + StandardEntitlement.SCHEMA_CREATE + "')")
@SuppressWarnings("unchecked")
public <T extends SchemaTO> T create(final SchemaType schemaType, final T schemaTO) {
    if (StringUtils.isBlank(schemaTO.getKey())) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
        sce.getElements().add("Schema key");
        throw sce;
    }
    if (doesSchemaExist(schemaType, schemaTO.getKey())) {
        throw new DuplicateException(schemaType + "/" + schemaTO.getKey());
    }
    T created;
    switch(schemaType) {
        case VIRTUAL:
            VirSchema virSchema = virSchemaDAO.save(binder.create((VirSchemaTO) schemaTO));
            created = (T) binder.getVirSchemaTO(virSchema);
            break;
        case DERIVED:
            DerSchema derSchema = derSchemaDAO.save(binder.create((DerSchemaTO) schemaTO));
            created = (T) binder.getDerSchemaTO(derSchema);
            break;
        case PLAIN:
        default:
            PlainSchema plainSchema = plainSchemaDAO.save(binder.create((PlainSchemaTO) schemaTO));
            created = (T) binder.getPlainSchemaTO(plainSchema);
    }
    return created;
}
Also used : PlainSchemaTO(org.apache.syncope.common.lib.to.PlainSchemaTO) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) DuplicateException(org.apache.syncope.core.persistence.api.dao.DuplicateException) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) VirSchemaTO(org.apache.syncope.common.lib.to.VirSchemaTO) DerSchemaTO(org.apache.syncope.common.lib.to.DerSchemaTO) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

PlainSchemaTO (org.apache.syncope.common.lib.to.PlainSchemaTO)39 Test (org.junit.jupiter.api.Test)26 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)19 AnyTypeClassTO (org.apache.syncope.common.lib.to.AnyTypeClassTO)11 UserTO (org.apache.syncope.common.lib.to.UserTO)9 AttrTO (org.apache.syncope.common.lib.to.AttrTO)6 Response (javax.ws.rs.core.Response)5 IOException (java.io.IOException)4 Set (java.util.Set)4 StandardCharsets (java.nio.charset.StandardCharsets)3 AnyTypeTO (org.apache.syncope.common.lib.to.AnyTypeTO)3 DerSchemaTO (org.apache.syncope.common.lib.to.DerSchemaTO)3 GroupTO (org.apache.syncope.common.lib.to.GroupTO)3 MembershipTO (org.apache.syncope.common.lib.to.MembershipTO)3 SchemaService (org.apache.syncope.common.rest.api.service.SchemaService)3 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 ListIterator (java.util.ListIterator)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2