Repository navigation
Expand file tree
/
Copy pathBasePathManager.java
More file actions
75 lines (61 loc) · 1.93 KB
/
Copy pathBasePathManager.java
File metadata and controls
75 lines (61 loc) · 1.93 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
package com.pubnub.api.managers;
import com.pubnub.api.PNConfiguration;
/**
* A stateful manager to support base path construction, proxying and cache busting.
*/
public class BasePathManager {
/**
* PubNub configuration storage.
*/
private PNConfiguration config;
/**
* for cache busting, the current subdomain number used.
*/
private int currentSubdomain;
/**
* if using cache busting, this is the max number of subdomains that are supported.
*/
private static final int MAX_SUBDOMAIN = 20;
/**
* default subdomain used if cache busting is disabled.
*/
private static final String DEFAULT_SUBDOMAIN = "ps";
/**
* default base path if a custom one is not provided.
*/
private static final String DEFAULT_BASE_PATH = "pndsn.com";
/**
* Initialize the path management.
*
* @param initialConfig configuration object
*/
public BasePathManager(PNConfiguration initialConfig) {
this.config = initialConfig;
currentSubdomain = 1;
}
/**
* Prepares a next usable base url.
*
* @return usable base url.
*/
public String getBasePath() {
StringBuilder constructedUrl = new StringBuilder("http");
if (config.isSecure()) {
constructedUrl.append("s");
}
constructedUrl.append("://");
if (config.getOrigin() != null) {
constructedUrl.append(config.getOrigin());
} else if (config.isCacheBusting()) {
constructedUrl.append("ps").append(currentSubdomain).append(".").append(DEFAULT_BASE_PATH);
if (currentSubdomain == MAX_SUBDOMAIN) {
currentSubdomain = 1;
} else {
currentSubdomain += 1;
}
} else {
constructedUrl.append(DEFAULT_SUBDOMAIN).append(".").append(DEFAULT_BASE_PATH);
}
return constructedUrl.toString();
}
}