make Jackson inject repoId rather than looping later to add it

repoId is used in Repo, App, and Apk instances to point to the Repo data
in the database.  It does not come from the index files, but rather the
client database.
This commit is contained in:
Hans-Christoph Steiner 2017-03-22 13:58:27 +01:00 committed by Peter Serwylo
parent 38d21cd178
commit 6f58c2a13d
4 changed files with 16 additions and 7 deletions

View File

@ -13,6 +13,7 @@ import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -133,6 +134,7 @@ public class IndexV1Updater extends RepoUpdater {
throws IOException, UpdateException { throws IOException, UpdateException {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setInjectableValues(new InjectableValues.Std().addValue(long.class, repo.getId()));
JsonFactory f = mapper.getFactory(); JsonFactory f = mapper.getFactory();
JsonParser parser = f.createParser(indexInputStream); JsonParser parser = f.createParser(indexInputStream);
HashMap<String, Object> repoMap = null; HashMap<String, Object> repoMap = null;
@ -192,7 +194,6 @@ public class IndexV1Updater extends RepoUpdater {
RepoPersister repoPersister = new RepoPersister(context, repo); RepoPersister repoPersister = new RepoPersister(context, repo);
if (apps != null && apps.length > 0) { if (apps != null && apps.length > 0) {
for (App app : apps) { for (App app : apps) {
app.repoId = repo.getId(); // TODO this should be "injected" i.e. @JacksonInject
List<Apk> apks = null; List<Apk> apks = null;
if (packages != null) { if (packages != null) {
apks = packages.get(app.packageName); apks = packages.get(app.packageName);

View File

@ -8,6 +8,7 @@ import android.os.Build;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import org.fdroid.fdroid.BuildConfig; import org.fdroid.fdroid.BuildConfig;
@ -47,8 +48,6 @@ public class Apk extends ValueObject implements Comparable<Apk>, Parcelable {
// these are never set by the Apk/package index metadata // these are never set by the Apk/package index metadata
@JsonIgnore @JsonIgnore
public long repo; // ID of the repo it comes from
@JsonIgnore
String repoAddress; String repoAddress;
@JsonIgnore @JsonIgnore
int repoVersion; int repoVersion;
@ -57,6 +56,9 @@ public class Apk extends ValueObject implements Comparable<Apk>, Parcelable {
@JsonIgnore @JsonIgnore
public boolean compatible; // True if compatible with the device. public boolean compatible; // True if compatible with the device.
@JacksonInject
public long repo; // ID of the repo it comes from
// these come directly from the index metadata // these come directly from the index metadata
public String packageName; public String packageName;
public String versionName; public String versionName;

View File

@ -16,6 +16,7 @@ import android.support.annotation.Nullable;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
@ -85,8 +86,6 @@ public class App extends ValueObject implements Comparable<App>, Parcelable {
* This is required for getting the full URL to the various graphics and screenshots. * This is required for getting the full URL to the various graphics and screenshots.
*/ */
@JsonIgnore @JsonIgnore
public long repoId;
@JsonIgnore
public Apk installedApk; // might be null if not installed public Apk installedApk; // might be null if not installed
@JsonIgnore @JsonIgnore
public String installedSig; public String installedSig;
@ -99,6 +98,9 @@ public class App extends ValueObject implements Comparable<App>, Parcelable {
@JsonIgnore @JsonIgnore
private AppPrefs prefs; private AppPrefs prefs;
@JacksonInject
public long repoId;
// the remaining properties are set directly from the index metadata // the remaining properties are set directly from the index metadata
public String packageName = "unknown"; public String packageName = "unknown";
public String name = "Unknown"; public String name = "Unknown";

View File

@ -7,6 +7,7 @@ import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.ObjectReader;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@ -38,6 +39,7 @@ import static org.junit.Assert.fail;
public class IndexV1UpdaterTest extends FDroidProviderTest { public class IndexV1UpdaterTest extends FDroidProviderTest {
public static final String TAG = "IndexV1UpdaterTest"; public static final String TAG = "IndexV1UpdaterTest";
private static final long FAKE_REPO_ID = 0xdeadbeef;
private static final String TESTY_JAR = "testy.at.or.at_index-v1.jar"; private static final String TESTY_JAR = "testy.at.or.at_index-v1.jar";
private static final String TESTY_CERT = "308204e1308202c9a0030201020204483450fa300d06092a864886f70d01010b050030213110300e060355040b1307462d44726f6964310d300b06035504031304736f7661301e170d3136303832333133333131365a170d3434303130393133333131365a30213110300e060355040b1307462d44726f6964310d300b06035504031304736f766130820222300d06092a864886f70d01010105000382020f003082020a0282020100dfdcd120f3ab224999dddf4ea33ea588d295e4d7130bef48c143e9d76e5c0e0e9e5d45e64208e35feebc79a83f08939dd6a343b7d1e2179930a105a1249ccd36d88ff3feffc6e4dc53dae0163a7876dd45ecc1ddb0adf5099aa56c1a84b52affcd45d0711ffa4de864f35ac0333ebe61ea8673eeda35a88f6af678cc4d0f80b089338ac8f2a8279a64195c611d19445cab3fd1a020afed9bd739bb95142fb2c00a8f847db5ef3325c814f8eb741bacf86ed3907bfe6e4564d2de5895df0c263824e0b75407589bae2d3a4666c13b92102d8781a8ee9bb4a5a1a78c4a9c21efdaf5584da42e84418b28f5a81d0456a3dc5b420991801e6b21e38c99bbe018a5b2d690894a114bc860d35601416aa4dc52216aff8a288d4775cddf8b72d45fd2f87303a8e9c0d67e442530be28eaf139894337266e0b33d57f949256ab32083bcc545bc18a83c9ab8247c12aea037e2b68dee31c734cb1f04f241d3b94caa3a2b258ffaf8e6eae9fbbe029a934dc0a0859c5f120334812693a1c09352340a39f2a678dbc1afa2a978bfee43afefcb7e224a58af2f3d647e5745db59061236b8af6fcfd93b3602f9e456978534f3a7851e800071bf56da80401c81d91c45f82568373af0576b1cc5eef9b85654124b6319770be3cdba3fbebe3715e8918fb6c8966624f3d0e815effac3d2ee06dd34ab9c693218b2c7c06ba99d6b74d4f17b8c3cb0203010001a321301f301d0603551d0e04160414d62bee9f3798509546acc62eb1de14b08b954d4f300d06092a864886f70d01010b05000382020100743f7c5692085895f9d1fffad390fb4202c15f123ed094df259185960fd6dadf66cb19851070f180297bba4e6996a4434616573b375cfee94fee73a4505a7ec29136b7e6c22e6436290e3686fe4379d4e3140ec6a08e70cfd3ed5b634a5eb5136efaaabf5f38e0432d3d79568a556970b8cfba2972f5d23a3856d8a981b9e9bbbbb88f35e708bde9cbc5f681cbd974085b9da28911296fe2579fa64bbe9fa0b93475a7a8db051080b0c5fade0d1c018e7858cd4cbe95145b0620e2f632cbe0f8af9cbf22e2fdaa72245ae31b0877b07181cc69dd2df74454251d8de58d25e76354abe7eb690f22e59b08795a8f2c98c578e0599503d9085927634072c82c9f82abd50fd12b8fd1a9d1954eb5cc0b4cfb5796b5aaec0356643b4a65a368442d92ef94edd3ac6a2b7fe3571b8cf9f462729228aab023ef9183f73792f5379633ccac51079177d604c6bc1873ada6f07d8da6d68c897e88a5fa5d63fdb8df820f46090e0716e7562dd3c140ba279a65b996f60addb0abe29d4bf2f5abe89480771d492307b926d91f02f341b2148502903c43d40f3c6c86a811d060711f0698b384acdcc0add44eb54e42962d3d041accc715afd49407715adc09350cb55e8d9281a3b0b6b5fcd91726eede9b7c8b13afdebb2c2b377629595f1096ba62fb14946dbac5f3c5f0b4e5b712e7acc7dcf6c46cdc5e6d6dfdeee55a0c92c2d70f080ac6"; private static final String TESTY_CERT = "308204e1308202c9a0030201020204483450fa300d06092a864886f70d01010b050030213110300e060355040b1307462d44726f6964310d300b06035504031304736f7661301e170d3136303832333133333131365a170d3434303130393133333131365a30213110300e060355040b1307462d44726f6964310d300b06035504031304736f766130820222300d06092a864886f70d01010105000382020f003082020a0282020100dfdcd120f3ab224999dddf4ea33ea588d295e4d7130bef48c143e9d76e5c0e0e9e5d45e64208e35feebc79a83f08939dd6a343b7d1e2179930a105a1249ccd36d88ff3feffc6e4dc53dae0163a7876dd45ecc1ddb0adf5099aa56c1a84b52affcd45d0711ffa4de864f35ac0333ebe61ea8673eeda35a88f6af678cc4d0f80b089338ac8f2a8279a64195c611d19445cab3fd1a020afed9bd739bb95142fb2c00a8f847db5ef3325c814f8eb741bacf86ed3907bfe6e4564d2de5895df0c263824e0b75407589bae2d3a4666c13b92102d8781a8ee9bb4a5a1a78c4a9c21efdaf5584da42e84418b28f5a81d0456a3dc5b420991801e6b21e38c99bbe018a5b2d690894a114bc860d35601416aa4dc52216aff8a288d4775cddf8b72d45fd2f87303a8e9c0d67e442530be28eaf139894337266e0b33d57f949256ab32083bcc545bc18a83c9ab8247c12aea037e2b68dee31c734cb1f04f241d3b94caa3a2b258ffaf8e6eae9fbbe029a934dc0a0859c5f120334812693a1c09352340a39f2a678dbc1afa2a978bfee43afefcb7e224a58af2f3d647e5745db59061236b8af6fcfd93b3602f9e456978534f3a7851e800071bf56da80401c81d91c45f82568373af0576b1cc5eef9b85654124b6319770be3cdba3fbebe3715e8918fb6c8966624f3d0e815effac3d2ee06dd34ab9c693218b2c7c06ba99d6b74d4f17b8c3cb0203010001a321301f301d0603551d0e04160414d62bee9f3798509546acc62eb1de14b08b954d4f300d06092a864886f70d01010b05000382020100743f7c5692085895f9d1fffad390fb4202c15f123ed094df259185960fd6dadf66cb19851070f180297bba4e6996a4434616573b375cfee94fee73a4505a7ec29136b7e6c22e6436290e3686fe4379d4e3140ec6a08e70cfd3ed5b634a5eb5136efaaabf5f38e0432d3d79568a556970b8cfba2972f5d23a3856d8a981b9e9bbbbb88f35e708bde9cbc5f681cbd974085b9da28911296fe2579fa64bbe9fa0b93475a7a8db051080b0c5fade0d1c018e7858cd4cbe95145b0620e2f632cbe0f8af9cbf22e2fdaa72245ae31b0877b07181cc69dd2df74454251d8de58d25e76354abe7eb690f22e59b08795a8f2c98c578e0599503d9085927634072c82c9f82abd50fd12b8fd1a9d1954eb5cc0b4cfb5796b5aaec0356643b4a65a368442d92ef94edd3ac6a2b7fe3571b8cf9f462729228aab023ef9183f73792f5379633ccac51079177d604c6bc1873ada6f07d8da6d68c897e88a5fa5d63fdb8df820f46090e0716e7562dd3c140ba279a65b996f60addb0abe29d4bf2f5abe89480771d492307b926d91f02f341b2148502903c43d40f3c6c86a811d060711f0698b384acdcc0add44eb54e42962d3d041accc715afd49407715adc09350cb55e8d9281a3b0b6b5fcd91726eede9b7c8b13afdebb2c2b377629595f1096ba62fb14946dbac5f3c5f0b4e5b712e7acc7dcf6c46cdc5e6d6dfdeee55a0c92c2d70f080ac6";
@ -116,6 +118,7 @@ public class IndexV1UpdaterTest extends FDroidProviderTest {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
// the app ignores all unknown fields when complete, do not ignore during dev to catch mistakes // the app ignores all unknown fields when complete, do not ignore during dev to catch mistakes
mapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setInjectableValues(new InjectableValues.Std().addValue(long.class, FAKE_REPO_ID));
JsonFactory f = mapper.getFactory(); JsonFactory f = mapper.getFactory();
JsonParser parser = f.createParser(TestUtils.copyResourceToTempFile("guardianproject_index-v1.json")); JsonParser parser = f.createParser(TestUtils.copyResourceToTempFile("guardianproject_index-v1.json"));
@ -209,6 +212,7 @@ public class IndexV1UpdaterTest extends FDroidProviderTest {
"packageName", "packageName",
"phoneScreenshots", "phoneScreenshots",
"promoGraphic", "promoGraphic",
"repoId",
"requirements", "requirements",
"sevenInchScreenshots", "sevenInchScreenshots",
"sourceCode", "sourceCode",
@ -233,7 +237,6 @@ public class IndexV1UpdaterTest extends FDroidProviderTest {
"installedVersionCode", "installedVersionCode",
"installedVersionName", "installedVersionName",
"prefs", "prefs",
"repoId",
"TAG", "TAG",
}; };
runJsonIgnoreTest(new App(), allowedInApp, ignoredInApp); runJsonIgnoreTest(new App(), allowedInApp, ignoredInApp);
@ -258,6 +261,7 @@ public class IndexV1UpdaterTest extends FDroidProviderTest {
"obbPatchFile", "obbPatchFile",
"obbPatchFileSha256", "obbPatchFileSha256",
"packageName", "packageName",
"repo",
"requestedPermissions", "requestedPermissions",
"sig", "sig",
"size", "size",
@ -270,7 +274,6 @@ public class IndexV1UpdaterTest extends FDroidProviderTest {
"compatible", "compatible",
"CREATOR", "CREATOR",
"installedFile", "installedFile",
"repo",
"repoAddress", "repoAddress",
"repoVersion", "repoVersion",
"SDK_VERSION_MAX_VALUE", "SDK_VERSION_MAX_VALUE",
@ -307,6 +310,7 @@ public class IndexV1UpdaterTest extends FDroidProviderTest {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
// testing with unknown metadata in it // testing with unknown metadata in it
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setInjectableValues(new InjectableValues.Std().addValue(long.class, FAKE_REPO_ID));
JsonFactory f = mapper.getFactory(); JsonFactory f = mapper.getFactory();
JsonParser parser = f.createParser(TestUtils.copyResourceToTempFile("all_fields_index-v1.json")); JsonParser parser = f.createParser(TestUtils.copyResourceToTempFile("all_fields_index-v1.json"));