Search in sources :

Example 1 with DocumentStateChangedEvent

use of de.tudarmstadt.ukp.clarin.webanno.api.event.DocumentStateChangedEvent 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 DocumentStateChangedEvent

use of de.tudarmstadt.ukp.clarin.webanno.api.event.DocumentStateChangedEvent 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 DocumentStateChangedEvent

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

the class DocumentServiceImpl method setSourceDocumentState.

@Override
@Transactional
public SourceDocumentState setSourceDocumentState(SourceDocument aDocument, SourceDocumentState aState) {
    SourceDocumentState oldState = aDocument.getState();
    aDocument.setState(aState);
    createSourceDocument(aDocument);
    // Notify about change in document state
    if (!Objects.equals(oldState, aDocument.getState())) {
        applicationEventPublisher.publishEvent(new DocumentStateChangedEvent(this, aDocument, oldState));
    }
    return oldState;
}
Also used : SourceDocumentState(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState) DocumentStateChangedEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.DocumentStateChangedEvent) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

DocumentStateChangedEvent (de.tudarmstadt.ukp.clarin.webanno.api.event.DocumentStateChangedEvent)3 AnnotationStateChangeEvent (de.tudarmstadt.ukp.clarin.webanno.api.event.AnnotationStateChangeEvent)2 ProjectStateChangedEvent (de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent)2 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)1 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)1 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)1 SourceDocumentState (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState)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 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 Transactional (org.springframework.transaction.annotation.Transactional)1 TransactionalEventListener (org.springframework.transaction.event.TransactionalEventListener)1