diff --git a/jenkins-client/pom.xml b/jenkins-client/pom.xml index b70b08fc..f127cbd9 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. @@ -114,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 4eadc5c9..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,10 +47,8 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.nio.charset.StandardCharsets; +import java.util.*; import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; import com.offbytwo.jenkins.client.util.ResponseUtils; @@ -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} */ @@ -298,7 +314,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 { @@ -325,7 +342,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 { @@ -355,7 +372,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); @@ -373,7 +390,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); } /** @@ -493,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/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 * 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/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/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<>(); } /** 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 @@ - + -->