Skip to content

Commit

Permalink
Merge pull request #11 from blackducksoftware/adding-module-batching
Browse files Browse the repository at this point in the history
feat: Adding the 'module' dimension
  • Loading branch information
rottebds authored Aug 8, 2019
2 parents 3776843 + e69fb53 commit c16fcf7
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
package com.synopsys.integration.phonehome;

import java.util.Map;
import java.util.Optional;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -37,7 +36,6 @@
import com.google.gson.Gson;
import com.synopsys.integration.log.IntLogger;
import com.synopsys.integration.phonehome.exception.PhoneHomeException;
import com.synopsys.integration.phonehome.google.analytics.GoogleAnalyticsConstants;
import com.synopsys.integration.phonehome.google.analytics.GoogleAnalyticsRequestHelper;

public class PhoneHomeClient {
Expand All @@ -46,40 +44,49 @@ public class PhoneHomeClient {
public static final String SKIP_PHONE_HOME_VARIABLE = "SYNOPSYS_SKIP_PHONE_HOME";
public static final String PHONE_HOME_URL_OVERRIDE_VARIABLE = "SYNOPSYS_PHONE_HOME_URL_OVERRIDE";

private final String googleAnalyticsTrackingId;
private final HttpClientBuilder httpClientBuilder;
private final IntLogger logger;
private final Gson gson;

private String phoneHomeBackendUrl;

public PhoneHomeClient(final String googleAnalyticsTrackingId, final IntLogger logger) {
this(googleAnalyticsTrackingId, logger, createInitialRequestConfigBuilder(10, Optional.empty()).build());
public PhoneHomeClient(final IntLogger logger) {
this(logger, createInitialRequestConfigBuilder(10).build());
}

public PhoneHomeClient(final String googleAnalyticsTrackingId, final IntLogger logger, final Gson gson) {
this(googleAnalyticsTrackingId, logger, createInitialRequestConfigBuilder(10, Optional.empty()).build(), gson);
public PhoneHomeClient(final IntLogger logger, final Gson gson) {
this(logger, createInitialRequestConfigBuilder(10).build(), gson);
}

public PhoneHomeClient(final String googleAnalyticsTrackingId, final IntLogger logger, final RequestConfig httpRequestConfig) {
this(googleAnalyticsTrackingId, logger, httpRequestConfig, new Gson());
public PhoneHomeClient(final IntLogger logger, final RequestConfig httpRequestConfig) {
this(logger, httpRequestConfig, new Gson());
}

public PhoneHomeClient(final String googleAnalyticsTrackingId, final IntLogger logger, final RequestConfig httpRequestConfig, final Gson gson) {
this(googleAnalyticsTrackingId, logger, HttpClientBuilder.create().setDefaultRequestConfig(httpRequestConfig), gson);
public PhoneHomeClient(final IntLogger logger, final RequestConfig httpRequestConfig, final Gson gson) {
this(logger, HttpClientBuilder.create().setDefaultRequestConfig(httpRequestConfig), gson);
}

public PhoneHomeClient(final String googleAnalyticsTrackingId, final IntLogger logger, final HttpClientBuilder httpClientBuilder) {
this(googleAnalyticsTrackingId, logger, httpClientBuilder, new Gson());
public PhoneHomeClient(final IntLogger logger, final HttpClientBuilder httpClientBuilder) {
this(logger, httpClientBuilder, new Gson());
}

public PhoneHomeClient(final String googleAnalyticsTrackingId, final IntLogger logger, final HttpClientBuilder httpClientBuilder, final Gson gson) {
this.googleAnalyticsTrackingId = googleAnalyticsTrackingId;
public PhoneHomeClient(final IntLogger logger, final HttpClientBuilder httpClientBuilder, final Gson gson) {
this.httpClientBuilder = httpClientBuilder;
this.logger = logger;
this.gson = gson;
}

public static RequestConfig.Builder createInitialRequestConfigBuilder(final int timeoutSeconds) {
final int timeoutInMillis = timeoutSeconds * 1000;
final RequestConfig.Builder builder = RequestConfig.custom();
builder.setConnectionRequestTimeout(timeoutInMillis);
builder.setConnectTimeout(timeoutInMillis);
builder.setSocketTimeout(timeoutInMillis);
return builder;
}

this.phoneHomeBackendUrl = GoogleAnalyticsConstants.BASE_URL + GoogleAnalyticsConstants.COLLECT_ENDPOINT;
public static RequestConfig.Builder createInitialRequestConfigBuilder(final int timeoutSeconds, final HttpHost proxyHost) {
final RequestConfig.Builder builder = createInitialRequestConfigBuilder(timeoutSeconds);
builder.setProxy(proxyHost);
return builder;
}

public void postPhoneHomeRequest(final PhoneHomeRequestBody phoneHomeRequestBody, final Map<String, String> environmentVariables) throws PhoneHomeException {
Expand All @@ -90,32 +97,27 @@ public void postPhoneHomeRequest(final PhoneHomeRequestBody phoneHomeRequestBody
if (phoneHomeRequestBody == null) {
throw new PhoneHomeException("The request body must not be null.");
}
checkOverridePhoneHomeUrl(environmentVariables);
logger.debug("Phoning home to " + phoneHomeBackendUrl);
String overrideUrl = checkOverridePhoneHomeUrl(environmentVariables);

try (final CloseableHttpClient client = httpClientBuilder.build()) {
final GoogleAnalyticsRequestHelper requestHelper = new GoogleAnalyticsRequestHelper(gson, googleAnalyticsTrackingId, phoneHomeRequestBody);
final HttpUriRequest request = requestHelper.createRequest(phoneHomeBackendUrl);
final GoogleAnalyticsRequestHelper requestHelper = new GoogleAnalyticsRequestHelper(gson);
final HttpUriRequest request;

if (overrideUrl != null) {
logger.debug("Overriding Phone-Home URL: " + overrideUrl);
request = requestHelper.createRequest(phoneHomeRequestBody, overrideUrl);
} else {
request = requestHelper.createRequest(phoneHomeRequestBody);
}

logger.debug("Phoning home to " + request.getURI());
final HttpResponse response = client.execute(request);
logger.trace("Response Code: " + response.getStatusLine().getStatusCode());
} catch (final Exception e) {
throw new PhoneHomeException(e.getMessage(), e);
}
}

public static RequestConfig.Builder createInitialRequestConfigBuilder(final int timeoutSeconds, final Optional<HttpHost> proxyHost) {
final int timeoutInMillis = timeoutSeconds * 1000;
final RequestConfig.Builder builder = RequestConfig.custom();
builder.setConnectionRequestTimeout(timeoutInMillis);
builder.setConnectTimeout(timeoutInMillis);
builder.setSocketTimeout(timeoutInMillis);
if (proxyHost.isPresent()) {
builder.setProxy(proxyHost.get());
}
return builder;
}

private boolean skipPhoneHome(final Map<String, String> environmentVariables) {
if (environmentVariables.containsKey(SKIP_PHONE_HOME_VARIABLE) || environmentVariables.containsKey(BLACKDUCK_SKIP_PHONE_HOME_VARIABLE)) {
String valueString = environmentVariables.get(SKIP_PHONE_HOME_VARIABLE);
Expand All @@ -127,17 +129,15 @@ private boolean skipPhoneHome(final Map<String, String> environmentVariables) {
return false;
}

private void checkOverridePhoneHomeUrl(final Map<String, String> environmentVariables) {
if (environmentVariables.containsKey(PHONE_HOME_URL_OVERRIDE_VARIABLE) || environmentVariables.containsKey(BLACKDUCK_PHONE_HOME_URL_OVERRIDE_VARIABLE)) {
String overrideUrl = environmentVariables.get(PHONE_HOME_URL_OVERRIDE_VARIABLE);
if (StringUtils.isBlank(overrideUrl)) {
overrideUrl = environmentVariables.get(BLACKDUCK_PHONE_HOME_URL_OVERRIDE_VARIABLE);
}
if (StringUtils.isNotBlank(overrideUrl)) {
phoneHomeBackendUrl = overrideUrl;
logger.debug("Overriding Phone-Home URL: " + overrideUrl);
}
private String checkOverridePhoneHomeUrl(final Map<String, String> environmentVariables) {
String overrideUrl;

overrideUrl = environmentVariables.get(PHONE_HOME_URL_OVERRIDE_VARIABLE);
if (StringUtils.isBlank(overrideUrl)) {
overrideUrl = environmentVariables.get(BLACKDUCK_PHONE_HOME_URL_OVERRIDE_VARIABLE);
}

return overrideUrl;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
Expand All @@ -45,6 +47,7 @@ public class PhoneHomeRequestBody {
private final String artifactVersion;
private final ProductIdEnum productId;
private final String productVersion;
private final List<String> artifactModules;
private final Map<String, String> metaData;

private PhoneHomeRequestBody(final PhoneHomeRequestBody.Builder builder) {
Expand All @@ -54,6 +57,7 @@ private PhoneHomeRequestBody(final PhoneHomeRequestBody.Builder builder) {
artifactVersion = builder.getArtifactVersion();
productId = builder.getProductId();
productVersion = builder.getProductVersion();
artifactModules = builder.getArtifactModules();
metaData = Collections.unmodifiableMap(builder.getMetaData());
}

Expand Down Expand Up @@ -85,16 +89,20 @@ public Map<String, String> getMetaData() {
return metaData;
}

public List<String> getArtifactModules() {
return artifactModules;
}

public static class Builder {
public static final String UNKNOWN_ID = "<unknown>";

private final Map<String, String> metaData = new HashMap<>();
private String customerId;
private String hostName;
private String artifactId;
private String artifactVersion;
private ProductIdEnum productId;
private String productVersion;
private final Map<String, String> metaData = new HashMap<>();
private List<String> artifactModules;

// PhoneHomeRequestBody only has a private constructor to force creation through the builder.
public PhoneHomeRequestBody build() throws IllegalStateException {
Expand Down Expand Up @@ -157,6 +165,14 @@ public void setProductVersion(final String productVersion) {
this.productVersion = productVersion;
}

public List<String> getArtifactModules() {
return artifactModules;
}

public void setArtifactModules(final String... artifactModules) {
this.artifactModules = Arrays.asList(artifactModules);
}

public Map<String, String> getMetaData() {
return Collections.unmodifiableMap(new HashMap<>(metaData));
}
Expand All @@ -180,7 +196,7 @@ public boolean addToMetaData(final String key, final String value) {
*/
public boolean addAllToMetaData(final Map<String, String> metadataMap) {
return metadataMap.entrySet().stream()
.allMatch(entry -> addToMetaData(entry.getKey(), entry.getValue()));
.allMatch(entry -> addToMetaData(entry.getKey(), entry.getValue()));
}

public String md5Hash(final String string) throws NoSuchAlgorithmException, UnsupportedEncodingException {
Expand All @@ -200,7 +216,6 @@ private int charactersInMetaDataMap(final String key, final String value) {
final String mapAsString = getMetaData().toString();
return mapEntryWrappingCharacters + mapAsString.length() + key.length() + value.length();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class PhoneHomeResponse {
private final Future<Boolean> phoneHomeTask;
private final Boolean result;

private PhoneHomeResponse(final Future<Boolean> phoneHomeTask, final Boolean result) {
this.phoneHomeTask = phoneHomeTask;
this.result = result;
}

public static PhoneHomeResponse createResponse(final Boolean result) {
return new PhoneHomeResponse(null, result);
}
Expand All @@ -39,11 +44,6 @@ public static PhoneHomeResponse createAsynchronousResponse(final Future<Boolean>
return new PhoneHomeResponse(phoneHomeTask, Boolean.FALSE);
}

private PhoneHomeResponse(final Future<Boolean> phoneHomeTask, final Boolean result) {
this.phoneHomeTask = phoneHomeTask;
this.result = result;
}

/**
* If Phone Home was called asynchronously: If the result is available, it will be returned, otherwise the task will be cancelled and this will return Boolean.FALSE.
* If Phone Home was called synchronously: The result of the synchronous call will be returned.
Expand All @@ -59,7 +59,10 @@ public Boolean awaitResult(final long timeoutInSeconds) {
if (phoneHomeTask != null) {
try {
return phoneHomeTask.get(timeoutInSeconds, TimeUnit.SECONDS);
} catch (final InterruptedException | ExecutionException | TimeoutException e) {
} catch (final ExecutionException | TimeoutException e) {
return Boolean.FALSE;
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
return Boolean.FALSE;
} finally {
endPhoneHome();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class GoogleAnalyticsConstants {
// Api Path(s)
public static final String BASE_URL = "https://www.google-analytics.com";
public static final String COLLECT_ENDPOINT = "/collect";
public static final String BATCH_ENDPOINT = "/batch";
public static final String DEBUG_ENDPOINT = "/debug" + COLLECT_ENDPOINT;

// Payload Data - Required
Expand All @@ -48,5 +49,6 @@ public class GoogleAnalyticsConstants {
public static final String PRODUCT_VERSION = "cd5";
public static final String META_DATA = "cd6";
public static final String HOST_NAME = "cd7";
public static final String MODULE_ID = "cd8";

}
Loading

0 comments on commit c16fcf7

Please sign in to comment.