|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.common.util; |
| 20 | + |
| 21 | +import static java.util.Objects.requireNonNull; |
| 22 | +import java.net.URI; |
| 23 | +import java.net.URISyntaxException; |
| 24 | +import java.util.function.Predicate; |
| 25 | +import javax.annotation.Nonnull; |
| 26 | +import javax.annotation.Nullable; |
| 27 | +import javax.annotation.concurrent.ThreadSafe; |
| 28 | + |
| 29 | +/** |
| 30 | + * Static convenience URI checker. |
| 31 | + */ |
| 32 | +@ThreadSafe |
| 33 | +public class URIPreconditions { |
| 34 | + |
| 35 | + /** |
| 36 | + * Check whether the given string is a legal URI and passes the user's check. |
| 37 | + * |
| 38 | + * @param uri URI String |
| 39 | + * @param predicate User defined rule |
| 40 | + * @throws IllegalArgumentException Illegal URI or failed in the user's rules |
| 41 | + */ |
| 42 | + public static void checkURI(@Nonnull String uri, |
| 43 | + @Nonnull Predicate<URI> predicate) throws IllegalArgumentException { |
| 44 | + checkURI(uri, predicate, null); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Check whether the given string is a legal URI and passes the user's check. |
| 49 | + * |
| 50 | + * @param uri URI String |
| 51 | + * @param predicate User defined rule |
| 52 | + * @throws IllegalArgumentException Illegal URI or failed in the user's rules |
| 53 | + */ |
| 54 | + public static void checkURIIfPresent(@Nullable String uri, |
| 55 | + @Nonnull Predicate<URI> predicate) throws IllegalArgumentException { |
| 56 | + checkURIIfPresent(uri, predicate, null); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Check whether the given string is a legal URI and passes the user's check. |
| 61 | + * |
| 62 | + * @param uri URI String |
| 63 | + * @param predicate User defined rule |
| 64 | + * @param errorMessage Error message |
| 65 | + * @throws IllegalArgumentException Illegal URI or failed in the user's rules |
| 66 | + */ |
| 67 | + public static void checkURIIfPresent(@Nullable String uri, |
| 68 | + @Nonnull Predicate<URI> predicate, |
| 69 | + @Nullable String errorMessage) throws IllegalArgumentException { |
| 70 | + if (uri == null) { |
| 71 | + return; |
| 72 | + } |
| 73 | + checkURI(uri, predicate, errorMessage); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Check whether the given string is a legal URI and passes the user's check. |
| 78 | + * |
| 79 | + * @param uri URI String |
| 80 | + * @param predicate User defined rule |
| 81 | + * @param errorMessage Error message |
| 82 | + * @throws IllegalArgumentException Illegal URI or failed in the user's rules |
| 83 | + */ |
| 84 | + public static void checkURI(@Nonnull String uri, |
| 85 | + @Nonnull Predicate<URI> predicate, |
| 86 | + @Nullable String errorMessage) throws IllegalArgumentException { |
| 87 | + requireNonNull(uri, "uri"); |
| 88 | + requireNonNull(predicate, "predicate"); |
| 89 | + try { |
| 90 | + URI u = new URI(uri); |
| 91 | + if (!predicate.test(u)) { |
| 92 | + throw new IllegalArgumentException(errorMessage == null ? "Illegal syntax: " + uri : errorMessage); |
| 93 | + } |
| 94 | + } catch (URISyntaxException e) { |
| 95 | + throw new IllegalArgumentException(errorMessage == null ? "Illegal syntax: " + uri : errorMessage); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments