Search in sources :

Example 71 with Task

use of com.evolveum.midpoint.task.api.Task in project midpoint by Evolveum.

the class WebModelServiceUtils method createSimpleTask.

public static Task createSimpleTask(String operation, PrismObject<UserType> owner, TaskManager manager) {
    Task task = manager.createTaskInstance(operation);
    if (owner == null) {
        MidPointPrincipal user = SecurityUtils.getPrincipalUser();
        if (user == null) {
            throw new RestartResponseException(PageLogin.class);
        } else {
            owner = user.getUser().asPrismObject();
        }
    }
    task.setOwner(owner);
    task.setChannel(SchemaConstants.CHANNEL_GUI_USER_URI);
    return task;
}
Also used : Task(com.evolveum.midpoint.task.api.Task) RestartResponseException(org.apache.wicket.RestartResponseException) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal)

Example 72 with Task

use of com.evolveum.midpoint.task.api.Task in project midpoint by Evolveum.

the class InitialDataImport method init.

public void init() throws SchemaException {
    LOGGER.info("Starting initial object import (if necessary).");
    OperationResult mainResult = new OperationResult(OPERATION_INITIAL_OBJECTS_IMPORT);
    Task task = taskManager.createTaskInstance(OPERATION_INITIAL_OBJECTS_IMPORT);
    task.setChannel(SchemaConstants.CHANNEL_GUI_INIT_URI);
    int count = 0;
    int errors = 0;
    File[] files = getInitialImportObjects();
    LOGGER.debug("Files to be imported: {}.", Arrays.toString(files));
    // We need to provide a fake Spring security context here.
    // We have to fake it because we do not have anything in the repository yet. And to get
    // something to the repository we need a context. Chicken and egg. So we fake the egg.
    SecurityContext securityContext = SecurityContextHolder.getContext();
    UserType userAdministrator = new UserType();
    prismContext.adopt(userAdministrator);
    userAdministrator.setName(new PolyStringType(new PolyString("initAdmin", "initAdmin")));
    MidPointPrincipal principal = new MidPointPrincipal(userAdministrator);
    AuthorizationType superAutzType = new AuthorizationType();
    prismContext.adopt(superAutzType, RoleType.class, new ItemPath(RoleType.F_AUTHORIZATION));
    superAutzType.getAction().add(AuthorizationConstants.AUTZ_ALL_URL);
    Authorization superAutz = new Authorization(superAutzType);
    Collection<Authorization> authorities = principal.getAuthorities();
    authorities.add(superAutz);
    Authentication authentication = new PreAuthenticatedAuthenticationToken(principal, null);
    securityContext.setAuthentication(authentication);
    for (File file : files) {
        try {
            LOGGER.debug("Considering initial import of file {}.", file.getName());
            PrismObject object = prismContext.parseObject(file);
            if (ReportType.class.equals(object.getCompileTimeClass())) {
                ReportTypeUtil.applyDefinition(object, prismContext);
            }
            Boolean importObject = importObject(object, file, task, mainResult);
            if (importObject == null) {
                continue;
            }
            if (importObject) {
                count++;
            } else {
                errors++;
            }
        } catch (Exception ex) {
            LoggingUtils.logUnexpectedException(LOGGER, "Couldn't import file {}", ex, file.getName());
            mainResult.recordFatalError("Couldn't import file '" + file.getName() + "'", ex);
        }
    }
    securityContext.setAuthentication(null);
    mainResult.recomputeStatus("Couldn't import objects.");
    LOGGER.info("Initial object import finished ({} objects imported, {} errors)", count, errors);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Initialization status:\n" + mainResult.debugDump());
    }
}
Also used : PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) Task(com.evolveum.midpoint.task.api.Task) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) URISyntaxException(java.net.URISyntaxException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) IOException(java.io.IOException) Authorization(com.evolveum.midpoint.security.api.Authorization) PrismObject(com.evolveum.midpoint.prism.PrismObject) Authentication(org.springframework.security.core.Authentication) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) SecurityContext(org.springframework.security.core.context.SecurityContext) AuthorizationType(com.evolveum.midpoint.xml.ns._public.common.common_3.AuthorizationType) File(java.io.File) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 73 with Task

use of com.evolveum.midpoint.task.api.Task in project midpoint by Evolveum.

the class PageAccounts method exportPerformed.

private void exportPerformed(AjaxRequestTarget target) {
    if (resourceModel.getObject() == null) {
        warn(getString("pageAccounts.message.resourceNotSelected"));
        refreshEverything(target);
        return;
    }
    String fileName = "accounts-" + WebComponentUtil.formatDate("yyyy-MM-dd-HH-mm-ss", new Date()) + ".xml";
    OperationResult result = new OperationResult(OPERATION_EXPORT);
    Writer writer = null;
    try {
        Task task = createSimpleTask(OPERATION_EXPORT);
        writer = createWriter(fileName);
        writeHeader(writer);
        final Writer handlerWriter = writer;
        ResultHandler handler = new AbstractSummarizingResultHandler() {

            @Override
            protected boolean handleObject(PrismObject object, OperationResult parentResult) {
                OperationResult result = parentResult.createMinorSubresult(OPERATION_EXPORT_ACCOUNT);
                try {
                    String xml = getPrismContext().serializeObjectToString(object, PrismContext.LANG_XML);
                    handlerWriter.write(xml);
                    result.computeStatus();
                } catch (Exception ex) {
                    LoggingUtils.logUnexpectedException(LOGGER, "Couldn't serialize account", ex);
                    result.recordFatalError("Couldn't serialize account.", ex);
                    return false;
                }
                return true;
            }
        };
        try {
            ObjectQuery query = ObjectQuery.createObjectQuery(createResourceAndQueryFilter());
            getModelService().searchObjectsIterative(ShadowType.class, query, handler, SelectorOptions.createCollection(GetOperationOptions.createRaw()), task, result);
        } finally {
            writeFooter(writer);
        }
        result.recomputeStatus();
    } catch (Exception ex) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't export accounts", ex);
        error(getString("PageAccounts.exportException", ex.getMessage()));
    } finally {
        IOUtils.closeQuietly(writer);
    }
    filesModel.reset();
    success(getString("PageAccounts.message.success.export", fileName));
    target.add(getFeedbackPanel(), get(createComponentPath(ID_FORM_ACCOUNT, ID_FILES_CONTAINER)));
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) Task(com.evolveum.midpoint.task.api.Task) AbstractSummarizingResultHandler(com.evolveum.midpoint.schema.AbstractSummarizingResultHandler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) AbstractSummarizingResultHandler(com.evolveum.midpoint.schema.AbstractSummarizingResultHandler) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) Date(java.util.Date) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) RestartResponseException(org.apache.wicket.RestartResponseException) IOException(java.io.IOException) CommonException(com.evolveum.midpoint.util.exception.CommonException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 74 with Task

use of com.evolveum.midpoint.task.api.Task in project midpoint by Evolveum.

the class PageAccounts method loadShadowOwner.

private <F extends FocusType> F loadShadowOwner(IModel<SelectableBean> model) {
    F owner = null;
    ShadowType shadow = getShadow(model);
    String shadowOid;
    if (shadow != null) {
        shadowOid = shadow.getOid();
    } else {
        return null;
    }
    Task task = createSimpleTask(OPERATION_LOAD_ACCOUNT_OWNER);
    OperationResult result = new OperationResult(OPERATION_LOAD_ACCOUNT_OWNER);
    try {
        PrismObject prismOwner = getModelService().searchShadowOwner(shadowOid, null, task, result);
        if (prismOwner != null) {
            owner = (F) prismOwner.asObjectable();
        }
    } catch (ObjectNotFoundException exception) {
    //owner was not found, it's possible and it's ok on unlinked accounts
    } catch (Exception ex) {
        result.recordFatalError(getString("PageAccounts.message.ownerNotFound", shadowOid), ex);
        LoggingUtils.logUnexpectedException(LOGGER, "Could not load owner of account with oid: " + shadowOid, ex);
    } finally {
        result.computeStatusIfUnknown();
    }
    if (WebComponentUtil.showResultInPage(result)) {
        showResult(result, false);
    }
    return owner;
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) Task(com.evolveum.midpoint.task.api.Task) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) RestartResponseException(org.apache.wicket.RestartResponseException) IOException(java.io.IOException) CommonException(com.evolveum.midpoint.util.exception.CommonException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 75 with Task

use of com.evolveum.midpoint.task.api.Task in project midpoint by Evolveum.

the class PageSelfDashboard method loadUser.

private PrismObject<UserType> loadUser() {
    MidPointPrincipal principal = SecurityUtils.getPrincipalUser();
    Validate.notNull(principal, "No principal");
    if (principal.getOid() == null) {
        throw new IllegalArgumentException("No OID in principal: " + principal);
    }
    Task task = createSimpleTask(OPERATION_LOAD_USER);
    OperationResult result = task.getResult();
    PrismObject<UserType> user = WebModelServiceUtils.loadObject(UserType.class, principal.getOid(), PageSelfDashboard.this, task, result);
    result.computeStatus();
    if (!WebComponentUtil.isSuccessOrHandledError(result)) {
        showResult(result);
    }
    return user;
}
Also used : Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal)

Aggregations

Task (com.evolveum.midpoint.task.api.Task)4943 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4698 Test (org.testng.annotations.Test)3991 AbstractInitializedModelIntegrationTest (com.evolveum.midpoint.model.intest.AbstractInitializedModelIntegrationTest)599 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)542 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)464 PrismObject (com.evolveum.midpoint.prism.PrismObject)327 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)323 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)290 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)282 ArrayList (java.util.ArrayList)249 QName (javax.xml.namespace.QName)208 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)199 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)193 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)175 DummyAccount (com.evolveum.icf.dummy.resource.DummyAccount)159 AbstractConfiguredModelIntegrationTest (com.evolveum.midpoint.model.intest.AbstractConfiguredModelIntegrationTest)140 AbstractModelIntegrationTest (com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)123 SelectorOptions (com.evolveum.midpoint.schema.SelectorOptions)108 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)108