From 89178b8864a5d412a2830f12d1bed6e630e16bd8 Mon Sep 17 00:00:00 2001 From: Shailesh Mishra Date: Mon, 19 Feb 2024 19:27:42 +0530 Subject: [PATCH 1/2] Auto-detect content type while uploading asset --- pom.xml | 8 +-- .../com/contentstack/cms/stack/Asset.java | 40 +----------- .../contentstack/cms/stack/FileUploader.java | 63 +++++++++++++++++++ .../com/contentstack/cms/stack/Taxonomy.java | 2 +- .../contentstack/cms/stack/AssetAPITest.java | 7 +-- .../cms/stack/EntryFieldsAPITest.java | 22 +++++++ 6 files changed, 96 insertions(+), 46 deletions(-) create mode 100644 src/main/java/com/contentstack/cms/stack/FileUploader.java diff --git a/pom.xml b/pom.xml index 3ebe4ab8..3bc378ad 100644 --- a/pom.xml +++ b/pom.xml @@ -91,10 +91,10 @@ 3.1.8 2.9.0 2.9.0 - 5.0.0-alpha.11 + 5.0.0-alpha.12 0.8.7 1.18.30 - 5.10.0 + 5.10.1 5.8.0-M1 2.10.1 3.3 @@ -138,7 +138,7 @@ org.jetbrains annotations - 24.0.1 + 24.1.0 provided @@ -168,7 +168,7 @@ org.mockito mockito-core - 5.6.0 + 5.9.0 test diff --git a/src/main/java/com/contentstack/cms/stack/Asset.java b/src/main/java/com/contentstack/cms/stack/Asset.java index 2f106282..7c42e134 100644 --- a/src/main/java/com/contentstack/cms/stack/Asset.java +++ b/src/main/java/com/contentstack/cms/stack/Asset.java @@ -327,50 +327,16 @@ public Call uploadAsset(@NotNull String filePath, String descripti * @return Call */ public Call uploadAsset(@NotNull String filePath, String parentUid, String title, String description, String[] tags) { - RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), description); + RequestBody body = RequestBody.create(Objects.requireNonNull(MediaType.parse("multipart/form-data")), description); MultipartBody.Part partFile = createMultipartBody(filePath, parentUid, title, description, tags); return this.service.uploadAsset(this.headers, partFile, body, this.params); } - private String tagConvertor(String[] tags) { - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < tags.length; i++) { - stringBuilder.append(tags[i]); - if (i < tags.length - 1) { - stringBuilder.append(", "); - } - } - return stringBuilder.toString(); - } - private MultipartBody.Part createMultipartBody(String filePath, String parentUid, String title, String description, String[] tags) { MultipartBody.Builder builder = new MultipartBody.Builder(); builder.setType(MultipartBody.FORM); - - if (!filePath.isEmpty()) { - File file = new File(filePath); - if (file.exists()) { - RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); - builder.addFormDataPart("asset[upload]", file.getName(), fileBody); - } - } - - // Add other parts - if (parentUid != null) { - builder.addFormDataPart("asset[parent_uid]", parentUid); - } - if (title != null) { - builder.addFormDataPart("asset[title]", title); - } - if (description != null) { - builder.addFormDataPart("asset[description]", description); - } - if (tags != null) { - builder.addFormDataPart("asset[tags]", tagConvertor(tags)); - } - - return builder.build().part(0); + return new FileUploader().createMultipartBody(filePath, parentUid, title, description, tags); } @@ -403,7 +369,7 @@ private MultipartBody.Part createMultipartBody(String filePath, String parentUid public Call replace(@NotNull String filePath, @NotNull String description) { Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty"); MultipartBody.Part assetPath = uploadFile(filePath); - RequestBody body = RequestBody.create(MediaType.parse(String.valueOf(MultipartBody.FORM)), description); + RequestBody body = RequestBody.create(Objects.requireNonNull(MediaType.parse(String.valueOf(MultipartBody.FORM))), description); return this.service.replace(this.headers, this.assetUid, assetPath, body, this.params); } diff --git a/src/main/java/com/contentstack/cms/stack/FileUploader.java b/src/main/java/com/contentstack/cms/stack/FileUploader.java new file mode 100644 index 00000000..df0f6d16 --- /dev/null +++ b/src/main/java/com/contentstack/cms/stack/FileUploader.java @@ -0,0 +1,63 @@ +package com.contentstack.cms.stack; + +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.RequestBody; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Objects; + +public class FileUploader { + + + public MultipartBody.Part createMultipartBody(String filePath, String parentUid, String title, String description, String[] tags) { + MultipartBody.Builder builder = new MultipartBody.Builder(); + builder.setType(MultipartBody.FORM); + + // Check if filePath is not empty and file exists + if (!filePath.isEmpty()) { + File file = new File(filePath); + if (file.exists()) { + String contentType = getContentType(file); + RequestBody fileBody = RequestBody.create(Objects.requireNonNull(MediaType.parse(contentType)), file); + builder.addFormDataPart("asset[upload]", file.getName(), fileBody); + } + } + + // Add other parts if not null + addFormDataPartIfNotNull(builder, "asset[parent_uid]", parentUid); + addFormDataPartIfNotNull(builder, "asset[title]", title); + addFormDataPartIfNotNull(builder, "asset[description]", description); + + // Handle tags array null case + if (tags != null) { + addFormDataPartIfNotNull(builder, "asset[tags]", tagConvertor(tags)); + } + + return builder.build().part(0); + } + + // Helper method to add form data part if value is not null + private void addFormDataPartIfNotNull(MultipartBody.Builder builder, String name, String value) { + if (value != null) { + builder.addFormDataPart(name, value); + } + } + + // Helper method to get content type of file + private String getContentType(File file) { + try { + return Files.probeContentType(file.toPath()); + } catch (IOException e) { + throw new RuntimeException("Failed to determine content type of file", e); + } + } + + // Dummy implementation of tagConvertor method + private String tagConvertor(String[] tags) { + // Implement your tag conversion logic here + return String.join(",", tags); + } +} diff --git a/src/main/java/com/contentstack/cms/stack/Taxonomy.java b/src/main/java/com/contentstack/cms/stack/Taxonomy.java index ea7ee77a..50640831 100644 --- a/src/main/java/com/contentstack/cms/stack/Taxonomy.java +++ b/src/main/java/com/contentstack/cms/stack/Taxonomy.java @@ -257,7 +257,7 @@ public Terms terms() { *

Example usage:

*
JSONObject object = new JSonObject();
*
object.put("taxonomies.color", Object)
- *
Taxonomy taxonomy = stack("authtoken").taxonomy("taxonomyId").filterTaxonomy(object);
+ *
Taxonomy taxonomy = stack("authtoken").taxonomy("taxonomyId").query(object);
* * @param query the query of type @{@link JSONObject} * @return instance of {@link Call} diff --git a/src/test/java/com/contentstack/cms/stack/AssetAPITest.java b/src/test/java/com/contentstack/cms/stack/AssetAPITest.java index c4df3ca2..c64b1a8b 100644 --- a/src/test/java/com/contentstack/cms/stack/AssetAPITest.java +++ b/src/test/java/com/contentstack/cms/stack/AssetAPITest.java @@ -30,7 +30,7 @@ static void setup() { @Order(1) @Test - void testFindAssets() throws IOException { + void testFindAssets() { asset.clearParams(); asset.addParam("include_folders", true); asset.addParam("environment", "production"); @@ -141,10 +141,9 @@ void testAssetUpload() { asset.addHeader("api_key", API_KEY); asset.addHeader("authorization", MANAGEMENT_TOKEN); asset.addHeader("authtoken", AUTHTOKEN); - String filePath = "/Users/shaileshmishra/Desktop/pexels.png"; - String description = "The calender has been placed to assets by ishaileshmishra"; + String filePath = "/Users/shaileshmishra/Desktop/image.jpeg"; + String description = "The calender has been placed to assets by shaileshmishra"; Request request = asset.uploadAsset(filePath, description).request(); - // The assertions Assertions.assertEquals(3, request.headers().size()); Assertions.assertTrue(request.headers().names().contains("api_key")); diff --git a/src/test/java/com/contentstack/cms/stack/EntryFieldsAPITest.java b/src/test/java/com/contentstack/cms/stack/EntryFieldsAPITest.java index 07281882..981635a0 100644 --- a/src/test/java/com/contentstack/cms/stack/EntryFieldsAPITest.java +++ b/src/test/java/com/contentstack/cms/stack/EntryFieldsAPITest.java @@ -2,8 +2,10 @@ import com.contentstack.cms.TestClient; import okhttp3.Request; +import okhttp3.ResponseBody; import org.json.simple.JSONObject; import org.junit.jupiter.api.*; +import retrofit2.Response; import java.io.IOException; @@ -363,4 +365,24 @@ void testPublishWithReference() { request.url().toString()); } + + @Test + public void testEntryQuery() { + JSONObject query = new JSONObject(); + query.put("taxonomies.taxonomy_uid", "{ \"$in\" : [\"term_uid1\" , \"term_uid2\" ] }"); + Request request = entry.query(query).request(); + Assertions.assertEquals(2, request.headers().names().size()); + Assertions.assertEquals("GET", request.method()); + Assertions.assertTrue(request.url().isHttps()); + Assertions.assertEquals("api.contentstack.io", request.url().host()); + Assertions.assertEquals(4, request.url().pathSegments().size()); + Assertions.assertEquals("v3", request.url().pathSegments().get(0)); + Assertions.assertEquals("content_types", request.url().pathSegments().get(1)); + Assertions.assertEquals("product", request.url().pathSegments().get(2)); + Assertions.assertEquals("entries", request.url().pathSegments().get(3)); + Assertions.assertNull(request.body()); + Assertions.assertEquals("query={\"taxonomies.taxonomy_uid\":\"{ \\\"$in\\\" : [\\\"term_uid1\\\" , \\\"term_uid2\\\" ] }\"}", request.url().query()); + + } + } From 33fdf7c4bd3cf9fcfa6ceb32231a76c09987e9b8 Mon Sep 17 00:00:00 2001 From: Shailesh Mishra Date: Mon, 19 Feb 2024 19:28:24 +0530 Subject: [PATCH 2/2] Auto-detect content type while uploading asset --- pom.xml | 2 +- src/main/java/com/contentstack/cms/core/Util.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 3bc378ad..8ff62e4c 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ cms jar contentstack-management-java - 1.3.0 + 1.3.1 Contentstack Java Management SDK for Content Management API, Contentstack is a headless CMS with an API-first approach diff --git a/src/main/java/com/contentstack/cms/core/Util.java b/src/main/java/com/contentstack/cms/core/Util.java index 08307a81..54092373 100644 --- a/src/main/java/com/contentstack/cms/core/Util.java +++ b/src/main/java/com/contentstack/cms/core/Util.java @@ -18,7 +18,7 @@ public class Util { // The line `public static final String SDK_VERSION = "1.3.0";` // named `SDK_VERSION` of type `String`. The value of this constant is set to // "1.2.0". - public static final String SDK_VERSION = "1.3.0"; + public static final String SDK_VERSION = "1.3.1"; static final String PRIVATE_CONSTRUCTOR = "private constructor can't be accessed outside the class"; public static final Boolean RETRY_ON_FAILURE = true;