Skip to content

Commit 8af6813

Browse files
🎉 asset includes
1 parent 07e9427 commit 8af6813

File tree

6 files changed

+204
-71
lines changed

6 files changed

+204
-71
lines changed

README.md

Lines changed: 158 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,253 @@
1-
[![Contentstack](https://www.contentstack.com/docs/static/images/contentstack.png)](https://www.contentstack.com/)
1+
## Contentstack Management SDK - Java
22

3-
## Contentstack Management Java SDK
3+
Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. All you have to do is build your application frontend, and Contentstack will take care of the rest. [Read More](https://www.contentstack.com/). This SDK uses the [Content Management API](https://www.contentstack.com/docs/developers/apis/content-management-api/) (CMA). The CMA is used to manage the content of your Contentstack account. This includes creating, updating, deleting, and fetching content of your account.
44

5-
Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. All you have to do is build your application frontend, and Contentstack will take care of the rest. [Read More](https://www.contentstack.com/).
5+
**Note:** _By using CMA, you can execute GET requests for fetching content. However, we strongly recommend that you always use the [Content Delivery API](https://www.contentstack.com/docs/developers/apis/content-delivery-api/) to deliver content to your web or mobile properties_.
66

7-
This SDK uses the [Content Management API](https://www.contentstack.com/docs/developers/apis/content-management-api/) (CMA). The CMA is used to manage the content of your Contentstack account. This includes creating, updating, deleting, and fetching content of your account. To use the CMA, you will need to authenticate your users with a [Management Token](https://www.contentstack.com/docs/developers/create-tokens/about-management-tokens) or an [Authtoken](https://www.contentstack.com/docs/developers/apis/content-management-api/#how-to-get-authtoken). Read more about it in [Authentication](https://www.contentstack.com/docs/developers/apis/content-management-api/#authentication).
7+
<details open>
8+
<summary>Table of contents</summary>
89

9-
Note: By using CMA, you can execute GET requests for fetching content. However, we strongly recommend that you always use the [Content Delivery API](https://www.contentstack.com/docs/developers/apis/content-delivery-api/) to deliver content to your web or mobile properties.
10+
- [Prerequisite](#prerequisite)
11+
- [Installation](#installation)
12+
- [Initialization](initialization)
13+
- [Authentication](#authentication)
14+
- [Users](#users)
15+
- [Organizations](#organizations)
16+
- [Stacks](#stacks)
17+
- [Content Types](#content-types)
18+
- [Global Fields](#global-fields)
19+
- [Entries](#entries)
20+
- [Assets](#assets)
21+
- [Bulk Operation](#bulk-operations)
1022

11-
### Prerequisite
23+
</details>
1224

13-
You need Java 1.8 or above installed on your machine to use the Contentstack Java CMA SDK.
25+
- Others
26+
[Create JSON Body](##RequestBody)
1427

15-
### Installation
28+
## Prerequisite
1629

17-
Install it via maven:
30+
To use the Contentstack management sdk for Java, you must have:
31+
32+
- a suitable [Java Development Environment](http://www.oracle.com/technetwork/java/javase/downloads/).
33+
- an [Contentstack](https://www.contentstack.com) account.
34+
- login your account
35+
- get the [Contentstack](https://www.contentstack.com) credentials (API keys and Management Token) set in your stack token section.
36+
- should be Java 1.8 or above installed.
37+
- make sure java environment is set on your machine
38+
39+
## Installation
40+
41+
To include the SDK your project, use one of the following methods depending on your build system or IDE:
42+
43+
- **Apache Maven** - If you use Apache Maven, you can specify dependency in the dependencies section in your project
44+
45+
- _Install using maven_
1846

1947
```java
2048
<dependency>
21-
<groupId>com.contentstack</groupId>
22-
<artifactId>cms</artifactId>
23-
<version>{version}</version>
49+
<groupId>com.contentstack</groupId>
50+
<artifactId>cms</artifactId>
51+
<version>{version}</version>
2452
</dependency>
2553
```
2654

27-
Install it via gradle:
55+
- **Gradle** - If you use Gradle, you can import the Maven Bill of Materials (BOM) in your Gradle project to automatically manage SDK dependencies
56+
57+
- _Install it via gradle_
2858

2959
```java
30-
implementation 'com.contentstack.sdk:1.0.0'
60+
implementation 'com.contentstack.cms:{version}'
3161
```
3262

33-
Get updated version from [here](https://search.maven.org/artifact/com.contentstack.cms)
63+
_Get the latest version from_ [HERE](https://search.maven.org/artifact/com.contentstack.cma) (_Install letest version always_)
3464

35-
To import the SDK, use the following command:
65+
## Initialization
3666

37-
```java
38-
import com.contentstack.cms.Contentstack;
67+
To use the Java CMA SDK, you need get the client first:
3968

40-
Contentstack client = new Contentstack.Builder().build();
69+
```java
70+
Contentstack client = new Contentstack.Builder().setAuthtoken("AUTHTOKEN").build();
4171
```
4272

43-
### Authentication
73+
## Authentication
4474

45-
To use this SDK, you need to authenticate your users by using the Authtoken, credentials, or Management Token (stack-level token).
75+
To use the CMA, you will need to authenticate your users with a [Management Token](https://www.contentstack.com/docs/developers/create-tokens/about-management-tokens) or an [Authtoken](https://www.contentstack.com/docs/developers/apis/content-management-api/#how-to-get-authtoken). Read more about it in [Authentication](https://www.contentstack.com/docs/developers/apis/content-management-api/#authentication)
4676

47-
### Authtoken
77+
- _Using authtoken_
78+
79+
---
4880

4981
An [Authtoken](https://www.contentstack.com/docs/developers/create-tokens/types-of-tokens/#authentication-tokens-authtokens-) is a read-write token used to make authorized CMA requests, and it is a **user-specific** token.
5082

5183
```java
52-
Contentstack client = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build();
84+
Contentstack client = new Contentstack.Builder().setAuthtoken("AUTHTOKEN").build();
5385
```
5486

55-
### Login
87+
- _Using login email id and password_
5688

5789
To Login to Contentstack by using credentials, you can use the following lines of code:
5890

5991
```java
60-
Contentstack client = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build();
92+
Contentstack client = new Contentstack.Builder().setAuthtoken("AUTHTOKEN").build();
6193
Response<ResponseBody> response = client.login("EMAIL", "PASSWORD").execute()
6294
```
6395

64-
### Management Token
96+
- _Using management token_
6597

6698
[Management Tokens](https://www.contentstack.com/docs/developers/create-tokens/about-management-tokens/) are **stack-level** tokens, with no users attached to them.
6799

68100
```java
69-
Contentstack client = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build();
101+
Contentstack client = new Contentstack.Builder().setAuthtoken("AUTHTOKEN").build();
70102
Response<ResponseBody> response = client.stack("API_KEY", "MANAGEMENT_TOKEN").contentType().execute()
71103
```
72104

73-
### Contentstack Management Java SDK: 5-minute Quickstart
105+
## Users
74106

75-
#### Initializing Your SDK
107+
[Users](https://www.contentstack.com/docs/developers/apis/content-management-api/#users) All accounts registered with Contentstack are known as Users. A stack can have many users with varying permissions and roles.
76108

77-
To use the Java CMA SDK, you need to first initialize it. To do this, use the following code:
109+
Access the user like below snippet:
78110

79111
```java
80-
import com.contentstack.cms.Contentstack
112+
Contentstack client = new Contentstack.Builder().build();
113+
client.login("EMAIL", "PASSWORD");
114+
User user = client.user();
115+
Response<ResponseBody> response = user.getUser().execute();
116+
```
117+
118+
## Organizations
119+
120+
[Organization](https://www.contentstack.com/docs/owners-and-admins/about-organizations) is the top-level entity in the hierarchy of Contentstack, consisting of stacks and [stack](https://www.contentstack.com/docs/developers/set-up-stack/about-stack/) resources, and users. Organization allows easy management of projects as well as users within the Organization.
81121

82-
Contentstack client = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build();
122+
```java
123+
Contentstack client = new Contentstack.Builder().build();
124+
client.login("EMAIL", "PASSWORD");
125+
Organization organization = contentstack.organization();
126+
Response<ResponseBody> response = organization.getAll().execute();
83127
```
84128

85-
#### Fetch Stack Detail
129+
## Stacks
130+
131+
A [stack](https://www.contentstack.com/docs/developers/set-up-stack/about-stack) is a space that stores the content of a project (a web or mobile property). Within a stack, you can create content structures, content entries, users, etc. related to the project.
132+
133+
```java
134+
Contentstack client = new Contentstack.Builder().build();
135+
client.login("EMAIL", "PASSWORD");
136+
Stack stack = contentstack.stack("APIKey");
137+
Response<ResponseBody> response = stack.fetch().execute();
138+
```
86139

87-
Use the following lines of code to fetch your stack detail using this SDK:
140+
OR
88141

89142
```java
90-
Contentstack client = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build();
143+
Contentstack client = new Contentstack.Builder().setAuthtoken("AUTHTOKEN").build();
91144
Response<ResponseBody> response = client.stack("API_KEY").exceute()
92145
```
93146

94-
#### Create Entry
147+
## Content Types
95148

96-
To create an entry in a specific content type of a stack, use the following lines of code:
149+
[Content type](https://www.contentstack.com/docs/developers/apis/content-management-api/#content-types) defines the structure or schema of a page or a section of your web or mobile property. To create content for your application, you are required to first create a content type, and then create entries using the content type.
97150

98151
```java
99-
Under development
152+
Contentstack contentstack = new Contentstack.Builder().build();
153+
ContentType contentType = contentstack.contentType("API_KEY", "MANAGEMENT_TOKEN");
154+
Response<ResponseBody> response = contentType.fetch(mapQuery).execute();
100155
```
101156

102-
#### Create Asset
157+
## Global Fields
103158

104-
The following lines of code can be used to upload assets to your stack:
159+
A [Global field](https://www.contentstack.com/docs/developers/apis/content-management-api/#global-fields) is a reusable field (or group of fields) that you can define once and reuse in any content type within your stack. This eliminates the need (and thereby time and efforts) to create the same set of fields repeatedly in multiple content types.
105160

106161
```java
107-
Under development
162+
GlobalField globalField = new Contentstack.Builder().setAuthtoken(GLOBAL_AUTHTOKEN).build()
163+
.globalField("API_KEY", "MANAGEMENT_TOKEN");
164+
Call<ResponseBody> response = globalField.update(GlobalFiledUid,
165+
"requestBody");
108166
```
109167

110-
#### Create JSON Body
168+
## Entries
111169

112-
JSON Body could be created by using Map passing in the JSONObject or directly create JSONObject.
113-
Let's suppose you have to make json like below:
170+
An [entry](https://www.contentstack.com/docs/developers/apis/content-management-api/#entries) is the actual piece of content created using one of the defined content types.
171+
172+
```java
173+
Entry entry = new Contentstack.Builder().setAuthtoken(authToken).build()
174+
.entry("API_KEY", "MANAGEMENT_TOKEN");
175+
HashMap<String, Object> queryParams = new HashMap<>();
176+
Response<ResponseBody> response = entry.fetch(queryParams).execute();
177+
```
178+
179+
## Assets
180+
181+
[Assets](https://www.contentstack.com/docs/developers/apis/content-management-api/#assets) refer to all the media files (images, videos, PDFs, audio files, and so on) uploaded in your Contentstack repository for future use.
182+
183+
```java
184+
Asset asset = new Contentstack.Builder().setAuthtoken("AUTHTOKEN").build().asset("API_KEY", "MANAGEMENT_TOKEN");
185+
Map<String, Object> queryParams = new HashMap<>();
186+
queryParams.put("folder", "folder_uid_some_example");
187+
queryParams.put("include_folders", true);
188+
queryParams.put("environment", "production");
189+
queryParams.put("version", 1);
190+
queryParams.put("include_publish_details", true);
191+
queryParams.put("include_count", true);
192+
queryParams.put("relative_urls", false);
193+
queryParams.put("asc_field_uid", "created_at");
194+
queryParams.put("desc_field_uid", 230);
195+
196+
Response<ResponseBody> response = asset.fetch(queryParams).execute();
197+
198+
```
199+
200+
## Bulk Operations
201+
202+
You can perform [bulk operations](https://www.contentstack.com/docs/developers/apis/content-management-api/#bulk-operations) such as Publish, Unpublish, and Delete on multiple entries or assets, or Change the Workflow Details of multiple entries or assets at the same time.
203+
204+
## Others
205+
206+
### Create RequestBody
207+
208+
- JSON Body could be created by using Map passing in the JSONObject or directly create JSONObject.
209+
Let's suppose you have to make json like below:
114210

115211
```java
116212
{
117-
"entry": {
118-
"title": "example",
119-
"url": "/example"
120-
}
213+
"entry": {
214+
"title": "example",
215+
"url": "/example"
216+
}
121217
}
122218
```
123219

124-
Create request body like below:
220+
- Create request body using HashMap like below
125221

126222
```java
127-
HashMap<String, Object> mapBody = new HashMap<>();
128-
HashMap<String, Object> bodyContent = new HashMap<>();
223+
HashMap<String, Object> mapBody = new HashMap<>();
224+
HashMap<String, Object> bodyContent = new HashMap<>();
129225
bodyContent.put("title", "example");
130226
bodyContent.put("url", "/example");
131227
requestBody.put("entry", bodyContent);
132-
133-
JSONObject requestBody = new JSONObject(mapBody)
228+
JSONObject requestBody = new JSONObject(mapBody)
134229
```
135230

136-
or
137-
Create using JSONObject like directly:
231+
- The other way to create RequestBody using JSONObject like below
138232

139233
```java
140-
JSONObject requestBody = new JSONObject();
141-
JSONObject bodyContent = new JSONObject();
234+
JSONObject requestBody = new JSONObject();
235+
JSONObject bodyContent = new JSONObject();
142236
bodyContent.put("title", "example");
143237
bodyContent.put("url", "/example");
144238
requestBody.put("entry", bodyContent);
145239
```
146240

147-
### Helpful Links
241+
### Documentation
242+
243+
- [Content Management API Docs](https://www.contentstack.com/docs/developers/apis/content-management-api/#api-reference)
244+
- [API References](https://www.contentstack.com/docs/developers/apis/content-management-api/#api-reference)
245+
246+
### Other helpful Links
148247

149248
- [Contentstack Website](https://www.contentstack.com/)
150249
- [Official Documentation](https://contentstack.com/docs)
151-
- [Content Management API Docs](https://www.contentstack.com/docs/developers/apis/content-management-api)
152250

153251
### The MIT License (MIT)
154252

155-
Copyright © 2012-2022 [Contentstack](https://www.contentstack.com/). All Rights Reserved
156-
157-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
158-
159-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
160-
161-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
253+
Copyright © 2012-2022 [Contentstack](https://www.contentstack.com/). All Rights Reserved Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
1-
package com.contentstack.cms.core;public class CMSLogger {
1+
package com.contentstack.cms.core;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.logging.Logger;
6+
7+
public class CMSLogger {
8+
9+
private Logger logger;
10+
11+
public Logger getLOGGER() {
12+
return logger;
13+
}
14+
15+
public CMSLogger(@NotNull Class className) {
16+
logger = Logger.getLogger(className.getSimpleName());
17+
}
18+
19+
public void info(@NotNull String message) {
20+
logger.info(message);
21+
}
22+
23+
public void fine(@NotNull String message) {
24+
logger.info(message);
25+
}
26+
27+
public void finer(@NotNull String message) {
28+
logger.finer(message);
29+
}
30+
31+
public void warning(@NotNull String message) {
32+
logger.warning(message);
33+
}
34+
35+
public void severe(@NotNull String message) {
36+
logger.severe(message);
37+
}
238
}

src/main/java/com/contentstack/cms/core/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Util {
1919
public static final String VERSION = "v3"; // api version
2020
public static final int TIMEOUT = 30; // default timeout in seconds
2121
public static final String SDK_NAME = "contentstack-management-java";
22-
public static final String SDK_VERSION = "v0.0.1";
22+
public static final String SDK_VERSION = "0.0.1";
2323

2424
public static final String USER_ALREADY_LOGGED_IN = "User is already loggedIn, "
2525
+ "Please logout then try to login again";

src/test/java/com/contentstack/cms/core/TestError.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111
class TestError {
1212

13+
static CMSLogger logger = new CMSLogger(TestError.class);
14+
1315
/**
1416
* Test gson mapper not null.
1517
*/
@@ -18,6 +20,7 @@ class TestError {
1820
void testGSONMapperNotNull() {
1921
String jsonInput = "{\"error_message\":\"Failed to fetch entries. Please try again with valid parameters.\",\"error_code\":141,\"errors\":{\"environment\":[\"is required.\"]}}";
2022
Error error = new Gson().fromJson(jsonInput, Error.class);
23+
logger.fine(error.errorMessage);
2124
Assertions.assertNotNull(error);
2225
}
2326

0 commit comments

Comments
 (0)