Skip to content

Commit b17997d

Browse files
authored
Merge pull request #143 from contentstack/staging
21st Oct 2024 Release
2 parents ef29655 + e33914c commit b17997d

File tree

5 files changed

+102
-18
lines changed

5 files changed

+102
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Publishing to Maven Packages
2+
#on: [ push ] # Trigger the workflow when a push (commit) event occurs
3+
on:
4+
release:
5+
types: [ created ]
6+
jobs:
7+
publish-maven:
8+
runs-on: ubuntu-latest
9+
permissions:
10+
contents: read
11+
packages: write
12+
steps:
13+
- uses: actions/checkout@v3
14+
- name: Set up Maven Central Repository
15+
uses: actions/setup-java@v3
16+
with:
17+
java-version: '11'
18+
distribution: 'adopt'
19+
server-id: ossrh
20+
server-username: MAVEN_USERNAME
21+
server-password: MAVEN_PASSWORD
22+
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
23+
gpg-passphrase: GPG_PASSPHRASE
24+
- name: Publish to Maven Central Repository
25+
run: mvn --batch-mode -Dgpg.passphrase=${{ secrets.GPG_PASSPHRASE }} deploy
26+
env:
27+
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
28+
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
29+
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
30+
31+
# run: mvn --batch-mode deploy

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELOG
22

3+
## v2.0.1
4+
5+
### Date: 21-October-2024
6+
7+
-Github Issues fixed
8+
-Issue with field ordering in SDK response
9+
310
## v2.0.0
411

512
### Date: 27-August-2024

pom.xml

+8-2
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>2.0.0</version>
8+
<version>2.0.1</version>
99
<packaging>jar</packaging>
1010
<name>contentstack-java</name>
1111
<description>Java SDK for Contentstack Content Delivery API</description>
@@ -17,7 +17,7 @@
1717
<maven.compiler.source>1.8</maven.compiler.source>
1818
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1919
<surefire-report-plugin.version>2.22.0</surefire-report-plugin.version>
20-
<maven-source-plugin.version>2.2.1</maven-source-plugin.version>
20+
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
2121
<maven-javadoc-plugin.version>3.4.1</maven-javadoc-plugin.version>
2222
<dotenv-source.version>3.0.0</dotenv-source.version>
2323
<rxjava-source.version>3.1.8</rxjava-source.version>
@@ -183,6 +183,12 @@
183183
<version>${json-simple-version}</version>
184184
<scope>compile</scope>
185185
</dependency>
186+
187+
<dependency>
188+
<groupId>com.fasterxml.jackson.core</groupId>
189+
<artifactId>jackson-databind</artifactId>
190+
<version>2.15.2</version>
191+
</dependency>
186192
</dependencies>
187193

188194
<build>

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

+55-15
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
import okhttp3.Request;
44
import okhttp3.ResponseBody;
5+
56
import org.json.JSONArray;
67
import org.json.JSONException;
78
import org.json.JSONObject;
9+
810
import retrofit2.Call;
911
import retrofit2.Response;
1012

1113
import java.io.IOException;
1214
import java.io.UnsupportedEncodingException;
1315
import java.net.URLEncoder;
16+
import java.net.SocketTimeoutException;
1417
import java.nio.charset.StandardCharsets;
1518
import java.util.HashMap;
1619
import java.util.Iterator;
@@ -19,6 +22,10 @@
1922
import java.util.logging.Level;
2023
import java.util.logging.Logger;
2124
import java.util.stream.IntStream;
25+
import com.fasterxml.jackson.databind.ObjectMapper; // Jackson for JSON parsing
26+
import com.fasterxml.jackson.databind.json.JsonMapper;
27+
import com.fasterxml.jackson.databind.node.ObjectNode;
28+
import com.fasterxml.jackson.databind.type.MapType;
2229

2330
import static com.contentstack.sdk.Constants.*;
2431

@@ -185,6 +192,14 @@ public void send() {
185192
}
186193
}
187194

195+
private JSONObject createOrderedJSONObject(Map<String, Object> map) {
196+
JSONObject json = new JSONObject();
197+
for (Map.Entry<String, Object> entry : map.entrySet()) {
198+
json.put(entry.getKey(), entry.getValue());
199+
}
200+
return json;
201+
}
202+
188203
private void getService(String requestUrl) throws IOException {
189204

190205
this.headers.put(X_USER_AGENT_KEY, "contentstack-delivery-java/" + SDK_VERSION);
@@ -202,22 +217,41 @@ private void getService(String requestUrl) throws IOException {
202217
requestUrl = request.url().toString();
203218
}
204219

205-
Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute();
206-
if (response.isSuccessful()) {
207-
assert response.body() != null;
208-
if (request != null) {
209-
response = pluginResponseImp(request, response);
210-
}
211-
responseJSON = new JSONObject(response.body().string());
212-
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
213-
handleJSONArray();
220+
try {
221+
Response<ResponseBody> response = this.service.getRequest(requestUrl, this.headers).execute();
222+
if (response.isSuccessful()) {
223+
assert response.body() != null;
224+
if (request != null) {
225+
response = pluginResponseImp(request, response);
226+
}
227+
try {
228+
// Use Jackson to parse the JSON while preserving order
229+
ObjectMapper mapper = JsonMapper.builder().build();
230+
MapType type = mapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class,
231+
Object.class);
232+
Map<String, Object> responseMap = mapper.readValue(response.body().string(), type);
233+
234+
// Use the custom method to create an ordered JSONObject
235+
responseJSON = createOrderedJSONObject(responseMap);
236+
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
237+
handleJSONArray();
238+
}
239+
connectionRequest.onRequestFinished(CSHttpConnection.this);
240+
} catch (JSONException e) {
241+
// Handle non-JSON response
242+
setError("Invalid JSON response");
243+
}
244+
} else {
245+
assert response.errorBody() != null;
246+
setError(response.errorBody().string());
214247
}
215-
connectionRequest.onRequestFinished(CSHttpConnection.this);
216-
} else {
217-
assert response.errorBody() != null;
218-
setError(response.errorBody().string());
248+
} catch (SocketTimeoutException e) {
249+
// Handle timeout
250+
setError("Request timed out: " + e.getMessage());
251+
} catch (IOException e) {
252+
// Handle other IO exceptions
253+
setError("IO error occurred: " + e.getMessage());
219254
}
220-
221255
}
222256

223257
private Request pluginRequestImp(String requestUrl) {
@@ -261,7 +295,13 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
261295
}
262296

263297
void setError(String errResp) {
264-
responseJSON = new JSONObject(errResp); // Parse error string to JSONObject
298+
try {
299+
responseJSON = new JSONObject(errResp);
300+
} catch (JSONException e) {
301+
// If errResp is not valid JSON, create a new JSONObject with the error message
302+
responseJSON = new JSONObject();
303+
responseJSON.put(ERROR_MESSAGE, errResp);
304+
}
265305
responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE));
266306
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE));
267307
responseJSON.put(ERRORS, responseJSON.optString(ERRORS));

src/main/overview.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h1 id="java-sdk-for-contentstack">Java SDK for Contentstack</h1>
2020
resources to get started with our Java SDK.</p>
2121
<h2 id="prerequisite">Prerequisite</h2>
2222
<p>You will need JDK installed on your machine. You can install it from <a
23-
href="http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html">here</a>.</p>
23+
href="https://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html">here</a>.</p>
2424
<h3 id="setup-and-installation">Setup and Installation</h3>
2525
<p>To use the Contentstack Java SDK to your existing project, perform the steps given below:</p>
2626
<p>Group id: <code>com.contentstack.sdk</code></p>

0 commit comments

Comments
 (0)