Uploaded image for project: 'XWiki Platform'
  1. XWiki Platform
  2. XWIKI-15391

Impossible to cancel the deletion of a document in a listener

    XMLWordPrintable

Details

    • Unknown
    • N/A
    • N/A

    Description

      When a document is deleted, an DocumentDeletingEvent is triggered. It extends AbstractCancelableEvent so it should be cancel-able.

      However, when a java listener call the "cancel()" method, it has no effect.

      Some code to reproduce:

      /**
       * @version $Id: $
       */
      @Component
      @Named("SomeName")
      @Singleton
      public class ProtectedPages implements EventListener
      {
          private static final List<Event> EVENTS = Arrays.<Event>asList(new DocumentDeletingEvent());
      
          private static final List<DocumentReference> PROTECTED_PAGES = Arrays.asList(
                  new DocumentReference("xwiki", "XWiki", "SomePage1"),
                  new DocumentReference("xwiki", "XWiki", "SomePage2")
          );
      
          @Inject
          private Logger logger;
      
          @Override
          public String getName()
          {
              return "Some Name";
          }
      
          @Override
          public List<Event> getEvents()
          {
              return EVENTS;
          }
      
          @Override
          public void onEvent(Event event, Object source, Object data)
          {
              XWikiDocument document = (XWikiDocument) source;
              DocumentReference documentReference = document.getDocumentReference();
              if (PROTECTED_PAGES.contains(documentReference)) {
                  if (event instanceof CancelableEvent) {
                      CancelableEvent cancelableEvent = (CancelableEvent) event;
                      cancelableEvent.cancel("This document must be present in the wiki.");
                  } else {
                      logger.error("Failed to cancel the event [{}] for the document [{}].", event, documentReference);
                  }
              }
          }
      }
      

      A possible workaround is to save the original document in a DocumentDeletedEvent listener.

      Attachments

        Issue Links

          Activity

            People

              gdelhumeau Guillaume Delhumeau
              gdelhumeau Guillaume Delhumeau
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: