From 54a2c9271c4a48555b66f0cd0284e79577a54c31 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Sat, 1 Feb 2020 08:57:32 -0500 Subject: [PATCH] use consistent (compact) json mapper when saving json fields to db --- bubble-server/src/main/java/bubble/ApiConstants.java | 4 ++++ bubble-server/src/main/java/bubble/model/BubbleTags.java | 3 ++- .../src/main/java/bubble/model/account/AccountContact.java | 4 ++-- .../src/main/java/bubble/model/account/AccountPolicy.java | 3 ++- .../src/main/java/bubble/model/app/AppMessage.java | 4 +++- bubble-server/src/main/java/bubble/model/app/AppRule.java | 3 ++- bubble-server/src/main/java/bubble/model/app/BubbleApp.java | 5 ++--- .../src/main/java/bubble/model/app/RuleDriver.java | 5 ++--- .../src/main/java/bubble/model/cloud/AnsibleRole.java | 5 +++-- .../src/main/java/bubble/model/cloud/BubbleDomain.java | 3 ++- .../src/main/java/bubble/model/cloud/BubbleFootprint.java | 6 +++--- .../src/main/java/bubble/model/cloud/CloudService.java | 5 +++-- .../src/main/java/bubble/model/cloud/CloudServiceData.java | 3 ++- .../java/bubble/model/cloud/notify/NotificationBase.java | 5 +++-- 14 files changed, 35 insertions(+), 23 deletions(-) diff --git a/bubble-server/src/main/java/bubble/ApiConstants.java b/bubble-server/src/main/java/bubble/ApiConstants.java index 00d473e2..412ab1a9 100644 --- a/bubble-server/src/main/java/bubble/ApiConstants.java +++ b/bubble-server/src/main/java/bubble/ApiConstants.java @@ -2,6 +2,7 @@ package bubble; import bubble.model.cloud.BubbleNode; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.warrenstrange.googleauth.GoogleAuthenticator; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -25,6 +26,7 @@ import static org.apache.http.HttpHeaders.USER_AGENT; import static org.cobbzilla.util.daemon.ZillaRuntime.die; import static org.cobbzilla.util.io.FileUtil.abs; import static org.cobbzilla.util.io.StreamUtil.stream2string; +import static org.cobbzilla.util.json.JsonUtil.COMPACT_MAPPER; import static org.cobbzilla.util.json.JsonUtil.json; import static org.cobbzilla.util.network.NetworkUtil.*; import static org.cobbzilla.wizard.resources.ResourceUtil.invalidEx; @@ -38,6 +40,8 @@ public class ApiConstants { private static final AtomicReference bubbleDefaultDomain = new AtomicReference<>(); + public static final ObjectMapper DB_JSON_MAPPER = COMPACT_MAPPER; + private static String initDefaultDomain() { final File f = new File(HOME_DIR, ".BUBBLE_DEFAULT_DOMAIN"); final String domain = FileUtil.toStringOrDie(f); diff --git a/bubble-server/src/main/java/bubble/model/BubbleTags.java b/bubble-server/src/main/java/bubble/model/BubbleTags.java index f2a40777..660705ec 100644 --- a/bubble-server/src/main/java/bubble/model/BubbleTags.java +++ b/bubble-server/src/main/java/bubble/model/BubbleTags.java @@ -16,6 +16,7 @@ import javax.validation.constraints.Size; import java.io.Serializable; +import static bubble.ApiConstants.DB_JSON_MAPPER; import static org.cobbzilla.util.json.JsonUtil.json; import static org.cobbzilla.wizard.model.crypto.EncryptedTypes.ENCRYPTED_STRING; import static org.cobbzilla.wizard.model.crypto.EncryptedTypes.ENC_PAD; @@ -28,7 +29,7 @@ public class BubbleTags implements Serializable { @JsonIgnore @Getter @Setter private String tagsJson; @Transient public NameAndValue[] getTags() { return tagsJson == null ? null : json(tagsJson, NameAndValue[].class); } - public BubbleTags setTags(NameAndValue[] tags) { return setTagsJson(tags == null ? null : json(tags)); } + public BubbleTags setTags(NameAndValue[] tags) { return setTagsJson(tags == null ? null : json(tags, DB_JSON_MAPPER)); } public String getTag (String name) { final NameAndValue[] tags = getTags(); diff --git a/bubble-server/src/main/java/bubble/model/account/AccountContact.java b/bubble-server/src/main/java/bubble/model/account/AccountContact.java index b668fba7..e4575aa7 100644 --- a/bubble-server/src/main/java/bubble/model/account/AccountContact.java +++ b/bubble-server/src/main/java/bubble/model/account/AccountContact.java @@ -23,10 +23,10 @@ import java.util.Arrays; import java.util.List; import java.util.function.Predicate; +import static bubble.ApiConstants.DB_JSON_MAPPER; import static bubble.ApiConstants.G_AUTH; import static java.util.UUID.randomUUID; import static org.cobbzilla.util.daemon.ZillaRuntime.*; -import static org.cobbzilla.util.json.JsonUtil.COMPACT_MAPPER; import static org.cobbzilla.util.json.JsonUtil.json; import static org.cobbzilla.util.reflect.ReflectionUtil.copy; import static org.cobbzilla.wizard.model.Identifiable.UUID; @@ -143,7 +143,7 @@ public class AccountContact implements Serializable { public static String getTotpInfo(Account account, BubbleConfiguration configuration) { final GoogleAuthenticatorKey creds = G_AUTH.createCredentials(); - return json(new TotpBean(creds, account, configuration), COMPACT_MAPPER); + return json(new TotpBean(creds, account, configuration), DB_JSON_MAPPER); } private static void checkNickInUse(AccountContact c, AccountContact[] contacts) { diff --git a/bubble-server/src/main/java/bubble/model/account/AccountPolicy.java b/bubble-server/src/main/java/bubble/model/account/AccountPolicy.java index d512fb4c..8fdc89a5 100644 --- a/bubble-server/src/main/java/bubble/model/account/AccountPolicy.java +++ b/bubble-server/src/main/java/bubble/model/account/AccountPolicy.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.List; import java.util.stream.Collectors; +import static bubble.ApiConstants.DB_JSON_MAPPER; import static bubble.model.account.AccountContact.contactMatch; import static java.util.concurrent.TimeUnit.DAYS; import static java.util.concurrent.TimeUnit.MINUTES; @@ -105,7 +106,7 @@ public class AccountPolicy extends IdentifiableBase implements HasAccount { } @Transient public AccountContact[] getAccountContacts () { return accountContactsJson == null ? null : json(accountContactsJson, AccountContact[].class); } - public AccountPolicy setAccountContacts(AccountContact[] contacts) { return setAccountContactsJson(contacts == null ? null : json(contacts)); } + public AccountPolicy setAccountContacts(AccountContact[] contacts) { return setAccountContactsJson(contacts == null ? null : json(contacts, DB_JSON_MAPPER)); } public AccountPolicy setContact(AccountContact c) { return setContact(c, null, null); } diff --git a/bubble-server/src/main/java/bubble/model/app/AppMessage.java b/bubble-server/src/main/java/bubble/model/app/AppMessage.java index 35a33d2e..e4536da9 100644 --- a/bubble-server/src/main/java/bubble/model/app/AppMessage.java +++ b/bubble-server/src/main/java/bubble/model/app/AppMessage.java @@ -19,6 +19,7 @@ import javax.persistence.Entity; import javax.persistence.Transient; import javax.validation.constraints.Size; +import static bubble.ApiConstants.DB_JSON_MAPPER; import static bubble.ApiConstants.EP_MESSAGES; import static org.cobbzilla.util.daemon.ZillaRuntime.empty; import static org.cobbzilla.util.json.JsonUtil.json; @@ -57,11 +58,12 @@ public class AppMessage extends IdentifiableBase implements AccountTemplate, Has @Getter @Setter private String locale; public boolean hasLocale () { return !empty(locale); } + @Size(max=100000, message="err.appMessages.length") @ECSearchable @ECField(index=40) @Column(length=100000, nullable=false) @JsonIgnore @Getter @Setter private String messagesJson; @Transient public NameAndValue[] getMessages () { return messagesJson == null ? null : json(messagesJson, NameAndValue[].class); } - public AppMessage setMessages(NameAndValue[] messages) { return setMessagesJson(messages == null ? null : json(messages));} + public AppMessage setMessages(NameAndValue[] messages) { return setMessagesJson(messages == null ? null : json(messages, DB_JSON_MAPPER));} @ECSearchable @ECField(index=50) @Column(nullable=false) @ECIndex @Getter @Setter private Integer priority = 1; diff --git a/bubble-server/src/main/java/bubble/model/app/AppRule.java b/bubble-server/src/main/java/bubble/model/app/AppRule.java index aa055c06..26f89726 100644 --- a/bubble-server/src/main/java/bubble/model/app/AppRule.java +++ b/bubble-server/src/main/java/bubble/model/app/AppRule.java @@ -22,6 +22,7 @@ import javax.persistence.Entity; import javax.persistence.Transient; import javax.validation.constraints.Size; +import static bubble.ApiConstants.DB_JSON_MAPPER; import static bubble.ApiConstants.EP_RULES; import static org.cobbzilla.util.json.JsonUtil.json; import static org.cobbzilla.util.reflect.ReflectionUtil.copy; @@ -108,6 +109,6 @@ public class AppRule extends IdentifiableBaseParentEntity implements AppTemplate @Override public Identifiable update(Identifiable other) { copy(this, other, UPDATE_FIELDS); return this; } @Transient public JsonNode getConfig () { return json(configJson, JsonNode.class); } - public AppRule setConfig(JsonNode config) { return setConfigJson(json(config)); } + public AppRule setConfig(JsonNode config) { return setConfigJson(json(config, DB_JSON_MAPPER)); } } diff --git a/bubble-server/src/main/java/bubble/model/app/BubbleApp.java b/bubble-server/src/main/java/bubble/model/app/BubbleApp.java index 4b98575e..dedf2dd7 100644 --- a/bubble-server/src/main/java/bubble/model/app/BubbleApp.java +++ b/bubble-server/src/main/java/bubble/model/app/BubbleApp.java @@ -18,8 +18,7 @@ import javax.persistence.Entity; import javax.persistence.Transient; import javax.validation.constraints.Size; -import static bubble.ApiConstants.APPS_ENDPOINT; -import static bubble.ApiConstants.EP_APPS; +import static bubble.ApiConstants.*; import static org.cobbzilla.util.json.JsonUtil.json; import static org.cobbzilla.util.reflect.ReflectionUtil.copy; import static org.cobbzilla.wizard.model.crypto.EncryptedTypes.ENCRYPTED_STRING; @@ -79,7 +78,7 @@ public class BubbleApp extends IdentifiableBaseParentEntity implements AccountTe @JsonIgnore @Getter @Setter private String dataConfigJson; @Transient public AppDataConfig getDataConfig () { return dataConfigJson == null ? null : json(dataConfigJson, AppDataConfig.class); } - public BubbleApp setDataConfig (AppDataConfig adc) { return setDataConfigJson(adc == null ? null : json(adc)); } + public BubbleApp setDataConfig (AppDataConfig adc) { return setDataConfigJson(adc == null ? null : json(adc, DB_JSON_MAPPER)); } public boolean hasDataConfig () { return getDataConfig() != null; } @ECSearchable @ECField(index=60) diff --git a/bubble-server/src/main/java/bubble/model/app/RuleDriver.java b/bubble-server/src/main/java/bubble/model/app/RuleDriver.java index 5c364197..35336153 100644 --- a/bubble-server/src/main/java/bubble/model/app/RuleDriver.java +++ b/bubble-server/src/main/java/bubble/model/app/RuleDriver.java @@ -22,8 +22,7 @@ import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.Transient; -import static bubble.ApiConstants.DRIVERS_ENDPOINT; -import static bubble.ApiConstants.EP_DRIVERS; +import static bubble.ApiConstants.*; import static org.cobbzilla.util.json.JsonUtil.json; import static org.cobbzilla.util.reflect.ReflectionUtil.copy; import static org.cobbzilla.util.reflect.ReflectionUtil.instantiate; @@ -85,7 +84,7 @@ public class RuleDriver extends IdentifiableBase implements AccountTemplate { @Column(length=100000) @ECField(index=80) @JsonIgnore @Getter @Setter private String userConfigJson; - @Transient public RuleDriver setUserConfig (JsonNode json) { return setUserConfigJson(json(json)); } + @Transient public RuleDriver setUserConfig (JsonNode json) { return setUserConfigJson(json(json, DB_JSON_MAPPER)); } public JsonNode getUserConfig () { return json(userConfigJson, JsonNode.class); } @Transient @JsonIgnore @Getter(lazy=true) private final AppRuleDriver driver = instantiate(this.driverClass); diff --git a/bubble-server/src/main/java/bubble/model/cloud/AnsibleRole.java b/bubble-server/src/main/java/bubble/model/cloud/AnsibleRole.java index 21cfd833..90a544eb 100644 --- a/bubble-server/src/main/java/bubble/model/cloud/AnsibleRole.java +++ b/bubble-server/src/main/java/bubble/model/cloud/AnsibleRole.java @@ -19,6 +19,7 @@ import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; import java.util.Arrays; +import static bubble.ApiConstants.DB_JSON_MAPPER; import static bubble.ApiConstants.EP_ROLES; import static bubble.cloud.storage.StorageServiceDriver.STORAGE_PREFIX; import static org.cobbzilla.util.json.JsonUtil.json; @@ -104,14 +105,14 @@ public class AnsibleRole extends IdentifiableBase implements AccountTemplate, Ha public boolean hasConfig () { return configJson != null; } @Transient public NameAndValue[] getConfig () { return configJson == null ? null : json(configJson, NameAndValue[].class); } - public AnsibleRole setConfig(NameAndValue[] config) { return setConfigJson(config == null ? null : json(config)); } + public AnsibleRole setConfig(NameAndValue[] config) { return setConfigJson(config == null ? null : json(config, DB_JSON_MAPPER)); } @Column(updatable=false, length=1000) @ECField(index=80) @JsonIgnore @Getter @Setter private String optionalConfigNamesJson; public boolean hasOptionalConfigNames () { return optionalConfigNamesJson != null; } @Transient public String[] getOptionalConfigNames() { return optionalConfigNamesJson == null ? null : json(optionalConfigNamesJson, String[].class); } - public AnsibleRole setOptionalConfigNames(String[] names) { return setOptionalConfigNamesJson(name == null ? null : json(names)); } + public AnsibleRole setOptionalConfigNames(String[] names) { return setOptionalConfigNamesJson(name == null ? null : json(names, DB_JSON_MAPPER)); } // The Base64-encoded .tgz archive for the role directory. all paths should start with roles//... // Then after it is stored (in AnsibleRoleDAO.preCreate), this becomes storage://CloudServiceName/path diff --git a/bubble-server/src/main/java/bubble/model/cloud/BubbleDomain.java b/bubble-server/src/main/java/bubble/model/cloud/BubbleDomain.java index 6272333b..087d2eec 100644 --- a/bubble-server/src/main/java/bubble/model/cloud/BubbleDomain.java +++ b/bubble-server/src/main/java/bubble/model/cloud/BubbleDomain.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import static bubble.ApiConstants.DB_JSON_MAPPER; import static bubble.ApiConstants.EP_DOMAINS; import static bubble.model.cloud.AnsibleRole.sameRoleName; import static org.apache.commons.lang3.StringUtils.countMatches; @@ -104,7 +105,7 @@ public class BubbleDomain extends IdentifiableBase implements AccountTemplate { public boolean hasRoles () { return !empty(getRoles()); } @Transient public String[] getRoles () { return rolesJson == null ? null : json(rolesJson, String[].class); } - public BubbleDomain setRoles (String[] roles) { return setRolesJson(roles == null ? null : json(roles)); } + public BubbleDomain setRoles (String[] roles) { return setRolesJson(roles == null ? null : json(roles, DB_JSON_MAPPER)); } public String findRole(String r) { return AnsibleRole.findRole(getRoles(), r); } diff --git a/bubble-server/src/main/java/bubble/model/cloud/BubbleFootprint.java b/bubble-server/src/main/java/bubble/model/cloud/BubbleFootprint.java index 42ac8372..d6116d7c 100644 --- a/bubble-server/src/main/java/bubble/model/cloud/BubbleFootprint.java +++ b/bubble-server/src/main/java/bubble/model/cloud/BubbleFootprint.java @@ -23,8 +23,8 @@ import java.util.HashSet; import java.util.Set; import java.util.function.Function; +import static bubble.ApiConstants.DB_JSON_MAPPER; import static bubble.ApiConstants.EP_FOOTPRINTS; -import static org.cobbzilla.util.daemon.ZillaRuntime.bool; import static org.cobbzilla.util.daemon.ZillaRuntime.die; import static org.cobbzilla.util.json.JsonUtil.json; import static org.cobbzilla.util.reflect.ReflectionUtil.copy; @@ -90,7 +90,7 @@ public class BubbleFootprint extends IdentifiableBase implements AccountTemplate public boolean hasAllowedCountries () { return allowedCountriesJson != null; } @Transient public String[] getAllowedCountries () { return allowedCountriesJson == null ? null : json(allowedCountriesJson, String[].class); } - public BubbleFootprint setAllowedCountries (String[] countries) { return setAllowedCountriesJson(countries == null ? null : json(countries)); } + public BubbleFootprint setAllowedCountries (String[] countries) { return setAllowedCountriesJson(countries == null ? null : json(countries, DB_JSON_MAPPER)); } @ECSearchable @ECField(index=70) @Size(max=5000, message="err.disallowedCountriesJson.length") @@ -99,7 +99,7 @@ public class BubbleFootprint extends IdentifiableBase implements AccountTemplate public boolean hasDisallowedCountries () { return disallowedCountriesJson != null; } @Transient public String[] getDisallowedCountries () { return disallowedCountriesJson == null ? null : json(disallowedCountriesJson, String[].class); } - public BubbleFootprint setDisallowedCountries (String[] countries) { return setDisallowedCountriesJson(countries == null ? null : json(countries)); } + public BubbleFootprint setDisallowedCountries (String[] countries) { return setDisallowedCountriesJson(countries == null ? null : json(countries, DB_JSON_MAPPER)); } public void addDisallowedCountries(String[] countries) { final Set disallowed = hasDisallowedCountries() ? new HashSet<>(Arrays.asList(getDisallowedCountries())) : new HashSet<>(); diff --git a/bubble-server/src/main/java/bubble/model/cloud/CloudService.java b/bubble-server/src/main/java/bubble/model/cloud/CloudService.java index 474f314d..82da5174 100644 --- a/bubble-server/src/main/java/bubble/model/cloud/CloudService.java +++ b/bubble-server/src/main/java/bubble/model/cloud/CloudService.java @@ -44,6 +44,7 @@ import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import static bubble.ApiConstants.DB_JSON_MAPPER; import static bubble.ApiConstants.EP_CLOUDS; import static bubble.cloud.storage.local.LocalStorageDriver.LOCAL_STORAGE; import static org.cobbzilla.util.daemon.ZillaRuntime.*; @@ -144,14 +145,14 @@ public class CloudService extends IdentifiableBaseParentEntity implements Accoun public boolean hasDriverConfig () { return !empty(driverConfigJson); } @Transient public JsonNode getDriverConfig () { return json(driverConfigJson, JsonNode.class); } - public CloudService setDriverConfig (JsonNode node) { return setDriverConfigJson(node == null ? null : json(node)); } + public CloudService setDriverConfig (JsonNode node) { return setDriverConfigJson(node == null ? null : json(node, DB_JSON_MAPPER)); } @Size(max=10000, message="err.credentialsJson.length") @Type(type=ENCRYPTED_STRING) @Column(columnDefinition="varchar("+(10000+ENC_PAD)+")") @JsonIgnore @Getter @Setter private String credentialsJson; @Transient public CloudCredentials getCredentials () { return credentialsJson == null ? null : json(credentialsJson, CloudCredentials.class); } - public CloudService setCredentials (CloudCredentials credentials) { return setCredentialsJson(credentials == null ? null : json(credentials)); } + public CloudService setCredentials (CloudCredentials credentials) { return setCredentialsJson(credentials == null ? null : json(credentials, DB_JSON_MAPPER)); } public boolean hasCredentials () { final CloudCredentials creds = getCredentials(); return creds != null && !empty(creds.getParams()); diff --git a/bubble-server/src/main/java/bubble/model/cloud/CloudServiceData.java b/bubble-server/src/main/java/bubble/model/cloud/CloudServiceData.java index 18673b83..26eac98c 100644 --- a/bubble-server/src/main/java/bubble/model/cloud/CloudServiceData.java +++ b/bubble-server/src/main/java/bubble/model/cloud/CloudServiceData.java @@ -19,6 +19,7 @@ import javax.persistence.Entity; import javax.persistence.Transient; import javax.validation.constraints.Size; +import static bubble.ApiConstants.DB_JSON_MAPPER; import static bubble.ApiConstants.EP_DATA; import static org.cobbzilla.util.json.JsonUtil.json; import static org.cobbzilla.util.reflect.ReflectionUtil.copy; @@ -60,7 +61,7 @@ public class CloudServiceData extends IdentifiableBase implements HasAccount { @Getter @Setter private String data; @Transient public JsonNode getDataJson () { return data == null ? null : json(data, JsonNode.class); } - public CloudServiceData setDataJson(JsonNode n) { return setData(n == null ? null : json(n)); } + public CloudServiceData setDataJson(JsonNode n) { return setData(n == null ? null : json(n, DB_JSON_MAPPER)); } @ECSearchable @ECField(index=50, type=EntityFieldType.expiration_time) @ECIndex @Getter @Setter private Long expiration; diff --git a/bubble-server/src/main/java/bubble/model/cloud/notify/NotificationBase.java b/bubble-server/src/main/java/bubble/model/cloud/notify/NotificationBase.java index 8b595a61..356f739c 100644 --- a/bubble-server/src/main/java/bubble/model/cloud/notify/NotificationBase.java +++ b/bubble-server/src/main/java/bubble/model/cloud/notify/NotificationBase.java @@ -19,6 +19,7 @@ import org.hibernate.annotations.Type; import javax.persistence.*; +import static bubble.ApiConstants.DB_JSON_MAPPER; import static bubble.ApiConstants.ERROR_MAXLEN; import static org.cobbzilla.util.daemon.ZillaRuntime.bool; import static org.cobbzilla.util.daemon.ZillaRuntime.errorString; @@ -68,7 +69,7 @@ public class NotificationBase extends IdentifiableBase implements HasAccountNoNa public boolean hasPayload () { return payloadJson != null; } @Transient public JsonNode getPayload () { return json(payloadJson, JsonNode.class); } - public T setPayload (JsonNode payload) { return (T) setPayloadJson(json(payload)); } + public T setPayload (JsonNode payload) { return (T) setPayloadJson(json(payload, DB_JSON_MAPPER)); } @Transient @JsonIgnore public BubbleNode getNode() { return json(payloadJson, BubbleNode.class); } @@ -81,7 +82,7 @@ public class NotificationBase extends IdentifiableBase implements HasAccountNoNa @JsonIgnore @Getter @Setter private String receiptJson; @Transient public NotificationReceipt getReceipt () { return receiptJson == null ? null : json(receiptJson, NotificationReceipt.class); } - public T setReceipt (NotificationReceipt receipt) { return (T) setReceiptJson(receipt == null ? null : json(receiptJson)); } + public T setReceipt (NotificationReceipt receipt) { return (T) setReceiptJson(receipt == null ? null : json(receiptJson, DB_JSON_MAPPER)); } @ECSearchable(filter=true) @ECField(index=100) @Type(type=ENCRYPTED_STRING) @Column(updatable=false, columnDefinition="varchar("+(ERROR_MAXLEN+ENC_PAD)+")")