NXP-14868: cleanup and factorization

See nuxeo/nuxeo-core#8