Hi Arun , Thanks for responding . 404 page is getting triggered from the model. Below is the code snippet
public class ResponseStatus extends WCMUsePojo {
private static final String DIR_404_HTML = "/404";
private static final String CONTENT_SAMPLE = "/content/sample/";
private static final String AUTHOR = "author";
private static final String EN_US = "en-us";
private static final Pattern REGEX_LANGUAGE_SELECTOR = Pattern
.compile("(/content/myproject)?/([a-zA-Z]{2}-[a-zA-Z]{2})");
private static final Logger log = LoggerFactory.getLogger(ResponseStatus.class);
private static final int ERROR_CODE_404 = 404;
@Override
public void activate() throws Exception {
Set<String> runModes;
SlingSettingsService slingSettings = this.getSlingScriptHelper().getService(SlingSettingsService.class);
if (null != slingSettings) {
runModes = slingSettings.getRunModes();
getResponse().setStatus(ERROR_CODE_404);
if (!runModes.contains(AUTHOR)) {
String requestUrl = getRequest().getRequestURL().toString();
if (StringUtils.isNotEmpty(requestUrl)) {
String languageCode = setLanguageCode(requestUrl);
redirectToErrorPage(languageCode);
}
}
}
}
protected void redirectToErrorPage(String languageCode) throws IOException {
String redirectUrl = CONTENT_SAMPLE + languageCode + DIR_404_HTML;
ResourceResolver resolver = getRequest().getResourceResolver();
Resource targetResource = resolver.getResource(redirectUrl);
if (targetResource != null) {
try {
RequestDispatcher dispatcher = getRequest().getRequestDispatcher(targetResource);
if (dispatcher != null) {
dispatcher.include(getRequest(), getResponse());
}
} catch (ServletException e) {
log.error("Error while forwarding the requesting {}", e);
}
} else {
log.info("404 Page not found {}", redirectUrl);
}
}
/**
* Returns the languageCode of the request URL
*/
private String setLanguageCode(String requestUrl) {
String language = EN_US;
String path = getPath(requestUrl);
if (path != null) {
Matcher languageMatcher = REGEX_LANGUAGE_SELECTOR.matcher(path);
if (languageMatcher.find()) {
language = languageMatcher.group(2);
}
}
return language;
}
private static String getPath(String uri)
{
String path = null;
try {
URI url = new URI(uri);
path = url.getPath();
} catch (URISyntaxException e) {
log.error("Could not process uri {}", uri, e);
}
return path;
}
}
Thanks