From 7e7d1f6f67766d5998d4631cffadb79da06a0acd Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Mon, 26 Jul 2010 17:20:46 +0000 Subject: [PATCH] --- .../wicket/page/AbstractPageBehavior.java | 51 +++++++ .../wicket/page/CompositePageBehavior.java | 77 ++++++++++ .../wicket/page/DisableCachingBehavior.java | 45 ++++++ .../page/ExpirePageImmediatelyBehavior.java | 34 +++++ .../page/FlushEntityManagerBehavior.java | 69 +++++++++ .../org/wamblee/wicket/page/PageBehavior.java | 56 ++++++++ .../wicket/page/WebApplicationBasePage.java | 132 ++++++++++++++++++ .../org/wamblee/wicket/page/package-info.java | 33 +++++ 8 files changed, 497 insertions(+) create mode 100644 wicket/components/src/main/java/org/wamblee/wicket/page/AbstractPageBehavior.java create mode 100644 wicket/components/src/main/java/org/wamblee/wicket/page/CompositePageBehavior.java create mode 100644 wicket/components/src/main/java/org/wamblee/wicket/page/DisableCachingBehavior.java create mode 100644 wicket/components/src/main/java/org/wamblee/wicket/page/ExpirePageImmediatelyBehavior.java create mode 100644 wicket/components/src/main/java/org/wamblee/wicket/page/FlushEntityManagerBehavior.java create mode 100644 wicket/components/src/main/java/org/wamblee/wicket/page/PageBehavior.java create mode 100644 wicket/components/src/main/java/org/wamblee/wicket/page/WebApplicationBasePage.java create mode 100644 wicket/components/src/main/java/org/wamblee/wicket/page/package-info.java diff --git a/wicket/components/src/main/java/org/wamblee/wicket/page/AbstractPageBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/page/AbstractPageBehavior.java new file mode 100644 index 00000000..81652596 --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/page/AbstractPageBehavior.java @@ -0,0 +1,51 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.wicket.page; + +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.protocol.http.WebResponse; + +/** + * Abstract page behavior to simplify implementations of behaviors thar don't + * need to implement all callbacks. + * + * @author Erik Brakkee + */ +public abstract class AbstractPageBehavior implements PageBehavior { + + /** + * Constructor. + */ + protected AbstractPageBehavior() { + // Empty + } + + @Override + public void onAfterRender(WebPage aPage) { + // Empty + } + + @Override + public void onBeforeRender(WebPage aPage) { + // Empty + } + + @Override + public void setHeaders(WebPage aPage, WebResponse aResponse) { + // Empty + } + +} diff --git a/wicket/components/src/main/java/org/wamblee/wicket/page/CompositePageBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/page/CompositePageBehavior.java new file mode 100644 index 00000000..d6946de6 --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/page/CompositePageBehavior.java @@ -0,0 +1,77 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.wicket.page; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.protocol.http.WebResponse; + +/** + * Implements a composite page behavior which invokes all behaviors in the order + * they were added. + * + * To use this in an arbitrary page, override onBeforeRender(), + * setHeaders() and onAfterRender in the page and + * delegate to the corresponding methods in this class. + * + * @author Erik Brakkee + */ +public class CompositePageBehavior implements PageBehavior { + + private List behaviors; + + /** + * Constructs the compositie. + */ + public CompositePageBehavior() { + behaviors = new ArrayList(); + } + + /** + * Adds a behavior. + * + * @param aBehavior + * Behavior + */ + public void add(PageBehavior aBehavior) { + behaviors.add(aBehavior); + } + + @Override + public void onAfterRender(WebPage aPage) { + for (PageBehavior behavior : behaviors) { + behavior.onAfterRender(aPage); + } + } + + @Override + public void onBeforeRender(WebPage aPage) { + for (PageBehavior behavior : behaviors) { + behavior.onBeforeRender(aPage); + } + } + + @Override + public void setHeaders(WebPage aPage, WebResponse aResponse) { + for (PageBehavior behavior : behaviors) { + behavior.setHeaders(aPage, aResponse); + } + } + +} diff --git a/wicket/components/src/main/java/org/wamblee/wicket/page/DisableCachingBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/page/DisableCachingBehavior.java new file mode 100644 index 00000000..04003a2c --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/page/DisableCachingBehavior.java @@ -0,0 +1,45 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.wicket.page; + +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.protocol.http.WebResponse; + +/** + *

+ * Behavior to disable browser caching. This also means that the page + * automatically expires the first time after it is rendered. + *

+ * + *

+ * Important: To make sure that form data cannot be submitted twice for the same + * data using the back button, it is still necessary to explicitly call + * getPage().getPageMap().remove(getPage()); in the + * onSubmit callback of the form. + *

+ * + * @author Erik Brakkee + * + */ +public class DisableCachingBehavior extends ExpirePageImmediatelyBehavior { + + @Override + public void setHeaders(WebPage aPage, WebResponse aResponse) { + aResponse.setHeader("Pragma", "no-cache"); + aResponse.setHeader("Cache-Control", + "no-cache, max-age=0, must-revalidate, no-store"); + } +} diff --git a/wicket/components/src/main/java/org/wamblee/wicket/page/ExpirePageImmediatelyBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/page/ExpirePageImmediatelyBehavior.java new file mode 100644 index 00000000..a1416d2a --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/page/ExpirePageImmediatelyBehavior.java @@ -0,0 +1,34 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.wicket.page; + +import org.apache.wicket.markup.html.WebPage; + +/** + * Page which will be removed from the page map after rendering so it can be + * viewed only once. + * + * @author Erik Brakkee + * + */ +public class ExpirePageImmediatelyBehavior extends AbstractPageBehavior { + + @Override + public void onAfterRender(WebPage aPage) { + aPage.getPageMap().remove(aPage); + } + +} diff --git a/wicket/components/src/main/java/org/wamblee/wicket/page/FlushEntityManagerBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/page/FlushEntityManagerBehavior.java new file mode 100644 index 00000000..d1b663d8 --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/page/FlushEntityManagerBehavior.java @@ -0,0 +1,69 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.wicket.page; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.annotation.Resource; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceException; +import javax.persistence.TransactionRequiredException; +import javax.transaction.Status; +import javax.transaction.SystemException; +import javax.transaction.UserTransaction; + +import org.apache.wicket.markup.html.WebPage; + +/** + * Behavior to flush the entity manager after rendering of the page. + * + * @author Erik Brakkee + * + */ +public class FlushEntityManagerBehavior extends AbstractPageBehavior { + + private static final Logger LOGGER = Logger + .getLogger(FlushEntityManagerBehavior.class.getName()); + + private EntityManager entityManager; + + /** + * Constructs the behavior. + * + * @param aEntityManager + * Contextual reference to an entitymanager. + */ + public FlushEntityManagerBehavior(EntityManager aEntityManager) { + entityManager = aEntityManager; + } + + @Override + public void onAfterRender(WebPage aPage) { + try { + if (entityManager.isOpen()) { + entityManager.flush(); + } + } catch (TransactionRequiredException e) { + throw e; + } catch (PersistenceException e) { + throw e; + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Could not flush entitymanager", e); + } + } + +} diff --git a/wicket/components/src/main/java/org/wamblee/wicket/page/PageBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/page/PageBehavior.java new file mode 100644 index 00000000..dbaf7dc0 --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/page/PageBehavior.java @@ -0,0 +1,56 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.wicket.page; + +import java.io.Serializable; + +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.protocol.http.WebResponse; + +/** + * Page behavior interface. This provides a mechanism to extend page behavior + * without subclassing. + * + * @author Erik Brakkee + */ +public interface PageBehavior extends Serializable { + + /** + * To be called as part of the Page's onBeforeRender + * + * @param aPage + * Page this is called for. + */ + void onBeforeRender(WebPage aPage); + + /** + * To be called as part of the Page's setHeaders + * + * @param aPage + * Page this is called for. + * @param aResponse + * Response to set headers for. + */ + void setHeaders(WebPage aPage, WebResponse aResponse); + + /** + * To be called as part of the Page's onAfterRender + * + * @param aPage + * Page this is called for. + */ + void onAfterRender(WebPage aPage); +} diff --git a/wicket/components/src/main/java/org/wamblee/wicket/page/WebApplicationBasePage.java b/wicket/components/src/main/java/org/wamblee/wicket/page/WebApplicationBasePage.java new file mode 100644 index 00000000..de46e9e3 --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/page/WebApplicationBasePage.java @@ -0,0 +1,132 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.wicket.page; + +import javax.persistence.EntityManager; +import javax.transaction.UserTransaction; + +import org.apache.wicket.IPageMap; +import org.apache.wicket.PageParameters; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.model.IModel; +import org.apache.wicket.protocol.http.WebResponse; + +/** + * Base page for wicket applications that allows customization of various + * bahaviors of the page. This page has no associated markup. + * + * The utility methods {@link #disableCaching()}, {@link #expireImmediately()}, + * and {@link #flushEntityManagerAfterRender(EntityManager)} are provide for + * quicly adding behaviors. Alternatively, the method + * {@link #addBehavior(PageBehavior)} can be used. + * + * @author Erik Brakkee + * + */ +public class WebApplicationBasePage extends WebPage { + + private CompositePageBehavior behaviors; + + protected WebApplicationBasePage() { + super(); + init(); + } + + protected WebApplicationBasePage(final IModel model) { + super(model); + init(); + } + + protected WebApplicationBasePage(final IPageMap pageMap) { + super(pageMap); + init(); + } + + protected WebApplicationBasePage(final IPageMap pageMap, + final IModel model) { + super(pageMap, model); + init(); + } + + protected WebApplicationBasePage(final PageParameters parameters) { + super(parameters); + init(); + } + + protected WebApplicationBasePage(final IPageMap pageMap, + final PageParameters parameters) { + super(pageMap, parameters); + init(); + } + + private void init() { + behaviors = new CompositePageBehavior(); + } + + /** + * Disables caching. This implies expiry of the page from the page map. + */ + protected void disableCaching() { + addBehavior(new DisableCachingBehavior()); + } + + /** + * Expires the page immediately. Refresh in the browser will lead to an + * expired page. + */ + protected void expireImmediately() { + addBehavior(new ExpirePageImmediatelyBehavior()); + } + + /** + * Flushes the entitymanager immedately after rendering to make sure that + * errors are caught early. + * + * @param aEntityManager + * Contextual reference to an entity manager. + */ + protected void flushEntityManagerAfterRender(EntityManager aEntityManager) { + addBehavior(new FlushEntityManagerBehavior(aEntityManager)); + } + + /** + * Adds a specific behavior to the page. + * + * @param aBehavior + * Behavior to add. + */ + public void addBehavior(PageBehavior aBehavior) { + behaviors.add(aBehavior); + } + + @Override + protected void onBeforeRender() { + behaviors.onBeforeRender(this); + super.onBeforeRender(); + } + + @Override + protected void setHeaders(WebResponse aResponse) { + super.setHeaders(aResponse); + behaviors.setHeaders(this, aResponse); + } + + @Override + protected void onAfterRender() { + super.onAfterRender(); + behaviors.onAfterRender(this); + } +} diff --git a/wicket/components/src/main/java/org/wamblee/wicket/page/package-info.java b/wicket/components/src/main/java/org/wamblee/wicket/page/package-info.java new file mode 100644 index 00000000..5598ed16 --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/page/package-info.java @@ -0,0 +1,33 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * This package contains various utilities for behaviors on components and pages. + * + * Entry points to this package are as follows: + *
    + *
  • {@link WebApplicationBasePage}: A generic web page base class to which various + * behaviors can be added. + *
  • + *
  • {@link CompositePageBehavior}: The composite behavior to which the various + * callbacks from wicket to the page can be delegated. Useful in case the inheritance + * approach based on {@link WebApplicationBasePage} canno tbe used. + *
  • + *
  • {@link PageBehavior}: The page behavior interface. + *
  • + *
+ */ +package org.wamblee.wicket.page; + -- 2.31.1