From: Erik Brakkee Date: Fri, 23 Jul 2010 18:43:51 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.7~198 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;ds=inline;h=e8442732ed5dd8c0e2c2bc02d7d1d5fce0ba1af5;p=utils --- diff --git a/wicket/components/src/main/java/org/wamblee/wicket/behavior/AbstractPageBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/AbstractPageBehavior.java new file mode 100644 index 00000000..7971e47d --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/behavior/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.behavior; + +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/behavior/CompositePageBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/CompositePageBehavior.java new file mode 100644 index 00000000..c6b7d5ab --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/behavior/CompositePageBehavior.java @@ -0,0 +1,74 @@ +/* + * 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.behavior; + +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/behavior/DisableCachingBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/DisableCachingBehavior.java new file mode 100644 index 00000000..9b2ee590 --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/behavior/DisableCachingBehavior.java @@ -0,0 +1,35 @@ +/* + * 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.behavior; + +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.protocol.http.WebResponse; + +/** + * Behavior to disable browser caching. + * + * @author Erik Brakkee + * + */ +public class DisableCachingBehavior extends AbstractPageBehavior { + + @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/behavior/ExpirePageImmediatelyBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/ExpirePageImmediatelyBehavior.java new file mode 100644 index 00000000..16e1c9bd --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/behavior/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.behavior; + +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/behavior/FlushEntityManagerBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/FlushEntityManagerBehavior.java new file mode 100644 index 00000000..e309e5ab --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/behavior/FlushEntityManagerBehavior.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.behavior; + +import javax.persistence.EntityManager; + +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 EntityManager entityManager; + + /** + * Constructs the behavior. + * @param aEntityManager Contextual reference to an entitymanager. + */ + public FlushEntityManagerBehavior(EntityManager aEntityManager) { + entityManager = aEntityManager; + } + + @Override + public void onAfterRender(WebPage aPage) { + entityManager.flush(); + } + +} diff --git a/wicket/components/src/main/java/org/wamblee/wicket/behavior/PageBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/PageBehavior.java new file mode 100644 index 00000000..4d9d3b3a --- /dev/null +++ b/wicket/components/src/main/java/org/wamblee/wicket/behavior/PageBehavior.java @@ -0,0 +1,47 @@ +/* + * 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.behavior; + +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 { + + /** + * 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/behavior/WebApplicationBasePage.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/WebApplicationBasePage.java index 9b89818c..6770b91a 100644 --- a/wicket/components/src/main/java/org/wamblee/wicket/behavior/WebApplicationBasePage.java +++ b/wicket/components/src/main/java/org/wamblee/wicket/behavior/WebApplicationBasePage.java @@ -15,46 +15,111 @@ */ package org.wamblee.wicket.behavior; +import javax.persistence.EntityManager; + 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)); + } + + + 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/behavior/package-info.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/package-info.java index 8f89c6c7..466e04ce 100644 --- a/wicket/components/src/main/java/org/wamblee/wicket/behavior/package-info.java +++ b/wicket/components/src/main/java/org/wamblee/wicket/behavior/package-info.java @@ -15,5 +15,18 @@ */ /** * 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.behavior; \ No newline at end of file