|
| 1 | +package io.dropwizard.jersey.setup; |
| 2 | + |
| 3 | +import com.google.common.base.Function; |
| 4 | +import com.sun.jersey.api.core.ResourceConfig; |
| 5 | +import com.sun.jersey.core.spi.scanning.PackageNamesScanner; |
| 6 | +import javax.servlet.Servlet; |
| 7 | +import io.dropwizard.jersey.DropwizardResourceConfig; |
| 8 | + |
| 9 | +import javax.annotation.Nullable; |
| 10 | + |
| 11 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 12 | + |
| 13 | +public class JerseyEnvironment { |
| 14 | + private final JerseyContainerHolder holder; |
| 15 | + private final DropwizardResourceConfig config; |
| 16 | + |
| 17 | + public JerseyEnvironment(JerseyContainerHolder holder, |
| 18 | + DropwizardResourceConfig config) { |
| 19 | + this.holder = holder; |
| 20 | + this.config = config; |
| 21 | + } |
| 22 | + |
| 23 | + public void disable() { |
| 24 | + holder.setContainer(null); |
| 25 | + } |
| 26 | + |
| 27 | + public void replace(Function<ResourceConfig, Servlet> replace) { |
| 28 | + holder.setContainer(replace.apply(config)); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Adds the given object as a Jersey singleton component. |
| 33 | + * |
| 34 | + * @param component a Jersey singleton component |
| 35 | + */ |
| 36 | + public void register(Object component) { |
| 37 | + config.getSingletons().add(checkNotNull(component)); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Adds the given class as a Jersey component. <p/><b>N.B.:</b> This class must either have a |
| 42 | + * no-args constructor or use Jersey's built-in dependency injection. |
| 43 | + * |
| 44 | + * @param componentClass a Jersey component class |
| 45 | + */ |
| 46 | + public void register(Class<?> componentClass) { |
| 47 | + config.getClasses().add(checkNotNull(componentClass)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Adds array of package names which will be used to scan for components. Packages will be |
| 52 | + * scanned recursively, including all nested packages. |
| 53 | + * |
| 54 | + * @param packages array of package names |
| 55 | + */ |
| 56 | + public void packages(String... packages) { |
| 57 | + config.init(new PackageNamesScanner(checkNotNull(packages))); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Enables the Jersey feature with the given name. |
| 62 | + * |
| 63 | + * @param featureName the name of the feature to be enabled |
| 64 | + * @see com.sun.jersey.api.core.ResourceConfig |
| 65 | + */ |
| 66 | + public void enable(String featureName) { |
| 67 | + config.getFeatures().put(checkNotNull(featureName), Boolean.TRUE); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Disables the Jersey feature with the given name. |
| 72 | + * |
| 73 | + * @param featureName the name of the feature to be disabled |
| 74 | + * @see com.sun.jersey.api.core.ResourceConfig |
| 75 | + */ |
| 76 | + public void disable(String featureName) { |
| 77 | + config.getFeatures().put(checkNotNull(featureName), Boolean.FALSE); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Sets the given Jersey property. |
| 82 | + * |
| 83 | + * @param name the name of the Jersey property |
| 84 | + * @param value the value of the Jersey property |
| 85 | + * @see com.sun.jersey.api.core.ResourceConfig |
| 86 | + */ |
| 87 | + public void property(String name, @Nullable Object value) { |
| 88 | + config.getProperties().put(checkNotNull(name), value); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Gets the given Jersey property. |
| 93 | + * |
| 94 | + * @param name the name of the Jersey property |
| 95 | + * @see com.sun.jersey.api.core.ResourceConfig |
| 96 | + */ |
| 97 | + @SuppressWarnings("unchecked") |
| 98 | + public <T> T getProperty(String name) { |
| 99 | + return (T) config.getProperties().get(name); |
| 100 | + } |
| 101 | + |
| 102 | + public String getUrlPattern() { |
| 103 | + return config.getUrlPattern(); |
| 104 | + } |
| 105 | + |
| 106 | + public void setUrlPattern(String urlPattern) { |
| 107 | + config.setUrlPattern(urlPattern); |
| 108 | + } |
| 109 | + |
| 110 | + public ResourceConfig getResourceConfig() { |
| 111 | + return config; |
| 112 | + } |
| 113 | +} |
0 commit comments