Mercurial > jhg
view src/org/tmatesoft/hg/repo/HgRemoteRepository.java @ 177:e10225daface
Use POST for long between queries. Batch between queries (pass multiple pairs to a server) to minimize number thereof
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Sat, 02 Apr 2011 23:05:28 +0200 |
parents | a8df7162ec75 |
children | 62665d8f0686 |
line wrap: on
line source
/* * Copyright (c) 2011 TMate Software Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * For information on how to redistribute this software under * the terms of a license other than GNU General Public License * contact TMate Software at support@hg4j.com */ package org.tmatesoft.hg.repo; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.StreamTokenizer; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.tmatesoft.hg.core.HgBadStateException; import org.tmatesoft.hg.core.HgException; import org.tmatesoft.hg.core.Nodeid; /** * WORK IN PROGRESS, DO NOT USE * * @see http://mercurial.selenic.com/wiki/WireProtocol * * @author Artem Tikhomirov * @author TMate Software Ltd. */ public class HgRemoteRepository { private final URL url; private final SSLContext sslContext; private final String authInfo; HgRemoteRepository(URL url) throws HgException { if (url == null) { throw new IllegalArgumentException(); } this.url = url; if ("https".equals(url.getProtocol())) { try { sslContext = SSLContext.getInstance("SSL"); class TrustEveryone implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { System.out.println("checkClientTrusted " + authType); } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { System.out.println("checkServerTrusted" + authType); } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }; sslContext.init(null, new TrustManager[] { new TrustEveryone() }, null); } catch (Exception ex) { throw new HgException(ex); } } else { sslContext = null; } if (url.getUserInfo() != null) { String ai = null; try { // Hack to get Base64-encoded credentials Preferences tempNode = Preferences.userRoot().node("xxx"); tempNode.putByteArray("xxx", url.getUserInfo().getBytes()); ai = tempNode.get("xxx", null); tempNode.removeNode(); } catch (BackingStoreException ex) { ex.printStackTrace(); // IGNORE } authInfo = ai; } else { authInfo = null; } } public List<Nodeid> heads() { return Collections.singletonList(Nodeid.fromAscii("71ddbf8603e8e09d54ac9c5fe4bb5ae824589f1d")); // return Collections.emptyList(); } public List<Nodeid> between(Nodeid tip, Nodeid base) throws HgException { Range r = new Range(base, tip); // XXX shall handle errors like no range key in the returned map, not sure how. return between(Collections.singletonList(r)).get(r); } /** * @param ranges * @return map, where keys are input instances, values are corresponding server reply * @throws HgException */ public Map<Range, List<Nodeid>> between(Collection<Range> ranges) throws HgException { if (ranges.isEmpty()) { return Collections.emptyMap(); } // if fact, shall do other way round, this method shall send LinkedHashMap<Range, List<Nodeid>> rv = new LinkedHashMap<HgRemoteRepository.Range, List<Nodeid>>(ranges.size() * 4 / 3); StringBuilder sb = new StringBuilder(20 + ranges.size() * 82); sb.append("pairs="); for (Range r : ranges) { sb.append(r.end.toString()); sb.append('-'); sb.append(r.start.toString()); sb.append('+'); } if (sb.charAt(sb.length() - 1) == '+') { // strip last space sb.setLength(sb.length() - 1); } try { boolean usePOST = ranges.size() > 3; URL u = new URL(url, url.getPath() + "?cmd=between" + (usePOST ? "" : '&' + sb.toString())); HttpURLConnection c = setupConnection(u.openConnection()); if (usePOST) { c.setRequestMethod("POST"); c.setRequestProperty("Content-Length", String.valueOf(sb.length()/*nodeids are ASCII, bytes == characters */)); c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); c.setDoOutput(true); c.connect(); OutputStream os = c.getOutputStream(); os.write(sb.toString().getBytes()); os.flush(); os.close(); } else { c.connect(); } System.out.printf("%d ranges, method:%s \n", ranges.size(), c.getRequestMethod()); System.out.printf("Query (%d bytes):%s\n", u.getQuery().length(), u.getQuery()); System.out.println("Response headers:"); final Map<String, List<String>> headerFields = c.getHeaderFields(); for (String s : headerFields.keySet()) { System.out.printf("%s: %s\n", s, c.getHeaderField(s)); } InputStreamReader is = new InputStreamReader(c.getInputStream(), "US-ASCII"); StreamTokenizer st = new StreamTokenizer(is); st.ordinaryChars('0', '9'); st.wordChars('0', '9'); st.eolIsSignificant(true); Iterator<Range> rangeItr = ranges.iterator(); LinkedList<Nodeid> currRangeList = null; Range currRange = null; boolean possiblyEmptyNextLine = true; while (st.nextToken() != StreamTokenizer.TT_EOF) { if (st.ttype == StreamTokenizer.TT_EOL) { if (possiblyEmptyNextLine) { // newline follows newline; assert currRange == null; assert currRangeList == null; if (!rangeItr.hasNext()) { throw new HgBadStateException(); } rv.put(rangeItr.next(), Collections.<Nodeid>emptyList()); } else { if (currRange == null || currRangeList == null) { throw new HgBadStateException(); } // indicate next range value is needed currRange = null; currRangeList = null; possiblyEmptyNextLine = true; } } else { possiblyEmptyNextLine = false; if (currRange == null) { if (!rangeItr.hasNext()) { throw new HgBadStateException(); } currRange = rangeItr.next(); currRangeList = new LinkedList<Nodeid>(); rv.put(currRange, currRangeList); } Nodeid nid = Nodeid.fromAscii(st.sval); currRangeList.addLast(nid); } } is.close(); return rv; } catch (MalformedURLException ex) { throw new HgException(ex); } catch (IOException ex) { throw new HgException(ex); } } public List<RemoteBranch> branches(List<Nodeid> nodes) { return Collections.emptyList(); } // WireProtocol wiki: roots = a list of the latest nodes on every service side changeset branch that both the client and server know about. public HgBundle getChanges(List<Nodeid> roots) throws HgException { return new HgLookup().loadBundle(new File("/temp/hg/hg-bundle-000000000000-gz.tmp")); } private HttpURLConnection setupConnection(URLConnection urlConnection) { urlConnection.setRequestProperty("User-Agent", "hg4j/0.5.0"); urlConnection.addRequestProperty("Accept", "application/mercurial-0.1"); if (authInfo != null) { urlConnection.addRequestProperty("Authorization", "Basic " + authInfo); } if (sslContext != null) { ((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslContext.getSocketFactory()); } return (HttpURLConnection) urlConnection; } public static final class Range { /** * Root of the range, earlier revision */ public final Nodeid start; /** * Head of the range, later revision. */ public final Nodeid end; /** * @param from - root/base revision * @param to - head/tip revision */ public Range(Nodeid from, Nodeid to) { start = from; end = to; } } public static final class RemoteBranch { public final Nodeid head, root, p1, p2; public RemoteBranch(Nodeid h, Nodeid r, Nodeid parent1, Nodeid parent2) { head = h; root = r; p1 = parent1; p2 = parent2; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (false == obj instanceof RemoteBranch) { return false; } RemoteBranch o = (RemoteBranch) obj; return head.equals(o.head) && root.equals(o.root) && (p1 == null && o.p1 == null || p1.equals(o.p1)) && (p2 == null && o.p2 == null || p2.equals(o.p2)); } } }