tikhomirov@170: /* tikhomirov@645: * Copyright (c) 2011-2013 TMate Software Ltd tikhomirov@170: * tikhomirov@170: * This program is free software; you can redistribute it and/or modify tikhomirov@170: * it under the terms of the GNU General Public License as published by tikhomirov@170: * the Free Software Foundation; version 2 of the License. tikhomirov@170: * tikhomirov@170: * This program is distributed in the hope that it will be useful, tikhomirov@170: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@170: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@170: * GNU General Public License for more details. tikhomirov@170: * tikhomirov@170: * For information on how to redistribute this software under tikhomirov@170: * the terms of a license other than GNU General Public License tikhomirov@170: * contact TMate Software at support@hg4j.com tikhomirov@170: */ tikhomirov@170: package org.tmatesoft.hg.repo; tikhomirov@170: tikhomirov@456: import static org.tmatesoft.hg.util.LogFacility.Severity.Info; tikhomirov@650: import static org.tmatesoft.hg.util.Outcome.Kind.Failure; tikhomirov@650: import static org.tmatesoft.hg.util.Outcome.Kind.Success; tikhomirov@456: tikhomirov@428: import java.io.BufferedReader; tikhomirov@645: import java.io.ByteArrayOutputStream; tikhomirov@170: import java.io.File; tikhomirov@179: import java.io.FileOutputStream; tikhomirov@176: import java.io.IOException; tikhomirov@179: import java.io.InputStream; tikhomirov@177: import java.io.InputStreamReader; tikhomirov@177: import java.io.OutputStream; tikhomirov@176: import java.io.StreamTokenizer; tikhomirov@645: import java.net.ContentHandler; tikhomirov@645: import java.net.ContentHandlerFactory; tikhomirov@177: import java.net.HttpURLConnection; tikhomirov@176: import java.net.MalformedURLException; tikhomirov@171: import java.net.URL; tikhomirov@176: import java.net.URLConnection; tikhomirov@176: import java.security.cert.CertificateException; tikhomirov@176: import java.security.cert.X509Certificate; tikhomirov@178: import java.util.ArrayList; tikhomirov@428: import java.util.Arrays; tikhomirov@176: import java.util.Collection; tikhomirov@171: import java.util.Collections; tikhomirov@428: import java.util.HashSet; tikhomirov@177: import java.util.Iterator; tikhomirov@176: import java.util.LinkedHashMap; tikhomirov@176: import java.util.LinkedList; tikhomirov@170: import java.util.List; tikhomirov@176: import java.util.Map; tikhomirov@428: import java.util.Set; tikhomirov@176: import java.util.prefs.BackingStoreException; tikhomirov@176: import java.util.prefs.Preferences; tikhomirov@179: import java.util.zip.InflaterInputStream; tikhomirov@176: tikhomirov@176: import javax.net.ssl.HttpsURLConnection; tikhomirov@176: import javax.net.ssl.SSLContext; tikhomirov@176: import javax.net.ssl.TrustManager; tikhomirov@176: import javax.net.ssl.X509TrustManager; tikhomirov@170: tikhomirov@181: import org.tmatesoft.hg.core.HgBadArgumentException; tikhomirov@645: import org.tmatesoft.hg.core.HgIOException; tikhomirov@215: import org.tmatesoft.hg.core.HgRemoteConnectionException; tikhomirov@425: import org.tmatesoft.hg.core.HgRepositoryNotFoundException; tikhomirov@170: import org.tmatesoft.hg.core.Nodeid; tikhomirov@295: import org.tmatesoft.hg.core.SessionContext; tikhomirov@645: import org.tmatesoft.hg.internal.DataSerializer; tikhomirov@650: import org.tmatesoft.hg.internal.DataSerializer.OutputStreamSerializer; tikhomirov@673: import org.tmatesoft.hg.internal.BundleSerializer; tikhomirov@646: import org.tmatesoft.hg.internal.EncodingHelper; tikhomirov@645: import org.tmatesoft.hg.internal.Internals; tikhomirov@456: import org.tmatesoft.hg.internal.PropertyMarshal; tikhomirov@650: import org.tmatesoft.hg.util.LogFacility.Severity; tikhomirov@649: import org.tmatesoft.hg.util.Outcome; tikhomirov@646: import org.tmatesoft.hg.util.Pair; tikhomirov@170: tikhomirov@170: /** tikhomirov@170: * WORK IN PROGRESS, DO NOT USE tikhomirov@170: * tikhomirov@170: * @see http://mercurial.selenic.com/wiki/WireProtocol tikhomirov@646: * @see http://mercurial.selenic.com/wiki/HttpCommandProtocol tikhomirov@170: * tikhomirov@170: * @author Artem Tikhomirov tikhomirov@170: * @author TMate Software Ltd. tikhomirov@170: */ tikhomirov@490: public class HgRemoteRepository implements SessionContext.Source { tikhomirov@171: tikhomirov@176: private final URL url; tikhomirov@176: private final SSLContext sslContext; tikhomirov@176: private final String authInfo; tikhomirov@407: private final boolean debug; tikhomirov@186: private HgLookup lookupHelper; tikhomirov@295: private final SessionContext sessionContext; tikhomirov@428: private Set remoteCapabilities; tikhomirov@428: tikhomirov@645: static { tikhomirov@645: URLConnection.setContentHandlerFactory(new ContentHandlerFactory() { tikhomirov@645: tikhomirov@645: public ContentHandler createContentHandler(String mimetype) { tikhomirov@645: if ("application/mercurial-0.1".equals(mimetype)) { tikhomirov@645: return new ContentHandler() { tikhomirov@645: tikhomirov@645: @Override tikhomirov@645: public Object getContent(URLConnection urlc) throws IOException { tikhomirov@645: if (urlc.getContentLength() > 0) { tikhomirov@645: ByteArrayOutputStream bos = new ByteArrayOutputStream(); tikhomirov@645: InputStream is = urlc.getInputStream(); tikhomirov@645: int r; tikhomirov@645: while ((r = is.read()) != -1) { tikhomirov@645: bos.write(r); tikhomirov@645: } tikhomirov@645: return new String(bos.toByteArray()); tikhomirov@645: } tikhomirov@645: return ""; tikhomirov@645: } tikhomirov@645: }; tikhomirov@645: } tikhomirov@645: return null; tikhomirov@645: } tikhomirov@645: }); tikhomirov@645: } tikhomirov@645: tikhomirov@295: HgRemoteRepository(SessionContext ctx, URL url) throws HgBadArgumentException { tikhomirov@295: if (url == null || ctx == null) { tikhomirov@176: throw new IllegalArgumentException(); tikhomirov@176: } tikhomirov@176: this.url = url; tikhomirov@295: sessionContext = ctx; tikhomirov@456: debug = new PropertyMarshal(ctx).getBoolean("hg4j.remote.debug", false); tikhomirov@176: if ("https".equals(url.getProtocol())) { tikhomirov@176: try { tikhomirov@176: sslContext = SSLContext.getInstance("SSL"); tikhomirov@176: class TrustEveryone implements X509TrustManager { tikhomirov@176: public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { tikhomirov@207: if (debug) { tikhomirov@207: System.out.println("checkClientTrusted:" + authType); tikhomirov@207: } tikhomirov@176: } tikhomirov@176: public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { tikhomirov@207: if (debug) { tikhomirov@207: System.out.println("checkServerTrusted:" + authType); tikhomirov@207: } tikhomirov@176: } tikhomirov@176: public X509Certificate[] getAcceptedIssuers() { tikhomirov@176: return new X509Certificate[0]; tikhomirov@176: } tikhomirov@176: }; tikhomirov@176: sslContext.init(null, new TrustManager[] { new TrustEveryone() }, null); tikhomirov@176: } catch (Exception ex) { tikhomirov@181: throw new HgBadArgumentException("Can't initialize secure connection", ex); tikhomirov@176: } tikhomirov@176: } else { tikhomirov@176: sslContext = null; tikhomirov@176: } tikhomirov@176: if (url.getUserInfo() != null) { tikhomirov@176: String ai = null; tikhomirov@176: try { tikhomirov@176: // Hack to get Base64-encoded credentials tikhomirov@176: Preferences tempNode = Preferences.userRoot().node("xxx"); tikhomirov@176: tempNode.putByteArray("xxx", url.getUserInfo().getBytes()); tikhomirov@176: ai = tempNode.get("xxx", null); tikhomirov@176: tempNode.removeNode(); tikhomirov@176: } catch (BackingStoreException ex) { tikhomirov@456: sessionContext.getLog().dump(getClass(), Info, ex, null); tikhomirov@176: // IGNORE tikhomirov@176: } tikhomirov@176: authInfo = ai; tikhomirov@176: } else { tikhomirov@176: authInfo = null; tikhomirov@176: } tikhomirov@171: } tikhomirov@171: tikhomirov@215: public boolean isInvalid() throws HgRemoteConnectionException { tikhomirov@646: initCapabilities(); tikhomirov@428: return remoteCapabilities.isEmpty(); tikhomirov@181: } tikhomirov@181: tikhomirov@181: /** tikhomirov@181: * @return human-readable address of the server, without user credentials or any other security information tikhomirov@181: */ tikhomirov@181: public String getLocation() { tikhomirov@181: if (url.getUserInfo() == null) { tikhomirov@181: return url.toExternalForm(); tikhomirov@181: } tikhomirov@181: if (url.getPort() != -1) { tikhomirov@181: return String.format("%s://%s:%d%s", url.getProtocol(), url.getHost(), url.getPort(), url.getPath()); tikhomirov@181: } else { tikhomirov@181: return String.format("%s://%s%s", url.getProtocol(), url.getHost(), url.getPath()); tikhomirov@181: } tikhomirov@181: } tikhomirov@490: tikhomirov@490: public SessionContext getSessionContext() { tikhomirov@490: return sessionContext; tikhomirov@490: } tikhomirov@181: tikhomirov@215: public List heads() throws HgRemoteConnectionException { tikhomirov@645: HttpURLConnection c = null; tikhomirov@178: try { tikhomirov@178: URL u = new URL(url, url.getPath() + "?cmd=heads"); tikhomirov@645: c = setupConnection(u.openConnection()); tikhomirov@178: c.connect(); tikhomirov@178: if (debug) { tikhomirov@178: dumpResponseHeader(u, c); tikhomirov@178: } tikhomirov@178: InputStreamReader is = new InputStreamReader(c.getInputStream(), "US-ASCII"); tikhomirov@178: StreamTokenizer st = new StreamTokenizer(is); tikhomirov@428: st.ordinaryChars('0', '9'); // wordChars performs |, hence need to 0 first tikhomirov@178: st.wordChars('0', '9'); tikhomirov@178: st.eolIsSignificant(false); tikhomirov@178: LinkedList parseResult = new LinkedList(); tikhomirov@178: while (st.nextToken() != StreamTokenizer.TT_EOF) { tikhomirov@178: parseResult.add(Nodeid.fromAscii(st.sval)); tikhomirov@178: } tikhomirov@178: return parseResult; tikhomirov@178: } catch (MalformedURLException ex) { tikhomirov@215: throw new HgRemoteConnectionException("Bad URL", ex).setRemoteCommand("heads").setServerInfo(getLocation()); tikhomirov@178: } catch (IOException ex) { tikhomirov@215: throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand("heads").setServerInfo(getLocation()); tikhomirov@645: } finally { tikhomirov@645: if (c != null) { tikhomirov@645: c.disconnect(); tikhomirov@645: } tikhomirov@178: } tikhomirov@171: } tikhomirov@171: tikhomirov@215: public List between(Nodeid tip, Nodeid base) throws HgRemoteConnectionException { tikhomirov@177: Range r = new Range(base, tip); tikhomirov@177: // XXX shall handle errors like no range key in the returned map, not sure how. tikhomirov@177: return between(Collections.singletonList(r)).get(r); tikhomirov@177: } tikhomirov@177: tikhomirov@177: /** tikhomirov@177: * @param ranges tikhomirov@177: * @return map, where keys are input instances, values are corresponding server reply tikhomirov@215: * @throws HgRemoteConnectionException tikhomirov@177: */ tikhomirov@215: public Map> between(Collection ranges) throws HgRemoteConnectionException { tikhomirov@177: if (ranges.isEmpty()) { tikhomirov@177: return Collections.emptyMap(); tikhomirov@177: } tikhomirov@177: // if fact, shall do other way round, this method shall send tikhomirov@177: LinkedHashMap> rv = new LinkedHashMap>(ranges.size() * 4 / 3); tikhomirov@177: StringBuilder sb = new StringBuilder(20 + ranges.size() * 82); tikhomirov@177: sb.append("pairs="); tikhomirov@177: for (Range r : ranges) { tikhomirov@685: r.append(sb); tikhomirov@177: sb.append('+'); tikhomirov@177: } tikhomirov@177: if (sb.charAt(sb.length() - 1) == '+') { tikhomirov@177: // strip last space tikhomirov@177: sb.setLength(sb.length() - 1); tikhomirov@177: } tikhomirov@645: HttpURLConnection c = null; tikhomirov@176: try { tikhomirov@177: boolean usePOST = ranges.size() > 3; tikhomirov@177: URL u = new URL(url, url.getPath() + "?cmd=between" + (usePOST ? "" : '&' + sb.toString())); tikhomirov@645: c = setupConnection(u.openConnection()); tikhomirov@177: if (usePOST) { tikhomirov@177: c.setRequestMethod("POST"); tikhomirov@177: c.setRequestProperty("Content-Length", String.valueOf(sb.length()/*nodeids are ASCII, bytes == characters */)); tikhomirov@177: c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); tikhomirov@177: c.setDoOutput(true); tikhomirov@177: c.connect(); tikhomirov@177: OutputStream os = c.getOutputStream(); tikhomirov@177: os.write(sb.toString().getBytes()); tikhomirov@177: os.flush(); tikhomirov@177: os.close(); tikhomirov@177: } else { tikhomirov@177: c.connect(); tikhomirov@177: } tikhomirov@178: if (debug) { tikhomirov@178: System.out.printf("%d ranges, method:%s \n", ranges.size(), c.getRequestMethod()); tikhomirov@178: dumpResponseHeader(u, c); tikhomirov@176: } tikhomirov@177: InputStreamReader is = new InputStreamReader(c.getInputStream(), "US-ASCII"); tikhomirov@176: StreamTokenizer st = new StreamTokenizer(is); tikhomirov@176: st.ordinaryChars('0', '9'); tikhomirov@176: st.wordChars('0', '9'); tikhomirov@177: st.eolIsSignificant(true); tikhomirov@177: Iterator rangeItr = ranges.iterator(); tikhomirov@177: LinkedList currRangeList = null; tikhomirov@177: Range currRange = null; tikhomirov@177: boolean possiblyEmptyNextLine = true; tikhomirov@176: while (st.nextToken() != StreamTokenizer.TT_EOF) { tikhomirov@177: if (st.ttype == StreamTokenizer.TT_EOL) { tikhomirov@177: if (possiblyEmptyNextLine) { tikhomirov@177: // newline follows newline; tikhomirov@177: assert currRange == null; tikhomirov@177: assert currRangeList == null; tikhomirov@177: if (!rangeItr.hasNext()) { tikhomirov@423: throw new HgInvalidStateException("Internal error"); // TODO revisit-1.1 tikhomirov@177: } tikhomirov@177: rv.put(rangeItr.next(), Collections.emptyList()); tikhomirov@177: } else { tikhomirov@177: if (currRange == null || currRangeList == null) { tikhomirov@423: throw new HgInvalidStateException("Internal error"); // TODO revisit-1.1 tikhomirov@177: } tikhomirov@177: // indicate next range value is needed tikhomirov@177: currRange = null; tikhomirov@177: currRangeList = null; tikhomirov@177: possiblyEmptyNextLine = true; tikhomirov@177: } tikhomirov@177: } else { tikhomirov@177: possiblyEmptyNextLine = false; tikhomirov@177: if (currRange == null) { tikhomirov@177: if (!rangeItr.hasNext()) { tikhomirov@423: throw new HgInvalidStateException("Internal error"); // TODO revisit-1.1 tikhomirov@177: } tikhomirov@177: currRange = rangeItr.next(); tikhomirov@177: currRangeList = new LinkedList(); tikhomirov@177: rv.put(currRange, currRangeList); tikhomirov@177: } tikhomirov@177: Nodeid nid = Nodeid.fromAscii(st.sval); tikhomirov@177: currRangeList.addLast(nid); tikhomirov@177: } tikhomirov@176: } tikhomirov@176: is.close(); tikhomirov@176: return rv; tikhomirov@176: } catch (MalformedURLException ex) { tikhomirov@215: throw new HgRemoteConnectionException("Bad URL", ex).setRemoteCommand("between").setServerInfo(getLocation()); tikhomirov@176: } catch (IOException ex) { tikhomirov@215: throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand("between").setServerInfo(getLocation()); tikhomirov@645: } finally { tikhomirov@645: if (c != null) { tikhomirov@645: c.disconnect(); tikhomirov@645: } tikhomirov@176: } tikhomirov@176: } tikhomirov@176: tikhomirov@215: public List branches(List nodes) throws HgRemoteConnectionException { tikhomirov@645: StringBuilder sb = appendNodeidListArgument("nodes", nodes, null); tikhomirov@645: HttpURLConnection c = null; tikhomirov@178: try { tikhomirov@178: URL u = new URL(url, url.getPath() + "?cmd=branches&" + sb.toString()); tikhomirov@645: c = setupConnection(u.openConnection()); tikhomirov@178: c.connect(); tikhomirov@178: if (debug) { tikhomirov@178: dumpResponseHeader(u, c); tikhomirov@178: } tikhomirov@178: InputStreamReader is = new InputStreamReader(c.getInputStream(), "US-ASCII"); tikhomirov@178: StreamTokenizer st = new StreamTokenizer(is); tikhomirov@178: st.ordinaryChars('0', '9'); tikhomirov@178: st.wordChars('0', '9'); tikhomirov@178: st.eolIsSignificant(false); tikhomirov@178: ArrayList parseResult = new ArrayList(nodes.size() * 4); tikhomirov@178: while (st.nextToken() != StreamTokenizer.TT_EOF) { tikhomirov@178: parseResult.add(Nodeid.fromAscii(st.sval)); tikhomirov@178: } tikhomirov@178: if (parseResult.size() != nodes.size() * 4) { tikhomirov@215: throw new HgRemoteConnectionException(String.format("Bad number of nodeids in result (shall be factor 4), expected %d, got %d", nodes.size()*4, parseResult.size())); tikhomirov@178: } tikhomirov@178: ArrayList rv = new ArrayList(nodes.size()); tikhomirov@178: for (int i = 0; i < nodes.size(); i++) { tikhomirov@178: RemoteBranch rb = new RemoteBranch(parseResult.get(i*4), parseResult.get(i*4 + 1), parseResult.get(i*4 + 2), parseResult.get(i*4 + 3)); tikhomirov@178: rv.add(rb); tikhomirov@178: } tikhomirov@178: return rv; tikhomirov@178: } catch (MalformedURLException ex) { tikhomirov@215: throw new HgRemoteConnectionException("Bad URL", ex).setRemoteCommand("branches").setServerInfo(getLocation()); tikhomirov@178: } catch (IOException ex) { tikhomirov@215: throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand("branches").setServerInfo(getLocation()); tikhomirov@645: } finally { tikhomirov@645: if (c != null) { tikhomirov@645: c.disconnect(); tikhomirov@645: } tikhomirov@178: } tikhomirov@171: } tikhomirov@170: tikhomirov@186: /* tikhomirov@202: * XXX need to describe behavior when roots arg is empty; our RepositoryComparator code currently returns empty lists when tikhomirov@202: * no common elements found, which in turn means we need to query changes starting with NULL nodeid. tikhomirov@202: * tikhomirov@186: * WireProtocol wiki: roots = a list of the latest nodes on every service side changeset branch that both the client and server know about. tikhomirov@186: * tikhomirov@186: * Perhaps, shall be named 'changegroup' tikhomirov@186: tikhomirov@186: * Changegroup: tikhomirov@186: * http://mercurial.selenic.com/wiki/Merge tikhomirov@186: * http://mercurial.selenic.com/wiki/WireProtocol tikhomirov@186: * tikhomirov@186: * according to latter, bundleformat data is sent through zlib tikhomirov@186: * (there's no header like HG10?? with the server output, though, tikhomirov@186: * as one may expect according to http://mercurial.selenic.com/wiki/BundleFormat) tikhomirov@186: */ tikhomirov@425: public HgBundle getChanges(List roots) throws HgRemoteConnectionException, HgRuntimeException { tikhomirov@202: List _roots = roots.isEmpty() ? Collections.singletonList(Nodeid.NULL) : roots; tikhomirov@645: StringBuilder sb = appendNodeidListArgument("roots", _roots, null); tikhomirov@645: HttpURLConnection c = null; tikhomirov@179: try { tikhomirov@179: URL u = new URL(url, url.getPath() + "?cmd=changegroup&" + sb.toString()); tikhomirov@645: c = setupConnection(u.openConnection()); tikhomirov@179: c.connect(); tikhomirov@179: if (debug) { tikhomirov@179: dumpResponseHeader(u, c); tikhomirov@179: } tikhomirov@179: File tf = writeBundle(c.getInputStream(), false, "HG10GZ" /*didn't see any other that zip*/); tikhomirov@190: if (debug) { tikhomirov@210: System.out.printf("Wrote bundle %s for roots %s\n", tf, sb); tikhomirov@190: } tikhomirov@186: return getLookupHelper().loadBundle(tf); tikhomirov@215: } catch (MalformedURLException ex) { // XXX in fact, this exception might be better to be re-thrown as RuntimeEx, tikhomirov@215: // as there's little user can do about this issue (URLs are constructed by our code) tikhomirov@215: throw new HgRemoteConnectionException("Bad URL", ex).setRemoteCommand("changegroup").setServerInfo(getLocation()); tikhomirov@179: } catch (IOException ex) { tikhomirov@215: throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand("changegroup").setServerInfo(getLocation()); tikhomirov@425: } catch (HgRepositoryNotFoundException ex) { tikhomirov@425: throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand("changegroup").setServerInfo(getLocation()); tikhomirov@645: } finally { tikhomirov@645: if (c != null) { tikhomirov@645: c.disconnect(); tikhomirov@645: } tikhomirov@645: } tikhomirov@645: } tikhomirov@645: tikhomirov@646: public void unbundle(HgBundle bundle, List remoteHeads) throws HgRemoteConnectionException, HgRuntimeException { tikhomirov@646: if (remoteHeads == null) { tikhomirov@645: // TODO collect heads from bundle: tikhomirov@645: // bundle.inspectChangelog(new HeadCollector(for each c : if collected has c.p1 or c.p2, remove them. Add c)) tikhomirov@646: // or get from remote server??? tikhomirov@645: throw Internals.notImplemented(); tikhomirov@645: } tikhomirov@646: StringBuilder sb = appendNodeidListArgument("heads", remoteHeads, null); tikhomirov@645: tikhomirov@645: HttpURLConnection c = null; tikhomirov@673: DataSerializer.DataSource bundleData = BundleSerializer.newInstance(sessionContext, bundle); tikhomirov@645: try { tikhomirov@645: URL u = new URL(url, url.getPath() + "?cmd=unbundle&" + sb.toString()); tikhomirov@645: c = setupConnection(u.openConnection()); tikhomirov@645: c.setRequestMethod("POST"); tikhomirov@645: c.setRequestProperty("Content-Length", String.valueOf(bundleData.serializeLength())); tikhomirov@645: c.setRequestProperty("Content-Type", "application/mercurial-0.1"); tikhomirov@645: c.setDoOutput(true); tikhomirov@645: c.connect(); tikhomirov@645: OutputStream os = c.getOutputStream(); tikhomirov@645: bundleData.serialize(new OutputStreamSerializer(os)); tikhomirov@645: os.flush(); tikhomirov@645: os.close(); tikhomirov@645: if (debug) { tikhomirov@645: dumpResponseHeader(u, c); tikhomirov@645: dumpResponse(c); tikhomirov@645: } tikhomirov@646: checkResponseOk(c, "Push", "unbundle"); tikhomirov@645: } catch (MalformedURLException ex) { tikhomirov@645: throw new HgRemoteConnectionException("Bad URL", ex).setRemoteCommand("unbundle").setServerInfo(getLocation()); tikhomirov@645: } catch (IOException ex) { tikhomirov@645: throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand("unbundle").setServerInfo(getLocation()); tikhomirov@645: } catch (HgIOException ex) { tikhomirov@645: throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand("unbundle").setServerInfo(getLocation()); tikhomirov@645: } finally { tikhomirov@645: if (c != null) { tikhomirov@645: c.disconnect(); tikhomirov@645: } tikhomirov@179: } tikhomirov@170: } tikhomirov@186: tikhomirov@649: public Bookmarks getBookmarks() throws HgRemoteConnectionException, HgRuntimeException { tikhomirov@646: final String actionName = "Get remote bookmarks"; tikhomirov@646: final List> values = listkeys("bookmarks", actionName); tikhomirov@646: ArrayList> rv = new ArrayList>(); tikhomirov@646: for (Pair l : values) { tikhomirov@646: if (l.second().length() != Nodeid.SIZE_ASCII) { tikhomirov@646: sessionContext.getLog().dump(getClass(), Severity.Warn, "%s: bad nodeid '%s', ignored", actionName, l.second()); tikhomirov@646: continue; tikhomirov@646: } tikhomirov@646: Nodeid n = Nodeid.fromAscii(l.second()); tikhomirov@646: String bm = new String(l.first()); tikhomirov@646: rv.add(new Pair(bm, n)); tikhomirov@646: } tikhomirov@649: return new Bookmarks(rv); tikhomirov@646: } tikhomirov@646: tikhomirov@652: public Outcome updateBookmark(String name, Nodeid oldRev, Nodeid newRev) throws HgRemoteConnectionException, HgRuntimeException { tikhomirov@652: initCapabilities(); tikhomirov@652: if (!remoteCapabilities.contains("pushkey")) { tikhomirov@652: return new Outcome(Failure, "Server doesn't support pushkey protocol"); tikhomirov@646: } tikhomirov@652: if (pushkey("Update remote bookmark", "bookmarks", name, oldRev.toString(), newRev.toString())) { tikhomirov@652: return new Outcome(Success, String.format("Bookmark %s updated to %s", name, newRev.shortNotation())); tikhomirov@652: } tikhomirov@652: return new Outcome(Failure, String.format("Bookmark update (%s: %s -> %s) failed", name, oldRev.shortNotation(), newRev.shortNotation())); tikhomirov@646: } tikhomirov@646: tikhomirov@649: public Phases getPhases() throws HgRemoteConnectionException, HgRuntimeException { tikhomirov@649: initCapabilities(); tikhomirov@649: if (!remoteCapabilities.contains("pushkey")) { tikhomirov@649: // old server defaults to publishing tikhomirov@649: return new Phases(true, Collections.emptyList()); tikhomirov@649: } tikhomirov@646: final List> values = listkeys("phases", "Get remote phases"); tikhomirov@650: boolean publishing = false; tikhomirov@649: ArrayList draftRoots = new ArrayList(); tikhomirov@646: for (Pair l : values) { tikhomirov@649: if ("publishing".equalsIgnoreCase(l.first())) { tikhomirov@649: publishing = Boolean.parseBoolean(l.second()); tikhomirov@649: continue; tikhomirov@649: } tikhomirov@649: Nodeid root = Nodeid.fromAscii(l.first()); tikhomirov@649: int ph = Integer.parseInt(l.second()); tikhomirov@649: if (ph == HgPhase.Draft.mercurialOrdinal()) { tikhomirov@649: draftRoots.add(root); tikhomirov@649: } else { tikhomirov@649: assert false; tikhomirov@649: sessionContext.getLog().dump(getClass(), Severity.Error, "Unexpected phase value %d for revision %s", ph, root); tikhomirov@649: } tikhomirov@646: } tikhomirov@649: return new Phases(publishing, draftRoots); tikhomirov@646: } tikhomirov@646: tikhomirov@649: public Outcome updatePhase(HgPhase from, HgPhase to, Nodeid n) throws HgRemoteConnectionException, HgRuntimeException { tikhomirov@650: initCapabilities(); tikhomirov@650: if (!remoteCapabilities.contains("pushkey")) { tikhomirov@650: return new Outcome(Failure, "Server doesn't support pushkey protocol"); tikhomirov@650: } tikhomirov@652: if (pushkey("Update remote phases", "phases", n.toString(), String.valueOf(from.mercurialOrdinal()), String.valueOf(to.mercurialOrdinal()))) { tikhomirov@650: return new Outcome(Success, String.format("Phase of %s updated to %s", n.shortNotation(), to.name())); tikhomirov@649: } tikhomirov@650: return new Outcome(Failure, String.format("Phase update (%s: %s -> %s) failed", n.shortNotation(), from.name(), to.name())); tikhomirov@649: } tikhomirov@649: tikhomirov@203: @Override tikhomirov@203: public String toString() { tikhomirov@203: return getClass().getSimpleName() + '[' + getLocation() + ']'; tikhomirov@203: } tikhomirov@646: tikhomirov@646: tikhomirov@646: private void initCapabilities() throws HgRemoteConnectionException { tikhomirov@646: if (remoteCapabilities == null) { tikhomirov@646: remoteCapabilities = new HashSet(); tikhomirov@646: // say hello to server, check response tikhomirov@646: try { tikhomirov@646: URL u = new URL(url, url.getPath() + "?cmd=hello"); tikhomirov@646: HttpURLConnection c = setupConnection(u.openConnection()); tikhomirov@646: c.connect(); tikhomirov@646: if (debug) { tikhomirov@646: dumpResponseHeader(u, c); tikhomirov@646: } tikhomirov@646: BufferedReader r = new BufferedReader(new InputStreamReader(c.getInputStream(), "US-ASCII")); tikhomirov@646: String line = r.readLine(); tikhomirov@646: c.disconnect(); tikhomirov@646: final String capsPrefix = "capabilities:"; tikhomirov@646: if (line == null || !line.startsWith(capsPrefix)) { tikhomirov@646: // for whatever reason, some servers do not respond to hello command (e.g. svnkit) tikhomirov@646: // but respond to 'capabilities' instead. Try it. tikhomirov@646: // TODO [post-1.0] tests needed tikhomirov@646: u = new URL(url, url.getPath() + "?cmd=capabilities"); tikhomirov@646: c = setupConnection(u.openConnection()); tikhomirov@646: c.connect(); tikhomirov@646: if (debug) { tikhomirov@646: dumpResponseHeader(u, c); tikhomirov@646: } tikhomirov@646: r = new BufferedReader(new InputStreamReader(c.getInputStream(), "US-ASCII")); tikhomirov@646: line = r.readLine(); tikhomirov@646: c.disconnect(); tikhomirov@646: if (line == null || line.trim().length() == 0) { tikhomirov@646: return; tikhomirov@646: } tikhomirov@646: } else { tikhomirov@646: line = line.substring(capsPrefix.length()).trim(); tikhomirov@646: } tikhomirov@646: String[] caps = line.split("\\s"); tikhomirov@646: remoteCapabilities.addAll(Arrays.asList(caps)); tikhomirov@646: c.disconnect(); tikhomirov@646: } catch (MalformedURLException ex) { tikhomirov@646: throw new HgRemoteConnectionException("Bad URL", ex).setRemoteCommand("hello").setServerInfo(getLocation()); tikhomirov@646: } catch (IOException ex) { tikhomirov@646: throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand("hello").setServerInfo(getLocation()); tikhomirov@646: } tikhomirov@646: } tikhomirov@646: } tikhomirov@203: tikhomirov@186: private HgLookup getLookupHelper() { tikhomirov@186: if (lookupHelper == null) { tikhomirov@295: lookupHelper = new HgLookup(sessionContext); tikhomirov@186: } tikhomirov@186: return lookupHelper; tikhomirov@186: } tikhomirov@646: tikhomirov@646: private List> listkeys(String namespace, String actionName) throws HgRemoteConnectionException, HgRuntimeException { tikhomirov@646: HttpURLConnection c = null; tikhomirov@646: try { tikhomirov@646: URL u = new URL(url, url.getPath() + "?cmd=listkeys&namespace=" + namespace); tikhomirov@646: c = setupConnection(u.openConnection()); tikhomirov@646: c.connect(); tikhomirov@646: if (debug) { tikhomirov@646: dumpResponseHeader(u, c); tikhomirov@646: } tikhomirov@646: checkResponseOk(c, actionName, "listkeys"); tikhomirov@646: ArrayList> rv = new ArrayList>(); tikhomirov@685: // output of listkeys is encoded with UTF-8 tikhomirov@646: BufferedReader r = new BufferedReader(new InputStreamReader(c.getInputStream(), EncodingHelper.getUTF8())); tikhomirov@646: String l; tikhomirov@646: while ((l = r.readLine()) != null) { tikhomirov@646: int sep = l.indexOf('\t'); tikhomirov@646: if (sep == -1) { tikhomirov@646: sessionContext.getLog().dump(getClass(), Severity.Warn, "%s: bad line '%s', ignored", actionName, l); tikhomirov@646: continue; tikhomirov@646: } tikhomirov@646: rv.add(new Pair(l.substring(0, sep), l.substring(sep+1))); tikhomirov@646: } tikhomirov@646: r.close(); tikhomirov@646: return rv; tikhomirov@646: } catch (MalformedURLException ex) { tikhomirov@646: throw new HgRemoteConnectionException("Bad URL", ex).setRemoteCommand("listkeys").setServerInfo(getLocation()); tikhomirov@646: } catch (IOException ex) { tikhomirov@646: throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand("listkeys").setServerInfo(getLocation()); tikhomirov@646: } finally { tikhomirov@646: if (c != null) { tikhomirov@646: c.disconnect(); tikhomirov@646: } tikhomirov@646: } tikhomirov@646: } tikhomirov@176: tikhomirov@652: private boolean pushkey(String opName, String namespace, String key, String oldValue, String newValue) throws HgRemoteConnectionException, HgRuntimeException { tikhomirov@649: HttpURLConnection c = null; tikhomirov@649: try { tikhomirov@650: final String p = String.format("%s?cmd=pushkey&namespace=%s&key=%s&old=%s&new=%s", url.getPath(), namespace, key, oldValue, newValue); tikhomirov@649: URL u = new URL(url, p); tikhomirov@649: c = setupConnection(u.openConnection()); tikhomirov@650: c.setRequestMethod("POST"); tikhomirov@649: c.connect(); tikhomirov@649: if (debug) { tikhomirov@649: dumpResponseHeader(u, c); tikhomirov@649: } tikhomirov@652: checkResponseOk(c, opName, "pushkey"); tikhomirov@649: final InputStream is = c.getInputStream(); tikhomirov@649: int rv = is.read(); tikhomirov@649: is.close(); tikhomirov@649: return rv == '1'; tikhomirov@649: } catch (MalformedURLException ex) { tikhomirov@649: throw new HgRemoteConnectionException("Bad URL", ex).setRemoteCommand("pushkey").setServerInfo(getLocation()); tikhomirov@649: } catch (IOException ex) { tikhomirov@649: throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand("pushkey").setServerInfo(getLocation()); tikhomirov@649: } finally { tikhomirov@649: if (c != null) { tikhomirov@649: c.disconnect(); tikhomirov@649: } tikhomirov@649: } tikhomirov@649: } tikhomirov@649: tikhomirov@646: private void checkResponseOk(HttpURLConnection c, String opName, String remoteCmd) throws HgRemoteConnectionException, IOException { tikhomirov@646: if (c.getResponseCode() != 200) { tikhomirov@646: String m = c.getResponseMessage() == null ? "unknown reason" : c.getResponseMessage(); tikhomirov@646: String em = String.format("%s failed: %s (HTTP error:%d)", opName, m, c.getResponseCode()); tikhomirov@646: throw new HgRemoteConnectionException(em).setRemoteCommand(remoteCmd).setServerInfo(getLocation()); tikhomirov@646: } tikhomirov@646: } tikhomirov@646: tikhomirov@177: private HttpURLConnection setupConnection(URLConnection urlConnection) { tikhomirov@645: urlConnection.setRequestProperty("User-Agent", "hg4j/1.0.0"); tikhomirov@176: urlConnection.addRequestProperty("Accept", "application/mercurial-0.1"); tikhomirov@176: if (authInfo != null) { tikhomirov@176: urlConnection.addRequestProperty("Authorization", "Basic " + authInfo); tikhomirov@176: } tikhomirov@176: if (sslContext != null) { tikhomirov@176: ((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslContext.getSocketFactory()); tikhomirov@176: } tikhomirov@177: return (HttpURLConnection) urlConnection; tikhomirov@176: } tikhomirov@645: tikhomirov@645: private StringBuilder appendNodeidListArgument(String key, List values, StringBuilder sb) { tikhomirov@645: if (sb == null) { tikhomirov@645: sb = new StringBuilder(20 + values.size() * 41); tikhomirov@645: } tikhomirov@645: sb.append(key); tikhomirov@645: sb.append('='); tikhomirov@645: for (Nodeid n : values) { tikhomirov@645: sb.append(n.toString()); tikhomirov@645: sb.append('+'); tikhomirov@645: } tikhomirov@645: if (sb.charAt(sb.length() - 1) == '+') { tikhomirov@645: // strip last space tikhomirov@645: sb.setLength(sb.length() - 1); tikhomirov@645: } tikhomirov@645: return sb; tikhomirov@645: } tikhomirov@171: tikhomirov@178: private void dumpResponseHeader(URL u, HttpURLConnection c) { tikhomirov@178: System.out.printf("Query (%d bytes):%s\n", u.getQuery().length(), u.getQuery()); tikhomirov@178: System.out.println("Response headers:"); tikhomirov@178: final Map> headerFields = c.getHeaderFields(); tikhomirov@178: for (String s : headerFields.keySet()) { tikhomirov@178: System.out.printf("%s: %s\n", s, c.getHeaderField(s)); tikhomirov@178: } tikhomirov@178: } tikhomirov@179: tikhomirov@645: private void dumpResponse(HttpURLConnection c) throws IOException { tikhomirov@645: if (c.getContentLength() > 0) { tikhomirov@645: final Object content = c.getContent(); tikhomirov@645: System.out.println(content); tikhomirov@645: } tikhomirov@645: } tikhomirov@645: tikhomirov@179: private static File writeBundle(InputStream is, boolean decompress, String header) throws IOException { tikhomirov@179: InputStream zipStream = decompress ? new InflaterInputStream(is) : is; tikhomirov@646: File tf = File.createTempFile("hg4j-bundle-", null); tikhomirov@179: FileOutputStream fos = new FileOutputStream(tf); tikhomirov@179: fos.write(header.getBytes()); tikhomirov@179: int r; tikhomirov@179: byte[] buf = new byte[8*1024]; tikhomirov@179: while ((r = zipStream.read(buf)) != -1) { tikhomirov@179: fos.write(buf, 0, r); tikhomirov@179: } tikhomirov@179: fos.close(); tikhomirov@179: zipStream.close(); tikhomirov@179: return tf; tikhomirov@179: } tikhomirov@179: tikhomirov@178: tikhomirov@176: public static final class Range { tikhomirov@176: /** tikhomirov@176: * Root of the range, earlier revision tikhomirov@176: */ tikhomirov@176: public final Nodeid start; tikhomirov@176: /** tikhomirov@176: * Head of the range, later revision. tikhomirov@176: */ tikhomirov@176: public final Nodeid end; tikhomirov@176: tikhomirov@176: /** tikhomirov@176: * @param from - root/base revision tikhomirov@176: * @param to - head/tip revision tikhomirov@176: */ tikhomirov@176: public Range(Nodeid from, Nodeid to) { tikhomirov@176: start = from; tikhomirov@176: end = to; tikhomirov@176: } tikhomirov@685: tikhomirov@685: /** tikhomirov@685: * Append this range as pair of values 'end-start' to the supplied buffer and return the buffer. tikhomirov@685: */ tikhomirov@685: public StringBuilder append(StringBuilder sb) { tikhomirov@685: sb.append(end.toString()); tikhomirov@685: sb.append('-'); tikhomirov@685: sb.append(start.toString()); tikhomirov@685: return sb; tikhomirov@685: } tikhomirov@176: } tikhomirov@184: tikhomirov@171: public static final class RemoteBranch { tikhomirov@171: public final Nodeid head, root, p1, p2; tikhomirov@171: tikhomirov@171: public RemoteBranch(Nodeid h, Nodeid r, Nodeid parent1, Nodeid parent2) { tikhomirov@171: head = h; tikhomirov@171: root = r; tikhomirov@171: p1 = parent1; tikhomirov@171: p2 = parent2; tikhomirov@171: } tikhomirov@171: tikhomirov@171: @Override tikhomirov@171: public boolean equals(Object obj) { tikhomirov@171: if (this == obj) { tikhomirov@171: return true; tikhomirov@171: } tikhomirov@171: if (false == obj instanceof RemoteBranch) { tikhomirov@171: return false; tikhomirov@171: } tikhomirov@171: RemoteBranch o = (RemoteBranch) obj; tikhomirov@274: // in fact, p1 and p2 are not supposed to be null, ever (at least for RemoteBranch created from server output) tikhomirov@171: 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)); tikhomirov@171: } tikhomirov@171: } tikhomirov@649: tikhomirov@649: public static final class Bookmarks implements Iterable> { tikhomirov@649: private final List> bm; tikhomirov@649: tikhomirov@649: private Bookmarks(List> bookmarks) { tikhomirov@649: bm = bookmarks; tikhomirov@649: } tikhomirov@649: tikhomirov@649: public Iterator> iterator() { tikhomirov@649: return bm.iterator(); tikhomirov@649: } tikhomirov@649: } tikhomirov@649: tikhomirov@649: public static final class Phases { tikhomirov@649: private final boolean pub; tikhomirov@649: private final List droots; tikhomirov@649: tikhomirov@649: private Phases(boolean publishing, List draftRoots) { tikhomirov@649: pub = publishing; tikhomirov@649: droots = draftRoots; tikhomirov@649: } tikhomirov@649: tikhomirov@649: /** tikhomirov@649: * Non-publishing servers may (shall?) respond with a list of draft roots. tikhomirov@649: * This method doesn't make sense when {@link #isPublishingServer()} is true tikhomirov@649: * tikhomirov@649: * @return list of draft roots on remote server tikhomirov@649: */ tikhomirov@649: public List draftRoots() { tikhomirov@649: return droots; tikhomirov@649: } tikhomirov@649: tikhomirov@649: /** tikhomirov@649: * @return true if revisions on remote server shall be deemed published (either tikhomirov@649: * old server w/o explicit setting, or a new one with phases.publish == true) tikhomirov@649: */ tikhomirov@649: public boolean isPublishingServer() { tikhomirov@649: return pub; tikhomirov@649: } tikhomirov@649: } tikhomirov@170: }