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);
}
}
}
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());
}
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;
}
Aggregations