annotate src/org/tmatesoft/hg/internal/remote/SshConnector.java @ 698:822f3a83ff57

in, out and clone tests pass for ssh repositories. Infrastructure to decouple HgRemoteRepository from specific Connector implementation
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 06 Aug 2013 21:18:33 +0200
parents 24f4efedc9d5
children a483b2b68a2e
rev   line source
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2013 TMate Software Ltd
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal.remote;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.BufferedReader;
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
20 import java.io.ByteArrayInputStream;
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.Closeable;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.io.EOFException;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.io.File;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.io.FilterInputStream;
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
25 import java.io.FilterOutputStream;
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import java.io.IOException;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import java.io.InputStream;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 import java.io.InputStreamReader;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 import java.io.OutputStream;
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
30 import java.io.SequenceInputStream;
698
822f3a83ff57 in, out and clone tests pass for ssh repositories. Infrastructure to decouple HgRemoteRepository from specific Connector implementation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 697
diff changeset
31 import java.net.URI;
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 import java.util.ArrayList;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 import java.util.Collection;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 import java.util.Collections;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 import java.util.List;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 import org.tmatesoft.hg.core.HgRemoteConnectionException;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 import org.tmatesoft.hg.core.Nodeid;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 import org.tmatesoft.hg.core.SessionContext;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 import org.tmatesoft.hg.repo.HgRemoteRepository.Range;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 import org.tmatesoft.hg.repo.HgRuntimeException;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 import org.tmatesoft.hg.util.LogFacility.Severity;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43
696
5b5d199e2eb3 Replaced ganymed ssh2 library with trilead
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 687
diff changeset
44 import com.trilead.ssh2.Connection;
5b5d199e2eb3 Replaced ganymed ssh2 library with trilead
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 687
diff changeset
45 import com.trilead.ssh2.ConnectionInfo;
5b5d199e2eb3 Replaced ganymed ssh2 library with trilead
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 687
diff changeset
46 import com.trilead.ssh2.Session;
5b5d199e2eb3 Replaced ganymed ssh2 library with trilead
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 687
diff changeset
47 import com.trilead.ssh2.StreamGobbler;
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 /**
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 * Remote repository via SSH
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 *
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 * @author Artem Tikhomirov
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 * @author TMate Software Ltd.
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 */
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
55 public class SshConnector implements Connector {
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 private SessionContext sessionCtx;
698
822f3a83ff57 in, out and clone tests pass for ssh repositories. Infrastructure to decouple HgRemoteRepository from specific Connector implementation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 697
diff changeset
57 private URI uri;
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 private Connection conn;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 private Session session;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 private int sessionUse;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 private StreamGobbler remoteErr, remoteOut;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 private OutputStream remoteIn;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64
698
822f3a83ff57 in, out and clone tests pass for ssh repositories. Infrastructure to decouple HgRemoteRepository from specific Connector implementation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 697
diff changeset
65 public void init(URI uri, SessionContext sessionContext, Object globalConfig) throws HgRuntimeException {
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 sessionCtx = sessionContext;
698
822f3a83ff57 in, out and clone tests pass for ssh repositories. Infrastructure to decouple HgRemoteRepository from specific Connector implementation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 697
diff changeset
67 this.uri = uri;
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
68 }
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
69
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
70 public void connect() throws HgRemoteConnectionException, HgRuntimeException {
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 try {
698
822f3a83ff57 in, out and clone tests pass for ssh repositories. Infrastructure to decouple HgRemoteRepository from specific Connector implementation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 697
diff changeset
72 conn = new Connection(uri.getHost(), uri.getPort() == -1 ? 22 : uri.getPort());
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 conn.connect();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 } catch (IOException ex) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 throw new HgRemoteConnectionException("Failed to establish connection");
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 try {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 conn.authenticateWithPublicKey(System.getProperty("user.name"), new File(System.getProperty("user.home"), ".ssh/id_rsa"), null);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 ConnectionInfo ci = conn.getConnectionInfo();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 System.out.printf("%s %s %s %d %s %s %s\n", ci.clientToServerCryptoAlgorithm, ci.clientToServerMACAlgorithm, ci.keyExchangeAlgorithm, ci.keyExchangeCounter, ci.serverHostKeyAlgorithm, ci.serverToClientCryptoAlgorithm, ci.serverToClientMACAlgorithm);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 } catch (IOException ex) {
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
82 throw new HgRemoteConnectionException("Failed to authenticate", ex).setServerInfo(getServerLocation());
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 public void disconnect() throws HgRemoteConnectionException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 if (session != null) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 forceSessionClose();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 if (conn != null) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 conn.close();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 conn = null;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 public void sessionBegin() throws HgRemoteConnectionException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 if (sessionUse > 0) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 assert session != null;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 sessionUse++;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 return;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 try {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 session = conn.openSession();
698
822f3a83ff57 in, out and clone tests pass for ssh repositories. Infrastructure to decouple HgRemoteRepository from specific Connector implementation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 697
diff changeset
104 final String path = uri.getPath();
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
105 session.execCommand(String.format("hg -R %s serve --stdio", path.charAt(0) == '/' ? path.substring(1) : path));
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 remoteErr = new StreamGobbler(session.getStderr());
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 remoteOut = new StreamGobbler(session.getStdout());
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
108 remoteIn = session.getStdin();
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 sessionUse = 1;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 } catch (IOException ex) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 throw new HgRemoteConnectionException("Failed to create ssh session", ex);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 public void sessionEnd() throws HgRemoteConnectionException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 assert sessionUse > 0;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 assert session != null;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 if (sessionUse > 1) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 sessionUse--;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 return;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 forceSessionClose();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
125 public String getServerLocation() {
698
822f3a83ff57 in, out and clone tests pass for ssh repositories. Infrastructure to decouple HgRemoteRepository from specific Connector implementation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 697
diff changeset
126 return uri.toString(); // FIXME
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
127 }
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
128
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
129 public String getCapabilities() throws HgRemoteConnectionException {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
130 try {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
131 consume(remoteOut);
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
132 consume(remoteErr);
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
133 remoteIn.write(CMD_HELLO.getBytes());
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
134 remoteIn.write('\n');
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
135 remoteIn.write(CMD_CAPABILITIES.getBytes()); // see http connector for details
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
136 remoteIn.write('\n');
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
137 remoteIn.write(CMD_HEADS.getBytes());
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
138 remoteIn.write('\n');
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
139 checkError();
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
140 int responseLen = readResponseLength();
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
141 checkError();
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
142 FilterStream s = new FilterStream(remoteOut, responseLen);
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
143 BufferedReader r = new BufferedReader(new InputStreamReader(s));
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
144 String line;
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
145 while ((line = r.readLine()) != null) {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
146 if (line.startsWith(CMD_CAPABILITIES) && line.length() > (CMD_CAPABILITIES.length()+1)) {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
147 line = line.substring(CMD_CAPABILITIES.length());
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
148 if (line.charAt(0) == ':') {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
149 return line.substring(CMD_CAPABILITIES.length() + 1);
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
150 }
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
151 }
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
152 }
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
153 r.close();
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
154 consume(remoteOut);
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
155 checkError();
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
156 return new String();
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
157 } catch (IOException ex) {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
158 throw new HgRemoteConnectionException("Failed to initiate dialog with server", ex).setRemoteCommand(CMD_HELLO).setServerInfo(getServerLocation());
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
159 }
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
160 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
161
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
162 public InputStream heads() throws HgRemoteConnectionException {
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
163 return executeCommand("heads", Collections.<Parameter>emptyList(), true);
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
164 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
165
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
166 public InputStream between(Collection<Range> ranges) throws HgRemoteConnectionException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
167 StringBuilder sb = new StringBuilder(ranges.size() * 82);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
168 for (Range r : ranges) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
169 r.append(sb).append(' ');
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
170 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
171 if (!ranges.isEmpty()) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
172 sb.setLength(sb.length() - 1);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
173 }
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
174 return executeCommand("between", Collections.singletonList(new Parameter("pairs", sb.toString())), true);
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
175 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
176
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
177 public InputStream branches(List<Nodeid> nodes) throws HgRemoteConnectionException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
178 String l = join(nodes, ' ');
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
179 return executeCommand("branches", Collections.singletonList(new Parameter("nodes", l)), true);
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
180 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
181
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
182 public InputStream changegroup(List<Nodeid> roots) throws HgRemoteConnectionException, HgRuntimeException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
183 String l = join(roots, ' ');
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
184 InputStream cg = executeCommand("changegroup", Collections.singletonList(new Parameter("roots", l)), false);
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
185 InputStream prefix = new ByteArrayInputStream("HG10UN".getBytes());
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
186 return new SequenceInputStream(prefix, cg);
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
187 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
188
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
189 public OutputStream unbundle(long outputLen, List<Nodeid> remoteHeads) throws HgRemoteConnectionException, HgRuntimeException {
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
190 String l = join(remoteHeads, ' ');
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
191 try {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
192 consume(remoteOut);
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
193 consume(remoteErr);
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
194 remoteIn.write(CMD_UNBUNDLE.getBytes());
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
195 remoteIn.write('\n');
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
196 writeParameters(Collections.singletonList(new Parameter("heads", l)));
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
197 checkError();
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
198 return new FilterOutputStream(remoteIn) {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
199 @Override
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
200 public void close() throws IOException {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
201 out.flush();
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
202 @SuppressWarnings("unused")
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
203 int responseLen = readResponseLength();
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
204 checkError();
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
205 // XXX perhaps, need to return responseLen to caller?
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
206 }
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
207 };
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
208 } catch (IOException ex) {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
209 throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand(CMD_UNBUNDLE).setServerInfo(getServerLocation());
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
210 }
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
211 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
212
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
213 public InputStream pushkey(String opName, String namespace, String key, String oldValue, String newValue) throws HgRemoteConnectionException, HgRuntimeException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
214 ArrayList<Parameter> p = new ArrayList<Parameter>();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
215 p.add(new Parameter("namespace", namespace));
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
216 p.add(new Parameter("key", key));
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
217 p.add(new Parameter("old", oldValue));
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
218 p.add(new Parameter("new", newValue));
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
219 return executeCommand("pushkey", p, true);
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
220 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
221
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
222 public InputStream listkeys(String namespace, String actionName) throws HgRemoteConnectionException, HgRuntimeException {
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
223 return executeCommand("listkeys", Collections.singletonList(new Parameter("namespace", namespace)), true);
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
224 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
225
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
226 private InputStream executeCommand(String cmd, List<Parameter> parameters, boolean expectResponseLength) throws HgRemoteConnectionException {
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
227 try {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
228 consume(remoteOut);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
229 consume(remoteErr);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
230 remoteIn.write(cmd.getBytes());
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
231 remoteIn.write('\n');
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
232 writeParameters(parameters);
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
233 checkError();
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
234 if (expectResponseLength) {
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
235 int responseLen = readResponseLength();
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
236 checkError();
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
237 return new FilterStream(remoteOut, responseLen);
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
238 } else {
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
239 return new FilterStream(remoteOut, Integer.MAX_VALUE);
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
240 }
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
241 } catch (IOException ex) {
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
242 throw new HgRemoteConnectionException("Communication failure", ex).setRemoteCommand(cmd).setServerInfo(getServerLocation());
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
243 }
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
244 }
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
245
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
246 private void writeParameters(List<Parameter> parameters) throws IOException {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
247 for (Parameter p : parameters) {
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
248 remoteIn.write(p.name().getBytes());
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
249 remoteIn.write(' ');
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
250 remoteIn.write(String.valueOf(p.size()).getBytes());
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
251 remoteIn.write('\n');
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
252 remoteIn.write(p.data());
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
253 remoteIn.write('\n');
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
254 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
255 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
256
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
257 private void consume(InputStream is) throws IOException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
258 while (is.available() > 0) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
259 is.read();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
260 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
261 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
262
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
263 private void checkError() throws IOException {
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
264 if (remoteErr.available() > 0) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
265 StringBuilder sb = new StringBuilder();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
266 int c;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
267 while ((c = remoteErr.read()) != -1) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
268 sb.append((char)c);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
269 }
687
9859fcea475d Towards ssh remote repositories: refactor HgRemoteRepository - move http related code to HttpConnector
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 685
diff changeset
270 throw new IOException(sb.toString());
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
271 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
272 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
273
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
274 private int readResponseLength() throws IOException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
275 int c;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
276 StringBuilder sb = new StringBuilder();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
277 while ((c = remoteOut.read()) != -1) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
278 if (c == '\n') {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
279 break;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
280 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
281 sb.append((char) c);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
282 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
283 if (c == -1) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
284 throw new EOFException();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
285 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
286 try {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
287 return Integer.parseInt(sb.toString());
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
288 } catch (NumberFormatException ex) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
289 throw new IOException(String.format("Expected response length instead of %s", sb));
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
290 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
291 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
292
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
293
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
294 private void forceSessionClose() {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
295 if (session != null) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
296 closeQuietly(remoteErr);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
297 closeQuietly(remoteOut);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
298 remoteErr = remoteOut = null;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
299 closeQuietly(remoteIn);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
300 remoteIn = null;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
301 session.close();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
302 session = null;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
303 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
304 sessionUse = 0;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
305 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
306
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
307 private void closeQuietly(Closeable c) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
308 try {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
309 if (c != null) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
310 c.close();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
311 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
312 } catch (IOException ex) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
313 sessionCtx.getLog().dump(getClass(), Severity.Warn, ex, null);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
314 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
315 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
316
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
317 private static String join(List<Nodeid> values, char sep) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
318 StringBuilder sb = new StringBuilder(values.size() * 41);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
319 for (Nodeid n : values) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
320 sb.append(n.toString());
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
321 sb.append(sep);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
322 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
323 if (!values.isEmpty()) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
324 // strip last space
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
325 sb.setLength(sb.length() - 1);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
326 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
327 return sb.toString();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
328 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
329
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
330 private static final class Parameter {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
331 private final String name;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
332 private final byte[] data;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
333
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
334 public Parameter(String paramName, String paramValue) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
335 assert paramName != null;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
336 assert paramValue != null;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
337 name = paramName;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
338 data = paramValue.getBytes();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
339 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
340
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
341 public String name() {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
342 return name;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
343 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
344 public int size() {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
345 return data.length;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
346 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
347 public byte[] data() {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
348 return data;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
349 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
350 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
351
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
352 private static final class FilterStream extends FilterInputStream {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
353 private int length;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
354
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
355 public FilterStream(InputStream is, int initialLength) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
356 super(is);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
357 length = initialLength;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
358 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
359
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
360 @Override
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
361 public int available() throws IOException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
362 return Math.min(super.available(), length);
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
363 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
364 @Override
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
365 public int read() throws IOException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
366 if (length == 0) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
367 return -1;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
368 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
369 int r = super.read();
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
370 if (r >= 0) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
371 length--;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
372 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
373 return r;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
374 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
375 @Override
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
376 public int read(byte[] b, int off, int len) throws IOException {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
377 if (length == 0) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
378 return -1;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
379 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
380 int r = super.read(b, off, Math.min(len, length));
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
381 if (r >= 0) {
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
382 assert r <= length;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
383 length -= r;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
384 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
385 return r;
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
386 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
387 @Override
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
388 public void close() throws IOException {
697
24f4efedc9d5 Respect the fact ssh and http protocols use different compression approach to sent changegroup data
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 696
diff changeset
389 length = 0;
685
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
390 // INTENTIONALLY DOES NOT CLOSE THE STREAM
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
391 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
392 }
9897cbfd2790 Towards ssh remote repositories: use ganymed library for ssh transport
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
393 }