Search in sources :

Example 1 with ITaskResult

use of net.idea.restnet.i.task.ITaskResult in project ambit-mirror by ideaconsult.

the class AuthTokenFactory method testURI.

@Test
public void testURI() throws Exception {
    ICallableTask c = new ICallableTask() {

        protected UUID uuid;

        public ITaskResult call() throws Exception {
            return new TaskResult("quickTaskURI");
        }

        @Override
        public UUID getUuid() {
            return uuid;
        }

        @Override
        public void setUuid(UUID uuid) {
            this.uuid = uuid;
        }

        @Override
        public String getTaskCategory() {
            return null;
        }
    };
    app.addTask("Test task", c, new Reference(String.format("http://localhost:%d", port)), getUserToken());
    testGet(getTestURI(), MediaType.TEXT_URI_LIST);
}
Also used : ICallableTask(net.idea.restnet.i.task.ICallableTask) Reference(org.restlet.data.Reference) TaskResult(ambit2.rest.task.TaskResult) ITaskResult(net.idea.restnet.i.task.ITaskResult) UUID(java.util.UUID) Test(org.junit.Test) ResourceTest(ambit2.rest.test.ResourceTest)

Example 2 with ITaskResult

use of net.idea.restnet.i.task.ITaskResult in project ambit-mirror by ideaconsult.

the class PingResource method pingI5.

protected ITask<ITaskResult, String> pingI5(Reference uri, Context context) throws ResourceException {
    ITask<ITaskResult, String> task;
    DBConnection dbc = new DBConnection(context);
    I5LightClient cli = null;
    long now = System.currentTimeMillis();
    if (server == null)
        server = dbc.getProperty("i5.server");
    if (user == null)
        user = dbc.getProperty("i5.user");
    if (pass == null)
        pass = dbc.getProperty("i5.pass");
    task = new Task<ITaskResult, String>(user);
    task.setName("Ping " + server);
    task.setStatus(TaskStatus.Running);
    task.setUri(new TaskResult(uri.toString(), false));
    try {
        cli = new I5LightClient(server);
        if (cli.login(user, pass)) {
            task.setStatus(TaskStatus.Completed);
        } else {
            task.setStatus(TaskStatus.Error);
            task.setError(new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED));
        }
    } catch (Exception x) {
        task.setError(new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x.getMessage()));
        task.setStatus(TaskStatus.Error);
    } finally {
        try {
            cli.logout();
        } catch (Exception x) {
        }
    }
    task.setTimeCompleted(System.currentTimeMillis());
    return task;
}
Also used : DBConnection(ambit2.rest.DBConnection) I5LightClient(net.idea.i5.cli.I5LightClient) ITaskResult(net.idea.restnet.i.task.ITaskResult) TaskResult(net.idea.restnet.i.task.TaskResult) ITaskResult(net.idea.restnet.i.task.ITaskResult) ResourceException(org.restlet.resource.ResourceException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException)

Example 3 with ITaskResult

use of net.idea.restnet.i.task.ITaskResult in project ambit-mirror by ideaconsult.

the class TaskCreator method processItem.

@Override
public Object processItem(T item) throws AmbitException {
    try {
        ICallableTask callable = getCallable(form, item);
        if (async) {
            ITask<Reference, USERID> task = createTask(callable, item);
            tasks.add(task.getUuid());
        } else {
            ITaskResult ref = callable.call();
        }
    } catch (AmbitException x) {
        throw x;
    } catch (Exception x) {
        throw new AmbitException(x);
    }
    return item;
}
Also used : ICallableTask(net.idea.restnet.i.task.ICallableTask) Reference(org.restlet.data.Reference) ITaskResult(net.idea.restnet.i.task.ITaskResult) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException) ResourceException(org.restlet.resource.ResourceException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException)

Example 4 with ITaskResult

use of net.idea.restnet.i.task.ITaskResult in project ambit-mirror by ideaconsult.

the class TaskJSONReporter method processItem.

@Override
public void processItem(UUID item, Writer output) {
    try {
        if (comma != null)
            output.write(comma);
        ITask<ITaskResult, USERID> task = storage.findTask(item);
        String uri = task.getUri() == null ? null : task.getUri().toString();
        StringWriter errorTrace = null;
        if (task.getError() != null) {
            try {
                ResourceException exception = task.getError();
                errorTrace = new StringWriter();
                if (exception instanceof ImportSubstanceException) {
                    errorTrace.write(((ImportSubstanceException) exception).getDetails());
                } else if (exception.getCause() == null)
                    exception.printStackTrace(new PrintWriter(errorTrace));
                else
                    exception.getCause().printStackTrace(new PrintWriter(errorTrace));
            } catch (Exception x) {
            }
        }
        output.write(String.format(format, uri, item.toString(), task.getName() == null ? "" : JSONUtils.jsonEscape(task.getName()), task.getError() == null ? "" : JSONUtils.jsonEscape(task.getError().toString()), task.getPolicyError() == null ? "" : JSONUtils.jsonEscape(task.getPolicyError().toString()), task.getStatus() == null ? "" : task.getStatus(), task.getStarted(), task.getTimeCompleted(), task.getUri() == null ? "" : task.getUri(), errorTrace == null ? null : JSONUtils.jsonQuote(JSONUtils.jsonEscape(errorTrace.toString()))));
        comma = ",";
    } catch (IOException x) {
        Context.getCurrentLogger().severe(x.getMessage());
    }
}
Also used : ImportSubstanceException(ambit2.rest.exception.ImportSubstanceException) StringWriter(java.io.StringWriter) ITaskResult(net.idea.restnet.i.task.ITaskResult) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException) IOException(java.io.IOException) ResourceException(org.restlet.resource.ResourceException) ImportSubstanceException(ambit2.rest.exception.ImportSubstanceException) PrintWriter(java.io.PrintWriter)

Example 5 with ITaskResult

use of net.idea.restnet.i.task.ITaskResult in project ambit-mirror by ideaconsult.

the class OpenSSOPoliciesResource method post.

@Override
protected Representation post(Representation entity, Variant variant) throws ResourceException {
    synchronized (this) {
        ArrayList<UUID> tasks = new ArrayList<UUID>();
        try {
            Form form = entity.isAvailable() ? new Form(entity) : new Form();
            ICallableTask callable = createCallable(form, null);
            ITask<ITaskResult, String> task = ((ITaskApplication) getApplication()).addTask(String.format("Create policy"), callable, getRequest().getRootRef(), callable instanceof CallablePOST, getToken());
            task.update();
            setStatus(task.isDone() ? Status.SUCCESS_OK : Status.SUCCESS_ACCEPTED);
            tasks.add(task.getUuid());
        } catch (ResourceException x) {
            throw x;
        } catch (Exception x) {
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x.getMessage(), x);
        }
        if (tasks.size() == 0)
            throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
        else {
            ITaskStorage storage = ((ITaskApplication) getApplication()).getTaskStorage();
            FactoryTaskConvertor<Object> tc = new FactoryTaskConvertor<Object>(storage);
            if (tasks.size() == 1)
                return tc.createTaskRepresentation(tasks.get(0), variant, getRequest(), getResponse(), null);
            else
                return tc.createTaskRepresentation(tasks.iterator(), variant, getRequest(), getResponse(), null);
        }
    }
}
Also used : Form(org.restlet.data.Form) ITaskStorage(net.idea.restnet.i.task.ITaskStorage) ArrayList(java.util.ArrayList) ITaskApplication(net.idea.restnet.i.task.ITaskApplication) CallablePOST(ambit2.rest.task.CallablePOST) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) ICallableTask(net.idea.restnet.i.task.ICallableTask) FactoryTaskConvertor(ambit2.rest.task.FactoryTaskConvertor) ITaskResult(net.idea.restnet.i.task.ITaskResult) ResourceException(org.restlet.resource.ResourceException) UUID(java.util.UUID)

Aggregations

ITaskResult (net.idea.restnet.i.task.ITaskResult)16 ResourceException (org.restlet.resource.ResourceException)11 UUID (java.util.UUID)5 TaskResult (ambit2.rest.task.TaskResult)4 DBConnection (ambit2.rest.DBConnection)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)3 ICallableTask (net.idea.restnet.i.task.ICallableTask)3 StringRepresentation (org.restlet.representation.StringRepresentation)3 ISourceDataset (ambit2.base.data.ISourceDataset)2 IStoredQuery (ambit2.db.search.IStoredQuery)2 StoredQuery (ambit2.db.search.StoredQuery)2 CallableQueryResultsCreator (ambit2.rest.task.CallableQueryResultsCreator)2 FactoryTaskConvertor (ambit2.rest.task.FactoryTaskConvertor)2 PolicyProtectedTask (ambit2.rest.task.PolicyProtectedTask)2 ResourceTest (ambit2.rest.test.ResourceTest)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 IQueryRetrieval (net.idea.modbcum.i.IQueryRetrieval)2