Skip to content

Commit 23ce30b

Browse files
🪴 setting proxy, live_preview
1 parent c128399 commit 23ce30b

File tree

15 files changed

+1241
-1019
lines changed

15 files changed

+1241
-1019
lines changed

pom.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.contentstack.sdk</groupId>
77
<artifactId>java</artifactId>
8-
<version>1.9.0-SNAPSHOT</version>
8+
<version>1.9.0</version>
99
<packaging>jar</packaging>
1010
<name>contentstack-java</name>
1111
<description>Java SDK for Contentstack Content Delivery API</description>
@@ -146,6 +146,13 @@
146146
<version>${loggin.version}</version>
147147
<scope>compile</scope>
148148
</dependency>
149+
<dependency>
150+
<groupId>com.squareup.retrofit2</groupId>
151+
<artifactId>converter-jackson</artifactId>
152+
<version>2.9.0</version>
153+
<scope>compile</scope>
154+
</dependency>
155+
149156
<dependency>
150157
<groupId>org.projectlombok</groupId>
151158
<artifactId>lombok</artifactId>
@@ -366,9 +373,9 @@
366373
</execution>
367374
</executions>
368375
<configuration>
369-
<!-- <systemPropertyVariables>-->
370-
<!-- <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>-->
371-
<!-- </systemPropertyVariables>-->
376+
<!-- <systemPropertyVariables>-->
377+
<!-- <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>-->
378+
<!-- </systemPropertyVariables>-->
372379
<excludes>
373380
<exclude>src/main/java/com/contentstack/sdk/Group.class</exclude>
374381
<exclude>src/main/java/com/contentstack/sdk/FetchAssetsCallback.class</exclude>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.LinkedHashMap;
1010

1111
public interface APIService {
12-
1312
@GET
1413
Call<ResponseBody> getRequest(@Url String url, @HeaderMap LinkedHashMap<String, Object> headers);
1514
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.jetbrains.annotations.NotNull;
44
import org.json.JSONObject;
5+
import retrofit2.Retrofit;
56

67
import java.util.Calendar;
78
import java.util.HashMap;
@@ -28,6 +29,7 @@ public class Asset {
2829
protected String[] tagsArray = null;
2930
protected LinkedHashMap<String, Object> headers;
3031
protected Stack stackInstance;
32+
protected Retrofit retrofit;
3133

3234
protected Asset() {
3335
this.headers = new LinkedHashMap<>();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.jetbrains.annotations.NotNull;
44
import org.json.JSONObject;
5+
import retrofit2.Retrofit;
56

67
import java.util.*;
78
import java.util.logging.Logger;
@@ -19,6 +20,7 @@ public class AssetLibrary implements INotifyClass {
1920
protected LinkedHashMap<String, Object> headers;
2021
protected FetchAssetsCallback callback;
2122
protected int count;
23+
protected Retrofit retrofit;
2224

2325
protected AssetLibrary() {
2426
this.urlQueries = new JSONObject();

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.contentstack.sdk;
22

33
import org.jetbrains.annotations.NotNull;
4+
import retrofit2.Retrofit;
45

56
import java.util.HashMap;
67
import java.util.LinkedHashMap;
@@ -9,71 +10,79 @@
910

1011
class CSBackgroundTask {
1112

13+
protected APIService service;
14+
1215
protected CSBackgroundTask() {
1316
}
1417

1518
protected CSBackgroundTask(Stack stackInstance, String controller, String url, HashMap<String, Object> headers,
16-
HashMap<String, Object> urlParams, String requestInfo, ResultCallBack callback) {
19+
HashMap<String, Object> urlParams, String requestInfo, ResultCallBack callback) {
1720
checkHeader(headers);
1821
String completeUrl = stackInstance.config.getEndpoint() + url;
1922
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(stackInstance);
2023
csConnectionRequest.setStackInstance(stackInstance);
2124
csConnectionRequest.setURLQueries(urlParams);
22-
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback);
25+
this.service = stackInstance.service;
26+
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service);
2327

2428
}
2529

2630
protected CSBackgroundTask(Query queryInstance, Stack stackInstance, String controller, String url,
27-
LinkedHashMap<String, Object> headers, HashMap<String, Object> urlQueries, String requestInfo,
28-
ResultCallBack callback) {
31+
LinkedHashMap<String, Object> headers, HashMap<String, Object> urlQueries, String requestInfo,
32+
ResultCallBack callback) {
2933
checkHeader(headers);
3034
String completeUrl = stackInstance.config.getEndpoint() + url;
3135
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(queryInstance);
3236
csConnectionRequest.setQueryInstance(queryInstance);
3337
csConnectionRequest.setURLQueries(urlQueries);
34-
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback);
38+
this.service = stackInstance.service;
39+
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service);
3540

3641
}
3742

3843
protected CSBackgroundTask(Entry entryInstance, Stack stackInstance, String controller, String url,
39-
LinkedHashMap<String, Object> headers, HashMap<String, Object> urlQueries, String requestInfo,
40-
ResultCallBack callBack) {
44+
LinkedHashMap<String, Object> headers, HashMap<String, Object> urlQueries, String requestInfo,
45+
ResultCallBack callBack) {
4146
checkHeader(headers);
4247
String completeUrl = stackInstance.config.getEndpoint() + url;
4348
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(entryInstance);
4449
csConnectionRequest.setURLQueries(urlQueries);
45-
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callBack);
50+
this.service = stackInstance.service;
51+
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callBack, this.service);
4652
}
4753

4854
protected CSBackgroundTask(AssetLibrary assetLibrary, Stack stackInstance, String controller, String url,
49-
LinkedHashMap<String, Object> headers, HashMap<String, Object> urlQueries, String requestInfo,
50-
ResultCallBack callback) {
55+
LinkedHashMap<String, Object> headers, HashMap<String, Object> urlQueries, String requestInfo,
56+
ResultCallBack callback) {
5157
checkHeader(headers);
5258
String completeUrl = stackInstance.config.getEndpoint() + url;
5359
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(assetLibrary);
5460
csConnectionRequest.setURLQueries(urlQueries);
55-
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback);
61+
this.service = stackInstance.service;
62+
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service);
5663

5764
}
5865

5966
protected CSBackgroundTask(Asset asset, Stack stackInstance, String controller, String url,
60-
LinkedHashMap<String, Object> headers, HashMap<String, Object> urlQueries, String requestInfo,
61-
ResultCallBack callback) {
67+
LinkedHashMap<String, Object> headers, HashMap<String, Object> urlQueries, String requestInfo,
68+
ResultCallBack callback) {
6269
checkHeader(headers);
6370
String completeUrl = stackInstance.config.getEndpoint() + url;
6471
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(asset);
6572
csConnectionRequest.setURLQueries(urlQueries);
66-
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback);
73+
this.service = stackInstance.service;
74+
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service);
6775
}
6876

6977
protected CSBackgroundTask(ContentType contentType, Stack stackInstance, String controller, String url,
70-
HashMap<String, Object> headers, HashMap<String, Object> urlParams, String requestInfo,
71-
ResultCallBack callback) {
78+
HashMap<String, Object> headers, HashMap<String, Object> urlParams, String requestInfo,
79+
ResultCallBack callback) {
7280
checkHeader(headers);
7381
String completeUrl = stackInstance.config.getEndpoint() + url;
7482
CSConnectionRequest csConnectionRequest = new CSConnectionRequest(contentType);
7583
csConnectionRequest.setURLQueries(urlParams);
76-
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback);
84+
this.service = stackInstance.service;
85+
csConnectionRequest.setParams(completeUrl, headers, controller, requestInfo, callback, this.service);
7786
}
7887

7988
protected void checkHeader(@NotNull Map<String, Object> headers) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.contentstack.sdk;
2+
3+
import okhttp3.ConnectionPool;
4+
5+
import java.util.concurrent.TimeUnit;
6+
7+
8+
public class CSConnectionPool {
9+
10+
ConnectionPool create() {
11+
return new ConnectionPool();
12+
}
13+
14+
ConnectionPool create(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
15+
return new ConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit);
16+
}
17+
18+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.contentstack.sdk;
22

33
import org.json.JSONObject;
4+
import retrofit2.Retrofit;
45

56
import java.util.HashMap;
67
import java.util.LinkedHashMap;
@@ -19,7 +20,7 @@ class CSConnectionRequest implements IRequestModelHTTP {
1920
private ResultCallBack resultCallBack;
2021
private INotifyClass notifyClass;
2122
private AssetLibrary assetLibrary;
22-
23+
private APIService service;
2324
private Entry entryInstance;
2425
private Asset assetInstance;
2526
private Stack stackInstance;
@@ -73,6 +74,7 @@ public void setParams(Object... objects) {
7374
if (objects[4] != null) {
7475
resultCallBack = (ResultCallBack) objects[4];
7576
}
77+
this.service = (APIService) objects[5];
7678
sendRequest();
7779
}
7880

@@ -83,6 +85,7 @@ public void sendRequest() {
8385
connection.setHeaders(header);
8486
connection.setInfo(requestInfo);
8587
connection.setEndpoint(this.endpoint);
88+
connection.setAPIService(this.service);
8689
connection.setCallBackObject(resultCallBack);
8790
if (urlQueries != null && urlQueries.size() > 0) {
8891
connection.setFormParams(urlQueries);

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class CSHttpConnection implements IURLRequestHTTP {
2828
private String controller;
2929
private LinkedHashMap<String, Object> headers;
3030
private String info;
31+
private APIService service;
3132
private String endpoint;
3233
private ResultCallBack callBackObject;
3334
private JSONObject responseJSON;
@@ -182,11 +183,11 @@ public void send() {
182183
}
183184

184185
private void getService(String requestUrl) throws IOException {
185-
Retrofit retrofit = new Retrofit.Builder().baseUrl(this.endpoint).build();
186-
APIService service = retrofit.create(APIService.class);
186+
// Retrofit retrofit = new Retrofit.Builder().baseUrl(this.endpoint).build();
187+
// APIService service = retrofit.create(APIService.class);
187188
this.headers.put(X_USER_AGENT, CLIENT_USER_AGENT);
188189
this.headers.put(CONTENT_TYPE, APPLICATION_JSON);
189-
Response<ResponseBody> response = service.getRequest(requestUrl, this.headers).execute();
190+
Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute();
190191
if (response.isSuccessful()) {
191192
assert response.body() != null;
192193
String resp = response.body().string();
@@ -212,4 +213,8 @@ void setError(String errResp) {
212213
protected void setEndpoint(@NotNull String endpoint) {
213214
this.endpoint = endpoint;
214215
}
216+
217+
public void setAPIService(APIService service) {
218+
this.service = service;
219+
}
215220
}

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

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

3+
import okhttp3.ConnectionPool;
34
import org.jetbrains.annotations.NotNull;
45

6+
import java.net.Proxy;
7+
import java.util.concurrent.TimeUnit;
8+
9+
510
/**
611
* The type Config. enables optional parameters while passing from stack
712
*/
@@ -18,6 +23,8 @@ public class Config {
1823
protected ContentstackRegion region = ContentstackRegion.US;
1924
protected String managementToken;
2025
protected String branch;
26+
protected Proxy proxy = null;
27+
protected ConnectionPool connectionPool = new ConnectionPool();
2128

2229
public String getBranch() {
2330
return branch;
@@ -27,6 +34,51 @@ public void setBranch(String branch) {
2734
this.branch = branch;
2835
}
2936

37+
38+
/**
39+
* Proxy can be set like below.
40+
*
41+
* @param proxy
42+
* Proxy setting, typically a type (http, socks) and a socket address. A Proxy is an immutable object
43+
* <br><br><b>Example:</b><br><br>
44+
* <code>
45+
* java.net.Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyHost", "proxyPort"));
46+
* java.net.Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("sl.theproxyvpn.io", 80)); Config
47+
* config = new Config() config.setProxy(proxy)
48+
* </code>
49+
*/
50+
public void setProxy(Proxy proxy) {
51+
this.proxy = proxy;
52+
}
53+
54+
/**
55+
* Returns the Proxy instance
56+
*
57+
* @return Proxy
58+
*/
59+
public Proxy getProxy() {
60+
return this.proxy;
61+
}
62+
63+
64+
/**
65+
* Manages reuse of HTTP and HTTP/2 connections for reduced network latency. HTTP requests that * share the same
66+
* {@link okhttp3.Address} may share a {@link okhttp3.Connection}. This class implements the policy * of which
67+
* connections to keep open for future use.
68+
*
69+
* @param maxIdleConnections
70+
* the maxIdleConnections default value is 5
71+
* @param keepAliveDuration
72+
* the keepAliveDuration default value is 5
73+
* @param timeUnit
74+
* the timeUnit default value is TimeUnit.MINUTES
75+
* @return ConnectionPool
76+
*/
77+
public ConnectionPool connectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
78+
this.connectionPool = new ConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit);
79+
return this.connectionPool;
80+
}
81+
3082
/**
3183
* Gets region.
3284
*
@@ -39,7 +91,8 @@ public ContentstackRegion getRegion() {
3991
/**
4092
* Sets region.
4193
*
42-
* @param region the region
94+
* @param region
95+
* the region
4396
* @return the region
4497
*/
4598
public ContentstackRegion setRegion(ContentstackRegion region) {
@@ -68,7 +121,8 @@ public String getHost() {
68121
/**
69122
* Sets host.
70123
*
71-
* @param hostName the host name
124+
* @param hostName
125+
* the host name
72126
*/
73127
public void setHost(String hostName) {
74128
if (hostName != null && !hostName.isEmpty()) {
@@ -88,7 +142,8 @@ public String getVersion() {
88142
/**
89143
* Enable live preview config.
90144
*
91-
* @param enableLivePreview to enable live preview
145+
* @param enableLivePreview
146+
* to enable live preview
92147
* @return the config
93148
*/
94149
public Config enableLivePreview(boolean enableLivePreview) {
@@ -99,7 +154,8 @@ public Config enableLivePreview(boolean enableLivePreview) {
99154
/**
100155
* Sets live preview host.
101156
*
102-
* @param livePreviewHost the live preview host
157+
* @param livePreviewHost
158+
* the live preview host
103159
* @return the live preview host
104160
*/
105161
public Config setLivePreviewHost(@NotNull String livePreviewHost) {
@@ -110,7 +166,8 @@ public Config setLivePreviewHost(@NotNull String livePreviewHost) {
110166
/**
111167
* Sets management token.
112168
*
113-
* @param managementToken the management token
169+
* @param managementToken
170+
* the management token
114171
* @return the management token
115172
*/
116173
public Config setManagementToken(@NotNull String managementToken) {
@@ -119,8 +176,7 @@ public Config setManagementToken(@NotNull String managementToken) {
119176
}
120177

121178
/**
122-
* The enum Contentstack region. for now contentstack supports
123-
* [US, EU, AZURE_NA]
179+
* The enum Contentstack region. for now contentstack supports [US, EU, AZURE_NA]
124180
*/
125181
public enum ContentstackRegion {
126182
US, EU, AZURE_NA

0 commit comments

Comments
 (0)