Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import java.net.HttpURLConnection
  2. import java.net.MalformedURLException
  3. import java.net.URL
  4. import java.net.UnknownHostException
  5. import kotlin.system.exitProcess
  6.  
  7. fun main(args: Array<String>) {
  8. try {
  9. require(args.size in 1..2)
  10.  
  11. val url = URL(args[0])
  12. val expectedStatus = try {
  13. args[1].toInt()
  14. } catch (e: ArrayIndexOutOfBoundsException) {
  15. null
  16. }
  17.  
  18. with(url.openConnection() as HttpURLConnection) {
  19. connectTimeout = 5000
  20. readTimeout = 5000
  21. requestMethod = "HEAD"
  22.  
  23. val actualStatus = responseCode
  24. expectedStatus?.let {
  25. assert(expectedStatus == actualStatus) {
  26. "expected HTTP status $expectedStatus, got $actualStatus"
  27. }
  28. }
  29. }
  30. } catch (e: Throwable) {
  31. when (e) {
  32. is IllegalArgumentException,
  33. is MalformedURLException -> usage()
  34. is UnknownHostException -> die("IP address of a host could not be determined")
  35. else -> die("${e.message}")
  36. }
  37. }
  38. println("OK")
  39. }
  40.  
  41. fun die(message: String) {
  42. System.err.println(message)
  43. exitProcess(1)
  44. }
  45.  
  46. fun usage() {
  47. die("Usage: java -jar healthchek.jar URL [STATUS]")
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement