Sub page should override onPageInitialize() instead of onInitialize()

This commit is contained in:
robin shine 2013-10-02 22:01:20 +08:00
parent bde8789aec
commit 1edc128e69
10 changed files with 114 additions and 74 deletions

View File

@ -151,21 +151,23 @@ public class GitFilter implements Filter {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
if (gitop.isReady()) {
try {
if (GitSmartHttpTools.isInfoRefs(httpRequest)) {
try {
if (GitSmartHttpTools.isInfoRefs(httpRequest)) {
if (gitop.isReady())
processRefs(httpRequest, httpResponse);
} else if (GitSmartHttpTools.isReceivePack(httpRequest) || GitSmartHttpTools.isUploadPack(httpRequest)) {
else
throw new GeneralException("Server is not ready");
} else if (GitSmartHttpTools.isReceivePack(httpRequest) || GitSmartHttpTools.isUploadPack(httpRequest)) {
if (gitop.isReady())
processPacks(httpRequest, httpResponse);
} else {
chain.doFilter(request, response);
}
} catch (GeneralException e) {
logger.error("Error serving git request", e);
GitSmartHttpTools.sendError(httpRequest, httpResponse, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
else
throw new GeneralException("Server is not ready");
} else {
chain.doFilter(request, response);
}
} else {
GitSmartHttpTools.sendError(httpRequest, httpResponse, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Server is not ready.");
} catch (GeneralException e) {
logger.error("Error serving git request", e);
GitSmartHttpTools.sendError(httpRequest, httpResponse, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
}

View File

@ -1,8 +1,6 @@
package com.pmease.gitop.web.page;
import org.apache.shiro.SecurityUtils;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import com.google.common.base.Optional;
import com.pmease.gitop.core.model.User;
@ -10,21 +8,9 @@ import com.pmease.gitop.core.model.User;
@SuppressWarnings("serial")
public abstract class AbstractLayoutPage extends BasePage {
public AbstractLayoutPage() {
commonInit();
}
public AbstractLayoutPage(PageParameters params) {
super(params);
commonInit();
}
public AbstractLayoutPage(IModel<?> model) {
super(model);
commonInit();
}
private void commonInit() {
@Override
protected void onPageInitialize() {
super.onPageInitialize();
add(new GlobalHeaderPanel("header"));
}

View File

@ -32,6 +32,8 @@ public abstract class BasePage extends WebPage {
private WebMarkupContainer body;
private boolean shouldInitialize = true;
public BasePage() {
commonInit();
}
@ -47,23 +49,11 @@ public abstract class BasePage extends WebPage {
}
private void commonInit() {
body = new TransparentWebMarkupContainer("body");
add(body);
body.add(AttributeAppender.append("class",
new AbstractReadOnlyModel<String>() {
@Override
public String getObject() {
String css = getPageCssClass();
return Strings.isNullOrEmpty(css) ? "" : css;
}
}));
if (!Gitop.getInstance().isReady()
&& getClass() != ServerInitPage.class) {
throw new RestartResponseAtInterceptPageException(
ServerInitPage.class);
if (!Gitop.getInstance().isReady() && getClass() != ServerInitPage.class) {
redirect(ServerInitPage.class);
}
shouldInitialize = true;
}
@Override
@ -119,23 +109,47 @@ public abstract class BasePage extends WebPage {
}
}
public final void redirectWithIntercept(final Class<? extends Page> clazz) {
shouldInitialize = true;
throw new RestartResponseAtInterceptPageException(clazz);
}
public final void redirectWithIntercept(final Class<? extends Page> clazz, final PageParameters pageParams) {
shouldInitialize = true;
throw new RestartResponseAtInterceptPageException(clazz, pageParams);
}
public final void redirectWithIntercept(final Page page) {
shouldInitialize = true;
throw new RestartResponseAtInterceptPageException(page);
}
public final void redirect(final Class<? extends Page> clazz) {
shouldInitialize = false;
throw new RestartResponseException(clazz);
}
public final void redirect(final Class<? extends Page> clazz,
PageParameters parameters) {
shouldInitialize = false;
throw new RestartResponseException(clazz, parameters);
}
public final void redirect(final Page page) {
shouldInitialize = false;
throw new RestartResponseException(page);
}
public final void redirect(String url) {
shouldInitialize = false;
throw new RedirectToUrlException(url);
}
public final void redirectToOriginal() {
shouldInitialize = false;
continueToOriginalDestination();
}
protected String getPageCssClass() {
String name = getClass().getSimpleName();
return StringUtils.camelCaseToLowerCaseWithHyphen(name);
@ -145,9 +159,18 @@ public abstract class BasePage extends WebPage {
return true;
}
@Override
protected void onInitialize() {
super.onInitialize();
protected void onPageInitialize() {
body = new TransparentWebMarkupContainer("body");
add(body);
body.add(AttributeAppender.append("class",
new AbstractReadOnlyModel<String>() {
@Override
public String getObject() {
String css = getPageCssClass();
return Strings.isNullOrEmpty(css) ? "" : css;
}
}));
if (!isPermitted()) {
throw new AccessDeniedException();
@ -179,8 +202,16 @@ public abstract class BasePage extends WebPage {
* cause components with resources using global resources not working
* properly.
*/
add(new WebMarkupContainer("globalResourceBinder")
.add(new BaseResourceBehavior()));
add(new WebMarkupContainer("globalResourceBinder").add(new BaseResourceBehavior()));
}
@Override
protected final void onInitialize() {
super.onInitialize();
if (shouldInitialize) {
onPageInitialize();
}
}
protected abstract String getPageTitle();

View File

@ -36,6 +36,11 @@ public class AccountHomePage extends AbstractLayoutPage {
}
};
}
@Override
protected void onPageInitialize() {
super.onPageInitialize();
add(new Label("accountName", getAccount().getName()));

View File

@ -41,9 +41,20 @@ public class RegisterPage extends AbstractLayoutPage {
return "Gitop - Sign Up";
}
public RegisterPage() {
@Override
protected void onPageInitialize() {
super.onPageInitialize();
final IModel<User> model = Model.<User>of(new User());
Form<User> form = new Form<User>("form", model);
Form<User> form = new Form<User>("form", model) {
@Override
protected void onInitialize() {
super.onInitialize();
}
};
add(form);
form.add(new FeedbackPanel("feedback", form));

View File

@ -10,8 +10,6 @@ import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import com.google.common.collect.ImmutableList;
import com.pmease.gitop.core.model.User;
@ -50,21 +48,10 @@ public abstract class AccountSettingPage extends AbstractLayoutPage {
}
}
public AccountSettingPage() {
commonInit();
}
@Override
protected void onPageInitialize() {
super.onPageInitialize();
public AccountSettingPage(PageParameters params) {
super(params);
commonInit();
}
public AccountSettingPage(IModel<?> model) {
super(model);
commonInit();
}
private void commonInit() {
add(new UserAvatarLink("userlink", new UserModel(getAccount())));
add(new ListView<Category>("setting", ImmutableList.<Category>copyOf(Category.values())) {

View File

@ -34,7 +34,10 @@ public class AccountPasswordPage extends AccountSettingPage {
return Category.PASSWORD;
}
public AccountPasswordPage() {
@Override
protected void onPageInitialize() {
super.onPageInitialize();
Form<User> form = new Form<User>("form", new UserModel(getAccount()));
add(form);
form.add(new PasswordFieldElement("oldPass", "Current Password",

View File

@ -3,7 +3,6 @@ package com.pmease.gitop.web.page.init;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
@ -22,9 +21,15 @@ public class ServerInitPage extends BasePage {
public ServerInitPage() {
initStage = Gitop.getInstance().getInitStage();
if (initStage == null) {
continueToOriginalDestination();
throw new RestartResponseException(getApplication().getHomePage());
redirectToOriginal();
redirect(getApplication().getHomePage());
}
}
@Override
protected void onPageInitialize() {
super.onPageInitialize();
add(new Label("message", initStage.getMessage()));

View File

@ -1,5 +1,6 @@
package com.pmease.gitop.web.page.project;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
@ -12,6 +13,7 @@ import com.pmease.gitop.core.model.Project;
import com.pmease.gitop.web.page.AbstractLayoutPage;
@SuppressWarnings("serial")
@RequiresAuthentication
public class ProjectHomePage extends AbstractLayoutPage {
private final IModel<Project> projectModel;
@ -43,6 +45,11 @@ public class ProjectHomePage extends AbstractLayoutPage {
}
};
}
@Override
protected void onPageInitialize() {
super.onPageInitialize();
add(new Label("accountName", getProject().getOwner().getName()));
add(new Label("projectName", getProject().getName()));

View File

@ -14,7 +14,10 @@ import com.pmease.gitop.web.page.BasePage;
@SuppressWarnings("serial")
public class TestPage extends BasePage {
public TestPage() {
@Override
protected void onPageInitialize() {
super.onPageInitialize();
final EditContext editContext = EditableUtils.getContext(new Project());
Form<?> form = new Form<Void>("form") {