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