Another attempt to fix the redirect bug which occurs with some user agents...

git-svn-id: https://svn.libreccm.org/ccm/trunk@5095 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2017-11-01 13:29:43 +00:00
parent 2055a9f21a
commit ae645cb299
1 changed files with 72 additions and 34 deletions

View File

@ -93,13 +93,15 @@ import com.arsdigita.kernel.KernelConfig;
public class PublicPersonalProfilesServlet extends BaseApplicationServlet { public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
private static final long serialVersionUID = -1495852395804455609L; private static final long serialVersionUID = -1495852395804455609L;
private static final Logger logger = Logger.getLogger( private static final Logger LOGGER = Logger.getLogger(
PublicPersonalProfilesServlet.class); PublicPersonalProfilesServlet.class);
private static final String ADMIN = "admin"; private static final String ADMIN = "admin";
private static final String PREVIEW = "preview"; private static final String PREVIEW = "preview";
private static final String PPP_NS private static final String PPP_NS
= "http://www.arsdigita.com/PublicPersonalProfile/1.0"; = "http://www.arsdigita.com/PublicPersonalProfile/1.0";
public static final String SELECTED_NAV_ITEM = "selectedNavItem"; public static final String SELECTED_NAV_ITEM = "selectedNavItem";
private final PublicPersonalProfileConfig config = PublicPersonalProfiles private final PublicPersonalProfileConfig config = PublicPersonalProfiles
.getConfig(); .getConfig();
@ -109,15 +111,31 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
final Application app) throws ServletException, final Application app) throws ServletException,
IOException { IOException {
logger.debug("PublicPersonalProfileServlet is starting..."); LOGGER.debug("PublicPersonalProfileServlet is starting...");
logger.debug(String.format("pathInfo = '%s'", request.getPathInfo())); LOGGER.debug(String.format("pathInfo = '%s'", request.getPathInfo()));
logger.debug("Extracting path from pathInfo by removing leading and " LOGGER.debug("Extracting path from pathInfo by removing leading and "
+ "trailing slashes..."); + "trailing slashes...");
if (CMSConfig.getInstanceOf().getUseLanguageExtension()) { if (CMSConfig.getInstanceOf().getUseLanguageExtension()) {
final String pathInfo = request.getPathInfo(); final String pathInfo;
/*
* This is more or less a hack. For some Browsers (even the same
* version on different system behave differently) the redirected
* URL contains multiple ".." before the index part. If this the
* case we replace them...
*/
if (request.getPathInfo().contains("..")) {
LOGGER.warn(String.format("Multipe \".\" in pathInfo \"%s\"!",
request.getPathInfo()));
pathInfo = request.getPathInfo().replaceAll("\\.{2,}", ".");
} else {
pathInfo = request.getPathInfo();
}
if (!pathInfo.matches("(.*)/index\\.[a-zA-Z]{2}")) { if (!pathInfo.matches("(.*)/index\\.[a-zA-Z]{2}")) {
LOGGER
.debug("pathInfo does not end with index part. Redirecting.");
String lang; String lang;
if (GlobalizationHelper.getSelectedLocale(request) == null) { if (GlobalizationHelper.getSelectedLocale(request) == null) {
lang = GlobalizationHelper lang = GlobalizationHelper
@ -128,9 +146,12 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
.getSelectedLocale(request) .getSelectedLocale(request)
.getLanguage(); .getLanguage();
} }
LOGGER.debug(String.format("Current language is: %s",
lang));
final Path path = new Path(getPath(request)); final Path path = new Path(getPath(request));
final PublicPersonalProfile profile = getProfile(SessionManager.getSession(), final PublicPersonalProfile profile = getProfile(
SessionManager.getSession(),
path.getProfileOwner(), path.getProfileOwner(),
path.getPreview(), path.getPreview(),
lang); lang);
@ -148,14 +169,20 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
if (DispatcherHelper.getRequest().getServerPort() != 80 if (DispatcherHelper.getRequest().getServerPort() != 80
&& DispatcherHelper.getRequest().getServerPort() != 443) { && DispatcherHelper.getRequest().getServerPort() != 443) {
LOGGER.debug(String
.format("None standard port %d used. " + "Adding port",
DispatcherHelper.getRequest().getServerPort()));
redirectTo redirectTo
.append(":") .append(":")
.append(DispatcherHelper.getRequest().getServerPort()); .append(DispatcherHelper.getRequest().getServerPort());
} }
if (DispatcherHelper.getWebappContext() != null if (DispatcherHelper.getWebappContext() != null
&& !DispatcherHelper.getWebappContext().trim().isEmpty()) { && !DispatcherHelper.getWebappContext().trim().isEmpty()) {
LOGGER.debug(String
.format("webappContext is not null. Adding "
+ "webappContext \"%s\" to redirect URL.",
DispatcherHelper.getWebappContext()));
redirectTo.append(DispatcherHelper.getWebappContext()); redirectTo.append(DispatcherHelper.getWebappContext());
} }
@ -170,6 +197,8 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
.append("index") .append("index")
.append(".") .append(".")
.append(lang); .append(lang);
LOGGER.debug(String.format("Redirecting to \"%s\"...",
redirectTo.toString()));
response.setHeader("Location", redirectTo.toString()); response.setHeader("Location", redirectTo.toString());
response.sendError(HttpServletResponse.SC_MOVED_PERMANENTLY); response.sendError(HttpServletResponse.SC_MOVED_PERMANENTLY);
return; return;
@ -178,11 +207,11 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
final String pathStr = getPath(request); final String pathStr = getPath(request);
logger.debug(String.format("path = %s", pathStr)); LOGGER.debug(String.format("path = %s", pathStr));
//Displays a text/plain page with a message. //Displays a text/plain page with a message.
if (pathStr.isEmpty()) { if (pathStr.isEmpty()) {
logger.debug("pathInfo is null, responding with default..."); LOGGER.debug("pathInfo is null, responding with default...");
response.setContentType("text/plain"); response.setContentType("text/plain");
response.getWriter().append("Please choose an application."); response.getWriter().append("Please choose an application.");
@ -414,10 +443,10 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
IOException { IOException {
String path = ""; String path = "";
logger.debug("PublicPersonalProfileServlet is starting..."); LOGGER.debug("PublicPersonalProfileServlet is starting...");
logger.debug(String.format("pathInfo = '%s'", request.getPathInfo())); LOGGER.debug(String.format("pathInfo = '%s'", request.getPathInfo()));
logger.debug("Extracting path from pathInfo by removing leading and " LOGGER.debug("Extracting path from pathInfo by removing leading and "
+ "trailing slashes..."); + "trailing slashes...");
if (request.getPathInfo() != null) { if (request.getPathInfo() != null) {
if ("/".equals(request.getPathInfo())) { if ("/".equals(request.getPathInfo())) {
@ -436,11 +465,11 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
} }
} }
logger.debug(String.format("path = %s", path)); LOGGER.debug(String.format("path = %s", path));
//Displays a text/plain page with a message. //Displays a text/plain page with a message.
if (path.isEmpty()) { if (path.isEmpty()) {
logger.debug("pathInfo is null, responding with default..."); LOGGER.debug("pathInfo is null, responding with default...");
response.setContentType("text/plain"); response.setContentType("text/plain");
response.getWriter().append("Please choose an application."); response.getWriter().append("Please choose an application.");
@ -745,7 +774,7 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
if (item instanceof ContentPage) { if (item instanceof ContentPage) {
ContentPage contentPage ContentPage contentPage
= (ContentPage) item; = (ContentPage) item;
logger. LOGGER.
error( error(
"contentPage.getContentBundle().hasInstance(GlobalizationHelper.getNegotiatedLocale().getLanguage()) = " "contentPage.getContentBundle().hasInstance(GlobalizationHelper.getNegotiatedLocale().getLanguage()) = "
+ contentPage. + contentPage.
@ -767,7 +796,7 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
.getLanguage()); .getLanguage());
item = (ContentItem) contentPage; item = (ContentItem) contentPage;
} else { } else {
logger.error( LOGGER.error(
String. String.
format( format(
"Item '%s' not found in a suitable language variant. Negotiated langauge: %s, langugage independent items allowed is %s, language independent code is %s ", "Item '%s' not found in a suitable language variant. Negotiated langauge: %s, langugage independent items allowed is %s, language independent code is %s ",
@ -811,7 +840,7 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
if (item instanceof ContentPage) { if (item instanceof ContentPage) {
ContentPage contentPage = (ContentPage) item; ContentPage contentPage = (ContentPage) item;
logger. LOGGER.
error( error(
"contentPage.getContentBundle().hasInstance(GlobalizationHelper.getNegotiatedLocale().getLanguage()) = " "contentPage.getContentBundle().hasInstance(GlobalizationHelper.getNegotiatedLocale().getLanguage()) = "
+ contentPage.getContentBundle(). + contentPage.getContentBundle().
@ -828,7 +857,7 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
getLanguage()); getLanguage());
item = (ContentItem) contentPage; item = (ContentItem) contentPage;
} else { } else {
logger.error( LOGGER.error(
String. String.
format( format(
"Item '%s' not found in a suitable language variant. Negotiated langauge: %s, langugage independent items allowed is %s, language independent code is %s ", "Item '%s' not found in a suitable language variant. Negotiated langauge: %s, langugage independent items allowed is %s, language independent code is %s ",
@ -857,7 +886,7 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
""); "");
} catch (DataObjectNotFoundException ex) { } catch (DataObjectNotFoundException ex) {
logger.error(String.format( LOGGER.error(String.format(
"Item '%s' not found: ", "Item '%s' not found: ",
itemPath), itemPath),
ex); ex);
@ -1243,7 +1272,7 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
itemRoot, itemRoot,
""); "");
} catch (DataObjectNotFoundException ex) { } catch (DataObjectNotFoundException ex) {
logger.error(String.format( LOGGER.error(String.format(
"Item '%s' not found: ", "Item '%s' not found: ",
path.getItemPath()), path.getItemPath()),
ex); ex);
@ -1349,6 +1378,15 @@ public class PublicPersonalProfilesServlet extends BaseApplicationServlet {
if (request.getPathInfo() != null) { if (request.getPathInfo() != null) {
String pathInfo = request.getPathInfo(); String pathInfo = request.getPathInfo();
if (pathInfo.contains("..")) {
/*
* This is more or less a hack. For some Browsers (even the same
* version on different system behave differently) the redirected
* URL contains multiple ".." before the index part. If this the
* case we replace them...
*/
pathInfo = pathInfo.replaceAll("\\.{2,}", ".");
}
if (CMSConfig.getInstanceOf().getUseLanguageExtension() if (CMSConfig.getInstanceOf().getUseLanguageExtension()
&& pathInfo.matches("(.*)/index\\.[a-zA-Z]{2}")) { && pathInfo.matches("(.*)/index\\.[a-zA-Z]{2}")) {