ContentType
ContentType
A content type defines the structure that its entries follow. The ContentType class represents one content type of a stack. Use this class to
- read the schema of one content type
- open one entry of the content type
- build a query across the entries of the content type
Call the contentType method of the Stack class to get an instance. ContentType has no public constructor.
entry
entry creates an Entry object for one entry of this content type.
| Name | Type | Description |
|---|---|---|
| entryUid (required) | String | UID of the entry to work with. |
Returns: Entry. A new entry object.
Validation
- There is no client-side validation. entry stores the UID as given, including null or an empty string.
- The UID check happens in the fetch method of the Entry class, which you call next.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. entry sends no request.
- Each call returns a new Entry. The entry carries the UID of this content type and shares the header map of the stack.
- Call fetch on the returned entry to retrieve it. See the Entry class page for the methods you can chain before fetch.
Example
Basic usage: fetch one entry of a content type
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;
public class FetchEntryOfContentType {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
entry.fetch(new EntryResultCallBack() {
@Override
public void onCompletion(ResponseType responseType, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
System.out.println(entry.getTitle());
}
});
}
}fetch
fetch retrieves the schema of this content type, without its entries.
| Name | Type | Description |
|---|---|---|
| params (required) | JSONObject | Query parameters to send, such as include_branch. |
| callback (required) | ContentTypesCallback | Receives the content type or the error. See Callbacks. |
Returns: void. The result arrives in the callback.
Validation
- Throws IllegalAccessException if the content type UID is null or empty, because fetch reads one content type by its UID. Java requires you to catch or declare it, because IllegalAccessException is a checked exception.
- Throws NullPointerException if params is null, because fetch reads the keys of params before it sends the request. Pass new JSONObject() to send no extra parameters.
- When callback is null, fetch sends no request and reports nothing.
- fetch does not throw on an API rejection.
- The callback receives an Error and a null model.
- Check error first in every callback.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- fetch sends one request to Get a single content type (GET /content_types/{content_type_uid}).
- fetch adds an environment key to the JSONObject you pass, with the environment of the stack. The object still holds that key after the call.
- fetch appends each key of params to the URL as key=value without URL encoding. An ampersand in a value starts a new parameter, so encode any value that holds a reserved character.
- On success, fetch passes a ContentTypesModel whose getResponse() is a JSONObject with the content type. See The callback receives a ContentTypesModel on the ContentType class page.
- On success, fetch also fills getTitle, getUid, getSchema and the other getters of the same instance, before it calls your callback.
Limitations
- Does not return the entries of the content type. Use the query method and call find.
Example
Basic usage: read the schema of a content type
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import org.json.JSONObject;
public class FetchContentType {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
contentType.fetch(new JSONObject(), new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
JSONObject contentTypeJson = (JSONObject) contentTypesModel.getResponse();
System.out.println(contentTypeJson.optString("title"));
}
});
}
}All parameters: include the branch key in the response
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import org.json.JSONObject;
public class FetchContentTypeWithBranch {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
JSONObject params = new JSONObject();
params.put("include_branch", true);
contentType.fetch(params, new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error != null) { System.out.println(error.getErrorMessage()); return; }
System.out.println(contentTypesModel.getResponse());
}
});
}
}Edge case: an empty content type UID throws before any request
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import org.json.JSONObject;
public class FetchWithoutUid {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
try {
stack.contentType("").fetch(new JSONObject(), new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error != null) { System.out.println(error.getErrorMessage()); return; }
System.out.println(contentTypesModel.getResponse());
}
});
} catch (IllegalAccessException e) {
// Thrown when the content type UID is null or empty
System.out.println("No content type UID: " + e.getMessage());
}
}
}getContentTypeData
getContentTypeData returns the whole content type that fetch retrieved, as JSON.
Returns: JSONObject. The content type's full JSON.
Validation
- There is no client-side validation.
- getContentTypeData returns null until fetch succeeds on the same instance.
Behavior
- Client-side only. getContentTypeData reads a value that fetch stored and sends no request.
- The object is the content_type value of the Get a single content type response. It is the same object that getResponse() of the ContentTypesModel returns in the fetch callback.
- Use it to read keys that the other getters do not cover. getTitle, getDescription, getUid and getSchema read keys of this object.
- A failed fetch leaves the value from the last successful fetch on the instance, or null.
Example
Basic usage: read a key that no getter covers
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import org.json.JSONObject;
public class ReadContentTypeJson {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
contentType.fetch(new JSONObject(), new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
JSONObject data = contentType.getContentTypeData();
System.out.println(data.optString("updated_at"));
System.out.println(data.toString(2));
}
});
}
}getDescription
getDescription returns the description of the content type that fetch retrieved.
Returns: String. The content type's description.
Validation
- There is no client-side validation.
- getDescription returns null until fetch succeeds on the same instance.
Behavior
- Client-side only. getDescription reads a value that fetch stored and sends no request.
- fetch stores the description key of the content type. When the content type has no description, getDescription returns an empty string, not null.
- A failed fetch leaves the value from the last successful fetch on the instance, or null.
Example
Basic usage: print the description after fetch
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import org.json.JSONObject;
public class PrintContentTypeDescription {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
contentType.fetch(new JSONObject(), new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
String description = contentType.getDescription();
// An empty string means the content type has no description.
System.out.println(description.isEmpty() ? "No description" : description);
}
});
}
}getSchema
getSchema returns the field definitions of the content type that fetch retrieved.
Returns: JSONArray. The content type's field schema.
Validation
- There is no client-side validation.
- getSchema returns null until fetch succeeds on the same instance, and also when the fetched content type has no schema key. Check for null before you read the array.
Behavior
- Client-side only. getSchema reads a value that fetch stored and sends no request.
- The array holds one JSONObject per field, as the Get a single content type response returns it.
- getSchema returns the same array object on every call, so a change you make to it also changes the result of getContentTypeData.
- A failed fetch leaves the value from the last successful fetch on the instance, or null.
Example
Basic usage: list the field UIDs of a content type
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import org.json.JSONArray;
import org.json.JSONObject;
public class ListContentTypeFields {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
contentType.fetch(new JSONObject(), new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
JSONArray schema = contentType.getSchema();
if (schema == null) {
System.out.println("No schema");
return;
}
for (int i = 0; i < schema.length(); i++) {
JSONObject field = schema.getJSONObject(i);
System.out.println(field.optString("uid") + " " + field.optString("data_type"));
}
}
});
}
}getTitle
getTitle returns the title of the content type that fetch retrieved.
Returns: String. The content type's title.
Validation
- There is no client-side validation.
- getTitle returns null until fetch succeeds on the same instance.
Behavior
- Client-side only. getTitle reads a value that fetch stored and sends no request.
- fetch stores the title key of the content type. When the content type has no title key, getTitle returns an empty string.
- A failed fetch leaves the value from the last successful fetch on the instance, or null.
- The getContentTypes method of the Stack class does not fill this value.
Example
Basic usage: print the title after fetch
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import org.json.JSONObject;
public class PrintContentTypeTitle {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
contentType.fetch(new JSONObject(), new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
System.out.println(contentType.getTitle());
}
});
}
}Edge case: the title is null before fetch
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Stack;
public class TitleBeforeFetch {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
// Prints null, because fetch has not run on this instance.
System.out.println(contentType.getTitle());
}
}getUid
getUid returns the UID of the content type that fetch retrieved.
Returns: String. The content type's UID.
Validation
- There is no client-side validation.
- getUid returns null until fetch succeeds on the same instance.
Behavior
- Client-side only. getUid reads a value that fetch stored and sends no request.
- getUid returns the uid key of the fetched content type, not the UID you passed to the contentType method of the Stack class. Before fetch succeeds, it returns null even though the instance holds that UID.
- A failed fetch leaves the value from the last successful fetch on the instance, or null.
Example
Basic usage: print the UID after fetch
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import org.json.JSONObject;
public class PrintContentTypeUid {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
// Prints null, because fetch has not run on this instance.
System.out.println(contentType.getUid());
contentType.fetch(new JSONObject(), new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
System.out.println(contentType.getUid());
}
});
}
}query
query creates a Query object that searches the entries of this content type.
Returns: Query. A new query object.
Validation
- There is no client-side validation.
- Errors come from the find or findOne call that follows, not from query.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. query sends no request.
- Each call returns a new Query with no conditions. The query carries the UID of this content type and shares the header map of the stack.
- Chain conditions such as where and limit, then call find. See the Query class page for the full set.
Example
Basic usage: list entries of a content type
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Query;
import com.contentstack.sdk.QueryResult;
import com.contentstack.sdk.QueryResultsCallBack;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;
public class QueryContentType {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
Query query = stack.contentType("<CONTENT_TYPE_UID>").query();
query.where("title", "Spring launch").find(new QueryResultsCallBack() {
@Override
public void onCompletion(ResponseType responseType, QueryResult queryresult, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
for (Entry entry : queryresult.getResultObjects()) {
System.out.println(entry.getUid() + " " + entry.getTitle());
}
}
});
}
}removeHeader
removeHeader removes one request header from the stack that created this content type.
| Name | Type | Description |
|---|---|---|
| headerKey (required) | String | Name of the header to remove. |
Returns: void. This method returns nothing.
Validation
- Throws NullPointerException if headerKey is null, because removeHeader checks it for emptiness first.
- When headerKey is an empty string, or names a header that the stack does not hold, removeHeader does nothing and raises no error.
Behavior
- Client-side only. removeHeader sends no request.
- IMPORTANT: removeHeader removes the header from the header map of the stack, not from a copy. Every later request from the stack, and from the objects it creates, goes out without it. This includes the api_key, access_token and environment headers that every request needs, so remove only headers that you set yourself.
- Use the setHeader method to add a header again.
Example
Basic usage: stop sending a branch header
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import org.json.JSONObject;
public class RemoveContentTypeHeader {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
contentType.setHeader("branch", "<BRANCH>");
// Later requests from this stack go out without the branch header.
contentType.removeHeader("branch");
contentType.fetch(new JSONObject(), new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
System.out.println(contentType.getTitle());
}
});
}
}setHeader
setHeader adds a request header, or replaces the value of an existing one, on the stack that created this content type.
| Name | Type | Description |
|---|---|---|
| headerKey (required) | String | Name of the header to set. |
| headerValue (required) | String | Value to send for that header. |
Returns: void. This method returns nothing.
Validation
- Throws NullPointerException if headerKey or headerValue is null, because setHeader checks each one for emptiness before it stores the pair.
- When headerKey or headerValue is an empty string, setHeader ignores the call without an error.
Behavior
- Client-side only. setHeader sends no request. Later requests carry the header.
- IMPORTANT: setHeader writes to the header map of the stack, not to a copy. The header then travels on every later request from the stack and from the objects it creates, for example entries, queries and other content types. Nothing in your code shows the wider effect at the point of the call.
- Setting an existing name, such as branch, replaces its value.
- Use the removeHeader method to remove the header again.
Example
Basic usage: send a branch header
import com.contentstack.sdk.ContentType;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import org.json.JSONObject;
public class SetContentTypeHeader {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
ContentType contentType = stack.contentType("<CONTENT_TYPE_UID>");
// Every later request from this stack now carries the branch header.
contentType.setHeader("branch", "<BRANCH>");
contentType.fetch(new JSONObject(), new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
System.out.println(contentType.getTitle());
}
});
}
}