Skip to content

Commit

Permalink
Add new limit to user preference value size
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanrdoherty committed Apr 11, 2021
1 parent 3a7e66c commit bef6ee3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class UserPreferenceFactory {

private static final String GLOBAL_PREFERENCE_KEY = "[Global]";

// even though we store value as a CLOB, the entire value is stored in memory
// at once (no streaming), so we must still be conscious of size
public static final long MAX_PREFERENCE_VALUE_SIZE = 200000;
public static final long MAX_PREFERENCE_KEY_SIZE = 4000;

private final WdkModel _wdkModel;
private final DatabaseInstance _userDb;
private final String _userSchema;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.gusdb.wdk.service.request.user;

import static org.gusdb.wdk.model.user.UserPreferenceFactory.MAX_PREFERENCE_KEY_SIZE;
import static org.gusdb.wdk.model.user.UserPreferenceFactory.MAX_PREFERENCE_VALUE_SIZE;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.gusdb.fgputil.FormatUtil;
import org.gusdb.fgputil.json.JsonUtil;
import org.gusdb.wdk.service.request.exception.DataValidationException;
import org.gusdb.wdk.service.request.exception.RequestMisformatException;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -26,8 +31,9 @@ public class UserPreferencesRequest {
* @param json
* @return
* @throws RequestMisformatException
* @throws DataValidationException
*/
public static UserPreferencesRequest createFromJson(JSONObject json) throws RequestMisformatException {
public static UserPreferencesRequest createFromJson(JSONObject json) throws RequestMisformatException, DataValidationException {
try {
UserPreferencesRequest request = new UserPreferencesRequest();
loadPreferenceChanges(json, request._updates, request._deletes);
Expand All @@ -40,14 +46,21 @@ public static UserPreferencesRequest createFromJson(JSONObject json) throws Requ
}

private static void loadPreferenceChanges(JSONObject json,
Map<String,String> updates, List<String> deletes) {
Map<String,String> updates, List<String> deletes) throws DataValidationException {

for (String key : JsonUtil.getKeys(json)) {
if (json.isNull(key)) {
deletes.add(key);
}
else {
updates.put(key, json.getString(key));
if (FormatUtil.getUtf8EncodedBytes(key).length > MAX_PREFERENCE_KEY_SIZE) {
throw new DataValidationException("User preference key size cannot be greater than " + MAX_PREFERENCE_KEY_SIZE + " bytes.");
}
String pref = json.getString(key);
if (FormatUtil.getUtf8EncodedBytes(pref).length > MAX_PREFERENCE_VALUE_SIZE) {
throw new DataValidationException("User preference value size cannot be greater than " + MAX_PREFERENCE_VALUE_SIZE + " bytes.");
}
updates.put(key, pref);
}
}

Expand Down

0 comments on commit bef6ee3

Please sign in to comment.