2
2
3
3
import com .bunq .sdk .exception .BunqException ;
4
4
import com .bunq .sdk .json .BunqGsonBuilder ;
5
- import com .bunq .sdk .model .DeviceServer ;
6
5
import com .bunq .sdk .model .Installation ;
7
6
import com .bunq .sdk .model .SessionServer ;
7
+ import com .bunq .sdk .model .generated .DeviceServer ;
8
8
import com .bunq .sdk .model .generated .Session ;
9
9
import com .bunq .sdk .security .SecurityUtils ;
10
10
import com .google .gson .Gson ;
11
- import com .google .gson .JsonObject ;
12
11
import com .google .gson .annotations .Expose ;
13
12
import com .google .gson .annotations .SerializedName ;
14
- import com .sun .istack .internal .Nullable ;
15
13
import java .io .File ;
16
14
import java .io .IOException ;
17
15
import java .net .URI ;
18
16
import java .security .KeyPair ;
19
17
import java .util .ArrayList ;
20
18
import java .util .Date ;
19
+ import java .util .HashMap ;
21
20
import java .util .List ;
21
+ import java .util .Map ;
22
+ import javax .annotation .Nullable ;
22
23
import org .apache .commons .io .FileUtils ;
23
24
24
25
/**
@@ -81,6 +82,10 @@ public class ApiContext implements java.io.Serializable {
81
82
@ SerializedName ("session_context" )
82
83
private SessionContext sessionContext ;
83
84
85
+ @ Expose
86
+ @ SerializedName ("proxy" )
87
+ private String proxy ;
88
+
84
89
/**
85
90
* Create an empty API context.
86
91
*/
@@ -90,19 +95,36 @@ private ApiContext(ApiEnvironmentType environmentType, String apiKey) {
90
95
}
91
96
92
97
/**
93
- * Create and initialize an API Context with current IP as permitted.
98
+ * Create and initialize an API Context with current IP as permitted and no proxy .
94
99
*/
95
100
public static ApiContext create (ApiEnvironmentType environmentType , String apiKey ,
96
101
String deviceDescription ) {
97
102
return create (environmentType , apiKey , deviceDescription , new ArrayList <>());
98
103
}
99
104
100
105
/**
101
- * Create and initialize an API Context.
106
+ * Create and initialize an API Context with given permitted ips and no proxy .
102
107
*/
103
108
public static ApiContext create (ApiEnvironmentType environmentType , String apiKey ,
104
109
String deviceDescription , List <String > permittedIps ) {
110
+ return create (environmentType , apiKey , deviceDescription , permittedIps , null );
111
+ }
112
+
113
+ /**
114
+ * Create and initialize an API Context with current IP as permitted and a proxy.
115
+ */
116
+ public static ApiContext create (ApiEnvironmentType environmentType , String apiKey ,
117
+ String deviceDescription , String proxy ) {
118
+ return create (environmentType , apiKey , deviceDescription , new ArrayList <>(), proxy );
119
+ }
120
+
121
+ /**
122
+ * Create and initialize an API Context.
123
+ */
124
+ public static ApiContext create (ApiEnvironmentType environmentType , String apiKey ,
125
+ String deviceDescription , List <String > permittedIps , String proxy ) {
105
126
ApiContext apiContext = new ApiContext (environmentType , apiKey );
127
+ apiContext .proxy = proxy ;
106
128
apiContext .initialize (deviceDescription , permittedIps );
107
129
108
130
return apiContext ;
@@ -122,14 +144,20 @@ public static ApiContext restore(String fileName) {
122
144
try {
123
145
File file = new File (fileName );
124
146
String json = FileUtils .readFileToString (file , ENCODING_BUNQ_CONF );
125
- JsonObject jsonObject = gson .fromJson (json , JsonObject .class );
126
147
127
- return gson . fromJson (jsonObject , ApiContext . class );
148
+ return fromJson (json );
128
149
} catch (IOException exception ) {
129
150
throw new BunqException (ERROR_COULD_NOT_RESTORE_API_CONTEXT , exception );
130
151
}
131
152
}
132
153
154
+ /**
155
+ * Restores a context from a given JSON string.
156
+ */
157
+ public static ApiContext fromJson (String json ) {
158
+ return gson .fromJson (json , ApiContext .class );
159
+ }
160
+
133
161
private void initialize (String deviceDescription , List <String > permittedIps ) {
134
162
/* The calls below are order-sensitive: to initialize a Device Registration, we need an
135
163
* Installation, and to initialize a Session we need a Device Registration. */
@@ -146,19 +174,31 @@ private void initializeInstallation() {
146
174
Installation installation = Installation .create (
147
175
this ,
148
176
SecurityUtils .getPublicKeyFormattedString (keyPairClient .getPublic ())
149
- );
177
+ ). getValue () ;
150
178
installationContext = new InstallationContext (installation , keyPairClient );
151
179
}
152
180
153
181
private void initializeDeviceRegistration (String deviceDescription , List <String > permittedIps ) {
154
- DeviceServer .create (this , deviceDescription , permittedIps );
182
+ Map <String , Object > deviceServerRequestBody = generateDeviceServerRequestBodyBytes (
183
+ deviceDescription , permittedIps );
184
+ DeviceServer .create (this , deviceServerRequestBody );
185
+ }
186
+
187
+ private Map <String , Object > generateDeviceServerRequestBodyBytes (String description ,
188
+ List <String > permittedIps ) {
189
+ HashMap <String , Object > deviceServerRequestBody = new HashMap <>();
190
+ deviceServerRequestBody .put (DeviceServer .FIELD_DESCRIPTION , description );
191
+ deviceServerRequestBody .put (DeviceServer .FIELD_SECRET , apiKey );
192
+ deviceServerRequestBody .put (DeviceServer .FIELD_PERMITTED_IPS , permittedIps );
193
+
194
+ return deviceServerRequestBody ;
155
195
}
156
196
157
197
/**
158
198
* Create a new session and its data in a SessionContext.
159
199
*/
160
200
private void initializeSession () {
161
- sessionContext = new SessionContext (SessionServer .create (this ));
201
+ sessionContext = new SessionContext (SessionServer .create (this ). getValue () );
162
202
}
163
203
164
204
/**
@@ -219,13 +259,19 @@ public void save() {
219
259
public void save (String fileName ) {
220
260
try {
221
261
File file = new File (fileName );
222
- String json = gson .toJson (this );
223
- FileUtils .writeStringToFile (file , json , ENCODING_BUNQ_CONF );
262
+ FileUtils .writeStringToFile (file , toJson (), ENCODING_BUNQ_CONF );
224
263
} catch (IOException exception ) {
225
264
throw new BunqException (ERROR_COULD_NOT_SAVE_API_CONTEXT , exception );
226
265
}
227
266
}
228
267
268
+ /**
269
+ * Serializes the context to JSON.
270
+ */
271
+ public String toJson () {
272
+ return gson .toJson (this );
273
+ }
274
+
229
275
/**
230
276
* @return The base URI of the current environment.
231
277
*/
@@ -264,4 +310,8 @@ public SessionContext getSessionContext() {
264
310
return sessionContext ;
265
311
}
266
312
313
+ public String getProxy () {
314
+ return proxy ;
315
+ }
316
+
267
317
}
0 commit comments