comparison cmdline/org/tmatesoft/hg/console/Remote.java @ 74:6f1b88693d48

Complete refactoring to org.tmatesoft
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 24 Jan 2011 03:14:45 +0100
parents src/com/tmate/hgkit/console/Remote.java@5a69397f0f99
children a3a2e5deb320
comparison
equal deleted inserted replaced
73:0d279bcc4442 74:6f1b88693d48
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@svnkit.com
16 */
17 package org.tmatesoft.hg.console;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.InputStream;
22 import java.net.URL;
23 import java.security.cert.CertificateException;
24 import java.security.cert.X509Certificate;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.prefs.Preferences;
28 import java.util.zip.InflaterInputStream;
29
30 import javax.net.ssl.HttpsURLConnection;
31 import javax.net.ssl.SSLContext;
32 import javax.net.ssl.TrustManager;
33 import javax.net.ssl.X509TrustManager;
34
35 import org.tmatesoft.hg.internal.ConfigFile;
36
37 /**
38 * WORK IN PROGRESS, DO NOT USE
39 *
40 * @author Artem Tikhomirov
41 * @author TMate Software Ltd.
42 */
43 public class Remote {
44
45 /*
46 * @see http://mercurial.selenic.com/wiki/WireProtocol
47 cmd=branches gives 4 nodeids (head, root, first parent, second parent) per line (few lines possible, per branch, perhaps?)
48 cmd=capabilities gives lookup ...subset and 3 compress methods
49 // lookup changegroupsubset unbundle=HG10GZ,HG10BZ,HG10UN
50 cmd=heads gives space-separated list of nodeids (or just one)
51 nodeids are in hex (printable) format, need to convert fromAscii()
52 cmd=branchmap
53 */
54 public static void main(String[] args) throws Exception {
55 String nid = "d6d2a630f4a6d670c90a5ca909150f2b426ec88f";
56 ConfigFile cfg = new ConfigFile();
57 cfg.addLocation(new File(System.getProperty("user.home"), ".hgrc"));
58 String svnkitServer = cfg.getSection("paths").get("svnkit");
59 URL url = new URL(svnkitServer + "?cmd=changegroup&roots=a78c980749e3ccebb47138b547e9b644a22797a9");
60
61 SSLContext sslContext = SSLContext.getInstance("SSL");
62 class TrustEveryone implements X509TrustManager {
63 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
64 System.out.println("checkClientTrusted " + authType);
65 }
66 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
67 System.out.println("checkServerTrusted" + authType);
68 }
69 public X509Certificate[] getAcceptedIssuers() {
70 return new X509Certificate[0];
71 }
72 }
73 //
74 Preferences tempNode = Preferences.userRoot().node("xxx");
75 tempNode.putByteArray("xxx", url.getUserInfo().getBytes());
76 String authInfo = tempNode.get("xxx", null);
77 tempNode.removeNode();
78 //
79 sslContext.init(null, new TrustManager[] { new TrustEveryone() }, null);
80 HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
81 urlConnection.addRequestProperty("User-Agent", "jhg/0.1.0");
82 urlConnection.addRequestProperty("Accept", "application/mercurial-0.1");
83 urlConnection.addRequestProperty("Authorization", "Basic " + authInfo);
84 urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());
85 urlConnection.connect();
86 System.out.println("Response headers:");
87 final Map<String, List<String>> headerFields = urlConnection.getHeaderFields();
88 for (String s : headerFields.keySet()) {
89 System.out.printf("%s: %s\n", s, urlConnection.getHeaderField(s));
90 }
91 System.out.printf("Content type is %s and its length is %d\n", urlConnection.getContentType(), urlConnection.getContentLength());
92 InputStream is = urlConnection.getInputStream();
93 // int b;
94 // while ((b =is.read()) != -1) {
95 // System.out.print((char) b);
96 // }
97 // System.out.println();
98 InflaterInputStream zipStream = new InflaterInputStream(is);
99 File tf = File.createTempFile("hg-bundle-", null);
100 FileOutputStream fos = new FileOutputStream(tf);
101 int r;
102 byte[] buf = new byte[8*1024];
103 while ((r = zipStream.read(buf)) != -1) {
104 fos.write(buf, 0, r);
105 }
106 fos.close();
107 zipStream.close();
108 System.out.println(tf);
109
110 urlConnection.disconnect();
111 //
112 }
113 }