/** * A {@link Subject} has a name. The name must not be null or empty, and * cannot contain whitespace. If the name contains whitespace an * {@link IllegalArgumentException} is thrown. **/ protected Subject(String name) { RepositoryUtil.validateSchemaOrSubject(name); this.name = name; }
/** * @return The {@link org.schemarepo.SubjectConfig} for this Subject */ @Override public SubjectConfig getConfig() { try { // TODO: Allow this behavior to be disabled once we have async updating // of the cache via ZK Observers... This would protect ZK from getting // hammered too much at the expense of slightly stale data. Properties props = new Properties(); byte[] rawProperties = zkClient.getData().forPath(getConfigFilePath()); props.load(new ByteArrayInputStream(rawProperties)); return RepositoryUtil.configFromProperties(props); } catch (Exception e) { throw new RuntimeException("An exception occurred while accessing ZK!", e); } }
protected MemSubject(String name, SubjectConfig config) { super(name); this.config = RepositoryUtil.safeConfig(config); }
@Test public void testSchemasToFromString() { SchemaEntry e1 = new SchemaEntry("id1", "s1"); SchemaEntry e2 = new SchemaEntry("id2", "s2"); ArrayList<SchemaEntry> empty = new ArrayList<SchemaEntry>(); ArrayList<SchemaEntry> vals = new ArrayList<SchemaEntry>(); vals.add(e1); vals.add(e2); Iterable<SchemaEntry> emptyResult = RepositoryUtil .schemasFromString(RepositoryUtil.schemasToString(empty)); Iterable<SchemaEntry> emptyResult2 = RepositoryUtil.schemasFromString(null); Iterable<SchemaEntry> emptyResult3 = RepositoryUtil.schemasFromString(""); Assert.assertEquals(empty, emptyResult); Assert.assertEquals(emptyResult, emptyResult2); Assert.assertEquals(emptyResult, emptyResult3); Iterable<SchemaEntry> result = RepositoryUtil .schemasFromString(RepositoryUtil.schemasToString(vals)); Assert.assertEquals(vals, result); }
@Test public void testSubjectsToFromString() { Repository r = new InMemoryRepository(new ValidatorFactory.Builder().build()); Subject s1 = r.register("s1", null); Subject s2 = r.register("s2", null); ArrayList<Subject> empty = new ArrayList<Subject>(); ArrayList<Subject> vals = new ArrayList<Subject>(); vals.add(s1); vals.add(s2); Iterable<String> emptyResult = RepositoryUtil .subjectNamesFromString(RepositoryUtil.subjectsToString(empty)); Iterable<String> emptyResult2 = RepositoryUtil.subjectNamesFromString(null); Iterable<String> emptyResult3 = RepositoryUtil.subjectNamesFromString(""); validate(emptyResult, empty); Assert.assertEquals(emptyResult, emptyResult2); Assert.assertEquals(emptyResult, emptyResult3); Iterable<String> result = RepositoryUtil .subjectNamesFromString(RepositoryUtil.subjectsToString(vals)); validate(result, vals); }
public Builder set(String key, String value) { if(key.startsWith(RESERVED_PREFIX)) { if(VALIDATORS_KEY.equals(key)) { setValidators(RepositoryUtil.commaSplit(value)); } else { throw new RuntimeException("SubjectConfig keys starting with '" + RESERVED_PREFIX + "' are reserved, failed to set: " + key + " to value: " + value); } } else { conf.put(key, value); } return this; }
@Override public Iterable<Subject> subjects() { ArrayList<Subject> subjectList = new ArrayList<Subject>(); try { String subjects = webResource.get(String.class); for (String subjName : RepositoryUtil.subjectNamesFromString(subjects)) { subjectList.add(new RESTSubject(subjName)); } } catch (Exception e) { //no op. return empty list anyways } return subjectList; }
@Override public String renderSchemas(Iterable<SchemaEntry> schemaEntries) { return RepositoryUtil.schemasToString(schemaEntries); }
private Iterable<SchemaEntry> schemaEntriesFromStr(String entriesStr) { return RepositoryUtil.schemasFromString(entriesStr); }
@Override public String renderSubjects(Iterable<Subject> subjects) { return RepositoryUtil.subjectsToString(subjects); }
public Builder set(String key, String value) { if(key.startsWith(RESERVED_PREFIX)) { if(VALIDATORS_KEY.equals(key)) { setValidators(RepositoryUtil.commaSplit(value)); } else { throw new RuntimeException("SubjectConfig keys starting with '" + RESERVED_PREFIX + "' are reserved, failed to set: " + key + " to value: " + value); } } else { conf.put(key, value); } return this; }
@Override public Iterable<Subject> subjects() { ArrayList<Subject> subjectList = new ArrayList<Subject>(); try { String subjects = webResource.get(String.class); for (String subjName : RepositoryUtil.subjectNamesFromString(subjects)) { subjectList.add(new RESTSubject(subjName)); } } catch (Exception e) { //no op. return empty list anyways } return subjectList; }
@Override public String renderSchemas(Iterable<SchemaEntry> schemaEntries) { return RepositoryUtil.schemasToString(schemaEntries); }
private Iterable<SchemaEntry> schemaEntriesFromStr(String entriesStr) { return RepositoryUtil.schemasFromString(entriesStr); }
@Override public String renderSubjects(Iterable<Subject> subjects) { return RepositoryUtil.subjectsToString(subjects); }
/** * A {@link Subject} has a name. The name must not be null or empty, and * cannot contain whitespace. If the name contains whitespace an * {@link IllegalArgumentException} is thrown. **/ protected Subject(String name) { RepositoryUtil.validateSchemaOrSubject(name); this.name = name; }
protected MemSubject(String name, SubjectConfig config) { super(name); this.config = RepositoryUtil.safeConfig(config); }
@Override public SubjectConfig getConfig() { String path = getName() + "/config" ; try { String propString = webResource.path(path).get(String.class); Properties props = new Properties(); props.load(new StringReader(propString)); return RepositoryUtil.configFromProperties(props); } catch (Exception e) { return null; } }
@Provides @Singleton ValidatorFactory provideValidatorFactory(Injector injector, @Named(Config.DEFAULT_SUBJECT_VALIDATORS) String defaultSubjectValidators) { ValidatorFactory.Builder builder = new ValidatorFactory.Builder(); for(String prop : props.stringPropertyNames()) { if (prop.startsWith(Config.VALIDATOR_PREFIX)) { String validatorName = prop.substring(Config.VALIDATOR_PREFIX.length()); Class<Validator> validatorClass = injector.getInstance( Key.<Class<Validator>>get( new TypeLiteral<Class<Validator>>(){}, Names.named(prop))); builder.setValidator(validatorName, injector.getInstance(validatorClass)); } } // assign the default subject validators builder.setDefaultValidators(RepositoryUtil.commaSplit(defaultSubjectValidators)); return builder.build(); }
@Override public SchemaEntry register(String schema) throws SchemaValidationException { RepositoryUtil.validateSchemaOrSubject(schema); String path = getName() + "/register"; return handleRegisterRequest(path, schema); }