How to cancel page activation programmatically?

荒凉一梦 提交于 2019-12-23 05:32:29

问题


I would like to cancel the activation of some page, when x!=y is met. I am trying to do this with EventHandler. The author clicks on activate Page in the sidekick, and my EventHandler gets a replication event and tests if x!=y. If this is met, the page activation has to be canceled. My question is how do I cancel the page activation?

@Component(immediate = true, label = "TEST")
@Service
@Property(name = "event.topics", value = { ReplicationAction.EVENT_TOPIC })
public class EventHandler implements EventHandler {
    String feedback = "";

    public void handleEvent(final Event event) {
        String x = "foo";
        String y = "baar";
        if (x != y) {
            canclePageActivation();
            feedbackForAuthor = "Page can not be activated because x is not equal y";
        }
    }

}

回答1:


EventHandler is invoked after page is replicated. What you need here is com.day.cq.replication.Preprocessor. If you throw a ReplicationException in the preprocess() method, replication will be cancelled and user will get the exception message:

@Component(metatype = false, immediate = true)
@Service
public class SamplePreprocessor implements Preprocessor {

    @Override
    public void preprocess(ReplicationAction action, ReplicationOptions options) throws ReplicationException {
        if (somethingIsWrong()) {
            throw new ReplicationException("this message will be displayed to the user");
        }
    }
}


来源:https://stackoverflow.com/questions/19661983/how-to-cancel-page-activation-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!