PageRenderTime 61ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/automation-module/src/main/java/com/atlassian/plugin/automation/rest/RuleResource.java

https://bitbucket.org/atlassianlabs/automation
Java | 327 lines | 297 code | 25 blank | 5 comment | 22 complexity | 4823175c33afa1f4d4fa59b0d70b862b MD5 | raw file
  1. package com.atlassian.plugin.automation.rest;
  2. import com.atlassian.fugue.Either;
  3. import com.atlassian.plugin.automation.config.DefaultRule;
  4. import com.atlassian.plugin.automation.core.Action;
  5. import com.atlassian.plugin.automation.core.Rule;
  6. import com.atlassian.plugin.automation.core.Trigger;
  7. import com.atlassian.plugin.automation.core.action.ActionConfiguration;
  8. import com.atlassian.plugin.automation.core.trigger.TriggerConfiguration;
  9. import com.atlassian.plugin.automation.module.AutomationModuleManager;
  10. import com.atlassian.plugin.automation.service.RuleErrors;
  11. import com.atlassian.plugin.automation.service.RuleService;
  12. import com.atlassian.plugin.automation.status.RuleStatusService;
  13. import com.atlassian.plugin.automation.util.ErrorCollection;
  14. import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
  15. import com.atlassian.sal.api.user.UserManager;
  16. import com.atlassian.sal.api.websudo.WebSudoRequired;
  17. import com.google.common.base.Function;
  18. import com.google.common.collect.Iterables;
  19. import com.google.common.collect.Lists;
  20. import org.apache.log4j.Logger;
  21. import javax.annotation.Nullable;
  22. import javax.inject.Inject;
  23. import javax.servlet.http.HttpServletResponse;
  24. import javax.ws.rs.Consumes;
  25. import javax.ws.rs.DELETE;
  26. import javax.ws.rs.GET;
  27. import javax.ws.rs.POST;
  28. import javax.ws.rs.PUT;
  29. import javax.ws.rs.Path;
  30. import javax.ws.rs.PathParam;
  31. import javax.ws.rs.Produces;
  32. import javax.ws.rs.core.Context;
  33. import javax.ws.rs.core.MediaType;
  34. import javax.ws.rs.core.Response;
  35. import java.util.List;
  36. /**
  37. * Provides REST resource for configuring the rules
  38. */
  39. @Path ("rule")
  40. @Produces ({ MediaType.APPLICATION_JSON })
  41. @Consumes ({ MediaType.APPLICATION_JSON })
  42. @WebSudoRequired
  43. public class RuleResource
  44. {
  45. private static final Logger log = Logger.getLogger(RuleResource.class);
  46. private final RuleService ruleService;
  47. private final UserManager userManager;
  48. private final RuleStatusService ruleStatusService;
  49. private final AutomationModuleManager automationModuleManager;
  50. @Inject
  51. public RuleResource(
  52. @ComponentImport final UserManager userManager,
  53. final RuleService ruleService,
  54. final RuleStatusService ruleStatusService,
  55. final AutomationModuleManager automationModuleManager)
  56. {
  57. this.ruleService = ruleService;
  58. this.userManager = userManager;
  59. this.ruleStatusService = ruleStatusService;
  60. this.automationModuleManager = automationModuleManager;
  61. }
  62. @POST
  63. public Response createRule(final DefaultRule rule)
  64. {
  65. final Either<RuleErrors, RuleService.RuleValidationResult> validationResult =
  66. ruleService.validateAddRule(userManager.getRemoteUsername(), rule);
  67. return validationResult.fold(new HandleRuleErrorFunction(), new Function<RuleService.RuleValidationResult, Response>()
  68. {
  69. @Override
  70. public Response apply(@Nullable final RuleService.RuleValidationResult input)
  71. {
  72. if (input != null)
  73. {
  74. return Response.ok(convertRuleToView(ruleService.addRule(input))).build();
  75. }
  76. return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build();
  77. }
  78. });
  79. }
  80. @POST
  81. @Path ("validate/{step}")
  82. public Response validatePartialRule(final DefaultRule rule, @PathParam ("step") final int step)
  83. {
  84. final RuleErrors validationResult =
  85. ruleService.validatePartialRule(userManager.getRemoteUsername(), rule, step);
  86. if (validationResult.hasAnyErrors())
  87. {
  88. return new HandleRuleErrorFunction().apply(validationResult);
  89. }
  90. return Response.ok(convertRuleToView(rule)).build();
  91. }
  92. @PUT
  93. @Path ("{ruleId}")
  94. public Response updateRule(final DefaultRule rule)
  95. {
  96. final Either<RuleErrors, RuleService.RuleValidationResult> validationResult =
  97. ruleService.validateUpdateRule(userManager.getRemoteUsername(), rule);
  98. return validationResult.fold(new HandleRuleErrorFunction(), new Function<RuleService.RuleValidationResult, Response>()
  99. {
  100. @Override
  101. public Response apply(@Nullable final RuleService.RuleValidationResult input)
  102. {
  103. if (input != null)
  104. {
  105. return Response.ok(convertRuleToView(ruleService.updateRule(input))).build();
  106. }
  107. return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build();
  108. }
  109. });
  110. }
  111. @GET
  112. @Path ("{ruleId}")
  113. public Response getRule(@PathParam ("ruleId") final int ruleId)
  114. {
  115. Either<ErrorCollection, Rule> result = ruleService.getRule(userManager.getRemoteUsername(), ruleId);
  116. return result.fold(new HandleErrorFunction(), new Function<Rule, Response>()
  117. {
  118. @Override
  119. public Response apply(@Nullable final Rule input)
  120. {
  121. if (input != null)
  122. {
  123. return Response.ok(convertRuleToView(input)).build();
  124. }
  125. return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build();
  126. }
  127. });
  128. }
  129. @DELETE
  130. @Path ("{ruleId}")
  131. public Response deleteRule(@PathParam ("ruleId") final int ruleId)
  132. {
  133. Either<ErrorCollection, RuleService.DeleteRuleValidationResult> result = ruleService.validateDeleteRule(userManager.getRemoteUsername(), ruleId);
  134. return result.fold(new HandleErrorFunction(), new Function<RuleService.DeleteRuleValidationResult, Response>()
  135. {
  136. @Override
  137. public Response apply(@Nullable RuleService.DeleteRuleValidationResult input)
  138. {
  139. if (input != null)
  140. {
  141. ruleService.deleteRule(input);
  142. return Response.ok().build();
  143. }
  144. return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build();
  145. }
  146. }
  147. );
  148. }
  149. @PUT
  150. @Path ("{ruleId}/enable")
  151. public Response enableRule(@PathParam ("ruleId") final int ruleId)
  152. {
  153. Either<ErrorCollection, RuleService.UpdateRuleStatusValidationResult> result = ruleService.validateUpdateRuleStatus(userManager.getRemoteUsername(), ruleId);
  154. return result.fold(new HandleErrorFunction(), new Function<RuleService.UpdateRuleStatusValidationResult, Response>()
  155. {
  156. @Override
  157. public Response apply(@Nullable RuleService.UpdateRuleStatusValidationResult input)
  158. {
  159. if (input != null)
  160. {
  161. final Rule rule = ruleService.updateRuleStatus(input, true);
  162. return Response.ok(convertRuleToView(rule)).build();
  163. }
  164. return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build();
  165. }
  166. }
  167. );
  168. }
  169. @PUT
  170. @Path ("{ruleId}/disable")
  171. public Response disableRule(@PathParam ("ruleId") final int ruleId)
  172. {
  173. Either<ErrorCollection, RuleService.UpdateRuleStatusValidationResult> result = ruleService.validateUpdateRuleStatus(userManager.getRemoteUsername(), ruleId);
  174. return result.fold(new HandleErrorFunction(), new Function<RuleService.UpdateRuleStatusValidationResult, Response>()
  175. {
  176. @Override
  177. public Response apply(@Nullable RuleService.UpdateRuleStatusValidationResult input)
  178. {
  179. if (input != null)
  180. {
  181. final Rule rule = ruleService.updateRuleStatus(input, false);
  182. return Response.ok(convertRuleToView(rule)).build();
  183. }
  184. return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build();
  185. }
  186. }
  187. );
  188. }
  189. @GET
  190. public Response getAllRules()
  191. {
  192. Either<ErrorCollection, ? extends Iterable<Rule>> result = ruleService.getRules(userManager.getRemoteUsername());
  193. return result.fold(new HandleErrorFunction(), new Function<Iterable<Rule>, Response>()
  194. {
  195. @Override
  196. public Response apply(final Iterable<Rule> input)
  197. {
  198. final Iterable<RuleView> ruleViews = Iterables.transform(input, new Function<Rule, RuleView>()
  199. {
  200. @Override
  201. public RuleView apply(Rule input)
  202. {
  203. return convertRuleToView(input);
  204. }
  205. });
  206. return Response.ok(Iterables.toArray(ruleViews, RuleView.class)).build();
  207. }
  208. });
  209. }
  210. @GET
  211. @Path("export/{ruleId}")
  212. public Response exportRule(@PathParam("ruleId") final int ruleId, @Context final HttpServletResponse response)
  213. {
  214. Either<ErrorCollection, Rule> result = ruleService.getRule(userManager.getRemoteUsername(), ruleId);
  215. return result.fold(new HandleErrorFunction(), new Function<Rule, Response>()
  216. {
  217. @Override
  218. public Response apply(@Nullable final Rule input)
  219. {
  220. if (input != null)
  221. {
  222. DefaultRule rule = ((DefaultRule) input).unsetAllIds();
  223. return Response.ok(rule).
  224. header("Content-Description", "File Transfer").
  225. header("Content-Disposition", "attachment; filename=" + rule.getName()).
  226. build();
  227. }
  228. return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build();
  229. }
  230. });
  231. }
  232. @POST
  233. @Path("import")
  234. public Response importRule(final DefaultRule rule)
  235. {
  236. rule.unsetAllIds();
  237. final Either<RuleErrors, RuleService.RuleValidationResult> validationResult =
  238. ruleService.validateAddRule(userManager.getRemoteUsername(), rule);
  239. return validationResult.fold(new HandleRuleErrorFunction(), new Function<RuleService.RuleValidationResult, Response>()
  240. {
  241. @Override
  242. public Response apply(@Nullable final RuleService.RuleValidationResult input)
  243. {
  244. if (input != null)
  245. {
  246. return Response.ok(convertRuleToView(ruleService.addRule(input))).build();
  247. }
  248. return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND).build();
  249. }
  250. });
  251. }
  252. private RuleView convertRuleToView(final Rule input)
  253. {
  254. final String triggerViewHtml = renderTriggerView(input.getTriggerConfiguration(), input.getActor());
  255. final ViewContainer triggerView = new ViewContainer(input.getTriggerConfiguration().getId(), input.getTriggerConfiguration().getModuleKey(), triggerViewHtml);
  256. final List<ViewContainer> actionViews = Lists.newArrayList();
  257. for (final ActionConfiguration actionConfiguration : input.getActionsConfiguration())
  258. {
  259. actionViews.add(new ViewContainer(actionConfiguration.getId(), actionConfiguration.getModuleKey(), renderActionView(actionConfiguration, input.getActor())));
  260. }
  261. final RuleWithStatusWrapper ruleWithStatus = new RuleWithStatusWrapper(input, ruleStatusService.getRuleStatus(input.getId()));
  262. return new RuleView(ruleWithStatus, triggerView, actionViews);
  263. }
  264. private String renderTriggerView(final TriggerConfiguration triggerConfiguration, final String actor)
  265. {
  266. try
  267. {
  268. final Trigger trigger = automationModuleManager.getTrigger(triggerConfiguration.getModuleKey());
  269. if (trigger == null)
  270. {
  271. return null;
  272. }
  273. // There be dragons - if NPE or anything happens in any plugin, we still want to be able to render something
  274. return trigger.getViewTemplate(triggerConfiguration, actor);
  275. }
  276. catch (Exception e)
  277. {
  278. final String message = "Rendering the view template threw an exception. Trigger: " + triggerConfiguration.getModuleKey();
  279. log.error(message, e);
  280. return message;
  281. }
  282. }
  283. private String renderActionView(final ActionConfiguration actionConfiguration, final String actor)
  284. {
  285. try
  286. {
  287. final Action action = automationModuleManager.getAction(actionConfiguration.getModuleKey());
  288. if (action == null)
  289. {
  290. return null;
  291. }
  292. // There be dragons - if NPE or anything happens in any plugin, we still want to be able to render something
  293. return action.getViewTemplate(actionConfiguration, actor);
  294. }
  295. catch (Exception e)
  296. {
  297. final String message = "Rendering the view template threw an exception. Action: " + actionConfiguration.getModuleKey();
  298. log.error(message, e);
  299. return message;
  300. }
  301. }
  302. }