-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathUsersyncInfoBuilder.java
More file actions
98 lines (77 loc) · 4.01 KB
/
UsersyncInfoBuilder.java
File metadata and controls
98 lines (77 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package org.prebid.server.bidder;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.privacy.model.Privacy;
import org.prebid.server.proto.response.UsersyncInfo;
import org.prebid.server.util.HttpUtil;
import java.util.List;
import java.util.stream.Collectors;
public class UsersyncInfoBuilder {
private String usersyncUrl;
private String redirectUrl;
private UsersyncMethodType type;
public static UsersyncInfoBuilder from(UsersyncMethod usersyncMethod) {
final UsersyncInfoBuilder usersyncInfoBuilder = new UsersyncInfoBuilder();
usersyncInfoBuilder.usersyncUrl = StringUtils.defaultString(usersyncMethod.getUsersyncUrl());
usersyncInfoBuilder.redirectUrl = UsersyncUtil.enrichUrlWithFormat(
StringUtils.stripToEmpty(usersyncMethod.getRedirectUrl()),
UsersyncUtil.resolveFormat(usersyncMethod));
usersyncInfoBuilder.type = usersyncMethod.getType();
return usersyncInfoBuilder;
}
public UsersyncInfoBuilder usersyncUrl(String usersyncUrl) {
this.usersyncUrl = StringUtils.defaultString(usersyncUrl);
return this;
}
public UsersyncInfoBuilder redirectUrl(String redirectUrl) {
this.redirectUrl = StringUtils.defaultString(redirectUrl);
return this;
}
public UsersyncInfoBuilder privacy(Privacy privacy) {
final String gdpr = StringUtils.defaultString(privacy.getGdpr());
final String consent = StringUtils.defaultString(privacy.getConsentString());
final String ccpa = StringUtils.defaultString(privacy.getCcpa().getUsPrivacy());
final String gpp = StringUtils.defaultString(privacy.getGpp());
final String gppSid = toString(privacy.getGppSid());
redirectUrl = updateUrlWithPrivacy(redirectUrl, gdpr, consent, ccpa, gpp, gppSid);
final String encodedGdpr = HttpUtil.encodeUrl(gdpr);
final String encodedConsent = HttpUtil.encodeUrl(consent);
final String encodedUsPrivacy = HttpUtil.encodeUrl(ccpa);
final String encodedGpp = HttpUtil.encodeUrl(gpp);
final String endodedGppSid = HttpUtil.encodeUrl(gppSid);
usersyncUrl = updateUrlWithPrivacy(
usersyncUrl, encodedGdpr, encodedConsent, encodedUsPrivacy, encodedGpp, endodedGppSid);
return this;
}
private static String toString(List<Integer> gppSid) {
return CollectionUtils.emptyIfNull(gppSid).stream()
.map(String::valueOf)
.collect(Collectors.joining(","));
}
private static String updateUrlWithPrivacy(String url,
String gdpr,
String gdprConsent,
String usPrivacy,
String gpp,
String gppSid) {
return url
.replace(UsersyncInfo.GDPR_PLACEHOLDER, gdpr)
.replace(UsersyncInfo.GDPR_CONSENT_PLACEHOLDER, gdprConsent)
.replace(UsersyncInfo.US_PRIVACY_PLACEHOLDER, usPrivacy)
.replace(UsersyncInfo.GPP_PLACEHOLDER, gpp)
.replace(UsersyncInfo.GPP_SID_PLACEHOLDER, gppSid);
}
public UsersyncInfo build() {
final String resolvedRedirectUrl = StringUtils.countMatches(redirectUrl, '?') > 1
? resolveQueryParams(redirectUrl)
: HttpUtil.encodeUrl(redirectUrl);
final String resolvedUsersyncUrl = usersyncUrl.replace(
UsersyncInfo.REDIRECT_URL_PLACEHOLDER, resolvedRedirectUrl);
return UsersyncInfo.of(resolvedUsersyncUrl, type);
}
private static String resolveQueryParams(String redirectUrl) {
final int queryParamsIndex = redirectUrl.lastIndexOf('?');
final String queryParams = redirectUrl.substring(queryParamsIndex);
return HttpUtil.encodeUrl(redirectUrl.substring(0, queryParamsIndex)) + queryParams;
}
}