diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/utils/auth/tokens/TokensCliUtils.java b/pulsar-broker/src/main/java/org/apache/pulsar/utils/auth/tokens/TokensCliUtils.java index c089fa09953d4..47364bc68bdb4 100644 --- a/pulsar-broker/src/main/java/org/apache/pulsar/utils/auth/tokens/TokensCliUtils.java +++ b/pulsar-broker/src/main/java/org/apache/pulsar/utils/auth/tokens/TokensCliUtils.java @@ -22,6 +22,7 @@ import com.beust.jcommander.IUsageFormatter; import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; +import com.beust.jcommander.ParameterException; import com.beust.jcommander.Parameters; import com.google.common.base.Charsets; import io.jsonwebtoken.Claims; @@ -155,8 +156,13 @@ public void run() throws Exception { Optional optExpiryTime = Optional.empty(); if (expiryTime != null) { - long relativeTimeMillis = TimeUnit.SECONDS - .toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(expiryTime)); + long relativeTimeMillis; + try { + relativeTimeMillis = TimeUnit.SECONDS.toMillis( + RelativeTimeUtil.parseRelativeTimeInSeconds(expiryTime)); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } optExpiryTime = Optional.of(new Date(System.currentTimeMillis() + relativeTimeMillis)); } diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdNamespaces.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdNamespaces.java index 3d73ae9cc1792..e87e753cde48d 100644 --- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdNamespaces.java +++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdNamespaces.java @@ -720,7 +720,12 @@ private class SetRetention extends CliCommand { void run() throws PulsarAdminException { String namespace = validateNamespace(params); long sizeLimit = validateSizeString(limitStr); - long retentionTimeInSec = RelativeTimeUtil.parseRelativeTimeInSeconds(retentionTimeStr); + long retentionTimeInSec; + try { + retentionTimeInSec = RelativeTimeUtil.parseRelativeTimeInSeconds(retentionTimeStr); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } final int retentionTimeInMin; if (retentionTimeInSec != -1) { @@ -1449,7 +1454,13 @@ private class SetInactiveTopicPolicies extends CliCommand { @Override void run() throws PulsarAdminException { String namespace = validateNamespace(params); - long maxInactiveDurationInSeconds = TimeUnit.SECONDS.toSeconds(RelativeTimeUtil.parseRelativeTimeInSeconds(deleteInactiveTopicsMaxInactiveDuration)); + long maxInactiveDurationInSeconds; + try { + maxInactiveDurationInSeconds = TimeUnit.SECONDS.toSeconds( + RelativeTimeUtil.parseRelativeTimeInSeconds(deleteInactiveTopicsMaxInactiveDuration)); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } if (enableDeleteWhileInactive == disableDeleteWhileInactive) { throw new ParameterException("Need to specify either enable-delete-while-inactive or disable-delete-while-inactive"); @@ -1482,7 +1493,13 @@ private class SetDelayedDelivery extends CliCommand { @Override void run() throws PulsarAdminException { String namespace = validateNamespace(params); - long delayedDeliveryTimeInMills = TimeUnit.SECONDS.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(delayedDeliveryTimeStr)); + long delayedDeliveryTimeInMills; + try { + delayedDeliveryTimeInMills = TimeUnit.SECONDS.toMillis( + RelativeTimeUtil.parseRelativeTimeInSeconds(delayedDeliveryTimeStr)); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } if (enable == disable) { throw new ParameterException("Need to specify either --enable or --disable"); @@ -1864,7 +1881,13 @@ private class SetOffloadDeletionLag extends CliCommand { @Override void run() throws PulsarAdminException { String namespace = validateNamespace(params); - getAdmin().namespaces().setOffloadDeleteLag(namespace, RelativeTimeUtil.parseRelativeTimeInSeconds(lag), + long lagInSec; + try { + lagInSec = RelativeTimeUtil.parseRelativeTimeInSeconds(lag); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } + getAdmin().namespaces().setOffloadDeleteLag(namespace, lagInSec, TimeUnit.SECONDS); } } @@ -2196,7 +2219,13 @@ && maxValueCheck("ReadBufferSize", readBufferSize, Integer.MAX_VALUE)) { Long offloadAfterElapsedInMillis = OffloadPoliciesImpl.DEFAULT_OFFLOAD_DELETION_LAG_IN_MILLIS; if (StringUtils.isNotEmpty(offloadAfterElapsedStr)) { - Long offloadAfterElapsed = TimeUnit.SECONDS.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(offloadAfterElapsedStr)); + Long offloadAfterElapsed; + try { + offloadAfterElapsed = TimeUnit.SECONDS.toMillis( + RelativeTimeUtil.parseRelativeTimeInSeconds(offloadAfterElapsedStr)); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } if (positiveCheck("OffloadAfterElapsed", offloadAfterElapsed) && maxValueCheck("OffloadAfterElapsed", offloadAfterElapsed, Long.MAX_VALUE)) { offloadAfterElapsedInMillis = offloadAfterElapsed; diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdPersistentTopics.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdPersistentTopics.java index 42e9e4a3664ea..28f78f5b567c2 100644 --- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdPersistentTopics.java +++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdPersistentTopics.java @@ -20,6 +20,7 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank; import com.beust.jcommander.Parameter; +import com.beust.jcommander.ParameterException; import com.beust.jcommander.Parameters; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -527,8 +528,13 @@ void run() throws PulsarAdminException { MessageId messageId = validateMessageIdString(resetMessageIdStr); getPersistentTopics().resetCursor(persistentTopic, subName, messageId); } else if (isNotBlank(resetTimeStr)) { - long resetTimeInMillis = TimeUnit.SECONDS - .toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(resetTimeStr)); + long resetTimeInMillis; + try { + resetTimeInMillis = TimeUnit.SECONDS.toMillis( + RelativeTimeUtil.parseRelativeTimeInSeconds(resetTimeStr)); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } // now - go back time long timestamp = System.currentTimeMillis() - resetTimeInMillis; getPersistentTopics().resetCursor(persistentTopic, subName, timestamp); diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopics.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopics.java index ece4d3ea256f5..9ecda2da023b3 100644 --- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopics.java +++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopics.java @@ -804,8 +804,13 @@ void run() throws PulsarAdminException { getTopics().resetCursor(persistentTopic, subName, messageId); } } else if (isNotBlank(resetTimeStr)) { - long resetTimeInMillis = TimeUnit.SECONDS - .toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(resetTimeStr)); + long resetTimeInMillis; + try { + resetTimeInMillis = TimeUnit.SECONDS.toMillis( + RelativeTimeUtil.parseRelativeTimeInSeconds(resetTimeStr)); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } // now - go back time long timestamp = System.currentTimeMillis() - resetTimeInMillis; getTopics().resetCursor(persistentTopic, subName, timestamp); @@ -1336,7 +1341,13 @@ private class SetDelayedDelivery extends CliCommand { @Override void run() throws PulsarAdminException { String topicName = validateTopicName(params); - long delayedDeliveryTimeInMills = TimeUnit.SECONDS.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(delayedDeliveryTimeStr)); + long delayedDeliveryTimeInMills; + try { + delayedDeliveryTimeInMills = TimeUnit.SECONDS.toMillis( + RelativeTimeUtil.parseRelativeTimeInSeconds(delayedDeliveryTimeStr)); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } if (enable == disable) { throw new ParameterException("Need to specify either --enable or --disable"); @@ -1486,7 +1497,12 @@ private class SetRetention extends CliCommand { void run() throws PulsarAdminException { String persistentTopic = validatePersistentTopic(params); long sizeLimit = validateSizeString(limitStr); - long retentionTimeInSec = RelativeTimeUtil.parseRelativeTimeInSeconds(retentionTimeStr); + long retentionTimeInSec; + try { + retentionTimeInSec = RelativeTimeUtil.parseRelativeTimeInSeconds(retentionTimeStr); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } final int retentionTimeInMin; if (retentionTimeInSec != -1) { @@ -2352,7 +2368,13 @@ private class SetInactiveTopicPolicies extends CliCommand { @Override void run() throws PulsarAdminException { String persistentTopic = validatePersistentTopic(params); - long maxInactiveDurationInSeconds = TimeUnit.SECONDS.toSeconds(RelativeTimeUtil.parseRelativeTimeInSeconds(deleteInactiveTopicsMaxInactiveDuration)); + long maxInactiveDurationInSeconds; + try { + maxInactiveDurationInSeconds = TimeUnit.SECONDS.toSeconds( + RelativeTimeUtil.parseRelativeTimeInSeconds(deleteInactiveTopicsMaxInactiveDuration)); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } if (enableDeleteWhileInactive == disableDeleteWhileInactive) { throw new ParameterException("Need to specify either enable-delete-while-inactive or disable-delete-while-inactive"); diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTransactions.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTransactions.java index 7faf006066ca6..e6953817b0d26 100644 --- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTransactions.java +++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTransactions.java @@ -19,6 +19,7 @@ package org.apache.pulsar.admin.cli; import com.beust.jcommander.Parameter; +import com.beust.jcommander.ParameterException; import com.beust.jcommander.Parameters; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; @@ -133,8 +134,12 @@ private class GetSlowTransactions extends CliCommand { @Override void run() throws Exception { - long timeout = - TimeUnit.SECONDS.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(timeoutStr)); + long timeout; + try { + timeout = TimeUnit.SECONDS.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(timeoutStr)); + } catch (IllegalArgumentException exception) { + throw new ParameterException(exception.getMessage()); + } if (coordinatorId != null) { print(getAdmin().transactions().getSlowTransactionsByCoordinatorId(coordinatorId, timeout, TimeUnit.MILLISECONDS));