22
33import com .bunq .sdk .exception .BunqException ;
44import com .bunq .sdk .json .BunqGsonBuilder ;
5- import com .bunq .sdk .model .DeviceServer ;
65import com .bunq .sdk .model .Installation ;
76import com .bunq .sdk .model .SessionServer ;
7+ import com .bunq .sdk .model .generated .DeviceServer ;
88import com .bunq .sdk .model .generated .Session ;
99import com .bunq .sdk .security .SecurityUtils ;
1010import com .google .gson .Gson ;
11- import com .google .gson .JsonObject ;
1211import com .google .gson .annotations .Expose ;
1312import com .google .gson .annotations .SerializedName ;
14- import com .sun .istack .internal .Nullable ;
1513import java .io .File ;
1614import java .io .IOException ;
1715import java .net .URI ;
1816import java .security .KeyPair ;
1917import java .util .ArrayList ;
2018import java .util .Date ;
19+ import java .util .HashMap ;
2120import java .util .List ;
21+ import java .util .Map ;
22+ import javax .annotation .Nullable ;
2223import org .apache .commons .io .FileUtils ;
2324
2425/**
@@ -81,6 +82,10 @@ public class ApiContext implements java.io.Serializable {
8182 @ SerializedName ("session_context" )
8283 private SessionContext sessionContext ;
8384
85+ @ Expose
86+ @ SerializedName ("proxy" )
87+ private String proxy ;
88+
8489 /**
8590 * Create an empty API context.
8691 */
@@ -90,19 +95,36 @@ private ApiContext(ApiEnvironmentType environmentType, String apiKey) {
9095 }
9196
9297 /**
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 .
9499 */
95100 public static ApiContext create (ApiEnvironmentType environmentType , String apiKey ,
96101 String deviceDescription ) {
97102 return create (environmentType , apiKey , deviceDescription , new ArrayList <>());
98103 }
99104
100105 /**
101- * Create and initialize an API Context.
106+ * Create and initialize an API Context with given permitted ips and no proxy .
102107 */
103108 public static ApiContext create (ApiEnvironmentType environmentType , String apiKey ,
104109 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 ) {
105126 ApiContext apiContext = new ApiContext (environmentType , apiKey );
127+ apiContext .proxy = proxy ;
106128 apiContext .initialize (deviceDescription , permittedIps );
107129
108130 return apiContext ;
@@ -122,14 +144,20 @@ public static ApiContext restore(String fileName) {
122144 try {
123145 File file = new File (fileName );
124146 String json = FileUtils .readFileToString (file , ENCODING_BUNQ_CONF );
125- JsonObject jsonObject = gson .fromJson (json , JsonObject .class );
126147
127- return gson . fromJson (jsonObject , ApiContext . class );
148+ return fromJson (json );
128149 } catch (IOException exception ) {
129150 throw new BunqException (ERROR_COULD_NOT_RESTORE_API_CONTEXT , exception );
130151 }
131152 }
132153
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+
133161 private void initialize (String deviceDescription , List <String > permittedIps ) {
134162 /* The calls below are order-sensitive: to initialize a Device Registration, we need an
135163 * Installation, and to initialize a Session we need a Device Registration. */
@@ -146,19 +174,31 @@ private void initializeInstallation() {
146174 Installation installation = Installation .create (
147175 this ,
148176 SecurityUtils .getPublicKeyFormattedString (keyPairClient .getPublic ())
149- );
177+ ). getValue () ;
150178 installationContext = new InstallationContext (installation , keyPairClient );
151179 }
152180
153181 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 ;
155195 }
156196
157197 /**
158198 * Create a new session and its data in a SessionContext.
159199 */
160200 private void initializeSession () {
161- sessionContext = new SessionContext (SessionServer .create (this ));
201+ sessionContext = new SessionContext (SessionServer .create (this ). getValue () );
162202 }
163203
164204 /**
@@ -219,13 +259,19 @@ public void save() {
219259 public void save (String fileName ) {
220260 try {
221261 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 );
224263 } catch (IOException exception ) {
225264 throw new BunqException (ERROR_COULD_NOT_SAVE_API_CONTEXT , exception );
226265 }
227266 }
228267
268+ /**
269+ * Serializes the context to JSON.
270+ */
271+ public String toJson () {
272+ return gson .toJson (this );
273+ }
274+
229275 /**
230276 * @return The base URI of the current environment.
231277 */
@@ -264,4 +310,8 @@ public SessionContext getSessionContext() {
264310 return sessionContext ;
265311 }
266312
313+ public String getProxy () {
314+ return proxy ;
315+ }
316+
267317}
0 commit comments