Skip to content

Commit

Permalink
[#60feature/#60-restlib] lib usage refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: huluvu424242 <huluvu424242@gmail.com>
  • Loading branch information
Huluvu424242 committed Jan 29, 2023
1 parent 0401f1a commit b63e373
Showing 1 changed file with 54 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,16 @@
* #L%
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.cdancy.jenkins.rest.JenkinsClient;
import com.cdancy.jenkins.rest.domain.job.BuildInfo;
import com.cdancy.jenkins.rest.features.JobsApi;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.cdancy.jenkins.rest.JenkinsClient;
import com.cdancy.jenkins.rest.domain.job.BuildInfo;
import com.cdancy.jenkins.rest.features.JobsApi;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;

public class JobAbfrage implements Callable<JobStatusBeschreibung> {

Expand Down Expand Up @@ -84,32 +69,18 @@ public JobStatusBeschreibung call() throws Exception {


protected JobStatusBeschreibung getJobStatus() {

try {
final BuildInfo resultJSON = sendGetRequest();
final String jobName = resultJSON.fullDisplayName();// getString(JSONKEY_FULL_DISPLAY_NAME);
final String jobStatus = resultJSON.result(); //getString(JSONKEY_RESULT);
final BuildInfo buildInfo = sendGetRequest();
final String jobName = buildInfo.fullDisplayName();// getString(JSONKEY_FULL_DISPLAY_NAME);
final String jobStatus = buildInfo.result(); //getString(JSONKEY_RESULT);
return new JobStatusBeschreibung(jobName, JobStatus.valueOf(jobStatus), jobAbfragedaten.getJenkinsJobUrl(), jobOrderId);
} catch (JobNotFoundException e) {
return new JobStatusBeschreibung("Job Not Found ERROR: " + jobAbfragedaten.getJenkinsJobUrl(), JobStatus.OTHER, jobAbfragedaten.getJenkinsJobUrl(), jobOrderId);
} catch (ConnectionErrorException e) {
return new JobStatusBeschreibung("HTTP Status:" + e.getStatusCode(), JobStatus.OTHER, jobAbfragedaten.getJenkinsJobUrl(), jobOrderId);
} catch (ConnectionFailedException e) {
return new JobStatusBeschreibung("Connection ERROR: " + jobAbfragedaten.getJenkinsJobUrl(), JobStatus.OTHER, jobAbfragedaten.getJenkinsJobUrl(), jobOrderId);
} catch (NullPointerException | JSONException ex) {
} catch (NullPointerException ex) {
return new JobStatusBeschreibung(jobAbfragedaten.getJenkinsJobUrl().toExternalForm(), JobStatus.OTHER, jobAbfragedaten.getJenkinsJobUrl(), jobOrderId);
}
}

protected BuildInfo sendGetRequest() throws JobNotFoundException, ConnectionFailedException, ConnectionErrorException {
protected BuildInfo sendGetRequest() {
final URL statusAbfrageUrl = jobAbfragedaten.getStatusAbfrageUrl();
int statusCode = -1;
int timeout = 1;
// final RequestConfig config = RequestConfig.custom()
// .setConnectTimeout(timeout * 1000)
// .setConnectionRequestTimeout(timeout * 1000)
// .setSocketTimeout(timeout * 1000).build();
// try (final CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build()) {

final JenkinsClient.Builder clientBuilder = JenkinsClient.builder()
.endPoint(String.format("%s://%s:%d/", statusAbfrageUrl.getProtocol(), statusAbfrageUrl.getHost(), statusAbfrageUrl.getPort()));
Expand All @@ -122,75 +93,66 @@ protected BuildInfo sendGetRequest() throws JobNotFoundException, ConnectionFail

final String abfrageURL = statusAbfrageUrl.getPath();
Pattern pattern = Pattern.compile(".*/job/(.*)/job/(.*)");
Matcher matcher = pattern.matcher(statusAbfrageUrl.getPath());
Matcher matcher = pattern.matcher(abfrageURL);
final String folderName;
final String jobName;
final boolean matched= matcher.matches();
if(matched){
final boolean matched = matcher.matches();
if (matched) {
folderName = matcher.group(1);
jobName = matcher.group(2);
}else{
folderName=null;
jobName=null;
} else {
folderName = null;
jobName = null;
}


final JobsApi api = client.api().jobsApi();

// final int lastBuildNumber = api.lastBuildNumber(null, statusAbfrageUrl.getPath());
final int lastBuildNumber = api.lastBuildNumber(folderName, jobName);
LOGGER.debug("Letzte Buildnummer von {} ist {}", statusAbfrageUrl.getPath(), lastBuildNumber);
LOGGER.debug("Letzte Buildnummer von {} ist {}", abfrageURL, lastBuildNumber);

final BuildInfo buildInfo = client.api().jobsApi().buildInfo(folderName,jobName, lastBuildNumber);
LOGGER.debug("RESPONSE: URL: {}\n STATUS: {} \n", buildInfo.url(), buildInfo.result());


// statusCode = httpResponse.getStatusLine().getStatusCode();
// if (statusCode == 404) {
// throw new JobNotFoundException(new HttpResponseException(statusCode, httpResponse.getStatusLine().getReasonPhrase()));
// } else if (statusCode != 200) {
// throw new ConnectionErrorException(new HttpResponseException(statusCode, httpResponse.getStatusLine().getReasonPhrase()));
// }
// return getJsonObjectFromResponse(httpResponse);
// } catch (JSONException | IOException ex) {
// LOGGER.warn(String.format("Could not retrieve data from jenkins: %s", ex));
// }
return buildInfo;
}

protected HttpResponse getHttpResponse(CloseableHttpClient httpClient, HttpHost target, HttpGet httpGetRequest) throws ConnectionFailedException {
HttpResponse httpResponse;
try {
httpResponse = httpClient.execute(target, httpGetRequest);
} catch (IOException ex) {
throw new ConnectionFailedException(ex);
final BuildInfo buildInfo = client.api().jobsApi().buildInfo(folderName, jobName, lastBuildNumber);
if (LOGGER.isEnabledForLevel(Level.DEBUG)) {
LOGGER.debug("RESPONSE: URL: {}\n STATUS: {} \n",
buildInfo != null ? buildInfo.url() : "null",
buildInfo != null ? buildInfo.result() : "null");
}
return httpResponse;
return buildInfo;
}


protected static JSONObject getJsonObjectFromResponse(HttpResponse httpResponse) {
final HttpEntity entity = httpResponse.getEntity();
final String requestResult;
try {
final InputStream inputStream = entity.getContent();
requestResult = readStreamIntoString(inputStream);
} catch (IOException e) {
LOGGER.warn(String.format("Jenkins Response could not be read: %s", e));
return null;
}
LOGGER.debug("Empfangen als JSON:\n {}", requestResult);
return new JSONObject(requestResult);
}
// protected HttpResponse getHttpResponse(CloseableHttpClient httpClient, HttpHost target, HttpGet httpGetRequest) throws ConnectionFailedException {
// HttpResponse httpResponse;
// try {
// httpResponse = httpClient.execute(target, httpGetRequest);
// } catch (IOException ex) {
// throw new ConnectionFailedException(ex);
// }
// return httpResponse;
// }


// protected static JSONObject getJsonObjectFromResponse(HttpResponse httpResponse) {
// final HttpEntity entity = httpResponse.getEntity();
// final String requestResult;
// try {
// final InputStream inputStream = entity.getContent();
// requestResult = readStreamIntoString(inputStream);
// } catch (IOException e) {
// LOGGER.warn(String.format("Jenkins Response could not be read: %s", e));
// return null;
// }
// LOGGER.debug("Empfangen als JSON:\n {}", requestResult);
// return new JSONObject(requestResult);
// }


protected static String readStreamIntoString(InputStream inputStream) throws IOException {
String requestResult;
// wegen json gehen wir von utf-8 aus
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
requestResult = buffer.lines().collect(Collectors.joining("\n"));
}
return requestResult;
}
// protected static String readStreamIntoString(InputStream inputStream) throws IOException {
// String requestResult;
// // wegen json gehen wir von utf-8 aus
// try (BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
// requestResult = buffer.lines().collect(Collectors.joining("\n"));
// }
// return requestResult;
// }

}

0 comments on commit b63e373

Please sign in to comment.