comparison src/org/tmatesoft/hg/repo/HgRemoteRepository.java @ 181:cd3371670f0b

Refactor incoming and outgoing code to be shared with RepositoryComparator. Placeholders for in/out commands. Refactor common remote lookup code
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 12 Apr 2011 19:10:38 +0200
parents da426c2fe1ec
children ec1820f64d2b
comparison
equal deleted inserted replaced
180:42fe9a94b9d0 181:cd3371670f0b
44 import javax.net.ssl.HttpsURLConnection; 44 import javax.net.ssl.HttpsURLConnection;
45 import javax.net.ssl.SSLContext; 45 import javax.net.ssl.SSLContext;
46 import javax.net.ssl.TrustManager; 46 import javax.net.ssl.TrustManager;
47 import javax.net.ssl.X509TrustManager; 47 import javax.net.ssl.X509TrustManager;
48 48
49 import org.tmatesoft.hg.core.HgBadArgumentException;
49 import org.tmatesoft.hg.core.HgBadStateException; 50 import org.tmatesoft.hg.core.HgBadStateException;
50 import org.tmatesoft.hg.core.HgException; 51 import org.tmatesoft.hg.core.HgException;
51 import org.tmatesoft.hg.core.Nodeid; 52 import org.tmatesoft.hg.core.Nodeid;
52 53
53 /** 54 /**
61 public class HgRemoteRepository { 62 public class HgRemoteRepository {
62 63
63 private final URL url; 64 private final URL url;
64 private final SSLContext sslContext; 65 private final SSLContext sslContext;
65 private final String authInfo; 66 private final String authInfo;
66 private final boolean debug = Boolean.FALSE.booleanValue(); 67 private final boolean debug = Boolean.parseBoolean(System.getProperty("hg4j.remote.debug"));
67 68
68 HgRemoteRepository(URL url) throws HgException { 69 HgRemoteRepository(URL url) throws HgBadArgumentException {
69 if (url == null) { 70 if (url == null) {
70 throw new IllegalArgumentException(); 71 throw new IllegalArgumentException();
71 } 72 }
72 this.url = url; 73 this.url = url;
73 if ("https".equals(url.getProtocol())) { 74 if ("https".equals(url.getProtocol())) {
84 return new X509Certificate[0]; 85 return new X509Certificate[0];
85 } 86 }
86 }; 87 };
87 sslContext.init(null, new TrustManager[] { new TrustEveryone() }, null); 88 sslContext.init(null, new TrustManager[] { new TrustEveryone() }, null);
88 } catch (Exception ex) { 89 } catch (Exception ex) {
89 throw new HgException(ex); 90 throw new HgBadArgumentException("Can't initialize secure connection", ex);
90 } 91 }
91 } else { 92 } else {
92 sslContext = null; 93 sslContext = null;
93 } 94 }
94 if (url.getUserInfo() != null) { 95 if (url.getUserInfo() != null) {
107 } else { 108 } else {
108 authInfo = null; 109 authInfo = null;
109 } 110 }
110 } 111 }
111 112
113 public boolean isInvalid() throws HgException {
114 // say hello to server, check response
115 if (Boolean.FALSE.booleanValue()) {
116 throw HgRepository.notImplemented();
117 }
118 return false; // FIXME
119 }
120
121 /**
122 * @return human-readable address of the server, without user credentials or any other security information
123 */
124 public String getLocation() {
125 if (url.getUserInfo() == null) {
126 return url.toExternalForm();
127 }
128 if (url.getPort() != -1) {
129 return String.format("%s://%s:%d%s", url.getProtocol(), url.getHost(), url.getPort(), url.getPath());
130 } else {
131 return String.format("%s://%s%s", url.getProtocol(), url.getHost(), url.getPath());
132 }
133 }
134
112 public List<Nodeid> heads() throws HgException { 135 public List<Nodeid> heads() throws HgException {
113 try { 136 try {
114 URL u = new URL(url, url.getPath() + "?cmd=heads"); 137 URL u = new URL(url, url.getPath() + "?cmd=heads");
115 HttpURLConnection c = setupConnection(u.openConnection()); 138 HttpURLConnection c = setupConnection(u.openConnection());
116 c.connect(); 139 c.connect();