mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-08 05:11:04 +00:00
encode/decode keys used in Mongo coming from arbitrary strings to avoid leading $ and . in between
This commit is contained in:
1 parent
c8119cc241
commit
6f274aa51b
5 files changed
+52
-9
No files matched your search
+4
-4
@@ -148,10 +148,10 @@ public class TestStoringAndRetrievingLeaderboards extends AbstractMongoDBTest {
|
||||
}
|
||||
|
||||
private Competitor createCompetitor() {
|
||||
Competitor competitor = new CompetitorImpl(123, "Wolfgang Hunger", new TeamImpl("STG", Collections.singleton(
|
||||
new PersonImpl("Wolfgang Hunger", new NationalityImpl("Germany", "GER"),
|
||||
/* dateOfBirth */ null, "This is famous Wolfgang Hunger")), new PersonImpl("Rigo van Maas", new NationalityImpl("The Netherlands", "NED"),
|
||||
/* dateOfBirth */ null, "This is Rigo, the coach")), new BoatImpl("Wolfgang Hunger's boat", new BoatClassImpl("505")));
|
||||
Competitor competitor = new CompetitorImpl(123, "$$$Dr. Wolfgang Hunger$$$", new TeamImpl("STG", Collections.singleton(
|
||||
new PersonImpl("$$$Dr. Wolfgang Hunger$$$", new NationalityImpl("Germany", "GER"),
|
||||
/* dateOfBirth */ null, "This is famous Dr. Wolfgang Hunger")), new PersonImpl("Rigo van Maas", new NationalityImpl("The Netherlands", "NED"),
|
||||
/* dateOfBirth */ null, "This is Rigo, the coach")), new BoatImpl("Dr. Wolfgang Hunger's boat", new BoatClassImpl("505")));
|
||||
return competitor;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -176,7 +176,7 @@ public class DomainObjectFactoryImpl implements DomainObjectFactory {
|
||||
for (String competitorName : carriedPoints.keySet()) {
|
||||
Integer carriedPointsForCompetitor = (Integer) carriedPoints.get(competitorName);
|
||||
if (carriedPointsForCompetitor != null) {
|
||||
result.setCarriedPoints(competitorName, carriedPointsForCompetitor);
|
||||
result.setCarriedPoints(MongoUtils.unescapeDollarAndDot(competitorName), carriedPointsForCompetitor);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -187,12 +187,12 @@ public class DomainObjectFactoryImpl implements DomainObjectFactory {
|
||||
RaceInLeaderboard raceColumn = result.getRaceColumnByName(raceName);
|
||||
DBObject dbScoreCorrectionForCompetitorInRace = (DBObject) dbScoreCorrectionForRace.get(competitorName);
|
||||
if (dbScoreCorrectionForCompetitorInRace.containsField(FieldNames.LEADERBOARD_SCORE_CORRECTION_MAX_POINTS_REASON.name())) {
|
||||
result.setMaxPointsReason(competitorName, raceColumn, MaxPointsReason
|
||||
result.setMaxPointsReason(MongoUtils.unescapeDollarAndDot(competitorName), raceColumn, MaxPointsReason
|
||||
.valueOf((String) dbScoreCorrectionForCompetitorInRace
|
||||
.get(FieldNames.LEADERBOARD_SCORE_CORRECTION_MAX_POINTS_REASON.name())));
|
||||
}
|
||||
if (dbScoreCorrectionForCompetitorInRace.containsField(FieldNames.LEADERBOARD_CORRECTED_SCORE.name())) {
|
||||
result.correctScore(competitorName, raceColumn, (Integer) dbScoreCorrectionForCompetitorInRace
|
||||
result.correctScore(MongoUtils.unescapeDollarAndDot(competitorName), raceColumn, (Integer) dbScoreCorrectionForCompetitorInRace
|
||||
.get(FieldNames.LEADERBOARD_CORRECTED_SCORE.name()));
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -125,7 +125,7 @@ public class MongoObjectFactoryImpl implements MongoObjectFactory {
|
||||
BasicDBObject dbCarriedPoints = new BasicDBObject();
|
||||
result.put(FieldNames.LEADERBOARD_CARRIED_POINTS.name(), dbCarriedPoints);
|
||||
for (Competitor competitor : leaderboard.getCompetitors()) {
|
||||
dbCarriedPoints.put(competitor.getName(), leaderboard.getCarriedPoints(competitor));
|
||||
dbCarriedPoints.put(MongoUtils.escapeDollarAndDot(competitor.getName()), leaderboard.getCarriedPoints(competitor));
|
||||
}
|
||||
}
|
||||
BasicDBObject dbScoreCorrections = new BasicDBObject();
|
||||
@@ -157,7 +157,7 @@ public class MongoObjectFactoryImpl implements MongoObjectFactory {
|
||||
dbCorrectionForCompetitor.put(FieldNames.LEADERBOARD_CORRECTED_SCORE.name(),
|
||||
explicitScoreCorrection);
|
||||
}
|
||||
dbCorrectionForRace.put(competitor.getName(), dbCorrectionForCompetitor);
|
||||
dbCorrectionForRace.put(MongoUtils.escapeDollarAndDot(competitor.getName()), dbCorrectionForCompetitor);
|
||||
}
|
||||
}
|
||||
if (!dbCorrectionForRace.isEmpty()) {
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.sap.sailing.mongodb.impl;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
|
||||
public class MongoUtils {
|
||||
/**
|
||||
* Key names in MongoDB must not start with a $ sign and must not contain a "." character. This method escapes
|
||||
* those. {@link #unescapeDollarAndDot} is the inverse function.
|
||||
*
|
||||
* @param key
|
||||
* a non-<code>null</code> string which may have length 0
|
||||
* @return a string that can be used as a key in a MongoDB key-value pair and that can be decoded again using
|
||||
* {@link #unescapeDollarAndDot(String)}.
|
||||
*/
|
||||
public static String escapeDollarAndDot(String key) {
|
||||
String result = key.replace("%", "%25");
|
||||
if (key.length() > 0 && key.charAt(0) == '$') {
|
||||
result = "%24"+key.substring(1);
|
||||
} else {
|
||||
result = key;
|
||||
}
|
||||
return result.replace(".", "%2E");
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #escapeDollarAndDot(String)
|
||||
* @param key
|
||||
* a non-<code>null</code> string which may have length 0 and that was returned by
|
||||
* {@link #escapeDollarAndDot(String)}.
|
||||
* @return the string originally passed to {@link #escapeDollarAndDot(String)}
|
||||
*/
|
||||
public static String unescapeDollarAndDot(String escapedKey) {
|
||||
try {
|
||||
return URLDecoder.decode(escapedKey, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException("How come this VM doesn't know UTF-8?");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user