/** temporary until we have decided how to deal with null configs or create a SubjectConfig class **/ public static SubjectConfig safeConfig(SubjectConfig config) { if (null == config) { return SubjectConfig.emptyConfig(); } else { return config; } }
public SubjectConfig build() { return new SubjectConfig( Collections.unmodifiableMap(new HashMap<String, String>(conf)), Collections.unmodifiableSet(new HashSet<String>(validators))); }
@GET @Path("{subject}/config") public String subjectConfig(@HeaderParam("Accept") String mediaType, @PathParam("subject") String subject) { Subject s = repo.lookup(subject); if (null == s) { throw new NotFoundException(MessageStrings.SUBJECT_DOES_NOT_EXIST_ERROR); } Properties props = new Properties(); props.putAll(s.getConfig().asMap()); return getRenderer(mediaType).renderProperties(props, "Configuration of subject " + subject); }
@Test public void testBuilderHashAndEquals() { SubjectConfig empty = SubjectConfig.emptyConfig(); Assert.assertEquals(empty, empty); SubjectConfig conf = new SubjectConfig.Builder().build(); Assert.assertEquals(empty, conf); SubjectConfig conf2 = new SubjectConfig.Builder() .set("repo.validators", null) .build(); // Explicitly setting empty or null validators is NOT the same as no validators // due to the default subject validators functionality (see Issue 38) Assert.assertNotEquals(conf, conf2); Assert.assertNotEquals(conf2, empty); Assert.assertNotEquals(conf.hashCode(), conf2.hashCode()); Assert.assertFalse(conf.equals(null)); Assert.assertFalse(conf.equals(new Object())); Assert.assertEquals(conf.hashCode(), empty.hashCode()); String k = "key"; String v = "val"; SubjectConfig custom = new SubjectConfig.Builder() .set(k, v).build(); SubjectConfig custom2 = new SubjectConfig.Builder() .set(custom.asMap()).build(); SubjectConfig custom3 = new SubjectConfig.Builder() .set(custom.asMap()).addValidator("foo").build(); Assert.assertEquals(custom, custom2); Assert.assertFalse(custom.equals(custom3)); Assert.assertFalse(custom.equals(conf)); Assert.assertEquals(custom.hashCode(), custom2.hashCode()); }
@Test public void testBuilder() { SubjectConfig conf = SubjectConfig.emptyConfig(); Assert.assertTrue(conf.getValidators().isEmpty()); SubjectConfig custom = new SubjectConfig.Builder() .set("k", "v") .set("repo.validators", "valid1, valid2 ,,") .addValidator("oneMore") .build(); Assert.assertEquals("v", custom.get("k")); Set<String> validators = custom.getValidators(); Assert.assertEquals(3, validators.size()); Assert.assertTrue(validators.contains("valid1")); Assert.assertTrue(validators.contains("valid2")); Assert.assertTrue(validators.contains("oneMore")); }
/** * Create a {@link Subject} that validates schemas as configured. */ public static Subject validatingSubject(Subject subject, ValidatorFactory factory) { if (null == subject) { return subject; } List<Validator> validators; SubjectConfig config = subject.getConfig(); // if the validators key is not specified in the subject config, get the default ones. // ensure that even an empty set is honored as "no validators" if (config.get(SubjectConfig.VALIDATORS_KEY) != null) validators = factory.getValidators(config.getValidators()); else validators = factory.getValidators(factory.getDefaultSubjectValidators()); if (!validators.isEmpty()) { return new ValidatingSubject(subject, new CompositeValidator(validators)); } else { return subject; } }
@Override protected void registerSubjectInBackend(final String subjectName, final SubjectConfig config) { final File subjectDir = new File(rootDir, subjectName); if (subjectDir.exists()) { throw new RuntimeException( "Cannot create a FileSubject, directory already exists: " + subjectDir.getAbsolutePath()); } if (!subjectDir.mkdir()) { throw new RuntimeException("Cannot create a FileSubject dir: " + subjectDir.getAbsolutePath()); } createNewFileInDir(subjectDir, SCHEMA_IDS); File subjectProperties = createNewFileInDir(subjectDir, SUBJECT_PROPERTIES); Properties props = new Properties(); props.putAll(RepositoryUtil.safeConfig(config).asMap()); writePropertyFile(subjectProperties, props); }
/** * Create a {@link Subject} that validates schemas as configured. */ public static Subject validatingSubject(Subject subject, ValidatorFactory factory) { if (null == subject) { return subject; } List<Validator> validators; SubjectConfig config = subject.getConfig(); // if the validators key is not specified in the subject config, get the default ones. // ensure that even an empty set is honored as "no validators" if (config.get(SubjectConfig.VALIDATORS_KEY) != null) validators = factory.getValidators(config.getValidators()); else validators = factory.getValidators(factory.getDefaultSubjectValidators()); if (!validators.isEmpty()) { return new ValidatingSubject(subject, new CompositeValidator(validators)); } else { return subject; } }
@Override protected void registerSubjectInBackend(final String subjectName, final SubjectConfig config) { final File subjectDir = new File(rootDir, subjectName); if (subjectDir.exists()) { throw new RuntimeException( "Cannot create a FileSubject, directory already exists: " + subjectDir.getAbsolutePath()); } if (!subjectDir.mkdir()) { throw new RuntimeException("Cannot create a FileSubject dir: " + subjectDir.getAbsolutePath()); } createNewFileInDir(subjectDir, SCHEMA_IDS); File subjectProperties = createNewFileInDir(subjectDir, SUBJECT_PROPERTIES); Properties props = new Properties(); props.putAll(RepositoryUtil.safeConfig(config).asMap()); writePropertyFile(subjectProperties, props); }
/** temporary until we have decided how to deal with null configs or create a SubjectConfig class **/ public static SubjectConfig safeConfig(SubjectConfig config) { if (null == config) { return SubjectConfig.emptyConfig(); } else { return config; } }
public SubjectConfig build() { return new SubjectConfig( Collections.unmodifiableMap(new HashMap<String, String>(conf)), Collections.unmodifiableSet(new HashSet<String>(validators))); }
@GET @Path("{subject}/config") public String subjectConfig(@HeaderParam("Accept") String mediaType, @PathParam("subject") String subject) { Subject s = repo.lookup(subject); if (null == s) { throw new NotFoundException(MessageStrings.SUBJECT_DOES_NOT_EXIST_ERROR); } Properties props = new Properties(); props.putAll(s.getConfig().asMap()); return getRenderer(mediaType).renderProperties(props, "Configuration of subject " + subject); }
@Override protected void registerSubjectInBackend(final String subjectName, final SubjectConfig config) { // If the Subject is not in the local cache, we acquire the lock to create it acquireLock(); try { zkClient.create().forPath(subjectName); // N.B.: There is a possibility that the Subject was already created by // another repository instance. If the create operation above didn't throw, // then we need to initialize the Subject. // Create schema IDs file zkClient.create().forPath(subjectName + "/" + SCHEMA_IDS); // Create properties file Properties props = new Properties(); props.putAll(RepositoryUtil.safeConfig(config).asMap()); StringWriter sw = new StringWriter(); props.store(sw, "Schema Repository Subject Properties"); byte[] content = sw.toString().getBytes(); zkClient.create().forPath(subjectName + "/" + SUBJECT_PROPERTIES, content); } catch (KeeperException.NodeExistsException e) { // The Subject was already created by another repository instance, we will // just fetch it, below, instead of creating a new one. } catch (Exception e) { logger.error("An exception occurred while accessing ZK!", e); throw new RuntimeException(e); } finally { releaseLock(); } }
@Override public Subject register(String subject, SubjectConfig config) { Form form = new Form(); for(Map.Entry<String, String> entry : RepositoryUtil.safeConfig(config).asMap().entrySet()) { form.putSingle(entry.getKey(), entry.getValue()); } String regSubjectName = webResource.path(subject).type(MediaType.APPLICATION_FORM_URLENCODED).put(String.class, form); return new RESTSubject(regSubjectName); }
@Override public Subject register(String subject, SubjectConfig config) { Form form = new Form(); for(Map.Entry<String, String> entry : RepositoryUtil.safeConfig(config).asMap().entrySet()) { form.putSingle(entry.getKey(), entry.getValue()); } String regSubjectName = webResource.path(subject).type(MediaType.APPLICATION_FORM_URLENCODED).put(String.class, form); return new RESTSubject(regSubjectName); }
@Override public Subject register(String subject, SubjectConfig config) { Form form = new Form(); for(Map.Entry<String, String> entry : RepositoryUtil.safeConfig(config).asMap().entrySet()) { form.putSingle(entry.getKey(), entry.getValue()); } String regSubjectName = webResource.path(subject).type(MediaType.APPLICATION_FORM_URLENCODED).put(String.class, form); return new RESTSubject(regSubjectName); }
@Override public Subject register(String subject, SubjectConfig config) { Form form = new Form(); for(Map.Entry<String, String> entry : RepositoryUtil.safeConfig(config).asMap().entrySet()) { form.putSingle(entry.getKey(), entry.getValue()); } String regSubjectName = webResource.path(subject).type(MediaType.APPLICATION_FORM_URLENCODED).put(String.class, form); return new RESTSubject(regSubjectName); }