Skip to content
51 changes: 51 additions & 0 deletions src/main/java/com/contentstack/cms/core/ErrorMessages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.contentstack.cms.core;

public final class ErrorMessages {

private ErrorMessages() {
throw new AssertionError("This class cannot be instantiated. Use the static constants to continue.");
}

public static final String NOT_LOGGED_IN = "You are not logged in. Login to continue.";
public static final String ALREADY_LOGGED_IN = "Operation not allowed. You are already logged in.";
public static final String OAUTH_LOGIN_REQUIRED = "Login or configure OAuth to continue.";
public static final String OAUTH_NO_TOKENS = "No OAuth tokens available. Please authenticate first.";
public static final String OAUTH_NO_REFRESH_TOKEN = "No refresh token available";
public static final String OAUTH_EMPTY_CODE = "Authorization code cannot be null or empty";
public static final String OAUTH_CONFIG_MISSING = "OAuth is not configured. Use Builder.setOAuth() with or without clientSecret for PKCE flow";
public static final String OAUTH_REFRESH_FAILED = "Failed to refresh access token";
public static final String OAUTH_REVOKE_FAILED = "Failed to revoke authorization";
public static final String OAUTH_STATUS_FAILED = "Failed to get authorization status";
public static final String OAUTH_ORG_EMPTY = "organizationUid can not be empty";

public static final String PRIVATE_CONSTRUCTOR = "This class cannot be instantiated. Use the static methods to continue.";

public static final String ALIAS_UID_REQUIRED = "Alias UID is required. Provide a valid Alias UID and try again.";
public static final String ASSET_UID_REQUIRED = "Asset UID is required. Provide a valid Asset UID and try again.";
public static final String LOG_ITEM_UID_REQUIRED = "Log Item UID is required. Provide a valid Log Item UID and try again.";
public static final String BRANCH_UID_REQUIRED = "Branch UID is required. Provide a valid Branch UID and try again.";
public static final String CUSTOM_FIELD_UID_REQUIRED = "Custom Field UID is required. Provide a valid Custom Field UID and try again.";
public static final String DELIVERY_TOKEN_UID_REQUIRED = "Delivery Token UID is required. Provide a valid Delivery Token UID and try again.";
public static final String ENVIRONMENT_REQUIRED = "Environment is required. Provide a valid Environment and try again.";
public static final String FOLDER_UID_REQUIRED = "Folder UID is required. Provide a valid Folder UID and try again.";
public static final String GLOBAL_FIELD_UID_REQUIRED = "Global Field UID is required. Provide a valid Global Field UID and try again.";
public static final String LABEL_UID_REQUIRED = "Label UID is required. Provide a valid Label UID and try again.";
public static final String LOCALE_CODE_REQUIRED = "Locale Code is required. Provide a valid Locale Code and try again.";
public static final String MANAGEMENT_TOKEN_UID_REQUIRED = "Management Token UID is required. Provide a valid Management Token UID and try again.";
public static final String ORGANIZATION_UID_REQUIRED = "Organization UID is required. Provide a valid Organization UID and try again.";
public static final String PUBLISH_QUEUE_UID_REQUIRED = "Publish Queue UID is required. Provide a valid Publish Queue UID and try again.";
public static final String RELEASE_UID_REQUIRED = "Release UID is required. Provide a valid Release UID and try again.";
public static final String ROLE_UID_REQUIRED = "Role UID is required. Provide a valid Role UID and try again.";
public static final String VARIANT_GROUP_UID_REQUIRED = "Variant Group UID is required. Provide a valid Variant Group UID and try again.";
public static final String WEBHOOK_UID_REQUIRED = "Webhook UID is required. Provide a valid Webhook UID and try again.";
public static final String WORKFLOW_UID_REQUIRED = "Workflow UID is required. Provide a valid Workflow UID and try again.";

public static final String CONTENT_TYPE_REQUIRED = "Content Type is required. Provide a valid Content Type and try again.";
public static final String REFERENCE_FIELDS_INVALID = "Reference Fields must be a string or an array of strings. Provide valid values and try again.";
public static final String TERM_STRING_REQUIRED = "Term String is required. Provide a valid Term String and try again.";

public static final String FILE_CONTENT_TYPE_UNKNOWN = "The file's content type could not be determined. Provide a valid file and try again.";

public static final String MISSING_INSTALLATION_ID = "installation uid is required";
public static final String MISSING_ORG_ID = "organization uid is required";
}
39 changes: 19 additions & 20 deletions src/main/java/com/contentstack/cms/core/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@ public class Util {
// "1.2.0".
public static final String SDK_VERSION = "1.3.1";

static final String PRIVATE_CONSTRUCTOR = "private constructor can't be accessed outside the class";
static final String PRIVATE_CONSTRUCTOR = ErrorMessages.PRIVATE_CONSTRUCTOR;
public static final Boolean RETRY_ON_FAILURE = true;
public static final String PROTOCOL = "https";
public static final String HOST = "api.contentstack.io";
public static final String PORT = "443";
public static final String VERSION = "v3";
public static final int TIMEOUT = 30;
public static final String SDK_NAME = "contentstack-management-java";
public static final String ILLEGAL_USER = "Please Login to access stack instance";
public static final String USER_ALREADY_LOGGED_IN = "User is already loggedIn, "
+ "Please logout then try to login again";
public static final String LOGIN_FLAG = "Please login to access user instance";
public static final String PLEASE_LOGIN = "Please Login to access stack instance";
public static final String ILLEGAL_USER = ErrorMessages.NOT_LOGGED_IN;
public static final String USER_ALREADY_LOGGED_IN = ErrorMessages.ALREADY_LOGGED_IN;
public static final String LOGIN_FLAG = ErrorMessages.NOT_LOGGED_IN;
public static final String PLEASE_LOGIN = ErrorMessages.NOT_LOGGED_IN;

// CONSTANT KEYS
public static final String API_KEY = "api_key";
Expand All @@ -47,9 +46,9 @@ public class Util {
public static final String CONTENT_TYPE_VALUE = "application/json";

// Error Messages
public static final String MISSING_INSTALLATION_ID = "installation uid is required";
public static final String ERROR_INSTALLATION = "installation uid is required";
public static final String MISSING_ORG_ID = "organization uid is required";
public static final String MISSING_INSTALLATION_ID = ErrorMessages.MISSING_INSTALLATION_ID;
public static final String ERROR_INSTALLATION = ErrorMessages.MISSING_INSTALLATION_ID;
public static final String MISSING_ORG_ID = ErrorMessages.MISSING_ORG_ID;

// OAuth Constants
public static final String OAUTH_APP_HOST = "app.contentstack.com";
Expand All @@ -58,15 +57,15 @@ public class Util {
public static final String OAUTH_AUTHORIZE_ENDPOINT = "/#!/apps/%s/authorize";

// OAuth Error Messages
public static final String OAUTH_NO_TOKENS = "No OAuth tokens available. Please authenticate first.";
public static final String OAUTH_NO_REFRESH_TOKEN = "No refresh token available";
public static final String OAUTH_EMPTY_CODE = "Authorization code cannot be null or empty";
public static final String OAUTH_CONFIG_MISSING = "OAuth is not configured. Use Builder.setOAuth() with or without clientSecret for PKCE flow";
public static final String OAUTH_REFRESH_FAILED = "Failed to refresh access token";
public static final String OAUTH_REVOKE_FAILED = "Failed to revoke authorization";
public static final String OAUTH_STATUS_FAILED = "Failed to get authorization status";
public static final String OAUTH_LOGIN_REQUIRED = "Please login or configure OAuth to access";
public static final String OAUTH_ORG_EMPTY = "organizationUid can not be empty";
public static final String OAUTH_NO_TOKENS = ErrorMessages.OAUTH_NO_TOKENS;
public static final String OAUTH_NO_REFRESH_TOKEN = ErrorMessages.OAUTH_NO_REFRESH_TOKEN;
public static final String OAUTH_EMPTY_CODE = ErrorMessages.OAUTH_EMPTY_CODE;
public static final String OAUTH_CONFIG_MISSING = ErrorMessages.OAUTH_CONFIG_MISSING;
public static final String OAUTH_REFRESH_FAILED = ErrorMessages.OAUTH_REFRESH_FAILED;
public static final String OAUTH_REVOKE_FAILED = ErrorMessages.OAUTH_REVOKE_FAILED;
public static final String OAUTH_STATUS_FAILED = ErrorMessages.OAUTH_STATUS_FAILED;
public static final String OAUTH_LOGIN_REQUIRED = ErrorMessages.OAUTH_LOGIN_REQUIRED;
public static final String OAUTH_ORG_EMPTY = ErrorMessages.OAUTH_ORG_EMPTY;

// The code `Util() throws IllegalAccessException` is a constructor for the
// `Util` class that throws an
Expand All @@ -76,7 +75,7 @@ public class Util {
// `IllegalAccessException` is to prevent
// the instantiation of the `Util` class from outside the class itself.
Util() throws IllegalAccessException {
throw new IllegalAccessException("private=modifier");
throw new IllegalAccessException(ErrorMessages.PRIVATE_CONSTRUCTOR);
}

/**
Expand Down Expand Up @@ -109,7 +108,7 @@ public static void nullEmptyThrowsException(@NotNull String field) {
try {
throw new CMARuntimeException(field + " cannot take in an empty String or null value");
} catch (CMARuntimeException e) {
System.out.println("Exception: " + e.getLocalizedMessage());
System.out.println("An error occurred due to " + e.getLocalizedMessage() + ".");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.contentstack.cms.organization;

import com.contentstack.cms.core.ErrorMessages;

import com.contentstack.cms.BaseImplementation;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
Expand All @@ -26,7 +28,7 @@ public class Organization implements BaseImplementation<Organization> {
protected HashMap<String, String> headers;
protected HashMap<String, Object> params;
private String organizationUid;
final String ERROR_MSG = "OrganizationUid Can Not Be Null OR Empty";
final String ERROR_MSG = ErrorMessages.ORGANIZATION_UID_REQUIRED;


/**
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/contentstack/cms/stack/Alias.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.contentstack.cms.stack;

import com.contentstack.cms.BaseImplementation;
import okhttp3.ResponseBody;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.jetbrains.annotations.NotNull;
import org.json.simple.JSONObject;

import com.contentstack.cms.BaseImplementation;
import com.contentstack.cms.core.ErrorMessages;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Retrofit;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* An alias acts as a pointer to a particular branch. You can specify the alias
* ID in your frontend code to pull content
Expand Down Expand Up @@ -160,7 +163,7 @@ public Call<ResponseBody> find() {
* @since 2022-10-20
*/
public Call<ResponseBody> fetch() {
Objects.requireNonNull(this.uid, "Global Field Uid can not be null or empty");
Objects.requireNonNull(this.uid, ErrorMessages.ALIAS_UID_REQUIRED);
return this.service.single(this.headers, this.uid);
}

Expand Down Expand Up @@ -206,7 +209,7 @@ public Call<ResponseBody> update(@NotNull JSONObject body) {
* @since 2022-10-20
*/
public Call<ResponseBody> delete() {
Objects.requireNonNull(this.uid, "Global Field Uid can not be null or empty");
Objects.requireNonNull(this.uid, ErrorMessages.ALIAS_UID_REQUIRED);
return this.service.delete(this.headers, this.uid, this.params);
}

Expand Down
26 changes: 14 additions & 12 deletions src/main/java/com/contentstack/cms/stack/Asset.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.contentstack.cms.stack;

import com.contentstack.cms.core.ErrorMessages;

import com.contentstack.cms.BaseImplementation;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
Expand Down Expand Up @@ -225,12 +227,12 @@ public Call<AssetListResponse> findAsPojo() {
* @since 2022-10-20
*/
public Call<ResponseBody> fetch() {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
return this.service.single(this.headers, this.assetUid, this.params);
}

public Call<AssetResponse> fetchAsPojo() { // New method for POJO conversion
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
return this.service.singlePojo(this.headers, this.assetUid, this.params);
}

Expand Down Expand Up @@ -390,7 +392,7 @@ private MultipartBody createMultipartBody(String filePath, String parentUid, Str
* @since 2022-10-20
*/
public Call<ResponseBody> replace(@NotNull String filePath, @NotNull String description) {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
MultipartBody.Part assetPath = uploadFile(filePath);
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);
Expand Down Expand Up @@ -443,7 +445,7 @@ private MultipartBody.Part uploadFile(@NotNull String filePath) {
* @since 2022-10-20
*/
public Call<ResponseBody> generatePermanentUrl(JSONObject body) {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
return this.service.generatePermanentUrl(this.headers, this.assetUid, body);
}

Expand Down Expand Up @@ -473,7 +475,7 @@ public Call<ResponseBody> generatePermanentUrl(JSONObject body) {
* @since 2022-10-20
*/
public Call<ResponseBody> getPermanentUrl(String slugUrl) {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
return this.service.downloadPermanentUrl(this.headers, this.assetUid, slugUrl, this.params);
}

Expand All @@ -490,7 +492,7 @@ public Call<ResponseBody> getPermanentUrl(String slugUrl) {
* @since 0.1.0
*/
public Call<ResponseBody> delete() {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
return this.service.delete(this.headers, this.assetUid);
}

Expand Down Expand Up @@ -580,7 +582,7 @@ public Call<ResponseBody> setVersionName(int versionNumber,
* @since 0.1.0
*/
public Call<ResponseBody> getVersionNameDetails() {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
return this.service.getVersionNameDetails(this.headers, this.assetUid, this.params);
}

Expand All @@ -601,7 +603,7 @@ public Call<ResponseBody> getVersionNameDetails() {
* @since 0.1.0
*/
public Call<ResponseBody> deleteVersionName(int versionNumber) {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
return this.service.deleteVersionName(this.headers, this.assetUid, versionNumber);
}

Expand All @@ -618,7 +620,7 @@ public Call<ResponseBody> deleteVersionName(int versionNumber) {
* @since 0.1.0
*/
public Call<ResponseBody> getReferences() {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
return this.service.getReferences(this.headers, this.assetUid);
}

Expand Down Expand Up @@ -676,7 +678,7 @@ public Call<ResponseBody> getByType(@NotNull String assetType) {
* @since 0.1.0
*/
public Call<ResponseBody> updateDetails(JSONObject requestBody) {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
return this.service.updateDetails(this.headers, this.assetUid, this.params, requestBody);
}

Expand All @@ -702,7 +704,7 @@ public Call<ResponseBody> updateDetails(JSONObject requestBody) {
* @since 0.1.0
*/
public Call<ResponseBody> publish(@NotNull JSONObject requestBody) {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
return this.service.publish(this.headers, this.assetUid, requestBody);
}

Expand All @@ -728,7 +730,7 @@ public Call<ResponseBody> publish(@NotNull JSONObject requestBody) {
*/
public Call<ResponseBody> unpublish(
@NotNull JSONObject requestBody) {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
Objects.requireNonNull(this.assetUid, ErrorMessages.ASSET_UID_REQUIRED);
return this.service.unpublish(this.headers, this.assetUid, requestBody);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/contentstack/cms/stack/AuditLog.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.contentstack.cms.stack;

import com.contentstack.cms.core.ErrorMessages;

import com.contentstack.cms.BaseImplementation;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -112,7 +114,7 @@ public Call<ResponseBody> find() {
* @return Call
*/
public Call<ResponseBody> fetch() {
Objects.requireNonNull(this.logItemUid, "Log Item uid can not be null or empty");
Objects.requireNonNull(this.logItemUid, ErrorMessages.LOG_ITEM_UID_REQUIRED);
return this.service.fetch(this.headers, this.logItemUid);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/contentstack/cms/stack/Branch.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.contentstack.cms.stack;

import com.contentstack.cms.core.ErrorMessages;

import com.contentstack.cms.BaseImplementation;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -53,7 +55,7 @@ protected Branch(Retrofit instance,Map<String, Object> headers, String uid) {
}

void validate() {
final String ERROR_MESSAGE = "The Branch UID Can Not Be Null ORr Empty";
final String ERROR_MESSAGE = ErrorMessages.BRANCH_UID_REQUIRED;
Objects.requireNonNull(this.baseBranchId, ERROR_MESSAGE);
if (this.baseBranchId.isEmpty())
throw new IllegalStateException(ERROR_MESSAGE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.contentstack.cms.stack;

import com.contentstack.cms.core.ErrorMessages;

import com.contentstack.cms.BaseImplementation;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -30,7 +32,7 @@ public class DeliveryToken implements BaseImplementation<DeliveryToken> {
protected HashMap<String, Object> headers;
protected HashMap<String, Object> params;
private String tokenUid;
String ERROR = "Token UID Can Not Be Null OR Empty";
String ERROR = ErrorMessages.DELIVERY_TOKEN_UID_REQUIRED;

protected DeliveryToken(TokenService service, Map<String, Object> headers) {
this.headers = new HashMap<>();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/contentstack/cms/stack/Entry.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.contentstack.cms.stack;

import com.contentstack.cms.core.ErrorMessages;

import com.contentstack.cms.BaseImplementation;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -145,7 +147,7 @@ public Call<ResponseBody> includeReference(@NotNull Object referenceField){
if (referenceField instanceof String || referenceField instanceof String[]) {
addToParams("include[]", referenceField);
} else {
throw new IllegalArgumentException("Reference fields must be a String or an array of Strings");
throw new IllegalArgumentException(ErrorMessages.REFERENCE_FIELDS_INVALID);
}
validateCT();
return this.service.fetch(this.headers, this.contentTypeUid, this.params);
Expand Down
Loading
Loading