Injecting currentPage using annotations does not return the correct page
https://github.com/adobe/aem-sample-we-retail-journal/issues/53

The same issue I was facing Breadcrumb was working fine in my local but UAT was not working I followed the above link and fixed the issue it worked in the UAT server. But once deploy to the Production server it is not working.
My code:
Page currentPage = request.getResourceResolver().adaptTo(PageManager.class)
.getContainingPage(request.getResource());
Instead of
@ScriptVariable
private Page currentPage;
Before this fix: It was showing child pages of the root page. All pages were displayed irrespective of the hideInNavigation property.
/content/page1/page2/page3/page4/page5
After Fix:
page3/page4/page5
It solved the problem, but it is working fine QA/UAT server but in production server not working.
In Production: Displaying all the pages
/content/page1/page2/page3/page4/page5
Please can help me with this issue?
Details:
AEM-SPA Angular Project
AEM Version: 6.5
import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.settings.SlingSettingsService;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.designer.Style;
@Model(adaptables = SlingHttpServletRequest.class, adapters = { BreadcrumbModel.class,
ComponentExporter.class }, resourceType = { BreadcrumbModel.RESOURCE_TYPE })
@3484101(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class BreadcrumbModel implements ComponentExporter {
protected static final String RESOURCE_TYPE = "projectA/components/breadcrumb";
String PN_SHOW_HIDDEN = "showHidden";
String PN_HIDE_CURRENT = "hideCurrent";
String PN_START_LEVEL = "startLevel";
protected static final boolean PROP_SHOW_HIDDEN_DEFAULT = false;
protected static final boolean PROP_HIDE_CURRENT_DEFAULT = false;
protected static final int PROP_START_LEVEL_DEFAULT = 2;
private final transient Logger logger = LoggerFactory.getLogger(getClass());
@ScriptVariable
private ValueMap properties;
@ScriptVariable
private Style currentStyle;
@OSGiService
private SlingSettingsService settingsService;
@1961677
private SlingHttpServletRequest request;
private boolean showHidden;
private boolean hideCurrent;
private int startLevel;
@PostConstruct
private void initModel() {
startLevel = properties.get(PN_START_LEVEL, currentStyle.get(PN_START_LEVEL, PROP_START_LEVEL_DEFAULT));
showHidden = properties.get(PN_SHOW_HIDDEN, currentStyle.get(PN_SHOW_HIDDEN, PROP_SHOW_HIDDEN_DEFAULT));
hideCurrent = properties.get(PN_HIDE_CURRENT, currentStyle.get(PN_HIDE_CURRENT, PROP_HIDE_CURRENT_DEFAULT));
}
public String getJsonVal() {
String jsonVal = "";
try {
jsonVal = createItems().toString();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonVal;
}
private JSONArray createItems() throws JSONException {
JSONArray items = new JSONArray();
Page currentPage = request.getResourceResolver().adaptTo(PageManager.class)
.getContainingPage(request.getResource());
logger.info("Breadcrumb pagepath= " + currentPage.getPath());
int currentLevel = currentPage.getDepth();
while (startLevel < currentLevel) {
Page page = currentPage.getAbsoluteParent(startLevel);
if (page != null) {
boolean isActivePage = page.equals(currentPage);
if (isActivePage && hideCurrent) {
break;
}
if (checkIfNotHidden(page)) {
JSONObject item = new JSONObject();
item.put("active", isActivePage);
item.put("title", getTitle(page));
if (!isAuthor()) {
item.put("url", getShortURL(getRedirectUrl(page)));
logger.info("short-hand URL path= " + getShortURL(getRedirectUrl(page)));
}else{
item.put("url", getRedirectUrl(page));
}
items.put(item);
}
}
startLevel++;
}
return items;
}
private String getTitle(Page page) {
return StringUtils.isEmpty(page.getNavigationTitle())
? StringUtils.isEmpty(page.getTitle()) ? page.getName() : page.getTitle()
: page.getNavigationTitle();
}
private String getRedirectUrl(Page page) {
String url = page.getProperties().containsKey("redirectTarget")
&& StringUtils.isNotEmpty(page.getProperties().get("redirectTarget").toString())
? page.getProperties().get("redirectTarget").toString()
: page.getPath();
return url.endsWith(".html") ? url : url + ".html";
}
private boolean checkIfNotHidden(Page page) {
return !page.isHideInNav() || showHidden;
}
@9944223
public String getExportedType() {
return RESOURCE_TYPE;
}
private boolean isAuthor() {
return settingsService.getRunModes().contains("author");
}
private String getShortURL(String path) {
// String shorturl = request.getResourceResolver().map(path);
String shorturl = "";
if (path != null) {
String[] patharray = path.split("/");
for (int i = 4; i < patharray.length; i++) {
shorturl = shorturl + "/" + patharray[i];
}
}
logger.info("shortt URL= " + shorturl);
return shorturl;
}
}