PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/content/server/framework/atlassian-sdk/inclusionslibrary.snippet.md

https://bitbucket.org/zchristmas/atlassian-sdk-docs
Markdown | 137 lines | 130 code | 7 blank | 0 comment | 0 complexity | e42a6e30a967e8c3e12b523084cf99bd MD5 | raw file
Possible License(s): LGPL-2.0
  1. ---
  2. aliases:
  3. - /server/framework/atlassian-sdk/-inclusionslibrary-5668913.html
  4. - /server/framework/atlassian-sdk/-inclusionslibrary-5668913.md
  5. category: devguide
  6. confluence_id: 5668913
  7. dac_edit_link: https://developer.atlassian.com/pages/editpage.action?cjm=wozere&pageId=5668913
  8. dac_view_link: https://developer.atlassian.com/pages/viewpage.action?cjm=wozere&pageId=5668913
  9. date: '2017-12-08'
  10. legacy_title: _InclusionsLibrary
  11. platform: server
  12. product: atlassian-sdk
  13. subcategory: other
  14. title: _InclusionsLibrary
  15. ---
  16. # \_InclusionsLibrary
  17. The children of this page contain information which is **included in other pages**, both in the DOCS space and **in other spaces too**. This is a library of re-usable information chunks.
  18. If you want to change any of these pages, be aware that:
  19. - Changing page names is problematic -- you will need to change all the `include` and `excerpt-include` macros manually.
  20. - The content is used in many places -- make sure your change is generic enough to fit the contexts in which the pages are used.
  21. ## Snippet Plugin Test
  22. ``` java
  23. 01. package com.example.plugins.tutorial.jira.mailhandlerdemo;
  24. 02.
  25. 03. import com.atlassian.crowd.embedded.api.User;
  26. 04. import com.atlassian.jira.issue.Issue;
  27. 05. import com.atlassian.jira.service.util.handler.MessageHandler;
  28. 06. import com.atlassian.jira.service.util.handler.MessageHandlerContext;
  29. 07. import com.atlassian.jira.service.util.handler.MessageHandlerErrorCollector;
  30. 08. import com.atlassian.jira.service.util.handler.MessageUserProcessor;
  31. 09. import com.atlassian.mail.MailUtils;
  32. 10. import org.apache.commons.lang.StringUtils;
  33. 11.
  34. 12. import java.util.Map;
  35. 13. import javax.mail.Message;
  36. 14. import javax.mail.MessagingException;
  37. 15.
  38. 16. public class DemoHandler implements MessageHandler {
  39. 17. private String issueKey;
  40. 18. private final IssueKeyValidator issueKeyValidator;
  41. 19. private final MessageUserProcessor messageUserProcessor;
  42. 20. public static final String KEY_ISSUE_KEY = "issueKey";
  43. 21.
  44. 22. // we can use dependency injection here too!
  45. 23. public DemoHandler(MessageUserProcessor messageUserProcessor, IssueKeyValidator issueKeyValidator) {
  46. 24. this.messageUserProcessor = messageUserProcessor;
  47. 25. this.issueKeyValidator = issueKeyValidator;
  48. 26. }
  49. 27.
  50. 28. @Override
  51. 29. public void init(Map<String, String> params, MessageHandlerErrorCollector monitor) {
  52. 30. // getting here issue key configured by the user
  53. 31. issueKey = params.get(KEY_ISSUE_KEY);
  54. 32. if (StringUtils.isBlank(issueKey)) {
  55. 33. // this message will be either logged or displayed to the user (if the handler is tested from web UI)
  56. 34. monitor.error("Issue key has not been specified ('" + KEY_ISSUE_KEY + "' parameter). This handler will not work correctly.");
  57. 35. }
  58. 36. issueKeyValidator.validateIssue(issueKey, monitor);
  59. 37. }
  60. 38.
  61. 39. @Override
  62. 40. public boolean handleMessage(Message message, MessageHandlerContext context) throws MessagingException {
  63. 41. // let's again validate the issue key - meanwhile issue could have been deleted, closed, etc..
  64. 42. final Issue issue = issueKeyValidator.validateIssue(issueKey, context.getMonitor());
  65. 43. if (issue == null) {
  66. 44. return false; // returning false means that we were unable to handle this message. It may be either
  67. 45. // forwarded to specified address or left in the mail queue (if forwarding not enabled)
  68. 46. }
  69. 47. // this is a small util method JIRA API provides for us, let's use it.
  70. 48. final User sender = messageUserProcessor.getAuthorFromSender(message);
  71. 49. if (sender == null) {
  72. 50. context.getMonitor().error("Message sender(s) '" + StringUtils.join(MailUtils.getSenders(message), ",")
  73. 51. + "' do not have corresponding users in JIRA. Message will be ignored");
  74. 52. return false;
  75. 53. }
  76. 54. final String body = MailUtils.getBody(message);
  77. 55. final StringBuilder commentBody = new StringBuilder(message.getSubject());
  78. 56. if (body != null) {
  79. 57. commentBody.append("\n").append(StringUtils.abbreviate(body, 100000)); // let trim too long bodies
  80. 58. }
  81. 59. // thanks to using passed context we don't need to worry about normal run vs. test run - our call
  82. 60. // will be dispatched accordingly
  83. 61. context.createComment(issue, sender, commentBody.toString(), false);
  84. 62. return true; // returning true means that we have handled the message successfully. It means it will be deleted next.
  85. 63. }
  86. 64. }
  87. ```
  88. ## Children
  89. - [\_About Plugin Framework 2 Documentation](/server/framework/atlassian-sdk/about-plugin-framework-2-documentation.snippet)
  90. - [\_Bamboo Plugin Tutorials](/server/framework/atlassian-sdk/bamboo-plugin-tutorials.snippet)
  91. - [\_Components of Plugin Development Platform](/server/framework/atlassian-sdk/components-of-plugin-development-platform.snippet)
  92. - [\_Confluence Plugin Tutorials](/server/framework/atlassian-sdk/confluence-plugin-tutorials.snippet)
  93. - [\_Cross-Product Plugin Tutorials](/server/framework/atlassian-sdk/cross-product-plugin-tutorials.snippet)
  94. - [\_Development Hubs](/server/framework/atlassian-sdk/development-hubs.snippet)
  95. - [\_FastDevWorkFlow](/server/framework/atlassian-sdk/fastdevworkflow.snippet)
  96. - [\_FECRU Plugin Tutorials](/server/framework/atlassian-sdk/fecru-plugin-tutorials.snippet)
  97. - [\_Getting Help on Plugin SDK](/server/framework/atlassian-sdk/getting-help-on-plugin-sdk.snippet)
  98. - [\_Images for Release Notes](/server/framework/atlassian-sdk/images-for-release-notes.snippet)
  99. - [\_JIRA Plugin Tutorials](/server/framework/atlassian-sdk/jira-plugin-tutorials.snippet)
  100. - [\_LocalM2PluginSettings](/server/framework/atlassian-sdk/localm2pluginsettings.snippet)
  101. - [\_MailingLists](/server/framework/atlassian-sdk/mailinglists.snippet)
  102. - [\_Note about Maven Bundled with SDK](/server/framework/atlassian-sdk/note-about-maven-bundled-with-sdk.snippet)
  103. - [\_Package Name](/server/framework/atlassian-sdk/package-name.snippet)
  104. - [\_PluginFramework\_InclusionsLibrary](/server/framework/atlassian-sdk/pluginframework-inclusionslibrary.snippet)
  105. - [\_PluginPlatformDiagram](/server/framework/atlassian-sdk/pluginplatformdiagram.snippet)
  106. - [\_Plugin Platform Version](/server/framework/atlassian-sdk/plugin-platform-version.snippet)
  107. - [\_PluginSDK\_StepByStep\_Diagram](/server/framework/atlassian-sdk/pluginsdk-stepbystep-diagram.snippet)
  108. - [\_Plugin SDK Download Latest](/server/framework/atlassian-sdk/plugin-sdk-download-latest.snippet)
  109. - [\_PluginSDKQuickStart](/server/framework/atlassian-sdk/pluginsdkquickstart.snippet)
  110. - [\_Plugin SDK Supported Applications and Default Ports](/server/framework/atlassian-sdk/plugin-sdk-supported-applications-and-default-ports.snippet)
  111. - [\_Plugin SDK Version](/server/framework/atlassian-sdk/plugin-sdk-version.snippet)
  112. - [\_Plugin Tutorial Step - Build and Test](/server/framework/atlassian-sdk/plugin-tutorial-step-build-and-test.snippet)
  113. - [\_Plugin Tutorial Step - Create Plugin](/server/framework/atlassian-sdk/plugin-tutorial-step-create-plugin.snippet)
  114. - [\_RABDescription](/server/framework/atlassian-sdk/rabdescription.snippet)
  115. - [\_\_newreleaseSharedAccessLayer](/server/framework/atlassian-sdk/newreleasesharedaccesslayer.snippet)
  116. - [\_Images](/server/framework/atlassian-sdk/images.snippet)
  117. - [\_SAL Overview](/server/framework/atlassian-sdk/sal-overview.snippet)
  118. - [\_Version Compatibility for Shared Access Layer](/server/framework/atlassian-sdk/version-compatibility-for-shared-access-layer.snippet)
  119. - [\_RABQuickStart](/server/framework/atlassian-sdk/rabquickstart.snippet)
  120. - [\_REST Plugin Module Overview](/server/framework/atlassian-sdk/rest-plugin-module-overview.snippet)
  121. - [\_SDK Known Issues](/server/framework/atlassian-sdk/sdk-known-issues.snippet)
  122. - [\_SDK Linux](/server/framework/atlassian-sdk/sdk-linux.snippet)
  123. - [\_SDK Version](/server/framework/atlassian-sdk/sdk-version.snippet)
  124. - [\_SDK Windows](/server/framework/atlassian-sdk/sdk-windows.snippet)
  125. - [\_TempPagetree](/server/framework/atlassian-sdk/temppagetree.snippet)
  126. - [\_Version Compatibility for Plugin Development Platform](/server/framework/atlassian-sdk/version-compatibility-for-plugin-development-platform.snippet)
  127. - [\_Version Compatibility for REST Plugin](/server/framework/atlassian-sdk/version-compatibility-for-rest-plugin.snippet)
  128. - [REST\_QuickStart\_References](/server/framework/atlassian-sdk/rest-quickstart-references.snippet)
  129. - [\_Module definition example](/server/framework/atlassian-sdk/module-definition-example.snippet)
  130. - [\_Copyright Notice](/server/framework/atlassian-sdk/copyright-notice.snippet)