Skip to content

The Unified and Autonomous Development Platform

Configure GitopWebApp so that browser url does not change upon errors.

robin shine55bcc3ed9eac
diff --git a/commons.jetty/src/main/java/com/pmease/commons/jetty/JettyPlugin.java b/commons.jetty/src/main/java/com/pmease/commons/jetty/JettyPlugin.java
index ae4f480083..9b10bb174a 100644
--- a/commons.jetty/src/main/java/com/pmease/commons/jetty/JettyPlugin.java
+++ b/commons.jetty/src/main/java/com/pmease/commons/jetty/JettyPlugin.java
@@ -93,8 +93,9 @@ public class JettyPlugin extends AbstractPlugin {
*/
List<ServletConfigurator> servletConfigurators = new ArrayList<>(servletConfiguratorsProvider.get());
Collections.reverse(servletConfigurators);
- for (ServletConfigurator configurator: servletConfigurators)
+ for (ServletConfigurator configurator: servletConfigurators) {
configurator.configure(contextHandler);
+ }
/*
* Add Guice filter as last filter in order to make sure that filters and servlets
diff --git a/commons.shiro/src/main/java/com/pmease/commons/shiro/BasicAuthenticationFilter.java b/commons.shiro/src/main/java/com/pmease/commons/shiro/BasicAuthenticationFilter.java
index 2b410b2962..b5c92b49d4 100644
--- a/commons.shiro/src/main/java/com/pmease/commons/shiro/BasicAuthenticationFilter.java
+++ b/commons.shiro/src/main/java/com/pmease/commons/shiro/BasicAuthenticationFilter.java
@@ -88,6 +88,9 @@ public class BasicAuthenticationFilter extends PathMatchingFilter {
String authcHeader = HttpServletRequest.BASIC_AUTH + " realm=\"" + appName + "\"";
httpResponse.setHeader(AUTHENTICATE_HEADER, authcHeader);
existing = null;
+ } else {
+ httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "Access denied.");
+ existing = null;
}
} else if (ExceptionUtils.find(existing, IncorrectCredentialsException.class) != null) {
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Incorrect credentials.");
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/GitopWebApp.java b/gitop.web/src/main/java/com/pmease/gitop/web/GitopWebApp.java
index 96637a995f..8d9c6d0d02 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/GitopWebApp.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/GitopWebApp.java
@@ -8,6 +8,7 @@ import java.util.List;
import javax.inject.Singleton;
import javax.persistence.EntityNotFoundException;
+import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
@@ -22,6 +23,7 @@ import org.apache.wicket.core.request.mapper.MountedMapper;
import org.apache.wicket.devutils.stateless.StatelessChecker;
import org.apache.wicket.markup.html.IPackageResourceGuard;
import org.apache.wicket.markup.html.SecurePackageResourceGuard;
+import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
import org.apache.wicket.request.IRequestHandler;
import org.apache.wicket.request.IRequestMapper;
import org.apache.wicket.request.Request;
@@ -29,6 +31,7 @@ import org.apache.wicket.request.Response;
import org.apache.wicket.request.Url;
import org.apache.wicket.request.cycle.AbstractRequestCycleListener;
import org.apache.wicket.request.cycle.RequestCycle;
+import org.apache.wicket.request.http.WebRequest;
import org.apache.wicket.request.resource.caching.FilenameWithVersionResourceCachingStrategy;
import org.apache.wicket.request.resource.caching.version.LastModifiedResourceVersion;
import org.apache.wicket.util.time.Duration;
@@ -57,6 +60,7 @@ import com.pmease.gitop.web.page.account.setting.teams.AccountTeamsPage;
import com.pmease.gitop.web.page.account.setting.teams.AddTeamPage;
import com.pmease.gitop.web.page.account.setting.teams.EditTeamPage;
import com.pmease.gitop.web.page.error.AccessDeniedPage;
+import com.pmease.gitop.web.page.error.ErrorPage;
import com.pmease.gitop.web.page.error.InternalErrorPage;
import com.pmease.gitop.web.page.error.PageExpiredPage;
import com.pmease.gitop.web.page.error.PageNotFoundPage;
@@ -120,6 +124,30 @@ public class GitopWebApp extends AbstractWicketConfig {
return new GitopSession(request);
}
+ @Override
+ public WebRequest newWebRequest(HttpServletRequest servletRequest, String filterPath) {
+ return new ServletWebRequest(servletRequest, filterPath) {
+
+ @Override
+ public boolean shouldPreserveClientUrl() {
+ if (RequestCycle.get().getActiveRequestHandler() instanceof RenderPageRequestHandler) {
+ RenderPageRequestHandler requestHandler =
+ (RenderPageRequestHandler) RequestCycle.get().getActiveRequestHandler();
+
+ /*
+ * Add this to make sure that the page url does not change upon errors, so that
+ * user can know which page is actually causing the error. This behavior is common
+ * for main stream applications.
+ */
+ if (requestHandler.getPage() instanceof ErrorPage)
+ return true;
+ }
+ return super.shouldPreserveClientUrl();
+ }
+
+ };
+ }
+
@Override
protected void init() {
this.startupDate = new Date();
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/WebServletConfigurator.java b/gitop.web/src/main/java/com/pmease/gitop/web/WebServletConfigurator.java
index bcbe4a617b..29a729457e 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/WebServletConfigurator.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/WebServletConfigurator.java
@@ -2,10 +2,8 @@ package com.pmease.gitop.web;
import javax.inject.Inject;
import javax.inject.Singleton;
-import javax.servlet.http.HttpServletResponse;
import org.apache.wicket.protocol.http.WicketServlet;
-import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
@@ -36,9 +34,6 @@ public class WebServletConfigurator implements ServletConfigurator {
servletHolder = new ServletHolder(new ClasspathAssetServlet(AssetLocator.class));
context.addServlet(servletHolder, "/assets/*");
context.addServlet(servletHolder, "/favicon.ico");
-
- ErrorPageErrorHandler errorHandler = (ErrorPageErrorHandler) context.getErrorHandler();
- errorHandler.addErrorPage(HttpServletResponse.SC_NOT_FOUND, "/assets/404.html");
}
}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/assets/404.html b/gitop.web/src/main/java/com/pmease/gitop/web/assets/404.html
deleted file mode 100644
index 4bc02ed024..0000000000
--- a/gitop.web/src/main/java/com/pmease/gitop/web/assets/404.html
+++ /dev/null
@@ -1 +0,0 @@
-SORRY! PAGE NOT FOUND!
\ No newline at end of file
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/BasePage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/BasePage.java
index 00c9018e0d..36e9691f01 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/BasePage.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/BasePage.java
@@ -132,6 +132,11 @@ public abstract class BasePage extends WebPage {
}
}
+ public final void onException(RuntimeException e) {
+ shouldInitialize = false;
+ throw e;
+ }
+
public final void redirectWithInterception(final Class<? extends Page> clazz) {
shouldInitialize = true;
throw new RestartResponseAtInterceptPageException(clazz);
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/account/AbstractAccountPage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/account/AbstractAccountPage.java
index 712e212255..6c5151c018 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/account/AbstractAccountPage.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/account/AbstractAccountPage.java
@@ -25,7 +25,7 @@ public abstract class AbstractAccountPage extends AbstractLayoutPage {
User user = Gitop.getInstance(UserManager.class).find(name);
if (user == null) {
- throw new EntityNotFoundException("User " + name + " not found");
+ onException(new EntityNotFoundException("User " + name + " not found"));
}
this.accountModel = new UserModel(user);
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/error/AccessDeniedPage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/error/AccessDeniedPage.java
index c82e5da485..35297e86be 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/error/AccessDeniedPage.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/error/AccessDeniedPage.java
@@ -1,8 +1,6 @@
package com.pmease.gitop.web.page.error;
-import com.pmease.gitop.web.page.BasePage;
-
-public class AccessDeniedPage extends BasePage {
+public class AccessDeniedPage extends ErrorPage {
private static final long serialVersionUID = 1L;
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/error/ErrorPage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/error/ErrorPage.java
new file mode 100644
index 0000000000..b089ee13a2
--- /dev/null
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/error/ErrorPage.java
@@ -0,0 +1,8 @@
+package com.pmease.gitop.web.page.error;
+
+import com.pmease.gitop.web.page.BasePage;
+
+@SuppressWarnings("serial")
+public abstract class ErrorPage extends BasePage {
+
+}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/error/InternalErrorPage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/error/InternalErrorPage.java
index 319daabf74..f80a9082d8 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/error/InternalErrorPage.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/error/InternalErrorPage.java
@@ -1,8 +1,6 @@
package com.pmease.gitop.web.page.error;
-import com.pmease.gitop.web.page.BasePage;
-
-public class InternalErrorPage extends BasePage {
+public class InternalErrorPage extends ErrorPage {
private static final long serialVersionUID = 1L;
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/error/PageExpiredPage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/error/PageExpiredPage.java
index 23a859bb0f..93787eeb63 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/error/PageExpiredPage.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/error/PageExpiredPage.java
@@ -1,9 +1,7 @@
package com.pmease.gitop.web.page.error;
-import com.pmease.gitop.web.page.BasePage;
-
@SuppressWarnings("serial")
-public class PageExpiredPage extends BasePage {
+public class PageExpiredPage extends ErrorPage {
@Override
protected String getPageTitle() {
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/error/PageNotFoundPage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/error/PageNotFoundPage.java
index 49b93fe9bd..60d2c7f437 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/error/PageNotFoundPage.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/error/PageNotFoundPage.java
@@ -1,9 +1,7 @@
package com.pmease.gitop.web.page.error;
-import com.pmease.gitop.web.page.BasePage;
-
@SuppressWarnings("serial")
-public class PageNotFoundPage extends BasePage {
+public class PageNotFoundPage extends ErrorPage {
@Override
protected String getPageTitle() {
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/shiro/LoginPage.java b/gitop.web/src/main/java/com/pmease/gitop/web/shiro/LoginPage.java
index 92a806868f..9d9645f3a2 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/shiro/LoginPage.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/shiro/LoginPage.java
@@ -107,7 +107,7 @@ public class LoginPage extends AbstractLayoutPage {
protected boolean remember() {
CheckBoxElement c = (CheckBoxElement) get("rememberme");
Boolean b = (Boolean) c.getFormComponent().getDefaultModelObject();
- System.out.println(b);
+// System.out.println(b);
return b;
}
}