001 /*
002 * Databinder: a simple bridge from Wicket to Hibernate
003 * Copyright (C) 2008 Nathan Hamblen nathan@technically.us
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either
008 * version 2.1 of the License, or (at your option) any later version.
009 *
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public
016 * License along with this library; if not, write to the Free Software
017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
018 */
019 package net.databinder;
020
021 import java.awt.Color;
022 import java.net.URI;
023
024 import javax.servlet.http.HttpServletResponse;
025
026 import net.databinder.components.PageExpiredCookieless;
027 import net.databinder.converters.ColorConverter;
028 import net.databinder.converters.URIConverter;
029 import net.databinder.web.NorewriteWebResponse;
030
031 import org.apache.wicket.IConverterLocator;
032 import org.apache.wicket.Page;
033 import org.apache.wicket.Request;
034 import org.apache.wicket.RequestCycle;
035 import org.apache.wicket.Response;
036 import org.apache.wicket.markup.html.pages.PageExpiredErrorPage;
037 import org.apache.wicket.protocol.http.WebApplication;
038 import org.apache.wicket.protocol.http.WebRequest;
039 import org.apache.wicket.protocol.http.WebResponse;
040 import org.apache.wicket.util.convert.ConverterLocator;
041
042 /** Common functionality for Databinder applications. */
043 public abstract class DataApplicationBase extends WebApplication {
044 /** true if cookieless use is supported through URL rewriting(defaults to true). */
045 private boolean cookielessSupported = true;
046
047 /**
048 * Internal initialization. Client applications should not normally override
049 * or call this method.
050 */
051 @Override
052 protected void internalInit() {
053 super.internalInit();
054 dataInit();
055 }
056
057 /** Databinder initialization, client applications should not normally override.*/
058 abstract protected void dataInit();
059
060 /** Adds converters to Wicket's base locator. */
061 @Override
062 protected IConverterLocator newConverterLocator() {
063 // register converters
064 ConverterLocator converterLocator = new ConverterLocator();
065 converterLocator.set(URI.class, new URIConverter());
066 converterLocator.set(Color.class, new ColorConverter());
067 return converterLocator;
068 }
069
070 /**
071 * If <code>isCookielessSupported()</code> returns false, this method returns
072 * a custom WebResponse that disables URL rewriting.
073 */
074 @Override
075 protected WebResponse newWebResponse(final HttpServletResponse servletResponse)
076 {
077 if (isCookielessSupported())
078 return super.newWebResponse(servletResponse);
079 return NorewriteWebResponse.getNew(this, servletResponse);
080 }
081
082 @Override
083 public RequestCycle newRequestCycle(Request request, Response response) {
084 return new CookieRequestCycle(this, (WebRequest) request, (WebResponse) response);
085 }
086
087 /**
088 * @return true if cookieless use is supported through URL rewriting.
089 */
090 public boolean isCookielessSupported() {
091 return cookielessSupported;
092 }
093
094 /**
095 * Set to false to disable URL rewriting and consequentally hamper cookieless
096 * browsing. Users with cookies disabled, and more importantly search engines,
097 * will still be able to browse the application through bookmarkable URLs. Because
098 * rewriting is disabled, these URLs will have no jsessionid appended and will
099 * remain static.
100 * <p> The Application's "page expired" error page will be set to PageExpiredCookieless
101 * if cookielessSupported is false, unless an alternate error page has already been
102 * specified. This page will appear when cookieless users try to follow a link or
103 * form-submit that requires a session, informing them that cookies are required.
104 * </p>
105 * @param cookielessSupported true if cookieless use is supported through
106 * URL rewriting
107 * @see net.databinder.components.PageExpiredCookieless
108 */
109 protected void setCookielessSupported(boolean cookielessSupported) {
110 Class<? extends Page> expected = this.cookielessSupported ?
111 PageExpiredErrorPage.class : PageExpiredCookieless.class;
112
113 this.cookielessSupported = cookielessSupported;
114
115 if (getApplicationSettings().getPageExpiredErrorPage().equals(expected))
116 getApplicationSettings().setPageExpiredErrorPage(cookielessSupported ?
117 PageExpiredErrorPage.class : PageExpiredCookieless.class);
118 }
119
120 /**
121 * Reports if the program is running in a development environment, as determined by the
122 * "wicket.configuration" environment variable or context/init parameter. If that variable
123 * is unset or set to "development", the app is considered to be running in development.
124 * @return true if running in a development environment
125 */
126 protected boolean isDevelopment() {
127 return getConfigurationType().equalsIgnoreCase(DEVELOPMENT);
128 }
129 }