Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a method to get the content of a jenkins page #452

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions jenkins-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</parent>

<artifactId>jenkins-client</artifactId>
<version>0.4.0-HACKED</version>
<name>Jenkins API client for Java :: The Client.</name>

<dependencies>
Expand Down Expand Up @@ -114,6 +115,13 @@
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -298,7 +314,8 @@ public void post_form(String path, Map<String, String> 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 {
Expand All @@ -325,7 +342,7 @@ public void post_form(String path, Map<String, String> data, boolean crumbFlag)
public HttpResponse post_form_with_result(String path, List<NameValuePair> 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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -493,6 +510,19 @@ protected void setLocalContext(final HttpContext localContext) {

private <T extends BaseModel> T objectFromResponse(Class<T> 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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}


Expand All @@ -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");
}


Expand All @@ -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");
}


Expand Down Expand Up @@ -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"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public BuildResult getResult() {

};

private List<LinkedHashMap<String, List<LinkedHashMap<String, Object>>>> actions; // TODO: Should be improved.
private List<Map<String, Object>> actions; // TODO: Should be improved.
private boolean building;
private String description;
private String displayName;
Expand Down Expand Up @@ -161,12 +161,7 @@ public boolean isBuilding() {
}

public List<BuildCause> 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<>();
}

/**
Expand Down Expand Up @@ -347,13 +342,7 @@ public List getActions() {
}

public Map<String, Object> getParameters() {
Map<String, Object> 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<>();
}

/**
Expand Down
20 changes: 10 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,23 @@
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</version>
<executions>
<!--
&lt;!&ndash;
! here we override the super-pom attach-sources execution id which
! calls sources:jar goal. That goals forks the lifecycle,
! causing the generate-sources phase to be called twice for the install goal.
! With Maven 3.4.0 this will become superfluous.
-->
&ndash;&gt;
<execution>
<id>attach-sources</id>
<phase>DISABLE_FORKED_LIFECYCLE_MSOURCES-13</phase>
</execution>
</executions>
</plugin>
</plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down Expand Up @@ -353,7 +353,7 @@
</plugins>
</pluginManagement>
<plugins>
<plugin>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<dependencies>
Expand All @@ -370,7 +370,7 @@
</dependencies>
<configuration>
<asciidoc>
<!-- optional site-wide AsciiDoc attributes -->
&lt;!&ndash; optional site-wide AsciiDoc attributes &ndash;&gt;
<attributes>
<icons>font</icons>
<source-highlighter>coderay</source-highlighter>
Expand All @@ -382,11 +382,11 @@
</attributes>
</asciidoc>
</configuration>
</plugin>
</plugin>-->
</plugins>
</build>

<reporting>
<!--<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -443,7 +443,7 @@
</execution>
</executions>
</plugin>
<!-- MSOURCES-13 related workaround overriding super-pom -->
&lt;!&ndash; MSOURCES-13 related workaround overriding super-pom &ndash;&gt;
<plugin>
<inherited>true</inherited>
<artifactId>maven-source-plugin</artifactId>
Expand Down Expand Up @@ -472,7 +472,7 @@
</plugins>
</build>
</profile>
</profiles>
</profiles>-->

<repositories>
<repository>
Expand Down