48
48
import org .jfrog .build .client .bintrayResponse .BintrayResponseFactory ;
49
49
import org .jfrog .build .extractor .clientConfiguration .deploy .DeployDetails ;
50
50
import org .jfrog .build .extractor .clientConfiguration .util .DeploymentUrlUtils ;
51
+ import org .jfrog .build .extractor .clientConfiguration .util .JsonSerializer ;
52
+ import org .jfrog .build .extractor .usageReport .UsageReporter ;
51
53
import org .jfrog .build .util .VersionCompatibilityType ;
52
54
import org .jfrog .build .util .VersionException ;
53
55
@@ -79,6 +81,8 @@ public class ArtifactoryBuildInfoClient extends ArtifactoryBaseClient implements
79
81
public static final String APPLICATION_VND_ORG_JFROG_ARTIFACTORY_JSON = "application/vnd.org.jfrog.artifactory+json" ;
80
82
public static final String APPLICATION_JSON = "application/json" ;
81
83
public static final String ITEM_LAST_MODIFIED = "/api/storage/" ;
84
+ private static final String USAGE_API = "/api/system/usage" ;
85
+ private static final ArtifactoryVersion USAGE_ARTIFACTORY_MIN_VERSION = new ArtifactoryVersion ("6.9.0" );
82
86
83
87
/**
84
88
* Creates a new client for the given Artifactory url.
@@ -101,7 +105,7 @@ public ArtifactoryBuildInfoClient(String artifactoryUrl, String username, String
101
105
}
102
106
103
107
public ArtifactoryBuildInfoClient (String artifactoryUrl , String username , String password , Log log ) {
104
- super (artifactoryUrl , username , password , StringUtils .EMPTY , log );
108
+ this (artifactoryUrl , username , password , StringUtils .EMPTY , log );
105
109
}
106
110
/**
107
111
* @return A list of local repositories available for deployment.
@@ -299,22 +303,16 @@ private void sendHttpEntityRequest(HttpEntityEnclosingRequestBase request, Strin
299
303
300
304
private HttpResponse sendHttpRequest (HttpUriRequest request , int ... httpStatuses ) throws IOException {
301
305
HttpResponse httpResponse ;
302
- int connectionRetries = httpClient .getConnectionRetries ();
303
- try {
304
- httpResponse = httpClient .getHttpClient ().execute (request );
305
- StatusLine statusLine = httpResponse .getStatusLine ();
306
- for (int status : httpStatuses ) {
307
- if (statusLine .getStatusCode () == status ) {
308
- return httpResponse ;
309
- }
306
+ httpResponse = httpClient .getHttpClient ().execute (request );
307
+ StatusLine statusLine = httpResponse .getStatusLine ();
308
+ for (int status : httpStatuses ) {
309
+ if (statusLine .getStatusCode () == status ) {
310
+ return httpResponse ;
310
311
}
311
-
312
- HttpEntity responseEntity = httpResponse .getEntity ();
313
- throw new IOException (statusLine .getStatusCode () + httpClient .getMessageFromEntity (responseEntity ));
314
- } finally {
315
- // We are using the same client for multiple operations therefore we need to restore the connectionRetries configuration.
316
- httpClient .setConnectionRetries (connectionRetries );
317
312
}
313
+
314
+ HttpEntity responseEntity = httpResponse .getEntity ();
315
+ throw new IOException (statusLine .getStatusCode () + httpClient .getMessageFromEntity (responseEntity ));
318
316
}
319
317
320
318
public ItemLastModified getItemLastModified (String path ) throws IOException {
@@ -468,7 +466,7 @@ private String createJsonRequestBody(BintrayUploadInfoOverride info) throws IOEx
468
466
private BintrayResponse parseResponse (HttpResponse response ) throws IOException {
469
467
InputStream content = response .getEntity ().getContent ();
470
468
int status = response .getStatusLine ().getStatusCode ();
471
- JsonParser parser = httpClient .createJsonFactory ().createJsonParser (content );
469
+ JsonParser parser = httpClient .createJsonFactory ().createParser (content );
472
470
BintrayResponse responseObject = BintrayResponseFactory .createResponse (status , parser );
473
471
return responseObject ;
474
472
}
@@ -855,5 +853,38 @@ private String getResponseMessage(List<ArtifactoryUploadResponse.Error> errorLis
855
853
}
856
854
return builder .toString ();
857
855
}
858
- }
859
856
857
+ public void reportUsage (UsageReporter usageReporter ) throws IOException {
858
+ // Check Artifactory supported version.
859
+ ArtifactoryVersion version = getArtifactoryVersion ();
860
+ if (version .isNotFound ()) {
861
+ throw new IOException ("Could not get Artifactory version." );
862
+ }
863
+ if (!version .isAtLeast (USAGE_ARTIFACTORY_MIN_VERSION )) {
864
+ throw new IOException ("Usage report is not supported on targeted Artifactory server." );
865
+ }
866
+
867
+ // Create request.
868
+ String url = artifactoryUrl + USAGE_API ;
869
+ String encodedUrl = ArtifactoryHttpClient .encodeUrl (url );
870
+ String requestBody = new JsonSerializer <UsageReporter >().toJSON (usageReporter );
871
+ StringEntity entity = new StringEntity (requestBody , "UTF-8" );
872
+ entity .setContentType ("application/json" );
873
+ HttpPost request = new HttpPost (encodedUrl );
874
+ request .setEntity (entity );
875
+
876
+ // Send request
877
+ HttpResponse httpResponse = null ;
878
+ try {
879
+ httpResponse = httpClient .getHttpClient ().execute (request );
880
+ StatusLine statusLine = httpResponse .getStatusLine ();
881
+ if (statusLine .getStatusCode () != HttpStatus .SC_OK ) {
882
+ throw new IOException (String .format ("Artifactory response: %s %s" , statusLine .getStatusCode (), statusLine .getReasonPhrase ()));
883
+ }
884
+ } finally {
885
+ if (httpResponse != null ) {
886
+ EntityUtils .consumeQuietly (httpResponse .getEntity ());
887
+ }
888
+ }
889
+ }
890
+ }
0 commit comments