HttpSession session = request.getSession(false);
String uri = request.getRequestURI();
uri = URLDecoder.decode(uri, "UTF-8");
Company company = PublicCompanyFactory.getDefaultCompany();
/*
* Here is a list of directories that we will ignore b/c of legacy code
* and servlet mappings. This is a mess and should be much cleaner
*/
if ( Xss.URIHasXSS( uri ) ) {
try {
uri = Xss.encodeForURL( uri );
} catch ( EncodingException e ) {
Logger.error( this, "Encoding failure. Unable to encode URI " + uri );
throw new ServletException( e.getMessage(), e );
}
if ( uri.equals( "" ) ) {
uri = "/";
}
response.sendRedirect( uri );
return;
}
if ( request.getQueryString() != null && !UtilMethods.decodeURL( request.getQueryString() ).equals( null ) ) {
//http://jira.dotmarketing.net/browse/DOTCMS-6141
if(request.getQueryString() != null && request.getQueryString().contains("\"")){
response.sendRedirect(uri+"?"+StringEscapeUtils.escapeHtml(StringEscapeUtils.unescapeHtml(request.getQueryString())));
return;
}
if ( Xss.ParamsHaveXSS( request ) ) {
response.sendRedirect( uri );
return;
}
}
if (excludeURI(uri)) {
chain.doFilter(request, response);
return;
}
// set the preview mode
boolean ADMIN_MODE = false;
boolean EDIT_MODE = false;
boolean PREVIEW_MODE = false;
LogFactory.getLog(this.getClass()).debug("CMS Filter URI = " + uri);
PermissionAPI permissionAPI = APILocator.getPermissionAPI();
if (session != null) {
// struts crappy messages have to be retrived from session
if (session.getAttribute(Globals.ERROR_KEY) != null) {
request.setAttribute(Globals.ERROR_KEY, session.getAttribute(Globals.ERROR_KEY));
session.removeAttribute(Globals.ERROR_KEY);
}
if (session.getAttribute(Globals.MESSAGE_KEY) != null) {
request.setAttribute(Globals.MESSAGE_KEY, session.getAttribute(Globals.MESSAGE_KEY));
session.removeAttribute(Globals.MESSAGE_KEY);
}
// set the preview mode
ADMIN_MODE = (session.getAttribute(com.dotmarketing.util.WebKeys.ADMIN_MODE_SESSION) != null);
PREVIEW_MODE = (session.getAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION) != null && ADMIN_MODE);
EDIT_MODE = (session.getAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION) != null && ADMIN_MODE);
if (request.getParameter("livePage") != null && request.getParameter("livePage").equals("1")) {
PREVIEW_MODE = false;
EDIT_MODE = false;
session.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, null);
request.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, null);
session.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, null);
request.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, null);
LogFactory.getLog(this.getClass()).debug("CMS FILTER Cleaning PREVIEW_MODE_SESSION LIVE!!!!");
}
if (request.getParameter("previewPage") != null && request.getParameter("previewPage").equals("1")) {
PREVIEW_MODE = false;
EDIT_MODE = true;
session.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, null);
request.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, null);
session.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, "true");
request.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, "true");
LogFactory.getLog(this.getClass()).debug("CMS FILTER Cleaning EDIT_MODE_SESSION PREVIEW!!!!");
}
if (request.getParameter("previewPage") != null && request.getParameter("previewPage").equals("2")) {
PREVIEW_MODE = true;
EDIT_MODE = false;
session.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, "true");
request.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, "true");
session.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, null);
request.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, null);
LogFactory.getLog(this.getClass()).debug("CMS FILTER Cleaning PREVIEW_MODE_SESSION PREVIEW!!!!");
}
}
/*
* 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;