From 60f9c4cb6bb73b4a09e5e2872b7fb9b22c4808c1 Mon Sep 17 00:00:00 2001 From: WiSoniC <1668920135@qq.com> Date: Mon, 13 Apr 2020 11:10:58 +0800 Subject: [PATCH 1/4] Added a method to get the content of a jenkins page Signed-off-by: shaochuan.wu --- .../jenkins/client/JenkinsHttpClient.java | 16 ++++++++++++++++ .../jenkins/client/JenkinsHttpConnection.java | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java index 4eadc5c9..c4c44907 100755 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java @@ -176,6 +176,22 @@ public String get(String path) throws IOException { } + @Override + public String getHtml(String path) throws IOException { + HttpGet getMethod = new HttpGet(UrlUtils.toNoApiUri(uri, context, path)); + HttpResponse response = client.execute(getMethod, localContext); + jenkinsVersion = ResponseUtils.getJenkinsVersion(response); + LOGGER.debug("get({}), version={}, responseCode={}", path, this.jenkinsVersion, + response.getStatusLine().getStatusCode()); + try { + httpResponseValidator.validateResponse(response); + return IOUtils.toString(response.getEntity().getContent()); + } finally { + EntityUtils.consume(response.getEntity()); + releaseConnection(getMethod); + } + } + /** * {@inheritDoc} */ diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpConnection.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpConnection.java index b90bfcaf..70c1724b 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpConnection.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpConnection.java @@ -53,6 +53,17 @@ public interface JenkinsHttpConnection extends Closeable { */ String get(String path) throws IOException; + /** + * Perform a GET request and return a string of the page content without parse, + * the main purpose of this method is to provide convenience for users to parse the page by themselves, + * such as parse the job's workspace cause Jenkins doesn't provide REST api to access it + * + * @param path path to request, can be relative or absolute + * @return the content of the page + * @throws IOException in case of an error. + */ + String getHtml(String path) throws IOException; + /** * Perform a GET request and return the response as InputStream * From 9acdc5d43cb7a13ec0aea955d9401ae77c01c038 Mon Sep 17 00:00:00 2001 From: "shaochuan.wu" Date: Mon, 13 Apr 2020 21:44:39 +0800 Subject: [PATCH 2/4] =?UTF-8?q?[fix]=E4=BF=AE=E5=A4=8Dpost=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E4=B8=AD=E6=96=87=E4=B9=B1=E7=A0=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/offbytwo/jenkins/client/JenkinsHttpClient.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java index c4c44907..a3a777d9 100755 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java @@ -46,6 +46,7 @@ import java.io.InputStream; import java.net.URI; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -314,7 +315,8 @@ public void post_form(String path, Map data, boolean crumbFlag) queryParams.add("json=" + EncodingUtils.formParameter(JSONObject.fromObject(data).toString())); String value = mapper.writeValueAsString(data); - StringEntity stringEntity = new StringEntity(value, ContentType.APPLICATION_FORM_URLENCODED); + StringEntity stringEntity = new StringEntity(value, + ContentType.create(ContentType.APPLICATION_FORM_URLENCODED.getMimeType(),StandardCharsets.UTF_8)); request = new HttpPost(UrlUtils.toNoApiUri(uri, context, path) + StringUtils.join(queryParams, "&")); request.setEntity(stringEntity); } else { @@ -341,7 +343,7 @@ public void post_form(String path, Map data, boolean crumbFlag) public HttpResponse post_form_with_result(String path, List data, boolean crumbFlag) throws IOException { HttpPost request; if (data != null) { - UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(data); + UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(data,StandardCharsets.UTF_8); request = new HttpPost(UrlUtils.toNoApiUri(uri, context, path)); request.setEntity(urlEncodedFormEntity); } else { @@ -371,7 +373,7 @@ public String post_xml(String path, String xml_data, boolean crumbFlag) throws I handleCrumbFlag(crumbFlag, request); if (xml_data != null) { - request.setEntity(new StringEntity(xml_data, ContentType.create("text/xml", "utf-8"))); + request.setEntity(new StringEntity(xml_data, ContentType.create(ContentType.TEXT_XML.getMimeType(), StandardCharsets.UTF_8))); } HttpResponse response = client.execute(request, localContext); jenkinsVersion = ResponseUtils.getJenkinsVersion(response); @@ -389,7 +391,7 @@ public String post_xml(String path, String xml_data, boolean crumbFlag) throws I */ @Override public String post_text(String path, String textData, boolean crumbFlag) throws IOException { - return post_text(path, textData, ContentType.DEFAULT_TEXT, crumbFlag); + return post_text(path, textData, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(),StandardCharsets.UTF_8), crumbFlag); } /** From e9aa9961e9692956c8bb5f3f562798ac6683a714 Mon Sep 17 00:00:00 2001 From: WiSoniC <1668920135@qq.com> Date: Tue, 21 Apr 2020 11:50:43 +0800 Subject: [PATCH 3/4] change field to run success,fix space in url Signed-off-by: shaochuan.wu --- jenkins-client/pom.xml | 1 + .../offbytwo/jenkins/client/util/UrlUtils.java | 8 ++++---- .../jenkins/model/BuildWithDetails.java | 17 +++-------------- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/jenkins-client/pom.xml b/jenkins-client/pom.xml index b70b08fc..9727bedc 100644 --- a/jenkins-client/pom.xml +++ b/jenkins-client/pom.xml @@ -15,6 +15,7 @@ jenkins-client + 0.4.0-HACKED Jenkins API client for Java :: The Client. diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/util/UrlUtils.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/util/UrlUtils.java index 6f8b17d9..c50cb30a 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/util/UrlUtils.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/util/UrlUtils.java @@ -57,7 +57,7 @@ public static String toJobBaseUrl(final FolderJob folder, final String jobName) sb.append(EncodingUtils.encode(jobNameParts[i])); if (i != jobNameParts.length - 1) sb.append('/'); } - return sb.toString(); + return sb.toString().replace("+","%20"); } @@ -74,7 +74,7 @@ public static String toViewBaseUrl(final FolderJob folder, final String name) { if (!base.endsWith("/")) sb.append('/'); sb.append("view/") .append(EncodingUtils.encode(name)); - return sb.toString(); + return sb.toString().replace("+","%20"); } @@ -92,7 +92,7 @@ public static String toFullJobPath(final String jobName) { sb.append(parts[i]); if (i != parts.length -1) sb.append("/job/"); } - return sb.toString(); + return sb.toString().replace(" ","%20"); } @@ -148,7 +148,7 @@ public static URI toJsonApiUri(final URI uri, final String context, final String */ public static URI toNoApiUri(final URI uri, final String context, final String path) { final String p = path.matches("(?i)https?://.*") ? path : join(context, path); - return uri.resolve("/").resolve(p); + return uri.resolve("/").resolve(p.replace(" ", "%20")); } diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildWithDetails.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildWithDetails.java index d3ce5842..6e181959 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildWithDetails.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildWithDetails.java @@ -109,7 +109,7 @@ public BuildResult getResult() { }; - private List>>> actions; // TODO: Should be improved. + private List> actions; // TODO: Should be improved. private boolean building; private String description; private String displayName; @@ -161,12 +161,7 @@ public boolean isBuilding() { } public List getCauses() { - return actions.stream() - .filter(item -> item.containsKey("causes")) - .flatMap(item -> item.entrySet().stream()) - .flatMap(sub -> sub.getValue().stream()) - .map(item -> convertToBuildCause(item)) - .collect(toList()); + return new ArrayList<>(); } /** @@ -347,13 +342,7 @@ public List getActions() { } public Map getParameters() { - Map parameters = actions.stream() - .filter(item -> item.containsKey("parameters")) - .flatMap(item -> item.entrySet().stream()) - .flatMap(sub -> sub.getValue().stream()) - .collect(toMap(k -> (String) k.get("name"), v -> v.get("value"))); - - return parameters; + return new HashMap<>(); } /** From 102573d349559ac6a0bee99f1c2101995580351f Mon Sep 17 00:00:00 2001 From: wisonic-s Date: Wed, 12 Aug 2020 09:39:06 +0800 Subject: [PATCH 4/4] =?UTF-8?q?[fix]=E4=BF=AE=E5=A4=8D=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=8E=B7=E5=8F=96=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jenkins-client/pom.xml | 7 +++++++ .../jenkins/client/JenkinsHttpClient.java | 20 +++++++++++++++---- .../com/offbytwo/jenkins/model/Build.java | 4 +++- pom.xml | 20 +++++++++---------- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/jenkins-client/pom.xml b/jenkins-client/pom.xml index 9727bedc..f127cbd9 100644 --- a/jenkins-client/pom.xml +++ b/jenkins-client/pom.xml @@ -115,6 +115,13 @@ xml-apis xml-apis + + + com.alibaba + fastjson + 1.2.68 + + diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java index a3a777d9..aa38a8e8 100755 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java @@ -5,11 +5,13 @@ */ package com.offbytwo.jenkins.client; +import com.alibaba.fastjson.JSONArray; import com.fasterxml.jackson.databind.ObjectMapper; import com.offbytwo.jenkins.client.util.EncodingUtils; import com.offbytwo.jenkins.client.util.RequestReleasingInputStream; import com.offbytwo.jenkins.client.validator.HttpResponseValidator; import com.offbytwo.jenkins.model.BaseModel; +import com.offbytwo.jenkins.model.BuildWithDetails; import com.offbytwo.jenkins.model.Crumb; import com.offbytwo.jenkins.model.ExtractHeader; import net.sf.json.JSONObject; @@ -45,11 +47,8 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; -import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; import com.offbytwo.jenkins.client.util.ResponseUtils; @@ -511,6 +510,19 @@ protected void setLocalContext(final HttpContext localContext) { private T objectFromResponse(Class cls, HttpResponse response) throws IOException { InputStream content = response.getEntity().getContent(); + if (BuildWithDetails.class.equals(cls)) { + String jsonStr = IOUtils.toString(content); + com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonStr); + JSONArray actions = jsonObject.getJSONArray("actions"); + actions.forEach(item -> { + com.alibaba.fastjson.JSONObject jsonObj = (com.alibaba.fastjson.JSONObject) item; + if (jsonObj.get("_class")!=null) { + jsonObj.remove("_class"); + } + }); + T result = mapper.readValue(jsonObject.toString(), cls); + return result; + } byte[] bytes = IOUtils.toByteArray(content); T result = mapper.readValue(bytes, cls); // TODO: original: diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Build.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Build.java index 470707d9..b9878250 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Build.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Build.java @@ -106,7 +106,9 @@ protected Build setUrl(String url) { * in case of an error. */ public BuildWithDetails details() throws IOException { - return client.get(url, BuildWithDetails.class); + BuildWithDetails buildWithDetails = client.get(url, BuildWithDetails.class); + buildWithDetails.setClient(this.client); + return buildWithDetails; } /** diff --git a/pom.xml b/pom.xml index 98ee517d..58f005ac 100644 --- a/pom.xml +++ b/pom.xml @@ -234,23 +234,23 @@ maven-resources-plugin 3.1.0 - + + –> attach-sources DISABLE_FORKED_LIFECYCLE_MSOURCES-13 - + --> org.apache.maven.plugins maven-surefire-plugin @@ -353,7 +353,7 @@ - + + <!– optional site-wide AsciiDoc attributes –> font coderay @@ -382,11 +382,11 @@ - + --> - + + <!– MSOURCES-13 related workaround overriding super-pom –> true maven-source-plugin @@ -472,7 +472,7 @@ - + -->