1 17 18 package org.apache.avalon.repository ; 19 20 import java.io.IOException ; 21 import java.net.URL ; 22 import java.net.URLConnection ; 23 import java.net.MalformedURLException ; 24 import java.util.StringTokenizer ; 25 26 import org.apache.avalon.repository.Artifact; 27 28 32 public abstract class AbstractURLConnection extends URLConnection 33 { 34 38 AbstractURLConnection( URL url ) 39 throws IOException 40 { 41 super( url ); 42 43 String path = url.getPath(); 44 int i = path.lastIndexOf( "/" ); 45 if( i<0 ) 46 { 47 final String error = 48 "Artifact specification does not contain a [group]/[name] seperator."; 49 throw new MalformedURLException ( error ); 50 } 51 } 52 53 59 public Object getContent( Class [] classes ) throws IOException 60 { 61 return getContent(); 62 } 63 64 67 public void connect() 68 { 69 } 71 72 78 protected Object getContent( String type ) throws IOException 79 { 80 try 81 { 82 final String path = getURL().getPath(); 83 final int i = path.lastIndexOf( "/" ); 84 final String group = path.substring( 0, i ); 85 final String name = path.substring( i+1 ); 86 final String version = getVersion( url ); 87 return Artifact.createArtifact( group, name, version, type ); 88 } 89 catch( Throwable e ) 90 { 91 final String error = 92 "Unexpected exception while resolving url [" + super.getURL() + "]."; 93 throw new CascadingIOException( error ); 94 } 95 } 96 97 102 protected String getVersion( URL url ) 103 { 104 if( null != url.getRef() ) return url.getRef(); 105 return getQueryField( url, "version", null ); 106 } 107 108 115 protected String getQueryField( URL url, String field, String fallback ) 116 { 117 String query = url.getQuery(); 118 if( null != query ) 119 { 120 StringTokenizer tokenizer = new StringTokenizer ( query, "&" ); 121 while( tokenizer.hasMoreElements() ) 122 { 123 String token = tokenizer.nextToken(); 124 if( token.startsWith( field + "=" ) ) 125 { 126 return token.substring( (field.length() + 1) ); 127 } 128 } 129 } 130 return fallback; 131 } 132 } 133 | Popular Tags |