1

How to verify if a String is valid URL (including deeplink) in Java. The method should return true for the following test cases

http://www.example.com/gizmos
https://www.example.com/gizmos
example://gizmos
aqel
  • 415
  • 1
  • 5
  • 11
  • Are you aware of class [`java.net.URL`](https://docs.oracle.com/javase/8/docs/api/java/net/URL.html) ? – Abra Jun 16 '20 at 13:35
  • @Ivar My requirement is not met by this answer https://stackoverflow.com/a/17894617/3845889. I have written a test case here https://gist.github.com/aqelkp/37ae9f941e5552d3a74460227ca58fe6. The third statement for example://gizmos fails here – aqel Jun 16 '20 at 14:28
  • @aqel The top voted/accepted answer should work for your case. Just change the constructor to `UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_ALL_SCHEMES);`. – Ivar Jun 16 '20 at 14:40
  • @aqel (To react to your comment on Dawid's answer) It fails because `gizmos` is not seen as a valid domain. Are you sure that that it should be? If it is a local URL, then you could change it to `UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_ALL_SCHEMES + UrlValidator.ALLOW_LOCAL_URLS);` – Ivar Jun 16 '20 at 15:11
  • @ivar ALLOW_LOCAL_URLS is for localhost, right? But I am talking about deep links here. It can be any word without space. Please have a look at this link https://developer.android.com/training/app-links/deep-linking – aqel Jun 16 '20 at 15:56
  • @aqel `ALLOW_LOCAL_URLS` is not _just_ for localhost, but any "registered name". That option specifically allows for any alpha-numeric name that is less than 63 characters and that can have a dash (`-`) (as long as it isn't the first or last character.) – Ivar Jun 16 '20 at 16:35
  • @Ivar as perviously mentioned, I won't be able to give a predefined set of domains – aqel Jun 17 '20 at 01:35
  • Well, if you don't have a 'template' there is not much you can do to actually check if it is right because even you don't know how the URL should look like. Maybe try to connect to any given URL and if the connection fails display an error/wrong URL message. – Dawid Wawrzynczyk Jun 17 '20 at 02:27
  • @aqel I didn't say anything about a predefined set of domains. Your example URL appears to use such registered names, so it seems to suit your needs. If you have cases where this still doesn't doesn't match, you could question if that is actually a valid URL. But in that case you can also pass a `authorityValidator` as an argument to the constructor. That gives you complete freedom on what you want to allow for that part. – Ivar Jun 17 '20 at 11:40

1 Answers1

-1

Try using UrlValidator class from the Apache Commons Validator project.

String[] URLPrefixes = {"http","https", "example"};
UrlValidator urlValidator = new UrlValidator(URLPrefixes);
if (urlValidator.isValid("http://www.example.com/gizmos")) {
   System.out.println("URL is valid");
} else {
   System.out.println("URL is invalid");
}
  • 2
    This is basically a copy of [this answer](https://stackoverflow.com/a/5078838/479156). If a question has already been answered at Stack Overflow, flag it as a duplicate. Don't answer it. (Especially without attribution.) – Ivar Jun 16 '20 at 13:45
  • I've answered the question based on the research and modified it to the asked needs (Look at his question). Don't complicate it more than it needs to be. – Dawid Wawrzynczyk Jun 16 '20 at 13:47
  • @DawidWawrzynczyk TBH I also can't recognize why/how is your answer substantially different to [this answer](https://stackoverflow.com/questions/1600291/validating-url-in-java/5078838#5078838) -> downvote. – lexicore Jun 16 '20 at 13:53
  • @DawidWawrzynczyk With closing questions as duplicate, [we keep the high quality content in one place](https://stackoverflow.com/help/duplicates). Answering those actually shatters the content and makes it harder to find the best solution to a problem. (Keep in mind that the goal of Stack Overflow is to be a library of high-quality Q&A's. We're **not** here to help OP, but to help those _beyond_ OP who are facing the same issue.) – Ivar Jun 16 '20 at 13:57
  • "_as long as I help the user_" Flagging the question as a duplicate will lead OP to that question which will help them just as much. – Ivar Jun 16 '20 at 13:58
  • I do understand that, but we still don't know if he knows the answer because no one ever gave him a chance to respond. The question is not 1 to 1 exact as in the answer you provided. He also wants "example:..." to work. – Dawid Wawrzynczyk Jun 16 '20 at 13:59
  • @DawidWawrzynczyk How do you know OP is a "he"? And if the duplicate question doesn't answer their question, they have the option to edit their question and explain why it doesn't. That will automatically push the question onto a reopen review queue where other community members are able to reopen their question. – Ivar Jun 16 '20 at 14:04
  • @Ivar Okay, as you say. – Dawid Wawrzynczyk Jun 16 '20 at 14:05
  • @DawidWawrzynczyk I don't have predefined set of schemas as in the solution you shared – aqel Jun 16 '20 at 14:24
  • @aqel Then use the `ALLOW_ALL_SCHEMES` option. `UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_ALL_SCHEMES)` – Ivar Jun 16 '20 at 14:27
  • @Ivar Still doesn't work for `example://gizmos` – aqel Jun 16 '20 at 14:56