PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/tasks/jira-connector/src/main/java/com/intellij/tasks/jira/JiraRepository.java

http://github.com/JetBrains/intellij-community
Java | 214 lines | 181 code | 25 blank | 8 comment | 19 complexity | 071e39bb49d267fa48313f59875b3166 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. package com.intellij.tasks.jira;
  2. import com.atlassian.connector.commons.jira.soap.axis.JiraSoapService;
  3. import com.atlassian.connector.commons.jira.soap.axis.JiraSoapServiceServiceLocator;
  4. import com.atlassian.theplugin.jira.api.JIRAIssueBean;
  5. import com.intellij.openapi.diagnostic.Logger;
  6. import com.intellij.tasks.Task;
  7. import com.intellij.tasks.TaskRepository;
  8. import com.intellij.tasks.TaskState;
  9. import com.intellij.tasks.impl.BaseRepositoryImpl;
  10. import com.intellij.util.Function;
  11. import com.intellij.util.containers.ContainerUtil;
  12. import com.intellij.util.net.HttpConfigurable;
  13. import com.intellij.util.xmlb.annotations.Tag;
  14. import org.apache.axis.AxisProperties;
  15. import org.apache.commons.httpclient.HttpClient;
  16. import org.apache.commons.httpclient.HttpStatus;
  17. import org.apache.commons.httpclient.methods.GetMethod;
  18. import org.apache.commons.httpclient.methods.PostMethod;
  19. import org.jdom.Element;
  20. import org.jdom.JDOMException;
  21. import org.jdom.input.SAXBuilder;
  22. import org.jetbrains.annotations.Nullable;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.net.URL;
  26. import java.rmi.RemoteException;
  27. import java.util.List;
  28. /**
  29. * @author Dmitry Avdeev
  30. */
  31. @Tag("JIRA")
  32. public class JiraRepository extends BaseRepositoryImpl {
  33. private final static Logger LOG = Logger.getInstance("#com.intellij.tasks.jira.JiraRepository");
  34. private boolean myJira4 = true;
  35. /**
  36. * for serialization
  37. */
  38. @SuppressWarnings({"UnusedDeclaration"})
  39. public JiraRepository() {
  40. super();
  41. }
  42. private JiraRepository(JiraRepository other) {
  43. super(other);
  44. }
  45. public JiraRepository(JiraRepositoryType type) {
  46. super(type);
  47. }
  48. public Task[] getIssues(@Nullable String request, int max, long since) throws Exception {
  49. return getIssues(max, login());
  50. }
  51. private Task[] getIssues(int max, HttpClient httpClient) throws IOException, JDOMException {
  52. StringBuilder url = new StringBuilder(getUrl());
  53. url.append("/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?");
  54. url.append("tempMax=").append(max);
  55. url.append("&assignee=").append(encodeUrl(getUsername()));
  56. // url.append("&resolution=-1");
  57. url.append("&reset=true");
  58. url.append("&sorter/field=").append("updated");
  59. url.append("&sorter/order=").append("DESC");
  60. url.append("&pager/start=").append(0);
  61. return processRSS(url.toString(), httpClient);
  62. }
  63. @Override
  64. public void setTaskState(Task task, TaskState state) throws Exception {
  65. }
  66. private Task[] processRSS(String url, HttpClient client) throws IOException, JDOMException {
  67. GetMethod method = new GetMethod(url);
  68. configureHttpMethod(method);
  69. client.executeMethod(method);
  70. int code = method.getStatusCode();
  71. if (code != HttpStatus.SC_OK) {
  72. throw new IOException(code == HttpStatus.SC_BAD_REQUEST ?
  73. method.getResponseBodyAsString() :
  74. ("HTTP " + code + " (" + HttpStatus.getStatusText(code) + ") " + method.getStatusText()));
  75. }
  76. InputStream stream = method.getResponseBodyAsStream();
  77. Element root = new SAXBuilder(false).build(stream).getRootElement();
  78. Element channel = root.getChild("channel");
  79. if (channel != null) {
  80. @SuppressWarnings({"unchecked"}) List<Element> children = channel.getChildren("item");
  81. LOG.info("JIRA: " + children.size() + " issues found");
  82. final List<Task> tasks = ContainerUtil.map(children, new Function<Element, Task>() {
  83. public Task fun(Element o) {
  84. return new JiraTask(new JIRAIssueBean(getUrl(), o, false)) {
  85. @Override
  86. public TaskRepository getRepository() {
  87. return JiraRepository.this;
  88. }
  89. };
  90. }
  91. });
  92. return tasks.toArray(new Task[tasks.size()]);
  93. }
  94. else {
  95. LOG.warn("JIRA channel not found");
  96. }
  97. return Task.EMPTY_ARRAY;
  98. }
  99. private HttpClient login() throws Exception {
  100. HttpClient client = getHttpClient();
  101. if (myJira4) {
  102. PostMethod postMethod = getLoginMethodFor4x();
  103. client.executeMethod(postMethod);
  104. if (checkLoginResult(postMethod)) {
  105. return client;
  106. }
  107. }
  108. // try 3.x protocol
  109. axisLogin();
  110. return client;
  111. }
  112. private void axisLogin() throws Exception {
  113. try {
  114. JiraSoapService soapService =
  115. new JiraSoapServiceServiceLocator().getJirasoapserviceV2(new URL(getUrl() + "/rpc/soap/jirasoapservice-v2"));
  116. if (isUseProxy()) {
  117. HttpConfigurable proxy = HttpConfigurable.getInstance();
  118. AxisProperties.setProperty("http.proxyHost", proxy.PROXY_HOST);
  119. AxisProperties.setProperty("http.proxyPort", String.valueOf(proxy.PROXY_PORT));
  120. if (proxy.PROXY_AUTHENTICATION) {
  121. AxisProperties.setProperty("http.proxyUser", String.valueOf(proxy.PROXY_LOGIN));
  122. AxisProperties.setProperty("http.proxyPassword", String.valueOf(proxy.getPlainProxyPassword()));
  123. }
  124. }
  125. soapService.login(getUsername(), getPassword());
  126. }
  127. catch (RemoteException e) {
  128. String message = e.toString();
  129. int i = message.indexOf(": ");
  130. if (i > 0) {
  131. message = message.substring(i + 2);
  132. }
  133. throw new Exception(message, e);
  134. }
  135. }
  136. private boolean checkLoginResult(PostMethod postMethod) throws IOException {
  137. int statusCode = postMethod.getStatusCode();
  138. if (statusCode == HttpStatus.SC_NOT_FOUND) {
  139. myJira4 = false;
  140. return false;
  141. }
  142. if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_MOVED_TEMPORARILY) {
  143. throw new IOException("Can't login: " + statusCode + " (" + HttpStatus.getStatusText(statusCode) + ")");
  144. }
  145. return true;
  146. }
  147. private PostMethod getLoginMethodFor4x() {
  148. String url = getUrl() + "/rest/gadget/1.0/login";
  149. PostMethod postMethod = new PostMethod(url);
  150. postMethod.addParameter("os_username", getUsername());
  151. postMethod.addParameter("os_password", getPassword());
  152. postMethod.addParameter("os_destination", "/success");
  153. configureHttpMethod(postMethod);
  154. return postMethod;
  155. }
  156. @Nullable
  157. @Override
  158. public CancellableConnection createCancellableConnection() {
  159. PostMethod method = getLoginMethodFor4x();
  160. return new HttpTestConnection<PostMethod>(method) {
  161. @Override
  162. public void doTest(PostMethod method) throws Exception {
  163. HttpClient client = getHttpClient();
  164. client.executeMethod(myMethod);
  165. if (!checkLoginResult(method)) {
  166. axisLogin();
  167. }
  168. getIssues(1, client);
  169. }
  170. };
  171. }
  172. public JiraRepository clone() {
  173. return new JiraRepository(this);
  174. }
  175. @Nullable
  176. public Task findTask(String id) {
  177. try {
  178. StringBuilder url = new StringBuilder(getUrl());
  179. url.append("/si/jira.issueviews:issue-xml/");
  180. url.append(id).append('/').append(id).append(".xml");
  181. Task[] tasks = processRSS(url.toString(), login());
  182. return tasks.length == 0 ? null : tasks[0];
  183. }
  184. catch (Exception e) {
  185. LOG.warn("Cannot get issue " + id + ": " + e.getMessage());
  186. return null;
  187. }
  188. }
  189. }