1   package com.atlassian.plugins.codegen.modules.common;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.List;
6   
7   import com.atlassian.plugins.codegen.modules.BasicClassModuleProperties;
8   
9   /**
10   * @since 3.6
11   */
12  public class RESTProperties extends BasicClassModuleProperties
13  {
14  
15      public static final String PATH = "PATH";
16      public static final String VERSION = "VERSION";
17      public static final String PACKAGES_TO_SCAN = "PACKAGES_TO_SCAN";
18      public static final String DISPATCHERS = "DISPATCHERS";
19  
20      public static final List<String> ALLOWED_DISPATCHERS = initDispatchers();
21  
22      private static List<String> initDispatchers()
23      {
24          List<String> dispatchers = new ArrayList<String>(4);
25          dispatchers.add("REQUEST");
26          dispatchers.add("INCLUDE");
27          dispatchers.add("FORWARD");
28          dispatchers.add("ERROR");
29  
30          return Collections.unmodifiableList(dispatchers);
31      }
32  
33      public RESTProperties()
34      {
35          this("MyRESTResource");
36      }
37  
38      public RESTProperties(String fqClassname)
39      {
40          super(fqClassname);
41  
42          put(DISPATCHERS, new ArrayList<String>());
43          put(PACKAGES_TO_SCAN, new ArrayList<String>());
44  
45          setPath("/" + getProperty(CLASSNAME).toLowerCase());
46          setVersion("1.0");
47      }
48  
49      public void setPath(String path)
50      {
51          if (!path.startsWith("/"))
52          {
53              path = "/" + path;
54          }
55  
56          setProperty(PATH, path);
57      }
58  
59      public String getPath()
60      {
61          return getProperty(PATH);
62      }
63  
64      public void setVersion(String version)
65      {
66          setProperty(VERSION, version);
67      }
68  
69      public String getVersion()
70      {
71          return getProperty(VERSION);
72      }
73  
74      public void setDispatchers(List<String> dispatchers)
75      {
76          put(DISPATCHERS, dispatchers);
77      }
78  
79      @SuppressWarnings(value = "unchecked")
80      public void addDispatcher(String dispatcher)
81      {
82          List<String> dispatchers = (List<String>) get(DISPATCHERS);
83          if (dispatchers == null)
84          {
85              dispatchers = new ArrayList<String>();
86              setDispatchers(dispatchers);
87          }
88  
89          dispatchers.add(dispatcher);
90      }
91  
92      public void setPackagesToScan(List<String> packages)
93      {
94          put(PACKAGES_TO_SCAN, packages);
95      }
96  
97      @SuppressWarnings(value = "unchecked")
98      public void addPackageToScan(String packageToScan)
99      {
100         List<String> packages = (List<String>) get(PACKAGES_TO_SCAN);
101         if (packages == null)
102         {
103             packages = new ArrayList<String>();
104             setPackagesToScan(packages);
105         }
106 
107         packages.add(packageToScan);
108     }
109 
110     public List<String> getPackagesToScan()
111     {
112         return (List<String>) get(PACKAGES_TO_SCAN);
113     }
114 
115     public List<String> getDispatchers()
116     {
117         return (List<String>) get(DISPATCHERS);
118     }
119 
120     public List<String> allowedDispatchers()
121     {
122         return ALLOWED_DISPATCHERS;
123     }
124 }