PageRenderTime 94ms CodeModel.GetById 43ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/jenkins/ci/plugins/jobimport/RemoteJobUtils.java

https://github.com/rseguy/job-import-plugin
Java | 194 lines | 117 code | 45 blank | 32 comment | 18 complexity | 0b6761c70a36862d1e381194ab441d60 MD5 | raw file
  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2011, Jesse Farinacci
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. package org.jenkins.ci.plugins.jobimport;
  25. import java.io.IOException;
  26. import java.util.SortedSet;
  27. import java.util.TreeSet;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import javax.xml.parsers.ParserConfigurationException;
  31. import javax.xml.xpath.XPathExpression;
  32. import javax.xml.xpath.XPathExpressionException;
  33. import org.apache.commons.lang.StringUtils;
  34. import org.w3c.dom.Document;
  35. import org.xml.sax.SAXException;
  36. /**
  37. * @author <a href="mailto:jieryn@gmail.com">Jesse Farinacci</a>
  38. * @since 1.0
  39. */
  40. public final class RemoteJobUtils {
  41. private static final Logger LOG = Logger.getLogger(RemoteJobUtils.class.getName());
  42. private static final XPathExpression XPATH_EXPRESSION_FREE_STYLE_PROJECT_DESCRIPTION;
  43. private static final XPathExpression XPATH_EXPRESSION_FREE_STYLE_PROJECT_NAME;
  44. private static final XPathExpression XPATH_EXPRESSION_FREE_STYLE_PROJECT_URL;
  45. private static final XPathExpression XPATH_EXPRESSION_HUDSON_JOB_URL;
  46. private static final XPathExpression XPATH_EXPRESSION_LIST_VIEW_JOB_URL;
  47. private static final XPathExpression XPATH_EXPRESSION_MAVEN_MODULE_SET_DESCRIPTION;
  48. private static final XPathExpression XPATH_EXPRESSION_MAVEN_MODULE_SET_NAME;
  49. private static final XPathExpression XPATH_EXPRESSION_MAVEN_MODULE_SET_URL;
  50. static {
  51. try {
  52. XPATH_EXPRESSION_FREE_STYLE_PROJECT_DESCRIPTION = XPathUtils.compile("/freeStyleProject/description");
  53. XPATH_EXPRESSION_FREE_STYLE_PROJECT_NAME = XPathUtils.compile("/freeStyleProject/name");
  54. XPATH_EXPRESSION_FREE_STYLE_PROJECT_URL = XPathUtils.compile("/freeStyleProject/url");
  55. XPATH_EXPRESSION_HUDSON_JOB_URL = XPathUtils.compile("/hudson/job/url");
  56. XPATH_EXPRESSION_LIST_VIEW_JOB_URL = XPathUtils.compile("/listView/job/url");
  57. XPATH_EXPRESSION_MAVEN_MODULE_SET_DESCRIPTION = XPathUtils.compile("/mavenModuleSet/description");
  58. XPATH_EXPRESSION_MAVEN_MODULE_SET_NAME = XPathUtils.compile("/mavenModuleSet/name");
  59. XPATH_EXPRESSION_MAVEN_MODULE_SET_URL = XPathUtils.compile("/mavenModuleSet/url");
  60. }
  61. catch (final XPathExpressionException e) {
  62. LOG.log(Level.WARNING, e.getMessage(), e);
  63. throw new IllegalStateException(e);
  64. }
  65. }
  66. protected static RemoteJob fromFreeStyleProjectXml(final String xml) throws SAXException, IOException,
  67. ParserConfigurationException, XPathExpressionException {
  68. if (StringUtils.isEmpty(xml)) {
  69. return null;
  70. }
  71. if (!xml.startsWith("<freeStyleProject>")) {
  72. return null;
  73. }
  74. final Document document = XPathUtils.parse(xml);
  75. return new RemoteJob(XPathUtils.evaluateStringXPath(document, XPATH_EXPRESSION_FREE_STYLE_PROJECT_NAME),
  76. XPathUtils.evaluateStringXPath(document, XPATH_EXPRESSION_FREE_STYLE_PROJECT_URL),
  77. XPathUtils.evaluateStringXPath(document, XPATH_EXPRESSION_FREE_STYLE_PROJECT_DESCRIPTION));
  78. }
  79. protected static SortedSet<RemoteJob> fromHudsonXml(final String xml) throws SAXException, IOException,
  80. ParserConfigurationException, XPathExpressionException {
  81. final SortedSet<RemoteJob> remoteJobs = new TreeSet<RemoteJob>();
  82. if (StringUtils.isEmpty(xml)) {
  83. return remoteJobs;
  84. }
  85. if (!xml.startsWith("<hudson>")) {
  86. return remoteJobs;
  87. }
  88. final Document document = XPathUtils.parse(xml);
  89. for (final String jobUrl : XPathUtils.evaluateNodeListTextXPath(document, XPATH_EXPRESSION_HUDSON_JOB_URL)) {
  90. remoteJobs.addAll(fromXml(URLUtils.fetchUrl(jobUrl + "/api/xml")));
  91. }
  92. return remoteJobs;
  93. }
  94. protected static SortedSet<RemoteJob> fromListViewXml(final String xml) throws SAXException, IOException,
  95. ParserConfigurationException, XPathExpressionException {
  96. final SortedSet<RemoteJob> remoteJobs = new TreeSet<RemoteJob>();
  97. if (StringUtils.isEmpty(xml)) {
  98. return remoteJobs;
  99. }
  100. if (!xml.startsWith("<listView>")) {
  101. return remoteJobs;
  102. }
  103. final Document document = XPathUtils.parse(xml);
  104. for (final String jobUrl : XPathUtils.evaluateNodeListTextXPath(document, XPATH_EXPRESSION_LIST_VIEW_JOB_URL)) {
  105. remoteJobs.addAll(fromXml(URLUtils.fetchUrl(jobUrl + "/api/xml")));
  106. }
  107. return remoteJobs;
  108. }
  109. protected static RemoteJob fromMavenModuleSetXml(final String xml) throws SAXException, IOException,
  110. ParserConfigurationException, XPathExpressionException {
  111. if (StringUtils.isEmpty(xml)) {
  112. return null;
  113. }
  114. if (!xml.startsWith("<mavenModuleSet>")) {
  115. return null;
  116. }
  117. final Document document = XPathUtils.parse(xml);
  118. return new RemoteJob(XPathUtils.evaluateStringXPath(document, XPATH_EXPRESSION_MAVEN_MODULE_SET_NAME),
  119. XPathUtils.evaluateStringXPath(document, XPATH_EXPRESSION_MAVEN_MODULE_SET_URL),
  120. XPathUtils.evaluateStringXPath(document, XPATH_EXPRESSION_MAVEN_MODULE_SET_DESCRIPTION));
  121. }
  122. public static SortedSet<RemoteJob> fromXml(final String xml) throws XPathExpressionException, SAXException,
  123. IOException, ParserConfigurationException {
  124. final SortedSet<RemoteJob> remoteJobs = new TreeSet<RemoteJob>();
  125. if (StringUtils.isEmpty(xml)) {
  126. return remoteJobs;
  127. }
  128. // ---
  129. if (xml.startsWith("<hudson>")) {
  130. remoteJobs.addAll(fromHudsonXml(xml));
  131. }
  132. else if (xml.startsWith("<freeStyleProject>")) {
  133. remoteJobs.add(fromFreeStyleProjectXml(xml));
  134. }
  135. else if (xml.startsWith("<listView>")) {
  136. remoteJobs.addAll(fromListViewXml(xml));
  137. }
  138. else if (xml.startsWith("<mavenModuleSet>")) {
  139. remoteJobs.add(fromMavenModuleSetXml(xml));
  140. }
  141. return remoteJobs;
  142. }
  143. /**
  144. * Static-only access.
  145. */
  146. private RemoteJobUtils() {
  147. // static-only access
  148. }
  149. }