Repository: kafka
Updated Branches:
  refs/heads/trunk a598c4d26 -> 941e2177c


KAFKA-5098; ProducerRecord should validate the topic name

Author: huxihx <[email protected]>

Reviewers: Ismael Juma <[email protected]>

Closes #3223 from huxihx/KAFKA-5098_Does_not_check_topic_name_before_sending


Project: http://git-wip-us.apache.org/repos/asf/kafka/repo
Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/941e2177
Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/941e2177
Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/941e2177

Branch: refs/heads/trunk
Commit: 941e2177c0cf57eff818192258300d256971a11e
Parents: a598c4d
Author: huxihx <[email protected]>
Authored: Sat Jun 3 10:35:44 2017 +0100
Committer: Ismael Juma <[email protected]>
Committed: Sat Jun 3 10:38:03 2017 +0100

----------------------------------------------------------------------
 .../org/apache/kafka/clients/producer/ProducerRecord.java   | 9 +++++++++
 1 file changed, 9 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kafka/blob/941e2177/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java
----------------------------------------------------------------------
diff --git 
a/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java 
b/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java
index 85428e5..5cac3ac 100644
--- 
a/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java
+++ 
b/clients/src/main/java/org/apache/kafka/clients/producer/ProducerRecord.java
@@ -16,9 +16,11 @@
  */
 package org.apache.kafka.clients.producer;
 
+import org.apache.kafka.common.errors.InvalidTopicException;
 import org.apache.kafka.common.header.Header;
 import org.apache.kafka.common.header.Headers;
 import org.apache.kafka.common.header.internals.RecordHeaders;
+import org.apache.kafka.common.internals.Topic;
 
 /**
  * A key/value pair to be sent to Kafka. This consists of a topic name to 
which the record is being sent, an optional
@@ -62,10 +64,17 @@ public class ProducerRecord<K, V> {
      * @param key The key that will be included in the record
      * @param value The record contents
      * @param headers the headers that will be included in the record
+     * @throws IllegalArgumentException if the topic name is null or invalid
+     * @throws IllegalArgumentException if the partition or timestamp is 
negative
      */
     public ProducerRecord(String topic, Integer partition, Long timestamp, K 
key, V value, Iterable<Header> headers) {
         if (topic == null)
             throw new IllegalArgumentException("Topic cannot be null.");
+        try {
+            Topic.validate(topic);
+        } catch (InvalidTopicException e) {
+            throw new IllegalArgumentException(e.getMessage());
+        }
         if (timestamp != null && timestamp < 0)
             throw new IllegalArgumentException(
                     String.format("Invalid timestamp: %d. Timestamp should 
always be non-negative or null.", timestamp));

Reply via email to