|
1 | | -[](https://www.contentstack.com/) |
| 1 | +## Contentstack Management SDK - Java |
2 | 2 |
|
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. |
4 | 4 |
|
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_. |
6 | 6 |
|
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> |
8 | 9 |
|
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) |
10 | 22 |
|
11 | | -### Prerequisite |
| 23 | +</details> |
12 | 24 |
|
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) |
14 | 27 |
|
15 | | -### Installation |
| 28 | +## Prerequisite |
16 | 29 |
|
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_ |
18 | 46 |
|
19 | 47 | ```java |
20 | 48 | <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> |
24 | 52 | </dependency> |
25 | 53 | ``` |
26 | 54 |
|
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_ |
28 | 58 |
|
29 | 59 | ```java |
30 | | -implementation 'com.contentstack.sdk:1.0.0' |
| 60 | +implementation 'com.contentstack.cms:{version}' |
31 | 61 | ``` |
32 | 62 |
|
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_) |
34 | 64 |
|
35 | | -To import the SDK, use the following command: |
| 65 | +## Initialization |
36 | 66 |
|
37 | | -```java |
38 | | -import com.contentstack.cms.Contentstack; |
| 67 | +To use the Java CMA SDK, you need get the client first: |
39 | 68 |
|
40 | | -Contentstack client = new Contentstack.Builder().build(); |
| 69 | +```java |
| 70 | +Contentstack client = new Contentstack.Builder().setAuthtoken("AUTHTOKEN").build(); |
41 | 71 | ``` |
42 | 72 |
|
43 | | -### Authentication |
| 73 | +## Authentication |
44 | 74 |
|
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) |
46 | 76 |
|
47 | | -### Authtoken |
| 77 | +- _Using authtoken_ |
| 78 | + |
| 79 | +--- |
48 | 80 |
|
49 | 81 | 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. |
50 | 82 |
|
51 | 83 | ```java |
52 | | -Contentstack client = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build(); |
| 84 | +Contentstack client = new Contentstack.Builder().setAuthtoken("AUTHTOKEN").build(); |
53 | 85 | ``` |
54 | 86 |
|
55 | | -### Login |
| 87 | +- _Using login email id and password_ |
56 | 88 |
|
57 | 89 | To Login to Contentstack by using credentials, you can use the following lines of code: |
58 | 90 |
|
59 | 91 | ```java |
60 | | -Contentstack client = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build(); |
| 92 | +Contentstack client = new Contentstack.Builder().setAuthtoken("AUTHTOKEN").build(); |
61 | 93 | Response<ResponseBody> response = client.login("EMAIL", "PASSWORD").execute() |
62 | 94 | ``` |
63 | 95 |
|
64 | | -### Management Token |
| 96 | +- _Using management token_ |
65 | 97 |
|
66 | 98 | [Management Tokens](https://www.contentstack.com/docs/developers/create-tokens/about-management-tokens/) are **stack-level** tokens, with no users attached to them. |
67 | 99 |
|
68 | 100 | ```java |
69 | | -Contentstack client = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build(); |
| 101 | +Contentstack client = new Contentstack.Builder().setAuthtoken("AUTHTOKEN").build(); |
70 | 102 | Response<ResponseBody> response = client.stack("API_KEY", "MANAGEMENT_TOKEN").contentType().execute() |
71 | 103 | ``` |
72 | 104 |
|
73 | | -### Contentstack Management Java SDK: 5-minute Quickstart |
| 105 | +## Users |
74 | 106 |
|
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. |
76 | 108 |
|
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: |
78 | 110 |
|
79 | 111 | ```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. |
81 | 121 |
|
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(); |
83 | 127 | ``` |
84 | 128 |
|
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 | +``` |
86 | 139 |
|
87 | | -Use the following lines of code to fetch your stack detail using this SDK: |
| 140 | +OR |
88 | 141 |
|
89 | 142 | ```java |
90 | | -Contentstack client = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build(); |
| 143 | +Contentstack client = new Contentstack.Builder().setAuthtoken("AUTHTOKEN").build(); |
91 | 144 | Response<ResponseBody> response = client.stack("API_KEY").exceute() |
92 | 145 | ``` |
93 | 146 |
|
94 | | -#### Create Entry |
| 147 | +## Content Types |
95 | 148 |
|
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. |
97 | 150 |
|
98 | 151 | ```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(); |
100 | 155 | ``` |
101 | 156 |
|
102 | | -#### Create Asset |
| 157 | +## Global Fields |
103 | 158 |
|
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. |
105 | 160 |
|
106 | 161 | ```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"); |
108 | 166 | ``` |
109 | 167 |
|
110 | | -#### Create JSON Body |
| 168 | +## Entries |
111 | 169 |
|
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: |
114 | 210 |
|
115 | 211 | ```java |
116 | 212 | { |
117 | | - "entry": { |
118 | | - "title": "example", |
119 | | - "url": "/example" |
120 | | - } |
| 213 | +"entry": { |
| 214 | + "title": "example", |
| 215 | + "url": "/example" |
| 216 | + } |
121 | 217 | } |
122 | 218 | ``` |
123 | 219 |
|
124 | | -Create request body like below: |
| 220 | +- Create request body using HashMap like below |
125 | 221 |
|
126 | 222 | ```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<>(); |
129 | 225 | bodyContent.put("title", "example"); |
130 | 226 | bodyContent.put("url", "/example"); |
131 | 227 | requestBody.put("entry", bodyContent); |
132 | | - |
133 | | -JSONObject requestBody = new JSONObject(mapBody) |
| 228 | +JSONObject requestBody = new JSONObject(mapBody) |
134 | 229 | ``` |
135 | 230 |
|
136 | | -or |
137 | | -Create using JSONObject like directly: |
| 231 | +- The other way to create RequestBody using JSONObject like below |
138 | 232 |
|
139 | 233 | ```java |
140 | | -JSONObject requestBody = new JSONObject(); |
141 | | -JSONObject bodyContent = new JSONObject(); |
| 234 | +JSONObject requestBody = new JSONObject(); |
| 235 | +JSONObject bodyContent = new JSONObject(); |
142 | 236 | bodyContent.put("title", "example"); |
143 | 237 | bodyContent.put("url", "/example"); |
144 | 238 | requestBody.put("entry", bodyContent); |
145 | 239 | ``` |
146 | 240 |
|
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 |
148 | 247 |
|
149 | 248 | - [Contentstack Website](https://www.contentstack.com/) |
150 | 249 | - [Official Documentation](https://contentstack.com/docs) |
151 | | -- [Content Management API Docs](https://www.contentstack.com/docs/developers/apis/content-management-api) |
152 | 250 |
|
153 | 251 | ### The MIT License (MIT) |
154 | 252 |
|
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. |
0 commit comments