View Javadoc
1   package com.atlassian.plugin.spring.scanner.test.servlet;
2   
3   import com.atlassian.plugin.PluginAccessor;
4   import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
5   import com.atlassian.plugin.spring.scanner.test.moduletype.BasicModuleDescriptor;
6   
7   import javax.inject.Inject;
8   import javax.servlet.ServletException;
9   import javax.servlet.http.HttpServlet;
10  import javax.servlet.http.HttpServletRequest;
11  import javax.servlet.http.HttpServletResponse;
12  import java.io.IOException;
13  import java.util.List;
14  
15  /**
16   * Retrieves and calls {@link com.atlassian.plugin.spring.scanner.test.moduletype.BasicModuleDescriptor} so test can check it's working.
17   */
18  public class CallCustomModuleTypeServlet extends HttpServlet {
19  
20      private final PluginAccessor pluginAccessor;
21  
22      @Inject
23      public CallCustomModuleTypeServlet(@ComponentImport PluginAccessor pluginAccessor) {
24          this.pluginAccessor = pluginAccessor;
25      }
26  
27      @Override
28      protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
29              throws IOException {
30          final List<BasicModuleDescriptor> enabledModuleDescriptorsByClass =
31                  pluginAccessor.getEnabledModuleDescriptorsByClass(BasicModuleDescriptor.class);
32  
33          if (enabledModuleDescriptorsByClass.size() != 1) {
34              response.setStatus(500);
35              throw new IllegalStateException("Test failed: expected exactly one BasicModuleDescriptor to be present "
36                      + "but got: "
37                      + enabledModuleDescriptorsByClass
38                      + ". This probably means the @ModuleType annotation hasn't been processed correctly.");
39          }
40  
41          response.setStatus(200);
42          response.setContentType("text/plain");
43  
44          // Should call BasicModuleDescriptor.getModule(), which returns a string for the test to validate
45          response.getWriter().write(enabledModuleDescriptorsByClass.get(0).getModule());
46  
47          response.flushBuffer();
48      }
49  }