| 1 |
package org.torquebox.mojo.mavengem.wagon; |
| 2 |
|
| 3 |
import org.apache.maven.wagon.ConnectionException; |
| 4 |
import org.apache.maven.wagon.InputData; |
| 5 |
import org.apache.maven.wagon.OutputData; |
| 6 |
import org.apache.maven.wagon.ResourceDoesNotExistException; |
| 7 |
import org.apache.maven.wagon.StreamWagon; |
| 8 |
import org.apache.maven.wagon.TransferFailedException; |
| 9 |
import org.apache.maven.wagon.authentication.AuthenticationException; |
| 10 |
import org.apache.maven.wagon.authorization.AuthorizationException; |
| 11 |
import org.apache.maven.wagon.proxy.ProxyInfo; |
| 12 |
import org.apache.maven.wagon.resource.Resource; |
| 13 |
import org.torquebox.mojo.mavengem.MavenGemURLConnection; |
| 14 |
import org.torquebox.mojo.mavengem.RubygemsFactory; |
| 15 |
|
| 16 |
import java.io.File; |
| 17 |
import java.io.FileNotFoundException; |
| 18 |
import java.io.IOException; |
| 19 |
import java.io.InputStream; |
| 20 |
import java.net.InetSocketAddress; |
| 21 |
import java.net.MalformedURLException; |
| 22 |
import java.net.Proxy; |
| 23 |
import java.net.Proxy.Type; |
| 24 |
import java.net.SocketAddress; |
| 25 |
import java.net.URL; |
| 26 |
import java.net.URLConnection; |
| 27 |
|
| 28 |
public class MavenGemWagon extends StreamWagon { |
| 29 |
|
| 30 |
public static final String MAVEN_GEM_PREFIX = "mavengem:"; |
| 31 |
|
| 32 |
private Proxy proxy = Proxy.NO_PROXY; |
| 33 |
private RubygemsFactory _factory_; |
| 34 |
|
| 35 |
// configurable and set via the settings.xml, i.e. names here need to match |
| 36 |
// the names in settings.xml |
| 37 |
private File cachedir; |
| 38 |
private String mirror; |
| 39 |
|
| 40 |
@Override |
| 41 |
public void fillInputData(InputData inputData) |
| 42 |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException { |
| 43 |
|
| 44 |
Resource resource = inputData.getResource(); |
| 45 |
|
| 46 |
try { |
| 47 |
|
| 48 |
URLConnection urlConnection = newConnection(resource.getName()); |
| 49 |
InputStream is = urlConnection.getInputStream(); |
| 50 |
|
| 51 |
inputData.setInputStream(is); |
| 52 |
resource.setLastModified(urlConnection.getLastModified()); |
| 53 |
resource.setContentLength(urlConnection.getContentLength()); |
| 54 |
|
| 55 |
} catch (MalformedURLException e) { |
| 56 |
throw new TransferFailedException("Invalid repository URL: " + e.getMessage(), e); |
| 57 |
} catch (FileNotFoundException e) { |
| 58 |
throw new ResourceDoesNotExistException("Unable to locate resource in repository", e); |
| 59 |
} catch (IOException e) { |
| 60 |
throw new TransferFailedException("Error transferring file: " + e.getMessage(), e); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private RubygemsFactory rubygemsFactory() |
| 65 |
throws MalformedURLException { |
| 66 |
if (_factory_ == null) { |
| 67 |
// use the default if not set |
| 68 |
if (cachedir == null) { |
| 69 |
cachedir = RubygemsFactory.DEFAULT_CACHEDIR; |
| 70 |
} |
| 71 |
if (mirror != null) { |
| 72 |
_factory_ = new RubygemsFactory(cachedir, withAuthentication(mirror)); |
| 73 |
} else { |
| 74 |
_factory_ = new RubygemsFactory(cachedir); |
| 75 |
} |
| 76 |
} |
| 77 |
return _factory_; |
| 78 |
} |
| 79 |
|
| 80 |
public URLConnection newConnection(String resourceName) |
| 81 |
throws MalformedURLException { |
| 82 |
return new MavenGemURLConnection(rubygemsFactory(), |
| 83 |
getRepositoryURL(), |
| 84 |
"/" + resourceName, |
| 85 |
proxy); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public boolean resourceExists(String resourceName) |
| 90 |
throws TransferFailedException, AuthorizationException { |
| 91 |
try { |
| 92 |
newConnection(resourceName).connect(); |
| 93 |
return true; |
| 94 |
} catch (FileNotFoundException e) { |
| 95 |
return false; |
| 96 |
} catch (MalformedURLException e) { |
| 97 |
throw new TransferFailedException("Invalid repository URL: " + e.getMessage(), e); |
| 98 |
} catch (IOException e) { |
| 99 |
throw new TransferFailedException("Error transferring file: " + e.getMessage(), e); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public void fillOutputData(OutputData outputData) |
| 105 |
throws TransferFailedException { |
| 106 |
throw new RuntimeException("only download is provided"); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public void closeConnection() |
| 111 |
throws ConnectionException { |
| 112 |
} |
| 113 |
|
| 114 |
private URL withAuthentication(String url) |
| 115 |
throws MalformedURLException { |
| 116 |
if (authenticationInfo != null && authenticationInfo.getUserName() != null) { |
| 117 |
String credentials = authenticationInfo.getUserName() + ":" + authenticationInfo.getPassword(); |
| 118 |
url = url.replaceFirst("^(https?://)(.*)$", "$1" + credentials + "@$2"); |
| 119 |
} |
| 120 |
return new URL(url); |
| 121 |
} |
| 122 |
|
| 123 |
private URL getRepositoryURL() throws MalformedURLException { |
| 124 |
String url = getRepository().getUrl().substring(MAVEN_GEM_PREFIX.length()); |
| 125 |
return withAuthentication(url); |
| 126 |
} |
| 127 |
|
| 128 |
private Proxy getProxy(ProxyInfo proxyInfo) { |
| 129 |
return new Proxy(getProxyType(proxyInfo), getSocketAddress(proxyInfo)); |
| 130 |
} |
| 131 |
|
| 132 |
private Type getProxyType(ProxyInfo proxyInfo) { |
| 133 |
if (ProxyInfo.PROXY_SOCKS4.equals(proxyInfo.getType()) || ProxyInfo.PROXY_SOCKS5.equals(proxyInfo.getType())) { |
| 134 |
return Type.SOCKS; |
| 135 |
} else { |
| 136 |
return Type.HTTP; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
private SocketAddress getSocketAddress(ProxyInfo proxyInfo) { |
| 141 |
return InetSocketAddress.createUnresolved(proxyInfo.getHost(), proxyInfo.getPort()); |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
protected void openConnectionInternal() |
| 146 |
throws ConnectionException, AuthenticationException { |
| 147 |
|
| 148 |
try { |
| 149 |
final ProxyInfo proxyInfo = getProxyInfo(getRepositoryURL().getProtocol(), getRepository().getHost()); |
| 150 |
if (proxyInfo != null) { |
| 151 |
this.proxy = getProxy(proxyInfo); |
| 152 |
} |
| 153 |
} catch (MalformedURLException e) { |
| 154 |
throw new ConnectionException("cannot create repository url", e); |
| 155 |
} |
| 156 |
} |
| 157 |
} |