Check whether you are on author or publish

Issue

How do I check if the code is running on an author or a publish instance?

Solution

The best way to check if your code is running in the authoring environment in CQ is as follows.
DeliveryHttpServletRequest cqRequest = (DeliveryHttpServletRequest) request;

// flag to indicate whether application is Author or Publish
boolean isAdmin = !cqRequest.getCmsService().getDisplayControlSet(cqRequest).equals(DisplayControlSet.SHOW);

// flag to indicate whether application is in preview/show mode
boolean isShowMode = (cqRequest.getCmsService().getDisplayControlSet(cqRequest).equals(DisplayControlSet.SHOWMODE)
|| "ShowMode".equals(cqRequest.getParameter("Show")) );

This method is better than checking the URL, because this code does nor rely on a specific context, which can change. Also, the author server can be switched to a publish mode by setting parameter "Show" to 1 in the request. So, the request to /author/somepage.html?Show=1 renders as publish. Therefore, relying on the context is not a reliable solution.

 Adobe

Daha hızlı ve daha kolay yardım alın

Yeni kullanıcı mısınız?

Adobe MAX 2024

Adobe MAX
Yaratıcılık Konferansı

14–16 Ekim Miami Beach ve çevrimiçi

Adobe MAX

Yaratıcılık Konferansı

14–16 Ekim Miami Beach ve çevrimiçi

Adobe MAX 2024

Adobe MAX
Yaratıcılık Konferansı

14–16 Ekim Miami Beach ve çevrimiçi

Adobe MAX

Yaratıcılık Konferansı

14–16 Ekim Miami Beach ve çevrimiçi