
org.directwebremoting.guice.DwrGuiceServletModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dwr Show documentation
Show all versions of dwr Show documentation
DWR is easy Ajax for Java. It makes it simple to call Java code directly from Javascript.
It gets rid of almost all the boiler plate code between the web browser and your Java code.
/*
* Copyright 2007 Tim Peierls
*
* 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.directwebremoting.guice;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ServerContext;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import com.google.inject.AbstractModule;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import static org.directwebremoting.guice.DwrGuiceUtil.getServletContext;
import static org.directwebremoting.guice.DwrScopes.APPLICATION;
import static org.directwebremoting.guice.DwrScopes.GLOBAL;
import static org.directwebremoting.guice.DwrScopes.REQUEST;
import static org.directwebremoting.guice.DwrScopes.SCRIPT;
import static org.directwebremoting.guice.DwrScopes.SESSION;
/**
* Configures DWR scopes and creates bindings for commonly
* used objects related to those scopes: request, response,
* script session, session, servlet context, and web context.
*
* Since Guice's ServletModule makes its own bindings for requests,
* responses, and sessions, this class has a constructor that lets
* the user specify whether to avoid conflicts by not binding these types.
*
* @author Tim Peierls [tim at peierls dot net]
*/
class DwrGuiceServletModule extends AbstractModule
{
/**
* Creates a module to configure DWR scopes and bindings;
* conflicts with the bindings provided by Guice's ServletModule.
*
* Normally you would not use this constructor directly, but instead call
* {@link AbstractDwrModule#bindDwrScopes() bindDwrScopes} in your binding
* code.
*
*/
public DwrGuiceServletModule()
{
this.bindPotentiallyConflictingTypes = true;
}
/**
* Creates a module to configure DWR scopes and bindings;
* conflicts with the bindings provided by Guice's ServletModule
* unless {@code bindPotentiallyConflictingTypes} is false, in which
* case this module will not create its own bindings for requests,
* responses, and sessions.
*
* Normally you would not use this constructor directly, but instead call
* {@link AbstractDwrModule#bindDwrScopes(boolean) bindDwrScopes} in your binding
* code.
*
*/
public DwrGuiceServletModule(boolean bindPotentiallyConflictingTypes)
{
this.bindPotentiallyConflictingTypes = bindPotentiallyConflictingTypes;
}
@Override
protected void configure()
{
bindScope(RequestScoped.class, REQUEST);
bindScope(SessionScoped.class, SESSION);
bindScope(ScriptSessionScoped.class, SCRIPT);
bindScope(ApplicationScoped.class, APPLICATION);
bindScope(GlobalApplicationScoped.class, GLOBAL);
if (bindPotentiallyConflictingTypes)
{
// Use DWR request and session scopes for request, response, and session.
Provider requestProvider =
new Provider()
{
public HttpServletRequest get()
{
WebContext webcx = WebContextFactory.get();
return webcx.getHttpServletRequest();
}
@Override
public String toString()
{
return "RequestProvider";
}
};
bind(HttpServletRequest.class).toProvider(requestProvider);
bind(ServletRequest.class).toProvider(requestProvider);
Provider responseProvider =
new Provider()
{
public HttpServletResponse get()
{
WebContext webcx = WebContextFactory.get();
return webcx.getHttpServletResponse();
}
@Override
public String toString()
{
return "ResponseProvider";
}
};
bind(HttpServletResponse.class).toProvider(responseProvider);
bind(ServletResponse.class).toProvider(responseProvider);
Provider sessionProvider =
new Provider()
{
public HttpSession get()
{
WebContext webcx = WebContextFactory.get();
return webcx.getSession();
}
@Override
public String toString()
{
return "SessionProvider";
}
};
bind(HttpSession.class).toProvider(sessionProvider);
}
Provider
© 2015 - 2025 Weber Informatics LLC | Privacy Policy