/*
* Databinder: a simple bridge from Wicket to Hibernate
* Copyright (C) 2008 Nathan Hamblen nathan@technically.us
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.databinder;
import java.awt.Color;
import java.net.URI;
import javax.servlet.http.HttpServletResponse;
import net.databinder.components.PageExpiredCookieless;
import net.databinder.converters.ColorConverter;
import net.databinder.converters.URIConverter;
import net.databinder.web.NorewriteWebResponse;
import org.apache.wicket.IConverterLocator;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Response;
import org.apache.wicket.markup.html.pages.PageExpiredErrorPage;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.util.convert.ConverterLocator;
/** Common functionality for Databinder applications. */
public abstract class DataApplicationBase extends WebApplication {
/** true if cookieless use is supported through URL rewriting(defaults to true). */
private boolean cookielessSupported = true;
/**
* Internal initialization. Client applications should not normally override
* or call this method.
*/
@Override
protected void internalInit() {
super.internalInit();
dataInit();
}
/** Databinder initialization, client applications should not normally override.*/
abstract protected void dataInit();
/** Adds converters to Wicket's base locator. */
@Override
protected IConverterLocator newConverterLocator() {
// register converters
ConverterLocator converterLocator = new ConverterLocator();
converterLocator.set(URI.class, new URIConverter());
converterLocator.set(Color.class, new ColorConverter());
return converterLocator;
}
/**
* If <code>isCookielessSupported()</code> returns false, this method returns
* a custom WebResponse that disables URL rewriting.
*/
@Override
protected WebResponse newWebResponse(final HttpServletResponse servletResponse)
{
if (isCookielessSupported())
return super.newWebResponse(servletResponse);
return NorewriteWebResponse.getNew(this, servletResponse);
}
@Override
public RequestCycle newRequestCycle(Request request, Response response) {
return new CookieRequestCycle(this, (WebRequest) request, (WebResponse) response);
}
/**
* @return true if cookieless use is supported through URL rewriting.
*/
public boolean isCookielessSupported() {
return cookielessSupported;
}
/**
* Set to false to disable URL rewriting and consequentally hamper cookieless
* browsing. Users with cookies disabled, and more importantly search engines,
* will still be able to browse the application through bookmarkable URLs. Because
* rewriting is disabled, these URLs will have no jsessionid appended and will
* remain static.
* <p> The Application's "page expired" error page will be set to PageExpiredCookieless
* if cookielessSupported is false, unless an alternate error page has already been
* specified. This page will appear when cookieless users try to follow a link or
* form-submit that requires a session, informing them that cookies are required.
* </p>
* @param cookielessSupported true if cookieless use is supported through
* URL rewriting
* @see net.databinder.components.PageExpiredCookieless
*/
protected void setCookielessSupported(boolean cookielessSupported) {
Class<? extends Page> expected = this.cookielessSupported ?
PageExpiredErrorPage.class : PageExpiredCookieless.class;
this.cookielessSupported = cookielessSupported;
if (getApplicationSettings().getPageExpiredErrorPage().equals(expected))
getApplicationSettings().setPageExpiredErrorPage(cookielessSupported ?
PageExpiredErrorPage.class : PageExpiredCookieless.class);
}
/**
* Reports if the program is running in a development environment, as determined by the
* "wicket.configuration" environment variable or context/init parameter. If that variable
* is unset or set to "development", the app is considered to be running in development.
* @return true if running in a development environment
*/
protected boolean isDevelopment() {
return getConfigurationType().equalsIgnoreCase(DEVELOPMENT);
}
}