}
/*
* Getting host object form the session
*/
HostWebAPI hostWebAPI = WebAPILocator.getHostWebAPI();
Host host;
try {
host = hostWebAPI.getCurrentHost(request);
} catch (PortalException e) {
Logger.error(this, "Unable to retrieve current request host for URI " + uri);
throw new ServletException(e.getMessage(), e);
} catch (SystemException e) {
Logger.error(this, "Unable to retrieve current request host for URI " + uri);
throw new ServletException(e.getMessage(), e);
} catch (DotDataException e) {
Logger.error(this, "Unable to retrieve current request host for URI " + uri);
throw new ServletException(e.getMessage(), e);
} catch (DotSecurityException e) {
Logger.error(this, "Unable to retrieve current request host for URI " + uri);
throw new ServletException(e.getMessage(), e);
}
/*
* If someone is trying to go right to an asset without going through
* the cms, give them a 404
*/
if (UtilMethods.isSet(ASSET_PATH) && uri.startsWith(ASSET_PATH)) {
response.sendError(403, "Forbidden");
return;
}
String pointer = null;
boolean isVanityURL = UtilMethods.isSet(VirtualLinksCache.getPathFromCache(host.getHostname() + ":" + uri));
if (!isVanityURL)
isVanityURL = UtilMethods.isSet(VirtualLinksCache.getPathFromCache(uri));
if(!uri.equals(pointer) && !uri.endsWith("/")
&& ! RegEX.contains(uri, folderPathRegEx)
&& uri.indexOf("/dotCMS/") == -1
&& !isVanityURL ) {
Enumeration enm = req.getParameterNames();
StringBuffer params = new StringBuffer("");
for (; enm.hasMoreElements(); ) {
String name = (String)enm.nextElement();
params.append(name + "=" + req.getParameter(name));
if(enm.hasMoreElements())
params.append(StringPool.AMPERSAND);
}
response.sendRedirect(uri + "/" + (params.length() > 0 ? "?" + params : ""));
return;
}
//Verify if the request is for a specific language
Long languageId;
if ( !UtilMethods.isSet( req.getParameter( "language_id" ) ) ) {
languageId = APILocator.getLanguageAPI().getDefaultLanguage().getId();
} else {
languageId = Long.parseLong( req.getParameter( "language_id" ) );
}
/* if edit mode */
if (PREVIEW_MODE || EDIT_MODE) {
try {
pointer = WorkingCache.getPathFromCache(uri, host);
if(!UtilMethods.isSet(pointer)){//DOTCMS-7062
pointer = LiveCache.getPathFromCache(uri, host);
}
if (!UtilMethods.isSet(pointer)
&& !uri.equals("/")
&& (uri.endsWith(dotExtension)
|| InodeUtils.isSet(APILocator
.getFolderAPI().findFolderByPath(uri, host,APILocator.getUserAPI().getSystemUser(),false)
.getInode()))) {
String url = uri;
if (!uri.endsWith(dotExtension)) {
url += "index" + dotExtension;
}
request.getRequestDispatcher("/html/portlet/ext/htmlpages/page_not_found_404.jsp?url=" + url + "&hostId=" + host.getIdentifier()).forward(
req, res);
return;
}
LogFactory.getLog(this.getClass()).debug("CMS preview pointer = " + uri + ":" + pointer);
} catch (Exception e) {
Logger.debug(this.getClass(), "Can't find pointer " + uri);
}
/* if live mode */
} else {
try {
pointer = LiveCache.getPathFromCache( uri, host, languageId );
} catch (Exception e) {
Logger.debug(this.getClass(), "Can't find pointer " + uri);
try {
if(WebAPILocator.getUserWebAPI().isLoggedToBackend(request)){
response.setHeader( "Pragma", "no-cache" );
response.setHeader( "Cache-Control", "no-cache" );
response.setDateHeader( "Expires", 0 );
response.sendError(404);
return;
}
} catch (Exception e1) {
Logger.debug(this.getClass(), "Can't find pointer " + uri);
}
}
// If the cache hits the db the connection needs to be manually
// closed
try {
HibernateUtil.closeSession();
} catch (DotHibernateException e) {
Logger.error(CMSFilter.class, e.getMessage(), e);
}
LogFactory.getLog(this.getClass()).debug("CMS live pointer = " + uri + ":" + pointer);
}
/*
* Checking if host is active
*/
boolean hostlive;
try {
hostlive = APILocator.getVersionableAPI().hasLiveVersion(host);
} catch (Exception e1) {
throw new ServletException(e1);
}
if(!ADMIN_MODE && !hostlive) {
//Checking if it has a maintenance virtual link
pointer = (String) VirtualLinksCache.getPathFromCache(host.getHostname() + ":/cmsMaintenancePage");
if(pointer == null) {
try {
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, LanguageUtil.get(company.getCompanyId(), company.getLocale(), "server-unavailable-error-message"));
} catch (LanguageException e) {
Logger.error(CMSFilter.class, e.getMessage(), e);
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
}
return;
}
}
// if absolute link somewhere else
if (UtilMethods.isSet(pointer) && (pointer.startsWith(httpProtocol) || pointer.startsWith(httpsProtocol))) {
response.sendRedirect(pointer);
return;
}
// virtual links only after other links
if (!UtilMethods.isSet(pointer)) {
if (uri.endsWith("/"))
uri = uri.substring(0, uri.length() - 1);
pointer = VirtualLinksCache.getPathFromCache(host.getHostname() + ":" + uri);
if (!UtilMethods.isSet(pointer)) {
pointer = VirtualLinksCache.getPathFromCache(uri);
}
if (UtilMethods.isSet(pointer)) { // is it a virtual link?
LogFactory.getLog(this.getClass()).debug("CMS found virtual link pointer = " + uri + ":" + pointer);
boolean external = false;
String auxPointer = pointer;
if(auxPointer.indexOf("http://") != -1 || auxPointer.indexOf("https://") != -1)
{
try {
User systemUser = APILocator.getUserAPI().getSystemUser();
auxPointer = auxPointer.replace("https://","");
auxPointer = auxPointer.replace("http://","");
int startIndex = 0;
int endIndex = auxPointer.indexOf("/");
if(startIndex < endIndex)
{
String localHostName = auxPointer.substring(startIndex,endIndex);
Host localHost = hostWebAPI.findByName(localHostName, systemUser, false);
if(localHost ==null || !InodeUtils.isSet(localHost.getInode())){
external=true;
}
}
else