/** * Create a new Jenkins server reference given the address and credentials * * @param serverUri address of jenkins server (ex. http://localhost:8080/jenkins) * @param username username to use when connecting * @param passwordOrToken password (not recommended) or token (recommended) */ public JenkinsServer(URI serverUri, String username, String passwordOrToken) { this(new JenkinsHttpClient(serverUri, username, passwordOrToken)); }
public JobWithDetails details() throws IOException { return client.get(url, JobWithDetails.class); }
/** * {@inheritDoc} */ @Override public void post(String path) throws IOException { post(path, null, null, null, false); }
/** * {@inheritDoc} */ @Override public String post_text(String path, String textData, ContentType contentType, boolean crumbFlag) throws IOException { HttpPost request = new HttpPost(UrlUtils.toJsonApiUri(uri, context, path)); if (crumbFlag == true) { Crumb crumb = get("/crumbIssuer", Crumb.class); if (crumb != null) { request.addHeader(new BasicHeader(crumb.getCrumbRequestField(), crumb.getCrumb())); } } if (textData != null) { request.setEntity(new StringEntity(textData, contentType)); } HttpResponse response = client.execute(request, localContext); jenkinsVersion = ResponseUtils.getJenkinsVersion(response); try { httpResponseValidator.validateResponse(response); return IOUtils.toString(response.getEntity().getContent()); } finally { EntityUtils.consume(response.getEntity()); releaseConnection(request); } }
/** * {@inheritDoc} */ @Override public String post_xml(String path, String xml_data) throws IOException { return post_xml(path, xml_data, true); }
HttpPost request = new HttpPost(UrlUtils.toJsonApiUri(uri, context, path)); if (crumbFlag == true) { Crumb crumb = getQuietly("/crumbIssuer", Crumb.class); if (crumb != null) { request.addHeader(new BasicHeader(crumb.getCrumbRequestField(), crumb.getCrumb())); responseObject = (R) location; } else { responseObject = objectFromResponse(cls, response); releaseConnection(request);
/** * Perform a GET request and parse the response to the given class * * @param path * path to request, can be relative or absolute * @param cls * class of the response * @param <T> * type of the response * @return an instance of the supplied class * @throws IOException * , HttpResponseException */ public <T extends BaseModel> T get(String path, Class<T> cls) throws IOException, HttpResponseException { HttpResponse response = client.execute(new HttpGet(api(path)), localContext); int status = response.getStatusLine().getStatusCode(); if (status < 200 || status >= 300) { throw new HttpResponseException(status, response.getStatusLine().getReasonPhrase()); } try { return objectFromResponse(cls, response); } finally { EntityUtils.consume(response.getEntity()); } }
/** * {@inheritDoc} */ @Override public String post_xml(String path, String xml_data, boolean crumbFlag) throws IOException { HttpPost request = new HttpPost(UrlUtils.toJsonApiUri(uri, context, path)); if (crumbFlag == true) { Crumb crumb = getQuietly("/crumbIssuer", Crumb.class); if (crumb != null) { request.addHeader(new BasicHeader(crumb.getCrumbRequestField(), crumb.getCrumb())); } } if (xml_data != null) { request.setEntity(new StringEntity(xml_data, ContentType.create("text/xml", "utf-8"))); } HttpResponse response = client.execute(request, localContext); jenkinsVersion = ResponseUtils.getJenkinsVersion(response); try { httpResponseValidator.validateResponse(response); return IOUtils.toString(response.getEntity().getContent()); } finally { EntityUtils.consume(response.getEntity()); releaseConnection(request); } }
/** * Create an unauthenticated Jenkins HTTP client * * @param uri Location of the jenkins server (ex. http://localhost:8080) * @param client Configured CloseableHttpClient to be used */ public JenkinsHttpClient(URI uri, CloseableHttpClient client) { this.context = uri.getPath(); if (!context.endsWith("/")) { context += "/"; } this.uri = uri; this.mapper = getDefaultMapper(); this.client = client; this.httpResponseValidator = new HttpResponseValidator(); // this.contentExtractor = new HttpResponseContentExtractor(); this.jenkinsVersion = EMPTY_VERSION; LOGGER.debug("uri={}", uri.toString()); }
/** * Perform a GET request and parse the response and return a simple string of the content * * @param path * path to request, can be relative or absolute * @return the entity text * @throws IOException * , HttpResponseException */ public String get(String path) throws IOException, HttpResponseException { HttpResponse response = client.execute(new HttpGet(api(path)), localContext); int status = response.getStatusLine().getStatusCode(); if (status < 200 || status >= 300) { throw new HttpResponseException(status, response.getStatusLine().getReasonPhrase()); } Scanner s = new Scanner(response.getEntity().getContent(), CHARSET_UTF_8); s.useDelimiter("\\z"); StringBuffer sb = new StringBuffer(); while (s.hasNext()) { sb.append(s.next()); } return sb.toString(); }
/** * Create an authenticated Jenkins HTTP client * * @param uri Location of the jenkins server (ex. http://localhost:8080) * @param builder Configured HttpClientBuilder to be used * @param username Username to use when connecting * @param password Password or auth token to use when connecting */ public JenkinsHttpClient(URI uri, HttpClientBuilder builder, String username, String password) { this(uri, addAuthentication(builder, uri, username, password)); if (isNotBlank(username)) { localContext = new BasicHttpContext(); localContext.setAttribute("preemptive-auth", new BasicScheme()); } }
public InputStream downloadArtifact(Artifact a) throws IOException, URISyntaxException { //We can't just put the artifact's relative path at the end of the url string, //as there could be characters that need to be escaped. URI uri = new URI(getUrl()); String artifactPath = uri.getPath() + "artifact/" + a.getRelativePath(); URI artifactUri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), artifactPath, "", ""); return client.getFile(artifactUri); } }
Crumb crumb = get("/crumbIssuer", Crumb.class); if (crumb != null) { request.addHeader(new BasicHeader(crumb.getCrumbRequestField(), crumb.getCrumb())); } finally { EntityUtils.consume(response.getEntity()); releaseConnection(request);
/** * Update the xml description of an existing job * * @return the new job object * @throws IOException */ public void updateJob(String jobName, String jobXml) throws IOException { client.post_xml("/job/" + encode(jobName) + "/config.xml", jobXml); }
HttpPost request = new HttpPost(api(path)); if (data != null) { StringEntity stringEntity = return objectFromResponse(cls, response); } else { return null;
/** * Create an unauthenticated Jenkins HTTP client * * @param uri * Location of the jenkins server (ex. http://localhost:8080) */ public JenkinsHttpClient(URI uri) { this.context = uri.getPath(); if (!context.endsWith("/")) { context += "/"; } this.uri = uri; this.mapper = getDefaultMapper(); HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 10000); HttpConnectionParams.setSoTimeout(httpParameters, 10000); this.client = new DefaultHttpClient(new PoolingClientConnectionManager(), httpParameters); }
HttpPost request = new HttpPost(api(path)); if (xml_data != null) { request.setEntity(new StringEntity(xml_data, ContentType.APPLICATION_XML));
public BuildWithDetails details() throws IOException { return client.get(url, BuildWithDetails.class); }
/** * Create a new Jenkins server reference given only the server address * * @param serverUri address of jenkins server (ex. http://localhost:8080/jenkins) */ public JenkinsServer(URI serverUri) { this(new JenkinsHttpClient(serverUri)); }
/** * {@inheritDoc} */ @Override public <R extends BaseModel, D> R post(String path, D data, Class<R> cls) throws IOException { return post(path, data, cls, null, true); }