io.soabase.guice.JerseyMultiGuiceModule Maven / Gradle / Ivy
/**
* Copyright 2014 Jordan Zimmerman
*
* 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 io.soabase.guice;
import com.google.common.collect.Lists;
import com.google.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.internal.UniqueAnnotations;
import javax.servlet.Filter;
import javax.servlet.http.HttpServlet;
import java.util.List;
import java.util.Map;
/**
*
* Provides much of the functionality of the Guice ServletModule (https://github.com/google/guice/wiki/ServletModule).
* All registrations are forwarded to the appropriate Dropwizard/Jersey/Jetty methods.
*
*
*
* heavily copied from Guice Servlet
*
*
*
* Abstracted so that you can have multiple jersey-style modules in your injector. You must
* have at least one {@link JerseyGuiceModule} but can have as many {@link JerseyMultiGuiceModule}s as you
* want.
*
*/
public class JerseyMultiGuiceModule extends AbstractModule
{
private final List filterDefinitions = Lists.newArrayList();
private final List servletDefinitions = Lists.newArrayList();
public FilterKeyBindingBuilder filter(String... urlPatterns)
{
return new FilterKeyBindingBuilderImpl(this, Lists.newArrayList(urlPatterns));
}
public ServletKeyBindingBuilder serve(String... urlPatterns)
{
return new ServletKeyBindingBuilderImpl(this, Lists.newArrayList(urlPatterns));
}
@Override
protected final void configure()
{
internalConfigure();
configureServlets();
for ( FilterDefinition filterDefinition : filterDefinitions )
{
bind(FilterDefinition.class).annotatedWith(UniqueAnnotations.create()).toInstance(filterDefinition);
}
for ( ServletDefinition servletDefinition : servletDefinitions )
{
bind(ServletDefinition.class).annotatedWith(UniqueAnnotations.create()).toInstance(servletDefinition);
}
}
void internalConfigure()
{
}
/**
* Servlet Mapping EDSL
*
* Part of the EDSL builder language for configuring servlets
* and filters with guice-servlet. Think of this as an in-code replacement for web.xml.
* Filters and servlets are configured here using simple java method calls. Here is a typical
* example of registering a filter when creating your Guice injector:
*
*
* Guice.createInjector(..., new ServletModule() {
*
* {@literal @}Override
* protected void configureServlets() {
* serve("*.html").with(MyServlet.class)
* }
* }
*
*
* This registers a servlet (subclass of {@code HttpServlet}) called {@code MyServlet} to service
* any web pages ending in {@code .html}. You can also use a path-style syntax to register
* servlets:
*
*
* serve("/my/*").with(MyServlet.class)
*
*
* Every servlet (or filter) is required to be a singleton. If you cannot annotate the class
* directly, you should add a separate {@code bind(..).in(Singleton.class)} rule elsewhere in
* your module. Mapping a servlet that is bound under any other scope is an error.
*
*
* Initialization Parameters
*
* Servlets (and filters) allow you to pass in init params
* using the {@code } tag in web.xml. You can similarly pass in parameters to
* Servlets and filters registered in Guice-servlet using a {@link Map} of parameter
* name/value pairs. For example, to initialize {@code MyServlet} with two parameters
* ({@code name="Dhanji", site="google.com"}) you could write:
*
*
* Map<String, String> params = new HashMap<String, String>();
* params.put("name", "Dhanji");
* params.put("site", "google.com");
*
* ...
* serve("/*").with(MyServlet.class, params)
*
*
*
* Binding Keys
*
* You can also bind keys rather than classes. This lets you hide
* implementations with package-local visbility and expose them using
* only a Guice module and an annotation:
*
*
* ...
* filter("/*").through(Key.get(Filter.class, Fave.class));
*
*
* Where {@code Filter.class} refers to the Servlet API interface and {@code Fave.class} is a
* custom binding annotation. Elsewhere (in one of your own modules) you can bind this
* filter's implementation:
*
*
* bind(Filter.class).annotatedWith(Fave.class).to(MyFilterImpl.class);
*
*
* See {@link com.google.inject.Binder} for more information on binding syntax.
*/
protected void configureServlets()
{
}
void add(FilterDefinition filterDefinition)
{
filterDefinitions.add(filterDefinition);
}
void add(Key key, Filter filter)
{
bind(key).toInstance(filter);
}
void add(ServletDefinition servletDefinition)
{
servletDefinitions.add(servletDefinition);
}
void add(Key key, HttpServlet servlet)
{
bind(key).toInstance(servlet);
}
}