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

/components/camel-jira/src/main/java/org/apache/camel/component/jira/consumer/AbstractJIRAConsumer.java

https://gitlab.com/matticala/apache-camel
Java | 102 lines | 60 code | 22 blank | 20 comment | 8 complexity | 71c1f2d5b5f319c01adb0beb0112343d MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.jira.consumer;
  18. import java.net.URI;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import com.atlassian.jira.rest.client.JiraRestClient;
  22. import com.atlassian.jira.rest.client.SearchRestClient;
  23. import com.atlassian.jira.rest.client.domain.BasicIssue;
  24. import com.atlassian.jira.rest.client.domain.SearchResult;
  25. import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory;
  26. import org.apache.camel.Processor;
  27. import org.apache.camel.component.jira.JIRAEndpoint;
  28. import org.apache.camel.impl.ScheduledPollConsumer;
  29. import org.apache.camel.spi.Registry;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. public abstract class AbstractJIRAConsumer extends ScheduledPollConsumer {
  33. private static final transient Logger LOG = LoggerFactory.getLogger(AbstractJIRAConsumer.class);
  34. private final JIRAEndpoint endpoint;
  35. private final JiraRestClient client;
  36. public AbstractJIRAConsumer(JIRAEndpoint endpoint, Processor processor) {
  37. super(endpoint, processor);
  38. this.endpoint = endpoint;
  39. // support to set the delay from JIRA Endpoint
  40. setDelay(endpoint.getDelay());
  41. JerseyJiraRestClientFactory factory;
  42. Registry registry = endpoint.getCamelContext().getRegistry();
  43. Object target = registry.lookupByName("JerseyJiraRestClientFactory");
  44. if (target != null) {
  45. LOG.debug("JerseyJiraRestClientFactory found in registry " + target.getClass().getCanonicalName());
  46. factory = (JerseyJiraRestClientFactory) target;
  47. } else {
  48. factory = new JerseyJiraRestClientFactory();
  49. }
  50. final URI jiraServerUri = URI.create(endpoint.getServerUrl());
  51. client = factory.createWithBasicHttpAuthentication(jiraServerUri, endpoint.getUsername(),
  52. endpoint.getPassword());
  53. }
  54. protected List<BasicIssue> getIssues() {
  55. return getIssues(endpoint.getJql(), 0, 0, 500);
  56. }
  57. // Ignore maxResults if it's <= 0.
  58. protected List<BasicIssue> getIssues(String jql, int start, int maxResults, int maxPerQuery) {
  59. LOG.info("Indexing current JIRA issues...");
  60. List<BasicIssue> issues = new ArrayList<>();
  61. while (true) {
  62. SearchRestClient searchRestClient = client.getSearchClient();
  63. SearchResult searchResult = searchRestClient.searchJqlWithFullIssues(jql, maxPerQuery, start, null);
  64. for (BasicIssue issue : searchResult.getIssues()) {
  65. issues.add(issue);
  66. }
  67. // Note: #getTotal == the total # the query would return *without* pagination, effectively telling us
  68. // we've reached the end. Also exit early if we're limiting the # of results.
  69. if (start >= searchResult.getTotal() || (maxResults > 0 && issues.size() >= maxResults)) {
  70. break;
  71. }
  72. start += maxPerQuery;
  73. }
  74. return issues;
  75. }
  76. protected JiraRestClient client() {
  77. return client;
  78. }
  79. protected abstract int poll() throws Exception;
  80. }