diff options
-rw-r--r-- | pom.xml | 5 | ||||
-rw-r--r-- | src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java | 61 | ||||
-rw-r--r-- | src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java | 30 |
3 files changed, 96 insertions, 0 deletions
@@ -30,6 +30,11 @@ <version>1.6.1</version> </dependency> <dependency> + <groupId>org.jdom</groupId> + <artifactId>jdom</artifactId> + <version>2.0.2</version> + </dependency> + <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> <version>0.7.1.RELEASE</version> diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java new file mode 100644 index 0000000..ddf040c --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java @@ -0,0 +1,61 @@ +package io.trygvis.esper.testing.jenkins; + +import static org.apache.commons.lang.StringUtils.*; +import org.codehaus.httpcache4j.*; +import org.codehaus.httpcache4j.cache.*; +import org.codehaus.httpcache4j.util.*; +import org.jdom2.*; +import org.jdom2.input.*; + +import javax.xml.stream.*; +import java.net.*; +import java.util.*; + +public class JenkinsClient { + private static final XMLInputFactory xmlReader = XMLInputFactory.newFactory(); + private static final StAXStreamBuilder streamBuilder = new StAXStreamBuilder(); + private final HTTPCache http; + private final URI apiXmlUri; + + public JenkinsClient(HTTPCache http, URI jenkinsUri) { + this.http = http; + this.apiXmlUri = URIBuilder.fromURI(jenkinsUri).addRawPath("api/xml").toURI(); + } + + public List<JenkinsJobXml> fetchJobs() throws XMLStreamException, JDOMException { + HTTPResponse response = http.execute(new HTTPRequest(apiXmlUri)); + + if (response.getStatus().getCode() != 200) { + throw new RuntimeException("Did not get 200 back, got " + response.getStatus().getCode()); + } + + Element doc = streamBuilder.build(xmlReader.createXMLStreamReader(response.getPayload().getInputStream())).getRootElement(); + + List<JenkinsJobXml> jobs = new ArrayList<>(); + for (Element job : doc.getChildren("job")) { + String name = trimToNull(job.getChildText("name")); + String url = trimToNull(job.getChildText("url")); + String color = trimToNull(job.getChildText("color")); + + if (name == null || url == null || color == null) { + continue; + } + + jobs.add(new JenkinsJobXml(name, url, color)); + } + + return jobs; + } +} + +class JenkinsJobXml { + public final String name; + public final String url; + public final String color; + + JenkinsJobXml(String name, String url, String color) { + this.name = name; + this.url = url; + this.color = color; + } +}
\ No newline at end of file diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java new file mode 100644 index 0000000..942aa15 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java @@ -0,0 +1,30 @@ +package io.trygvis.esper.testing.jenkins; + +import io.trygvis.esper.testing.*; +import org.codehaus.httpcache4j.cache.*; +import org.codehaus.httpcache4j.client.*; + +import java.net.*; +import java.util.*; + +public class JenkinsImporter { + private final JenkinsClient jenkinsClient; + + public static void main(String[] args) throws Exception { + Main.configureLog4j(); + new JenkinsImporter().work(); + } + + public JenkinsImporter() { + HTTPCache http = new HTTPCache(new MemoryCacheStorage(), HTTPClientResponseResolver.createMultithreadedInstance()); + jenkinsClient = new JenkinsClient(http, URI.create("https://builds.apache.org")); + } + + private void work() throws Exception { + List<JenkinsJobXml> jobs = jenkinsClient.fetchJobs(); + + for (JenkinsJobXml job : jobs) { + System.out.println("job.name = " + job.name); + } + } +} |