Mercurial > jhg
comparison src/com/tmate/hgkit/console/Remote.java @ 69:5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Sat, 22 Jan 2011 22:53:57 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 68:0e499fed9b3d | 69:5a69397f0f99 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2011 Artem Tikhomirov | |
| 3 */ | |
| 4 package com.tmate.hgkit.console; | |
| 5 | |
| 6 import java.io.File; | |
| 7 import java.io.FileOutputStream; | |
| 8 import java.io.InputStream; | |
| 9 import java.net.URL; | |
| 10 import java.security.cert.CertificateException; | |
| 11 import java.security.cert.X509Certificate; | |
| 12 import java.util.List; | |
| 13 import java.util.Map; | |
| 14 import java.util.prefs.Preferences; | |
| 15 import java.util.zip.InflaterInputStream; | |
| 16 | |
| 17 import javax.net.ssl.HttpsURLConnection; | |
| 18 import javax.net.ssl.SSLContext; | |
| 19 import javax.net.ssl.TrustManager; | |
| 20 import javax.net.ssl.X509TrustManager; | |
| 21 | |
| 22 import org.tmatesoft.hg.internal.ConfigFile; | |
| 23 | |
| 24 /** | |
| 25 * | |
| 26 * @author artem | |
| 27 */ | |
| 28 public class Remote { | |
| 29 | |
| 30 /* | |
| 31 * @see http://mercurial.selenic.com/wiki/WireProtocol | |
| 32 cmd=branches gives 4 nodeids (head, root, first parent, second parent) per line (few lines possible, per branch, perhaps?) | |
| 33 cmd=capabilities gives lookup ...subset and 3 compress methods | |
| 34 // lookup changegroupsubset unbundle=HG10GZ,HG10BZ,HG10UN | |
| 35 cmd=heads gives space-separated list of nodeids (or just one) | |
| 36 nodeids are in hex (printable) format, need to convert fromAscii() | |
| 37 cmd=branchmap | |
| 38 */ | |
| 39 public static void main(String[] args) throws Exception { | |
| 40 String nid = "d6d2a630f4a6d670c90a5ca909150f2b426ec88f"; | |
| 41 ConfigFile cfg = new ConfigFile(); | |
| 42 cfg.addLocation(new File(System.getProperty("user.home"), ".hgrc")); | |
| 43 String svnkitServer = cfg.getSection("paths").get("svnkit"); | |
| 44 URL url = new URL(svnkitServer + "?cmd=changegroup&roots=a78c980749e3ccebb47138b547e9b644a22797a9"); | |
| 45 | |
| 46 SSLContext sslContext = SSLContext.getInstance("SSL"); | |
| 47 class TrustEveryone implements X509TrustManager { | |
| 48 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { | |
| 49 System.out.println("checkClientTrusted " + authType); | |
| 50 } | |
| 51 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { | |
| 52 System.out.println("checkServerTrusted" + authType); | |
| 53 } | |
| 54 public X509Certificate[] getAcceptedIssuers() { | |
| 55 return new X509Certificate[0]; | |
| 56 } | |
| 57 } | |
| 58 // | |
| 59 Preferences tempNode = Preferences.userRoot().node("xxx"); | |
| 60 tempNode.putByteArray("xxx", url.getUserInfo().getBytes()); | |
| 61 String authInfo = tempNode.get("xxx", null); | |
| 62 tempNode.removeNode(); | |
| 63 // | |
| 64 sslContext.init(null, new TrustManager[] { new TrustEveryone() }, null); | |
| 65 HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); | |
| 66 urlConnection.addRequestProperty("User-Agent", "jhg/0.1.0"); | |
| 67 urlConnection.addRequestProperty("Accept", "application/mercurial-0.1"); | |
| 68 urlConnection.addRequestProperty("Authorization", "Basic " + authInfo); | |
| 69 urlConnection.setSSLSocketFactory(sslContext.getSocketFactory()); | |
| 70 urlConnection.connect(); | |
| 71 System.out.println("Response headers:"); | |
| 72 final Map<String, List<String>> headerFields = urlConnection.getHeaderFields(); | |
| 73 for (String s : headerFields.keySet()) { | |
| 74 System.out.printf("%s: %s\n", s, urlConnection.getHeaderField(s)); | |
| 75 } | |
| 76 System.out.printf("Content type is %s and its length is %d\n", urlConnection.getContentType(), urlConnection.getContentLength()); | |
| 77 InputStream is = urlConnection.getInputStream(); | |
| 78 // int b; | |
| 79 // while ((b =is.read()) != -1) { | |
| 80 // System.out.print((char) b); | |
| 81 // } | |
| 82 // System.out.println(); | |
| 83 InflaterInputStream zipStream = new InflaterInputStream(is); | |
| 84 File tf = File.createTempFile("hg-bundle-", null); | |
| 85 FileOutputStream fos = new FileOutputStream(tf); | |
| 86 int r; | |
| 87 byte[] buf = new byte[8*1024]; | |
| 88 while ((r = zipStream.read(buf)) != -1) { | |
| 89 fos.write(buf, 0, r); | |
| 90 } | |
| 91 fos.close(); | |
| 92 zipStream.close(); | |
| 93 System.out.println(tf); | |
| 94 | |
| 95 urlConnection.disconnect(); | |
| 96 // | |
| 97 } | |
| 98 } |
