Search in sources :

Example 1 with ProjectStateChangedEvent

use of de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent in project webanno by webanno.

the class WebhookService method onApplicationEvent.

@TransactionalEventListener(fallbackExecution = true)
@Async
public void onApplicationEvent(ApplicationEvent aEvent) {
    String topic = EVENT_TOPICS.get(aEvent.getClass());
    if (topic == null) {
        return;
    }
    Object message;
    switch(topic) {
        case PROJECT_STATE:
            message = new ProjectStateChangeMessage((ProjectStateChangedEvent) aEvent);
            break;
        case DOCUMENT_STATE:
            message = new DocumentStateChangeMessage((DocumentStateChangedEvent) aEvent);
            break;
        case ANNOTATION_STATE:
            message = new AnnotationStateChangeMessage((AnnotationStateChangeEvent) aEvent);
            break;
        default:
            return;
    }
    for (Webhook hook : configuration.getGlobalHooks()) {
        if (!hook.isEnabled() || !hook.getTopics().contains(topic)) {
            continue;
        }
        try {
            // Configure rest template without SSL certification check if that is disabled.
            RestTemplate restTemplate;
            if (hook.isVerifyCertificates()) {
                restTemplate = restTemplateBuilder.build();
            } else {
                restTemplate = restTemplateBuilder.requestFactory(getNonValidatingRequestFactory()).build();
            }
            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
            requestHeaders.set(X_AERO_NOTIFICATION, topic);
            // If a secret is set, then add a digest header that allows the client to verify
            // the message integrity
            String json = JSONUtil.toJsonString(message);
            if (isNotBlank(hook.getSecret())) {
                String digest = DigestUtils.shaHex(hook.getSecret() + json);
                requestHeaders.set(X_AERO_SIGNATURE, digest);
            }
            HttpEntity<?> httpEntity = new HttpEntity<Object>(json, requestHeaders);
            restTemplate.postForEntity(hook.getUrl(), httpEntity, Void.class);
        } catch (Exception e) {
            log.error("Unable to invoke webhook [{}]", hook, e);
        }
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) ProjectStateChangedEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent) ProjectStateChangeMessage(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.webhooks.json.ProjectStateChangeMessage) DocumentStateChangedEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.DocumentStateChangedEvent) AnnotationStateChangeMessage(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.webhooks.json.AnnotationStateChangeMessage) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RestTemplate(org.springframework.web.client.RestTemplate) AnnotationStateChangeEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.AnnotationStateChangeEvent) DocumentStateChangeMessage(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.webhooks.json.DocumentStateChangeMessage) Async(org.springframework.scheduling.annotation.Async) TransactionalEventListener(org.springframework.transaction.event.TransactionalEventListener)

Example 2 with ProjectStateChangedEvent

use of de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent in project webanno by webanno.

the class WebhookServiceTest method test.

@Test
public void test() {
    Webhook hook = new Webhook();
    hook.setUrl("http://localhost:" + port + "/test/subscribe");
    hook.setTopics(asList(PROJECT_STATE, ANNOTATION_STATE, DOCUMENT_STATE));
    hook.setEnabled(true);
    webhooksConfiguration.setGlobalHooks(asList(hook));
    Project project = new Project();
    project.setState(ProjectState.NEW);
    project.setId(1l);
    SourceDocument doc = new SourceDocument();
    doc.setProject(project);
    doc.setId(2l);
    doc.setState(SourceDocumentState.ANNOTATION_IN_PROGRESS);
    AnnotationDocument ann = new AnnotationDocument();
    ann.setProject(project);
    ann.setId(3l);
    ann.setDocument(doc);
    ann.setState(AnnotationDocumentState.FINISHED);
    applicationEventPublisher.publishEvent(new ProjectStateChangedEvent(this, project, ProjectState.CURATION_FINISHED));
    applicationEventPublisher.publishEvent(new DocumentStateChangedEvent(this, doc, SourceDocumentState.NEW));
    applicationEventPublisher.publishEvent(new AnnotationStateChangeEvent(this, ann, AnnotationDocumentState.IN_PROGRESS));
    assertEquals(1, testService.projectStateChangeMsgs.size());
    assertEquals(1, testService.docStateChangeMsgs.size());
    assertEquals(1, testService.annStateChangeMsgs.size());
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) ProjectStateChangedEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent) AnnotationStateChangeEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.AnnotationStateChangeEvent) DocumentStateChangedEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.DocumentStateChangedEvent) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with ProjectStateChangedEvent

use of de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent in project webanno by webanno.

the class DocumentServiceImpl method recalculateProjectState.

private void recalculateProjectState(Project aProject) {
    Project project;
    try {
        project = projectService.getProject(aProject.getId());
    } catch (NoResultException e) {
        // updating its state. So then we do nothing here.
        return;
    }
    String query = "SELECT new " + SourceDocumentStateStats.class.getName() + "(" + "COUNT(*), " + "SUM(CASE WHEN state = :an  THEN 1 ELSE 0 END), " + "SUM(CASE WHEN (state = :aip OR state is NULL) THEN 1 ELSE 0 END), " + "SUM(CASE WHEN state = :af  THEN 1 ELSE 0 END), " + "SUM(CASE WHEN state = :cip THEN 1 ELSE 0 END), " + "SUM(CASE WHEN state = :cf  THEN 1 ELSE 0 END)) " + "FROM SourceDocument " + "WHERE project = :project";
    SourceDocumentStateStats stats = entityManager.createQuery(query, SourceDocumentStateStats.class).setParameter("project", aProject).setParameter("an", SourceDocumentState.NEW).setParameter("aip", SourceDocumentState.ANNOTATION_IN_PROGRESS).setParameter("af", SourceDocumentState.ANNOTATION_FINISHED).setParameter("cip", SourceDocumentState.CURATION_IN_PROGRESS).setParameter("cf", SourceDocumentState.CURATION_FINISHED).getSingleResult();
    ProjectState oldState = project.getState();
    if (stats.total == stats.cf) {
        project.setState(ProjectState.CURATION_FINISHED);
    } else if (stats.total == stats.af) {
        project.setState(ProjectState.ANNOTATION_FINISHED);
    } else if (stats.total == stats.an) {
        project.setState(ProjectState.NEW);
    } else if (stats.cip > 0) {
        project.setState(ProjectState.CURATION_IN_PROGRESS);
    } else if (stats.aip > 0) {
        project.setState(ProjectState.ANNOTATION_IN_PROGRESS);
    } else {
        throw new IllegalStateException("Unable to determine project state.");
    }
    if (!Objects.equals(oldState, project.getState())) {
        applicationEventPublisher.publishEvent(new ProjectStateChangedEvent(this, project, oldState));
    }
    projectService.updateProject(project);
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ProjectState(de.tudarmstadt.ukp.clarin.webanno.model.ProjectState) NoResultException(javax.persistence.NoResultException) ProjectStateChangedEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent)

Aggregations

ProjectStateChangedEvent (de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent)3 AnnotationStateChangeEvent (de.tudarmstadt.ukp.clarin.webanno.api.event.AnnotationStateChangeEvent)2 DocumentStateChangedEvent (de.tudarmstadt.ukp.clarin.webanno.api.event.DocumentStateChangedEvent)2 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)2 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)1 ProjectState (de.tudarmstadt.ukp.clarin.webanno.model.ProjectState)1 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)1 AnnotationStateChangeMessage (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.webhooks.json.AnnotationStateChangeMessage)1 DocumentStateChangeMessage (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.webhooks.json.DocumentStateChangeMessage)1 ProjectStateChangeMessage (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.webhooks.json.ProjectStateChangeMessage)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoResultException (javax.persistence.NoResultException)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 Async (org.springframework.scheduling.annotation.Async)1 TransactionalEventListener (org.springframework.transaction.event.TransactionalEventListener)1