You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepositoryURL.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package org.apache.archiva.model;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. /**
  21. * RepositoryURL - Mutable (and protocol forgiving) URL object.
  22. *
  23. *
  24. */
  25. public class RepositoryURL
  26. {
  27. private String url;
  28. private String protocol;
  29. private String host;
  30. private String port;
  31. private String username;
  32. private String password;
  33. private String path;
  34. public RepositoryURL( String url )
  35. {
  36. this.url = url;
  37. // .\ Parse the URL \.____________________________________________
  38. int pos;
  39. pos = url.indexOf( ":/" );
  40. if ( pos == ( -1 ) )
  41. {
  42. throw new IllegalArgumentException( "Invalid URL, unable to parse protocol:// from " + url );
  43. }
  44. protocol = url.substring( 0, pos );
  45. // Determine the post protocol position.
  46. int postProtocolPos = protocol.length() + 1;
  47. while ( url.charAt( postProtocolPos ) == '/' )
  48. {
  49. postProtocolPos++;
  50. }
  51. // Handle special case with file protocol (which has no host, port, username, or password)
  52. if ( "file".equals( protocol ) )
  53. {
  54. path = "/" + url.substring( postProtocolPos );
  55. return;
  56. }
  57. // attempt to find the start of the 'path'
  58. pos = url.indexOf( "/", postProtocolPos );
  59. // no path specified - ex "http://localhost"
  60. if ( pos == ( -1 ) )
  61. {
  62. // set pos to end of string. (needed for 'middle section')
  63. pos = url.length();
  64. // default path
  65. path = "/";
  66. }
  67. else
  68. {
  69. // get actual path.
  70. path = url.substring( pos );
  71. }
  72. // get the middle section ( username : password @ hostname : port )
  73. String middle = url.substring( postProtocolPos, pos );
  74. pos = middle.indexOf( '@' );
  75. // we have an authentication section.
  76. if ( pos > 0 )
  77. {
  78. String authentication = middle.substring( 0, pos );
  79. middle = middle.substring( pos + 1 ); // lop off authentication for host:port search
  80. pos = authentication.indexOf( ':' );
  81. // we have a password.
  82. if ( pos > 0 )
  83. {
  84. username = authentication.substring( 0, pos );
  85. password = authentication.substring( pos + 1 );
  86. }
  87. else
  88. {
  89. username = authentication;
  90. }
  91. }
  92. pos = middle.indexOf( ':' );
  93. // we have a defined port
  94. if ( pos > 0 )
  95. {
  96. host = middle.substring( 0, pos );
  97. port = middle.substring( pos + 1 );
  98. }
  99. else
  100. {
  101. host = middle;
  102. }
  103. }
  104. public String toString()
  105. {
  106. return url;
  107. }
  108. public String getUsername()
  109. {
  110. return username;
  111. }
  112. public String getPassword()
  113. {
  114. return password;
  115. }
  116. public String getHost()
  117. {
  118. return host;
  119. }
  120. public String getPath()
  121. {
  122. return path;
  123. }
  124. public String getPort()
  125. {
  126. return port;
  127. }
  128. public String getProtocol()
  129. {
  130. return protocol;
  131. }
  132. public String getUrl()
  133. {
  134. return url;
  135. }
  136. }