|
40 | 40 | import org.apache.http.protocol.HTTP;
|
41 | 41 | import org.apache.http.util.EntityUtils;
|
42 | 42 | import org.jfrog.build.api.Build;
|
| 43 | +import org.jfrog.build.api.BuildRetention; |
43 | 44 | import org.jfrog.build.api.release.BintrayUploadInfoOverride;
|
44 | 45 | import org.jfrog.build.api.release.Promotion;
|
45 | 46 | import org.jfrog.build.api.util.FileChecksumCalculator;
|
@@ -73,8 +74,11 @@ public class ArtifactoryBuildInfoClient extends ArtifactoryBaseClient {
|
73 | 74 | private static final String VIRTUAL_REPOS_REST_URL = "/api/repositories?type=virtual";
|
74 | 75 | private static final String PUSH_TO_BINTRAY_REST_URL = "/api/build/pushToBintray/";
|
75 | 76 | private static final String BUILD_REST_URL = "/api/build";
|
| 77 | + private static final String BUILD_RETENTION_REST_URL = BUILD_REST_URL + "/retention/"; |
76 | 78 | private static final int CHECKSUM_DEPLOY_MIN_FILE_SIZE = 10240; // Try checksum deploy of files greater than 10KB
|
77 | 79 | public static final String BUILD_BROWSE_URL = "/webapp/builds";
|
| 80 | + public static final String APPLICATION_VND_ORG_JFROG_ARTIFACTORY_JSON = "application/vnd.org.jfrog.artifactory+json"; |
| 81 | + public static final String APPLICATION_JSON = "application/json"; |
78 | 82 |
|
79 | 83 | /**
|
80 | 84 | * Version of Artifactory we work with.
|
@@ -180,7 +184,25 @@ private List<String> getRepositoriesList(String restUrl) throws IOException {
|
180 | 184 | public void sendBuildInfo(String buildInfoJson) throws IOException {
|
181 | 185 | String url = artifactoryUrl + BUILD_REST_URL;
|
182 | 186 | HttpPut httpPut = new HttpPut(url);
|
183 |
| - sendDescriptor(httpPut, buildInfoJson); |
| 187 | + try { |
| 188 | + sendDescriptor(httpPut, buildInfoJson, APPLICATION_VND_ORG_JFROG_ARTIFACTORY_JSON); |
| 189 | + } catch (IOException e) { |
| 190 | + throw new IOException("Failed to send build descriptor. " + e.getMessage(), e); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + public void sendBuildRetetion(BuildRetention buildRetantion, String buildName) throws IOException { |
| 195 | + String buildRetantionJson = toJsonString(buildRetantion); |
| 196 | + String url = artifactoryUrl + BUILD_RETENTION_REST_URL + buildName; |
| 197 | + HttpPost httpPost = new HttpPost(url); |
| 198 | + try { |
| 199 | + log.info("Executing build retention"); |
| 200 | + log.debug(buildRetantionJson); |
| 201 | + sendRequest(httpPost, buildRetantionJson, APPLICATION_JSON, true); |
| 202 | + } catch (IOException e) { |
| 203 | + log.error("Failed to execute build retention.", e); |
| 204 | + throw new IOException("Failed to execute build retention: " + e.getMessage(), e); |
| 205 | + } |
184 | 206 | }
|
185 | 207 |
|
186 | 208 | /**
|
@@ -209,23 +231,36 @@ public void sendModuleInfo(Build build) throws IOException {
|
209 | 231 | encodeUrl(build.getNumber());
|
210 | 232 | HttpPost httpPost = new HttpPost(url);
|
211 | 233 | String modulesAsJsonString = toJsonString(build.getModules());
|
212 |
| - sendDescriptor(httpPost, modulesAsJsonString); |
| 234 | + sendDescriptor(httpPost, modulesAsJsonString, APPLICATION_VND_ORG_JFROG_ARTIFACTORY_JSON); |
213 | 235 | } catch (Exception e) {
|
214 | 236 | log.error("Could not build the build-info modules object.", e);
|
215 |
| - throw new IOException("Could not publish build-info modules: " + e.getMessage()); |
| 237 | + throw new IOException("Could not publish build-info modules: " + e.getMessage(), e); |
216 | 238 | }
|
217 | 239 | }
|
218 | 240 |
|
219 |
| - private void sendDescriptor(HttpEntityEnclosingRequestBase request, String content) throws IOException { |
| 241 | + private void sendDescriptor(HttpEntityEnclosingRequestBase request, String content, String contentType) throws IOException { |
| 242 | + log.info("Deploying build descriptor to: " + request.getURI().toString()); |
| 243 | + sendRequest(request, content, contentType, true); |
| 244 | + } |
| 245 | + |
| 246 | + private void sendRequest(HttpEntityEnclosingRequestBase request, String content, String contentType, boolean retry) throws IOException { |
220 | 247 | StringEntity stringEntity = new StringEntity(content, "UTF-8");
|
221 |
| - stringEntity.setContentType("application/vnd.org.jfrog.artifactory+json"); |
| 248 | + stringEntity.setContentType(contentType); |
222 | 249 | request.setEntity(stringEntity);
|
223 |
| - log.info("Deploying build descriptor to: " + request.getURI().toString()); |
224 |
| - HttpResponse response = httpClient.getHttpClient().execute(request); |
225 |
| - StatusLine statusLine = response.getStatusLine(); |
226 |
| - if (statusLine.getStatusCode() != HttpStatus.SC_NO_CONTENT) { |
227 |
| - HttpEntity responseEntity = response.getEntity(); |
228 |
| - throw new IOException("Failed to send build descriptor. Status code: " + statusLine.getStatusCode() + getMessageFromEntity(responseEntity)); |
| 250 | + int connectionRetries = httpClient.getConnectionRetries(); |
| 251 | + try { |
| 252 | + if (!retry) { |
| 253 | + httpClient.setConnectionRetries(0); |
| 254 | + } |
| 255 | + HttpResponse response = httpClient.getHttpClient().execute(request); |
| 256 | + StatusLine statusLine = response.getStatusLine(); |
| 257 | + if (statusLine.getStatusCode() != HttpStatus.SC_NO_CONTENT) { |
| 258 | + HttpEntity responseEntity = response.getEntity(); |
| 259 | + throw new IOException(statusLine.getStatusCode() + getMessageFromEntity(responseEntity)); |
| 260 | + } |
| 261 | + } finally { |
| 262 | + // We are using the same client for multiple operations therefore we need to restore the connectionRetries configuration. |
| 263 | + httpClient.setConnectionRetries(connectionRetries); |
229 | 264 | }
|
230 | 265 | }
|
231 | 266 |
|
|
0 commit comments