1 package com.atlassian.plugins.rest.module;
2
3 import java.util.Arrays;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.Map;
7 import java.util.Set;
8
9 import javax.ws.rs.ext.MessageBodyReader;
10
11 import com.atlassian.plugins.rest.module.filter.CorsAcceptOptionsPreflightFilter;
12 import org.osgi.framework.Bundle;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.ServiceReference;
15
16 import com.atlassian.plugin.AutowireCapablePlugin;
17 import com.atlassian.plugins.rest.common.expand.SelfExpandingExpander;
18 import com.atlassian.plugins.rest.common.expand.interceptor.ExpandInterceptor;
19 import com.atlassian.plugins.rest.common.expand.resolver.ChainingEntityExpanderResolver;
20 import com.atlassian.plugins.rest.common.expand.resolver.CollectionEntityExpanderResolver;
21 import com.atlassian.plugins.rest.common.expand.resolver.EntityExpanderResolver;
22 import com.atlassian.plugins.rest.common.expand.resolver.ExpandConstraintEntityExpanderResolver;
23 import com.atlassian.plugins.rest.common.expand.resolver.IdentityEntityExpanderResolver;
24 import com.atlassian.plugins.rest.common.expand.resolver.ListWrapperEntityExpanderResolver;
25 import com.atlassian.plugins.rest.common.filter.ExtensionJerseyFilter;
26 import com.atlassian.plugins.rest.common.interceptor.impl.InterceptorChainBuilderProvider;
27 import com.atlassian.plugins.rest.module.expand.resolver.PluginEntityExpanderResolver;
28 import com.atlassian.plugins.rest.module.filter.AcceptHeaderJerseyMvcFilter;
29 import com.atlassian.plugins.rest.module.json.JsonWithPaddingResponseFilter;
30 import com.google.common.collect.Lists;
31 import com.sun.jersey.api.core.ResourceConfig;
32 import com.sun.jersey.spi.container.ResourceFilterFactory;
33 import com.sun.jersey.spi.container.ResourceMethodDispatchProvider;
34 import com.sun.jersey.spi.inject.InjectableProvider;
35 import com.sun.jersey.spi.template.TemplateProcessor;
36
37 public class ResourceConfigManager
38 {
39 private final OsgiServiceAccessor<ResourceFilterFactory> resourceFilterFactories;
40 private final OsgiServiceAccessor<InjectableProvider> injectableProviders;
41 private final OsgiServiceAccessor<TemplateProcessor> templateProcessors;
42 private final OsgiServiceAccessor<MessageBodyReader> messageBodyReaders;
43 private final OsgiServiceAccessor<ResourceMethodDispatchProvider> dispatchProviders;
44 private final AutowireCapablePlugin plugin;
45 private final Bundle bundle;
46
47 public ResourceConfigManager(AutowireCapablePlugin plugin, Bundle bundle)
48 {
49 this.plugin = plugin;
50 this.bundle = bundle;
51 BundleContext bundleContext = bundle.getBundleContext();
52
53
54 resourceFilterFactories = new OsgiServiceAccessor<ResourceFilterFactory>(ResourceFilterFactory.class, bundleContext, new OsgiFactory<ResourceFilterFactory>()
55 {
56 public ResourceFilterFactory getInstance(BundleContext bundleContext, ServiceReference serviceReference)
57 {
58 return new OsgiServiceReferenceResourceFilterFactory(bundleContext, serviceReference);
59 }
60 });
61
62
63 injectableProviders = new OsgiServiceAccessor<InjectableProvider>(InjectableProvider.class, bundleContext, new OsgiFactory<InjectableProvider>()
64 {
65 public InjectableProvider getInstance(BundleContext bundleContext, ServiceReference serviceReference)
66 {
67 return (InjectableProvider) bundleContext.getService(serviceReference);
68 }
69 });
70
71 templateProcessors = new OsgiServiceAccessor<TemplateProcessor>(TemplateProcessor.class, bundleContext, new OsgiFactory<TemplateProcessor>()
72 {
73 public TemplateProcessor getInstance(BundleContext bundleContext, ServiceReference serviceReference)
74 {
75 return (TemplateProcessor) bundleContext.getService(serviceReference);
76 }
77 });
78
79 messageBodyReaders = new OsgiServiceAccessor<MessageBodyReader>(MessageBodyReader.class, bundleContext, new OsgiFactory<MessageBodyReader>()
80 {
81 public MessageBodyReader getInstance(final BundleContext bundleContext, final ServiceReference serviceReference)
82 {
83 return (MessageBodyReader) bundleContext.getService(serviceReference);
84 }
85 });
86
87 dispatchProviders = new OsgiServiceAccessor<ResourceMethodDispatchProvider>(ResourceMethodDispatchProvider.class, bundleContext, new OsgiFactory<ResourceMethodDispatchProvider>()
88 {
89 public ResourceMethodDispatchProvider getInstance(final BundleContext bundleContext, final ServiceReference serviceReference)
90 {
91 return (ResourceMethodDispatchProvider) bundleContext.getService(serviceReference);
92 }
93 });
94 }
95
96 public ResourceConfig createResourceConfig(Map<String, Object> props, String[] excludes, Set<String> packages)
97 {
98
99 final Collection<String> excludesCollection = excludes != null ? Arrays.asList(excludes) : Collections.<String>emptyList();
100
101 final EntityExpanderResolver expanderResolver = new ChainingEntityExpanderResolver(Lists.<EntityExpanderResolver>newArrayList(
102 new PluginEntityExpanderResolver(plugin),
103 new CollectionEntityExpanderResolver(),
104 new ListWrapperEntityExpanderResolver(),
105 new ExpandConstraintEntityExpanderResolver(),
106 new SelfExpandingExpander.Resolver(),
107 new IdentityEntityExpanderResolver()
108 ));
109
110 final Collection<Object> providers = Lists.newLinkedList();
111 providers.addAll(injectableProviders.get());
112 providers.addAll(templateProcessors.get());
113 providers.addAll(messageBodyReaders.get());
114 providers.addAll(dispatchProviders.get());
115
116 providers.add(new InterceptorChainBuilderProvider(plugin, new ExpandInterceptor(expanderResolver)));
117
118 return new OsgiResourceConfig(bundle, packages,
119 Lists.newArrayList(new ExtensionJerseyFilter(excludesCollection), new AcceptHeaderJerseyMvcFilter(),
120 new CorsAcceptOptionsPreflightFilter()),
121 Lists.newArrayList(new JsonWithPaddingResponseFilter()),
122 resourceFilterFactories.get(),
123 providers);
124 }
125
126 public void destroy()
127 {
128 resourceFilterFactories.release();
129 injectableProviders.release();
130 templateProcessors.release();
131 messageBodyReaders.release();
132 dispatchProviders.release();
133 }
134
135
136 }