cc = Consumer.createJavaConsumerConnector(consumerConfig); final TopicFilter filter = new Whitelist(configuration.getString(CK_TOPIC_FILTER));
@Override public void init(SecorConfig config) throws UnknownHostException { this.mConfig = config; mConsumerConnector = Consumer.createJavaConsumerConnector(createConsumerConfig()); if (!mConfig.getKafkaTopicBlacklist().isEmpty() && !mConfig.getKafkaTopicFilter().isEmpty()) { throw new RuntimeException("Topic filter and blacklist cannot be both specified."); } TopicFilter topicFilter = !mConfig.getKafkaTopicBlacklist().isEmpty() ? new Blacklist(mConfig.getKafkaTopicBlacklist()) : new Whitelist(mConfig.getKafkaTopicFilter()); LOG.debug("Use TopicFilter {}({})", topicFilter.getClass(), topicFilter); List<KafkaStream<byte[], byte[]>> streams = mConsumerConnector.createMessageStreamsByFilter(topicFilter); KafkaStream<byte[], byte[]> stream = streams.get(0); mIterator = stream.iterator(); mKafkaMessageTimestampFactory = new KafkaMessageTimestampFactory(mConfig.getKafkaMessageTimestampClass()); }
private void init() { // register kafka offset lag metrics, one Gauge is for per consumer level granularity MetricRegistry registry = Metrics.getRegistry(); try { fetchedMsgCounter = registry.meter("kafkaIngesterConsumer." + this.getName() + "-msgFetchRate"); failedToIngestCounter = registry.meter("kafkaIngesterConsumer." + this.getName() + "-failedToIngest"); kafkaOffsetLagGauge = registry.register("kafkaIngesterConsumer." + this.getName() + "-kafkaOffsetLag", new JmxAttributeGauge( new ObjectName(maxLagMetricName), "Value")); } catch (MalformedObjectNameException | IllegalArgumentException e) { logger.error("Register failure for metrics of KafkaIngesterConsumer", e); } TopicFilter topicFilter = new Whitelist(AuditConfig.AUDIT_TOPIC_NAME); logger.info("{}: Topic filter is {}", getName(), AuditConfig.AUDIT_TOPIC_NAME); this.consumer = Consumer.createJavaConsumerConnector(createConsumerConfig()); KafkaStream<byte[], byte[]> stream = consumer.createMessageStreamsByFilter(topicFilter, 1).get(0); iterator = stream.iterator(); logger.info("KafkaIngesterConsumer thread {} is initialized successfully", getName()); if (AuditConfig.INGESTER_ENABLE_DEDUP) { deduplicator = new Deduplicator(threadId, AuditConfig.INGESTER_REDIS_HOST, AuditConfig.INGESTER_REDIS_PORT, AuditConfig.INGESTER_REDIS_KEY_TTL_SEC, AuditConfig.INGESTER_DUP_HOST_PREFIX, AuditConfig.INGESTER_HOSTS_WITH_DUP); deduplicator.open(); } else { deduplicator = null; } }
Whitelist whitelist = new Whitelist(); Whitelist.simpleText().addTags("table","td", "tr","tbody");
package stackoverflow; import org.apache.commons.lang3.StringEscapeUtils; import org.jsoup.Jsoup; import org.jsoup.safety.Whitelist; public class EmbeddedHTML { public static void main(String[] args) { String src = "<description> <div><p> An independent" + " inquiry into the September 11 attack on the US Consulate" + " in Benghazi that killed the US ambassador to Libya and" + " three other Americans has found that systematic failures" + " at the State Department led to “grossly†inadequate" + " security at the mission. </p></description>"; String unescaped = StringEscapeUtils.unescapeHtml4(src); System.out.println(Jsoup.clean(unescaped, new Whitelist())); } }
String text = "<b>Remove <i>bold</i> and italics</b>"; System.out.println(text); String doc = Jsoup.clean(text, new Whitelist()); System.out.println(doc);
@SpringBootApplication public class Application{ public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); XMLConverter converter = context.getBean(XMLConverter.class) Whitelist whitelist = new Whitelist("example"); converter.convertFromObjectToXML(whitelist, XML_FILE_NAME); } @Bean public XMLConverter xmlConverter(){ XMLConverter converter = new XMLConverter(); CastorMarshaller castorMarshaller = new CastorMarshaller() converter.setMarshaller(castorMarshaller); converter.setUnmarshaller(castorMarshaller); return converter; } }
protected String encodeHtml(String html) { return Jsoup.clean(html, getWhitelist()); } private Whitelist getWhitelist() { return new Whitelist() .addTags("a", "b", "blockquote", "br", "caption", "cite", "code", "col", "colgroup", "dd", "div", "dl", "dt", "em", "h1", "h2", "h3", "h4", "h5", "h6", "i", "img", "li", "ol", "p", "pre", "q", "small", "strike", "strong", "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "u", "ul", "iframe") .addAttributes("a", "href", "title").addAttributes("blockquote", "cite") .addAttributes("col", "span", "width").addAttributes("colgroup", "span", "width") .addAttributes("img", "align", "alt", "height", "src", "title", "width") .addAttributes("ol", "start", "type").addAttributes("q", "cite") .addAttributes("table", "summary", "width") .addAttributes("td", "abbr", "axis", "colspan", "rowspan", "width") .addAttributes("th", "abbr", "axis", "colspan", "rowspan", "scope", "width") .addAttributes("ul", "type") .addProtocols("a", "href", "ftp", "http", "https", "mailto") .addProtocols("blockquote", "cite", "http", "https").addProtocols("img", "src", "http", "https") .addProtocols("q", "cite", "http", "https"); }
public class MainActivity extends Activity { WithFileProgress api; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView content = (TextView)findViewById(R.id.content); api = new WithFileProgress(this, content); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { EditText input = (EditText)findViewById(R.id.input); String url = input.getText().toString(); if (Jsoup.isValid(url, new Whitelist())) { api.connect(url); } else { AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); builder.setTitle("MainActivity").setMessage("Invalid address: "+url).create(); } } }); }
int NUMBER_OF_PARTITIONS = 6; Properties consumerConfig = new Properties(); consumerConfig.put("zk.connect", "zookeeper.mydomain.com:2181" ); consumerConfig.put("backoff.increment.ms", "100"); consumerConfig.put("autooffset.reset", "largest"); consumerConfig.put("groupid", "java-consumer-example"); consumer = Consumer.createJavaConsumerConnector(new ConsumerConfig(consumerConfig)); TopicFilter sourceTopicFilter = new Whitelist("mytopic|myothertopic"); List<KafkaStream<Message>> streams = consumer.createMessageStreamsByFilter(sourceTopicFilter, NUMBER_OF_PARTITIONS); ExecutorService executor = Executors.newFixedThreadPool(streams.size()); for(final KafkaStream<Message> stream: streams){ executor.submit(new Runnable() { public void run() { for (MessageAndMetadata<Message> msgAndMetadata: stream) { ByteBuffer buffer = msgAndMetadata.message().payload(); byte [] bytes = new byte[buffer.remaining()]; buffer.get(bytes); //Do something with the bytes you just got off Kafka. } } }); }
private List<KafkaStream<byte[], byte[]>> _createStreams(ConsumerConnector consumer, String topicName) { int numStreams = Math.max(Integer.parseInt( _configuration.getValue(Property.KAFKA_CONSUMER_STREAMS_PER_TOPIC.getName(), Property.KAFKA_CONSUMER_STREAMS_PER_TOPIC.getDefaultValue())), 2); return consumer.createMessageStreamsByFilter(new Whitelist(topicName), numStreams); }
private List<KafkaStream<byte[], byte[]>> _createStreams(ConsumerConnector consumer, String topicName) { int numStreams = Math.max(Integer.parseInt( _configuration.getValue(Property.KAFKA_CONSUMER_STREAMS_PER_TOPIC.getName(), Property.KAFKA_CONSUMER_STREAMS_PER_TOPIC.getDefaultValue())), 2); return consumer.createMessageStreamsByFilter(new Whitelist(topicName), numStreams); }
public String escapeHtml(String source) { Document doc = Jsoup.parseBodyFragment(source); Elements elements = doc.select("b"); for (Element element : elements) { element.replaceWith(new TextNode(element.toString(),"")); } return Jsoup.clean(doc.body().toString(), new Whitelist().addTags("a").addAttributes("a", "href", "name", "rel", "target")); }
Whitelist whitelist = new Whitelist(); Set<String> allowedTags = new HashSet<String>(); Map<String, Set<String>> allowedAttributes = new HashMap<String, Set<String>>();
@Override public void startStream() { Properties props = new Properties(); props.setProperty("serializer.encoding", "UTF8"); ConsumerConfig consumerConfig = new ConsumerConfig(props); consumerConnector = Consumer.createJavaConsumerConnector(consumerConfig); Whitelist topics = new Whitelist(config.getTopic()); VerifiableProperties vprops = new VerifiableProperties(props); inStreams = consumerConnector.createMessageStreamsByFilter(topics, 1, new StringDecoder(vprops), new StringDecoder(vprops)); for (final KafkaStream stream : inStreams) { executor.submit(new KafkaPersistReaderTask(this, stream)); } }
cc = Consumer.createJavaConsumerConnector(consumerConfig); final TopicFilter filter = new Whitelist(configuration.getString(CK_TOPIC_FILTER));
cc = Consumer.createJavaConsumerConnector(consumerConfig); final TopicFilter filter = new Whitelist(configuration.getString(CK_TOPIC_FILTER));
new Whitelist(Pattern.quote(topic)), 1, DEFAULT_STRING_DECODER, DEFAULT_STRING_DECODER );
@Override public void run() { ConsumerConfig cfg = new ConsumerConfig(props); consumer = Consumer.createJavaConsumerConnector(cfg); TopicFilter arg0 = new Whitelist(topic); List<KafkaStream<byte[], byte[]>> partitions = consumer.createMessageStreamsByFilter(arg0); while (!Thread.interrupted()) { for (KafkaStream<byte[], byte[]> partition : partitions) { ConsumerIterator<byte[], byte[]> it = partition .iterator(); while (it.hasNext()) { MessageAndMetadata<byte[], byte[]> msg = it.next(); onMessage(msg.topic(), new String(msg.message())); } } } } }, "consumer-" + topic);