-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathApiConfig.java
169 lines (139 loc) · 4.07 KB
/
ApiConfig.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.force.api;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.URLDecoder;
public class ApiConfig {
ApiVersion apiVersion = ApiVersion.DEFAULT_VERSION;
String apiVersionString;
String username;
String password;
String loginEndpoint = "https://login.salesforce.com";
String clientId;
String clientSecret;
String redirectURI;
SessionRefreshListener sessionRefreshListener;
ObjectMapper objectMapper;
int requestTimeout = 0; // in milliseconds, defaults to 0 which is no timeout (infinity)
public ApiConfig() {
objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
public ApiConfig clone() {
return new ApiConfig()
.setApiVersion(apiVersion)
.setUsername(username)
.setPassword(password)
.setLoginEndpoint(loginEndpoint)
.setClientId(clientId)
.setClientSecret(clientSecret)
.setRedirectURI(redirectURI)
.setObjectMapper(objectMapper)
.setRequestTimeout(requestTimeout);
}
public ApiConfig setForceURL(String url) {
try {
URI uri = new URI(url);
loginEndpoint = "https://"+uri.getHost()+(uri.getPort()>0 ? ":"+uri.getPort() : "");
if (uri.getQuery() != null) {
String[] params = uri.getQuery().split("&");
for(String param : params) {
String[] kv = param.split("=");
if(kv[0].equals("user")) {
username = URLDecoder.decode(kv[1],"UTF-8");
} else if(kv[0].equals("password")) {
password = URLDecoder.decode(kv[1],"UTF-8");
} else if(kv[0].equals("oauth_key")) {
clientId = URLDecoder.decode(kv[1],"UTF-8");
} else if(kv[0].equals("oauth_secret")) {
clientSecret = URLDecoder.decode(kv[1],"UTF-8");
}
}
}
} catch (Exception e) {
throw new IllegalArgumentException("Couldn't parse URL: "+url,e);
}
return this;
}
public ApiConfig setRedirectURI(String redirectURI) {
this.redirectURI = redirectURI;
return this;
}
public ApiConfig setApiVersion(ApiVersion value) {
apiVersion = value;
return this;
}
public ApiConfig setApiVersionString(String value) {
apiVersionString = value;
return this;
}
public ApiConfig setUsername(String value) {
username = value;
return this;
}
public ApiConfig setPassword(String value) {
password = value;
return this;
}
public ApiConfig setLoginEndpoint(String value) {
loginEndpoint = value;
return this;
}
public ApiConfig setClientId(String value) {
clientId = value;
return this;
}
public ApiConfig setClientSecret(String value) {
clientSecret = value;
return this;
}
public ApiConfig setSessionRefreshListener(SessionRefreshListener value) {
sessionRefreshListener = value;
return this;
}
public ApiConfig setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return this;
}
/**
* sets both connect timeout and read timeout on HttpUrlConnection to the specified value. A value of 0 (zero)
* means no timeout. The default is no timeout.
* @param requestTimeout timeout in milliseconds. A value of 0 means no timeout.
* @return this ApiConfig instance
*/
public ApiConfig setRequestTimeout(int requestTimeout) {
this.requestTimeout = requestTimeout;
return this;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getLoginEndpoint() {
return loginEndpoint;
}
public String getClientId() {
return clientId;
}
public String getClientSecret() {
return clientSecret;
}
public String getRedirectURI() {
return redirectURI;
}
public SessionRefreshListener getSessionRefreshListener() { return sessionRefreshListener; }
public ObjectMapper getObjectMapper() { return objectMapper; }
public int getRequestTimeout() { return requestTimeout; }
/**
* @deprecated use #getApiVersionString instead
* @return enum representing api version
*/
public ApiVersion getApiVersion() {
return apiVersion;
}
public String getApiVersionString() {
return apiVersionString != null ? apiVersionString : apiVersion.toString();
}
}