Skip to content

Commit ca932ab

Browse files
🪴 plugins support
1 parent 78fa428 commit ca932ab

File tree

9 files changed

+158
-29
lines changed

9 files changed

+158
-29
lines changed

src/main/java/com/contentstack/sdk/Asset.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ public Asset configure(JSONObject jsonObject) {
8787
* <b>Example :</b><br>
8888
*
8989
* <pre class="prettyprint">
90-
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
91-
* Asset asset = stack.asset(asset_uid);
92-
* asset.setHeader();
93-
* </pre>
90+
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
91+
* Asset asset = stack.asset(asset_uid);
92+
* asset.setHeader();
93+
* </pre>
9494
*/
9595
public void setHeader(@NotNull String headerKey, @NotNull String headerValue) {
9696
headers.put(headerKey, headerValue);
@@ -108,11 +108,10 @@ public void setHeader(@NotNull String headerKey, @NotNull String headerValue) {
108108
* <b>Example :</b><br>
109109
*
110110
* <pre class="prettyprint">
111-
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
112-
* Asset asset = stack.asset(asset_uid);
113-
* asset.removeHeader();
114-
*
115-
* </pre>
111+
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
112+
* Asset asset = stack.asset(asset_uid);
113+
* asset.removeHeader();
114+
* </pre>
116115
*/
117116
public void removeHeader(@NotNull String headerKey) {
118117
headers.remove(headerKey);

src/main/java/com/contentstack/sdk/CSBackgroundTask.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected CSBackgroundTask(Stack stackInstance, String controller, String url, H
2323
csConnectionRequest.setStackInstance(stackInstance);
2424
csConnectionRequest.setURLQueries(urlParams);
2525
this.service = stackInstance.service;
26-
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service);
26+
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service, stackInstance);
2727

2828
}
2929

@@ -37,7 +37,7 @@ protected CSBackgroundTask(Query queryInstance, Stack stackInstance, String cont
3737
csConnectionRequest.setURLQueries(urlQueries);
3838
this.service = stackInstance.service;
3939
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service,
40-
stackInstance.config);
40+
stackInstance);
4141

4242
}
4343

@@ -49,7 +49,7 @@ protected CSBackgroundTask(Entry entryInstance, Stack stackInstance, String cont
4949
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(entryInstance);
5050
csConnectionRequest.setURLQueries(urlQueries);
5151
this.service = stackInstance.service;
52-
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callBack, this.service);
52+
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callBack, this.service, stackInstance);
5353
}
5454

5555
protected CSBackgroundTask(AssetLibrary assetLibrary, Stack stackInstance, String controller, String url,
@@ -60,7 +60,7 @@ protected CSBackgroundTask(AssetLibrary assetLibrary, Stack stackInstance, Strin
6060
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(assetLibrary);
6161
csConnectionRequest.setURLQueries(urlQueries);
6262
this.service = stackInstance.service;
63-
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service);
63+
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service, stackInstance);
6464

6565
}
6666

@@ -72,7 +72,7 @@ protected CSBackgroundTask(Asset asset, Stack stackInstance, String controller,
7272
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(asset);
7373
csConnectionRequest.setURLQueries(urlQueries);
7474
this.service = stackInstance.service;
75-
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service);
75+
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service, stackInstance);
7676
}
7777

7878
protected CSBackgroundTask(ContentType contentType, Stack stackInstance, String controller, String url,
@@ -83,7 +83,7 @@ protected CSBackgroundTask(ContentType contentType, Stack stackInstance, String
8383
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(contentType);
8484
csConnectionRequest.setURLQueries(urlParams);
8585
this.service = stackInstance.service;
86-
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service);
86+
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service, stackInstance);
8787
}
8888

8989
protected void checkHeader(@NotNull Map<String, Object> headers) {

src/main/java/com/contentstack/sdk/CSConnectionRequest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public void setParams(Object... objects) {
7676
}
7777
this.service = (APIService) objects[5];
7878
if (objects.length > 6) {
79-
this.config = (Config) objects[6];
79+
this.stackInstance = (Stack) objects[6];
80+
this.config = this.stackInstance.config;
8081
}
8182
sendRequest();
8283
}
@@ -89,6 +90,7 @@ public void sendRequest() {
8990
connection.setInfo(requestInfo);
9091
connection.setAPIService(this.service);
9192
connection.setConfig(this.config);
93+
connection.setStack(this.stackInstance);
9294
connection.setCallBackObject(resultCallBack);
9395
if (urlQueries != null && urlQueries.size() > 0) {
9496
connection.setFormParams(urlQueries);

src/main/java/com/contentstack/sdk/CSHttpConnection.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.contentstack.sdk;
22

3+
import okhttp3.Request;
34
import okhttp3.ResponseBody;
45
import org.json.JSONArray;
56
import org.json.JSONException;
67
import org.json.JSONObject;
8+
import retrofit2.Call;
79
import retrofit2.Response;
810

911
import java.io.IOException;
@@ -30,6 +32,7 @@ public class CSHttpConnection implements IURLRequestHTTP {
3032
private String info;
3133
private APIService service;
3234
private Config config;
35+
private Stack stackInstance;
3336
private ResultCallBack callBackObject;
3437
private JSONObject responseJSON;
3538
private HashMap<String, Object> formParams;
@@ -186,9 +189,15 @@ private void getService(String requestUrl) throws IOException {
186189
this.headers.put(X_USER_AGENT_KEY, "contentstack-java/" + SDK_VERSION);
187190
this.headers.put(USER_AGENT_KEY, USER_AGENT);
188191
this.headers.put(CONTENT_TYPE, APPLICATION_JSON);
192+
193+
Request request = pluginRequestImp(requestUrl);
194+
195+
//Call call = client.newCall(request);
189196
Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute();
190197
if (response.isSuccessful()) {
191198
assert response.body() != null;
199+
// TODO: On Response result
200+
response = pluginResponseImp(request, response);
192201
responseJSON = new JSONObject(response.body().string());
193202
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
194203
handleJSONArray();
@@ -201,6 +210,18 @@ private void getService(String requestUrl) throws IOException {
201210

202211
}
203212

213+
private Request pluginRequestImp(String requestUrl) {
214+
Call<ResponseBody> call = this.service.getRequest(requestUrl, this.headers);
215+
Request request = call.request();
216+
this.config.plugins.forEach(plugin -> plugin.onRequest(this.stackInstance, request));
217+
return request;
218+
}
219+
220+
private Response<ResponseBody> pluginResponseImp(Request request, Response<ResponseBody> response) {
221+
this.config.plugins.forEach(plugin -> plugin.onResponse(this.stackInstance, request, response));
222+
return response;
223+
}
224+
204225

205226
void handleJSONArray() {
206227
if (responseJSON.has("entries") && !responseJSON.optJSONArray("entries").isEmpty()) {
@@ -248,4 +269,8 @@ public void setAPIService(APIService service) {
248269
public void setConfig(Config config) {
249270
this.config = config;
250271
}
272+
273+
public void setStack(Stack stackInstance) {
274+
this.stackInstance = stackInstance;
275+
}
251276
}

src/main/java/com/contentstack/sdk/Config.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.json.JSONObject;
66

77
import java.net.Proxy;
8+
import java.util.ArrayList;
89
import java.util.concurrent.TimeUnit;
910

1011

@@ -29,6 +30,8 @@ public class Config {
2930
protected Proxy proxy = null;
3031
protected ConnectionPool connectionPool = new ConnectionPool();
3132

33+
protected ArrayList<ContentstackPlugin> plugins;
34+
3235
public String getBranch() {
3336
return branch;
3437
}
@@ -112,6 +115,10 @@ protected String setEndpoint(@NotNull String endpoint) {
112115
return this.endpoint;
113116
}
114117

118+
public void setPlugins(ArrayList<ContentstackPlugin> plugins) {
119+
this.plugins = plugins;
120+
}
121+
115122
/**
116123
* Gets host.
117124
*

src/main/java/com/contentstack/sdk/Contentstack.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ protected Contentstack() throws IllegalAccessException {
3636
* <b>Example</b>
3737
*
3838
* <pre>
39-
* {
40-
* &#64;Code
41-
* Stack stack = contentstack.Stack("apiKey", "deliveryToken", "environment");
42-
* }
39+
* {
40+
* &#64;Code
41+
* Stack stack = contentstack.Stack("apiKey", "deliveryToken", "environment");
42+
* }
4343
*
44-
* </pre>
44+
* </pre>
4545
*/
4646
public static Stack stack(String stackApiKey, String deliveryToken, String environment)
4747
throws IllegalAccessException {
@@ -75,6 +75,7 @@ public static Stack stack(String stackApiKey, String deliveryToken, String envir
7575
return initializeStack(stackApiKey, deliveryToken, environment, config);
7676
}
7777

78+
7879
private static void validateCredentials(String stackApiKey, String deliveryToken, String environment)
7980
throws IllegalAccessException {
8081
Objects.requireNonNull(stackApiKey, "API Key can not be null");
@@ -92,6 +93,7 @@ private static void validateCredentials(String stackApiKey, String deliveryToken
9293
}
9394
}
9495

96+
9597
private static Stack initializeStack(String stackApiKey, String deliveryToken, String environment, Config config) {
9698
Stack stack = new Stack(stackApiKey.trim());
9799
stack.setHeader("api_key", stackApiKey);
@@ -100,6 +102,7 @@ private static Stack initializeStack(String stackApiKey, String deliveryToken, S
100102
if (config.getBranch() != null && !config.getBranch().isEmpty()) {
101103
stack.setHeader("branch", config.getBranch());
102104
}
105+
// TODO: implement plugins
103106
stack.setConfig(config);
104107
return stack;
105108
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.contentstack.sdk;
2+
3+
import okhttp3.Request;
4+
import retrofit2.Response;
5+
6+
public interface ContentstackPlugin {
7+
8+
void onRequest(Stack stack, Request request);
9+
10+
Response onResponse(Stack stack, Request request, Response response);
11+
}

src/main/java/com/contentstack/sdk/Stack.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,10 @@ public void syncPaginationToken(@NotNull String paginationToken, SyncResultCallB
372372
* <br>
373373
* <b>Example :</b><br>
374374
* <pre class="prettyprint">
375-
* Stack stack = contentstack.Stack("apiKey", "deliveryToken", "environment");
376-
* stack.syncToken("syncToken")
377-
* stack.syncToken(sync_token, new SyncResultCallBack() ){ }
378-
* </pre>
375+
* Stack stack = contentstack.Stack("apiKey", "deliveryToken", "environment");
376+
* stack.syncToken("syncToken")
377+
* stack.syncToken(sync_token, new SyncResultCallBack() ){ }
378+
* </pre>
379379
*/
380380
public void syncToken(String syncToken, SyncResultCallBack syncCallBack) {
381381
this.sync(null);
@@ -477,10 +477,10 @@ public void syncLocale(String localeCode, SyncResultCallBack syncCallBack) {
477477
* <br>
478478
* <b>Example :</b><br>
479479
* <pre class="prettyprint">
480-
* Stack stack = contentstack.Stack("apiKey", "deliveryToken", "environment");
481-
* stack.syncPublishType(PublishType)
482-
* stackInstance.syncPublishType(Stack.PublishType.entry_published, new SyncResultCallBack()) { }
483-
* </pre>
480+
* Stack stack = contentstack.Stack("apiKey", "deliveryToken", "environment");
481+
* stack.syncPublishType(PublishType)
482+
* stackInstance.syncPublishType(Stack.PublishType.entry_published, new SyncResultCallBack()) { }
483+
* </pre>
484484
*/
485485
public void syncPublishType(PublishType publishType, SyncResultCallBack syncCallBack) {
486486
this.sync(null);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.contentstack.sdk;
2+
3+
import io.github.cdimascio.dotenv.Dotenv;
4+
import okhttp3.Request;
5+
import org.junit.jupiter.api.*;
6+
import retrofit2.Response;
7+
8+
import java.util.ArrayList;
9+
import java.util.logging.Logger;
10+
11+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
12+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
13+
class TestContentstackPlugin {
14+
15+
protected String API_KEY, DELIVERY_TOKEN, ENV;
16+
private final Logger logger = Logger.getLogger(TestContentstackPlugin.class.getName());
17+
18+
@BeforeAll
19+
public void initBeforeTests() {
20+
Dotenv dotenv = Dotenv.load();
21+
API_KEY = dotenv.get("API_KEY");
22+
DELIVERY_TOKEN = dotenv.get("DELIVERY_TOKEN");
23+
ENV = dotenv.get("ENVIRONMENT");
24+
}
25+
26+
class Plugin1 implements ContentstackPlugin {
27+
28+
29+
@Override
30+
public void onRequest(Stack stack, Request request) {
31+
32+
}
33+
34+
@Override
35+
public Response onResponse(Stack stack, Request request, retrofit2.Response response) {
36+
return null;
37+
}
38+
}
39+
40+
41+
class Plugin2 implements ContentstackPlugin {
42+
43+
44+
@Override
45+
public void onRequest(Stack stack, Request request) {
46+
47+
}
48+
49+
@Override
50+
public Response onResponse(Stack stack, Request request, Response response) {
51+
return response;
52+
}
53+
}
54+
55+
56+
@Test
57+
@Order(1)
58+
void testContentstackPlugin() {
59+
try {
60+
61+
ArrayList<ContentstackPlugin> plugins = new ArrayList<>();
62+
Plugin1 plugin1 = new Plugin1();
63+
Plugin2 plugin2 = new Plugin2();
64+
65+
plugins.add(plugin1);
66+
plugins.add(plugin2);
67+
68+
69+
// Create a config instance:
70+
Config config = new Config();
71+
config.setPlugins(plugins);
72+
73+
Stack stack = Contentstack.stack(API_KEY, DELIVERY_TOKEN, ENV, config);
74+
ContentType contentType = stack.contentType("fakeCT");
75+
Entry entry = contentType.entry();
76+
77+
} catch (IllegalAccessException e) {
78+
throw new RuntimeException(e);
79+
}
80+
}
81+
82+
}

0 commit comments

Comments
 (0)