tikhomirov@69: /* tikhomirov@69: * Copyright (c) 2011 Artem Tikhomirov tikhomirov@69: */ tikhomirov@69: package com.tmate.hgkit.console; tikhomirov@69: tikhomirov@69: import java.io.File; tikhomirov@69: import java.io.FileOutputStream; tikhomirov@69: import java.io.InputStream; tikhomirov@69: import java.net.URL; tikhomirov@69: import java.security.cert.CertificateException; tikhomirov@69: import java.security.cert.X509Certificate; tikhomirov@69: import java.util.List; tikhomirov@69: import java.util.Map; tikhomirov@69: import java.util.prefs.Preferences; tikhomirov@69: import java.util.zip.InflaterInputStream; tikhomirov@69: tikhomirov@69: import javax.net.ssl.HttpsURLConnection; tikhomirov@69: import javax.net.ssl.SSLContext; tikhomirov@69: import javax.net.ssl.TrustManager; tikhomirov@69: import javax.net.ssl.X509TrustManager; tikhomirov@69: tikhomirov@69: import org.tmatesoft.hg.internal.ConfigFile; tikhomirov@69: tikhomirov@69: /** tikhomirov@69: * tikhomirov@69: * @author artem tikhomirov@69: */ tikhomirov@69: public class Remote { tikhomirov@69: tikhomirov@69: /* tikhomirov@69: * @see http://mercurial.selenic.com/wiki/WireProtocol tikhomirov@69: cmd=branches gives 4 nodeids (head, root, first parent, second parent) per line (few lines possible, per branch, perhaps?) tikhomirov@69: cmd=capabilities gives lookup ...subset and 3 compress methods tikhomirov@69: // lookup changegroupsubset unbundle=HG10GZ,HG10BZ,HG10UN tikhomirov@69: cmd=heads gives space-separated list of nodeids (or just one) tikhomirov@69: nodeids are in hex (printable) format, need to convert fromAscii() tikhomirov@69: cmd=branchmap tikhomirov@69: */ tikhomirov@69: public static void main(String[] args) throws Exception { tikhomirov@69: String nid = "d6d2a630f4a6d670c90a5ca909150f2b426ec88f"; tikhomirov@69: ConfigFile cfg = new ConfigFile(); tikhomirov@69: cfg.addLocation(new File(System.getProperty("user.home"), ".hgrc")); tikhomirov@69: String svnkitServer = cfg.getSection("paths").get("svnkit"); tikhomirov@69: URL url = new URL(svnkitServer + "?cmd=changegroup&roots=a78c980749e3ccebb47138b547e9b644a22797a9"); tikhomirov@69: tikhomirov@69: SSLContext sslContext = SSLContext.getInstance("SSL"); tikhomirov@69: class TrustEveryone implements X509TrustManager { tikhomirov@69: public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { tikhomirov@69: System.out.println("checkClientTrusted " + authType); tikhomirov@69: } tikhomirov@69: public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { tikhomirov@69: System.out.println("checkServerTrusted" + authType); tikhomirov@69: } tikhomirov@69: public X509Certificate[] getAcceptedIssuers() { tikhomirov@69: return new X509Certificate[0]; tikhomirov@69: } tikhomirov@69: } tikhomirov@69: // tikhomirov@69: Preferences tempNode = Preferences.userRoot().node("xxx"); tikhomirov@69: tempNode.putByteArray("xxx", url.getUserInfo().getBytes()); tikhomirov@69: String authInfo = tempNode.get("xxx", null); tikhomirov@69: tempNode.removeNode(); tikhomirov@69: // tikhomirov@69: sslContext.init(null, new TrustManager[] { new TrustEveryone() }, null); tikhomirov@69: HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); tikhomirov@69: urlConnection.addRequestProperty("User-Agent", "jhg/0.1.0"); tikhomirov@69: urlConnection.addRequestProperty("Accept", "application/mercurial-0.1"); tikhomirov@69: urlConnection.addRequestProperty("Authorization", "Basic " + authInfo); tikhomirov@69: urlConnection.setSSLSocketFactory(sslContext.getSocketFactory()); tikhomirov@69: urlConnection.connect(); tikhomirov@69: System.out.println("Response headers:"); tikhomirov@69: final Map> headerFields = urlConnection.getHeaderFields(); tikhomirov@69: for (String s : headerFields.keySet()) { tikhomirov@69: System.out.printf("%s: %s\n", s, urlConnection.getHeaderField(s)); tikhomirov@69: } tikhomirov@69: System.out.printf("Content type is %s and its length is %d\n", urlConnection.getContentType(), urlConnection.getContentLength()); tikhomirov@69: InputStream is = urlConnection.getInputStream(); tikhomirov@69: // int b; tikhomirov@69: // while ((b =is.read()) != -1) { tikhomirov@69: // System.out.print((char) b); tikhomirov@69: // } tikhomirov@69: // System.out.println(); tikhomirov@69: InflaterInputStream zipStream = new InflaterInputStream(is); tikhomirov@69: File tf = File.createTempFile("hg-bundle-", null); tikhomirov@69: FileOutputStream fos = new FileOutputStream(tf); tikhomirov@69: int r; tikhomirov@69: byte[] buf = new byte[8*1024]; tikhomirov@69: while ((r = zipStream.read(buf)) != -1) { tikhomirov@69: fos.write(buf, 0, r); tikhomirov@69: } tikhomirov@69: fos.close(); tikhomirov@69: zipStream.close(); tikhomirov@69: System.out.println(tf); tikhomirov@69: tikhomirov@69: urlConnection.disconnect(); tikhomirov@69: // tikhomirov@69: } tikhomirov@69: }