--- /dev/null
+/*
+ * 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
+ }
+
+}
--- /dev/null
+/*
+ * 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 <code>onBeforeRender()</code>,
+ * <code>setHeaders()</code> and <code>onAfterRender</code> in the page and
+ * delegate to the corresponding methods in this class.
+ *
+ * @author Erik Brakkee
+ */
+public class CompositePageBehavior implements PageBehavior {
+
+ private List<PageBehavior> behaviors;
+
+ /**
+ * Constructs the compositie.
+ */
+ public CompositePageBehavior() {
+ behaviors = new ArrayList<PageBehavior>();
+ }
+
+ /**
+ * 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);
+ }
+ }
+
+}
--- /dev/null
+/*
+ * 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;
+
+/**
+ * <p>
+ * Behavior to disable browser caching. This also means that the page
+ * automatically expires the first time after it is rendered.
+ * </p>
+ *
+ * <p>
+ * 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
+ * <code>getPage().getPageMap().remove(getPage());</code> in the
+ * <code>onSubmit</code> callback of the form.
+ * </p>
+ *
+ * @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");
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+
+}
--- /dev/null
+/*
+ * 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);
+ }
+ }
+
+}
--- /dev/null
+/*
+ * 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 <code>onBeforeRender</code>
+ *
+ * @param aPage
+ * Page this is called for.
+ */
+ void onBeforeRender(WebPage aPage);
+
+ /**
+ * To be called as part of the Page's <code>setHeaders</code>
+ *
+ * @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 <code>onAfterRender</code>
+ *
+ * @param aPage
+ * Page this is called for.
+ */
+ void onAfterRender(WebPage aPage);
+}
--- /dev/null
+/*
+ * 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);
+ }
+}
--- /dev/null
+/*
+ * 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:
+ * <ul>
+ * <li> {@link WebApplicationBasePage}: A generic web page base class to which various
+ * behaviors can be added.
+ * </li>
+ * <li> {@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.
+ * </li>
+ * <li> {@link PageBehavior}: The page behavior interface.
+ * </li>
+ * </ul>
+ */
+package org.wamblee.wicket.page;
+