package com.alibaba.rocketmq.client;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.common.MixAll;
import com.alibaba.rocketmq.common.UtilAll;
import com.alibaba.rocketmq.common.message.Message;
/**
* 有效性检查公用类。
*
* @author manhong.yqd<jodie.yqd@gmail.com>
* @since 2013-8-28
*/
public class Validators {
public static final String validPatternStr = "^[a-zA-Z0-9_-]+$";
public static final int CHARACTER_MAX_LENGTH = 255;
/**
* 通过正则表达式进行字符匹配
*
* @param origin
* @param patternStr
* @return
*/
public static boolean regularExpressionMatcher(String origin, String patternStr) {
if (UtilAll.isBlank(origin)) {
return false;
}
if (UtilAll.isBlank(patternStr)) {
return true;
}
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(origin);
return matcher.matches();
}
/**
* 通过正则表达式查找匹配的字符
*
* @param origin
* @param patternStr
* @return
*/
public static String getGroupWithRegularExpression(String origin, String patternStr) {
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(origin);
while (matcher.find()) {
return matcher.group(0);
}
return null;
}
/**
* topic 有效性检查
*
* @param topic
* @throws com.alibaba.rocketmq.client.exception.MQClientException
*/
public static void checkTopic(String topic) throws MQClientException {
if (UtilAll.isBlank(topic)) {
throw new MQClientException("the specified topic is blank", null);
}
if (!regularExpressionMatcher(topic, validPatternStr)) {
throw new MQClientException(String.format(
"the specified topic[%s] contains illegal characters, allowing only %s", topic,
validPatternStr), null);
}
if (topic.length() > CHARACTER_MAX_LENGTH) {
throw new MQClientException("the specified topic is longer than topic max length 255.", null);
}
// Topic名字是否与保留字段冲突
if (topic.equals(MixAll.DEFAULT_TOPIC)) {
throw new MQClientException(
String.format("the topic[%s] is conflict with default topic.", topic), null);
}
}
/**
* group 有效性检查
*
* @param group
* @throws com.alibaba.rocketmq.client.exception.MQClientException
*/
public static void checkGroup(String group) throws MQClientException {
if (UtilAll.isBlank(group)) {
throw new MQClientException("the specified group is blank", null);
}
if (!regularExpressionMatcher(group, validPatternStr)) {
throw new MQClientException(String.format(
"the specified group[%s] contains illegal characters, allowing only %s", group,
validPatternStr), null);
}
if (group.length() > CHARACTER_MAX_LENGTH) {
throw new MQClientException("the specified group is longer than group max length 255.", null);
}
}
/**
* message 有效性检查
*
* @param msg
* @param defaultMQProducer
* @throws com.alibaba.rocketmq.client.exception.MQClientException
*/
public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer)
throws MQClientException {
if (null == msg) {
throw new MQClientException("the message is null", null);
}
// topic
Validators.checkTopic(msg.getTopic());
// body
if (null == msg.getBody()) {
throw new MQClientException("the message body is null", null);
}
if (0 == msg.getBody().length) {
throw new MQClientException("the message body length is zero", null);
}
if (msg.getBody().length > defaultMQProducer.getMaxMessageSize()) {
throw new MQClientException("the message body size over max value, MAX: "
+ defaultMQProducer.getMaxMessageSize(), null);
}
}
}