001package org.apache.maven.wagon.repository; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.wagon.PathUtils; 023import org.apache.maven.wagon.WagonConstants; 024import org.codehaus.plexus.util.StringUtils; 025 026import java.io.Serializable; 027import java.util.Properties; 028 029/** 030 * This class is an abstraction of the location from/to resources 031 * can be transfered. 032 * 033 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 034 * 035 * @todo [BP] some things are specific to certain wagons (eg key stuff in authInfo, permissions) 036 */ 037public class Repository 038 implements Serializable 039{ 040 private static final long serialVersionUID = 1312227676322136247L; 041 042 private String id; 043 044 private String name; 045 046 private String host; 047 048 private int port = WagonConstants.UNKNOWN_PORT; 049 050 private String basedir; 051 052 private String protocol; 053 054 private String url; 055 056 private RepositoryPermissions permissions; 057 058 /** 059 * Properties influencing wagon behaviour 060 * which are very specific to particular wagon. 061 */ 062 private Properties parameters = new Properties(); 063 064 // Username/password are sometimes encoded in the URL 065 private String username = null; 066 067 private String password = null; 068 069 /** 070 * @deprecated use {@link #Repository(String, String)} 071 */ 072 public Repository() 073 { 074 075 } 076 077 public Repository( String id, String url ) 078 { 079 if ( id == null ) 080 { 081 throw new NullPointerException( "id cannot be null" ); 082 } 083 084 setId( id ); 085 086 if ( url == null ) 087 { 088 throw new NullPointerException( "url cannot be null" ); 089 } 090 091 setUrl( url ); 092 } 093 094 public String getId() 095 { 096 return id; 097 } 098 099 public void setId( String id ) 100 { 101 this.id = id; 102 } 103 104 /** 105 * Retrieve the base directory of the repository. This is derived from the full repository URL, and 106 * contains the entire path component. 107 * 108 * @return the base directory 109 */ 110 public String getBasedir() 111 { 112 return basedir; 113 } 114 115 public void setBasedir( String basedir ) 116 { 117 this.basedir = basedir; 118 } 119 120 public void setName( String name ) 121 { 122 this.name = name; 123 } 124 125 public int getPort() 126 { 127 return port; 128 } 129 130 public void setPort( int port ) 131 { 132 this.port = port; 133 } 134 135 public void setUrl( String url ) 136 { 137 this.url = url; 138 139 // TODO [BP]: refactor out the PathUtils URL stuff into a class like java.net.URL, so you only parse once 140 // can't use URL class as is because it won't recognise our protocols, though perhaps we could attempt to 141 // register handlers for scp, etc? 142 143 this.protocol = PathUtils.protocol( url ); 144 145 this.host = PathUtils.host( url ); 146 147 this.port = PathUtils.port( url ); 148 149 this.basedir = PathUtils.basedir( url ); 150 151 String username = PathUtils.user( url ); 152 this.username = username; 153 154 if ( username != null ) 155 { 156 String password = PathUtils.password( url ); 157 158 if ( password != null ) 159 { 160 this.password = password; 161 162 username += ":" + password; 163 } 164 165 username += "@"; 166 167 int index = url.indexOf( username ); 168 this.url = url.substring( 0, index ) + url.substring( index + username.length() ); 169 } 170 } 171 172 public String getUrl() 173 { 174 if ( url != null ) 175 { 176 return url; 177 } 178 179 StringBuilder sb = new StringBuilder(); 180 181 sb.append( protocol ); 182 183 sb.append( "://" ); 184 185 if ( isIPv6Address() ) 186 { 187 // If this is IPv6 then we have to surround it 188 // with brackets '[' and ']' 189 sb.append( "[" ).append( getHost() ).append( "]" ); 190 } 191 else 192 { 193 sb.append( getHost() ); 194 } 195 196 if ( port != WagonConstants.UNKNOWN_PORT ) 197 { 198 sb.append( ":" ); 199 200 sb.append( port ); 201 } 202 203 sb.append( basedir ); 204 205 return sb.toString(); 206 } 207 208 /** 209 * Checks whtther provided url contains IPv6 format in host portion 210 * 211 * @return true if provide host part is of IPv6 format 212 */ 213 private boolean isIPv6Address() 214 { 215 return getHost().contains( ":" ); 216 } 217 218 public String getHost() 219 { 220 if ( host == null ) 221 { 222 return "localhost"; 223 } 224 return host; 225 } 226 227 public String getName() 228 { 229 if ( name == null ) 230 { 231 return getId(); 232 } 233 return name; 234 } 235 236 public String toString() 237 { 238 StringBuilder sb = new StringBuilder(); 239 240 sb.append( "Repository[" ); 241 242 if ( StringUtils.isNotEmpty( getName() ) ) 243 { 244 sb.append( getName() ).append( "|" ); 245 } 246 247 sb.append( getUrl() ); 248 sb.append( "]" ); 249 250 return sb.toString(); 251 } 252 253 public String getProtocol() 254 { 255 return protocol; 256 } 257 258 public RepositoryPermissions getPermissions() 259 { 260 return permissions; 261 } 262 263 public void setPermissions( RepositoryPermissions permissions ) 264 { 265 this.permissions = permissions; 266 } 267 268 public String getParameter( String key ) 269 { 270 return parameters.getProperty( key ); 271 } 272 273 public void setParameters( Properties parameters ) 274 { 275 this.parameters = parameters; 276 } 277 278 public int hashCode() 279 { 280 final int prime = 31; 281 int result = 1; 282 result = prime * result + ( ( id == null ) ? 0 : id.hashCode() ); 283 return result; 284 } 285 286 public boolean equals( Object obj ) 287 { 288 if ( this == obj ) 289 { 290 return true; 291 } 292 if ( obj == null ) 293 { 294 return false; 295 } 296 if ( getClass() != obj.getClass() ) 297 { 298 return false; 299 } 300 final Repository other = (Repository) obj; 301 if ( id == null ) 302 { 303 if ( other.id != null ) 304 { 305 return false; 306 } 307 } 308 else if ( !id.equals( other.id ) ) 309 { 310 return false; 311 } 312 return true; 313 } 314 315 public String getUsername() 316 { 317 return username; 318 } 319 320 public String getPassword() 321 { 322 return password; 323 } 324 325 public void setProtocol( String protocol ) 326 { 327 this.protocol = protocol; 328 } 329 330}