1 /*
2 Bloof - visualize the evolution of your software project
3 Copyright ( C ) 2003 Lukasz Pekacki <lukasz@pekacki.de>
4 http://bloof.sf.net/
5
6 This file was part of JavaCVS by Nicholas Allen ( nallen@freenet.co.uk ).
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 $RCSfile: PServerConnection.java,v $
21 Created on $Date: 2003/09/11 10:16:22 $
22 */
23
24 package net.sf.bloof.scm.cvsplugin;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.io.UnsupportedEncodingException;
30 import java.net.Socket;
31
32 /***
33 * Connection method via CVS Pserver protocol
34 * @author Nicholas Allen
35 * @author Lukasz Pekacki
36 */
37 public class PServerConnection extends CvsConnection {
38 /***
39 * Constructs a PServerConnection to the default Port specified by PSERVER_PORT
40 * @param aCvsAccess access to CVS repository
41 * @throws IOException on IO Error
42 * @throws CvsConnectionException if connection fails
43 */
44 public PServerConnection(CvsAccess aCvsAccess) throws IOException, CvsConnectionException {
45 this(aCvsAccess.getLocation(), aCvsAccess.getLoginDetails(), PSERVER_PORT);
46 }
47
48 /***
49 * Constructs a PServerConnection to the default Port specified by PSERVER_PORT
50 * @param aLocation location of the CVS repository
51 * @param aDetails login details containing user name and password
52 * @throws IOException on IO Error
53 * @throws CvsConnectionException if connection fails
54 */
55 public PServerConnection(RepositoryLocation aLocation, LoginDetails aDetails)
56 throws IOException, CvsConnectionException {
57 this(aLocation, aDetails, PSERVER_PORT);
58 }
59 /***
60 * Constructs the PServerConnection with the given parameters
61 * @param aLocation location of the CVS repository
62 * @param aDetails login details containing user name and password
63 * @param aPort port of the pserver
64 * @throws IOException on IO Error
65 * @throws CvsConnectionException if connection fails
66 */
67 public PServerConnection(RepositoryLocation aLocation, LoginDetails aDetails, int aPort)
68 throws IOException, CvsConnectionException {
69 super(aLocation);
70
71 try {
72 mPort = aPort;
73 mSocket = new Socket(aLocation.getHostName(), aPort);
74 mIn = mSocket.getInputStream();
75 mOut = mSocket.getOutputStream();
76
77 // Try to authenticate
78 sendLine("BEGIN AUTH REQUEST");
79 sendLine(aLocation.getRepositoryPath());
80 sendLine(aDetails.getUserName());
81 sendLine(scramblePassword(aDetails.getPassword()));
82 sendLine("END AUTH REQUEST");
83 String sResponse = receiveLine();
84
85 if (sResponse.equals("I LOVE YOU")) {
86 // connection accepted
87 } else if (sResponse.equals("I HATE YOU")) {
88 throw new CvsConnectionException("Connection refused");
89 } else {
90 throw new CvsConnectionException("Unrecognized response: " + sResponse);
91 }
92 } catch (IOException e) {
93 close();
94 throw e;
95 } catch (CvsConnectionException e) {
96 close();
97 throw e;
98 }
99 }
100
101 /*** This code is taken from the jCVS code. */
102 static {
103 int i;
104 int[] sh = new int[256];
105
106 for (i = 0; i < 32; ++i) {
107 sh[i] = i;
108 }
109
110 sh[i++] = 114;
111 sh[i++] = 120;
112 sh[i++] = 53;
113 sh[i++] = 79;
114 sh[i++] = 96;
115 sh[i++] = 109;
116 sh[i++] = 72;
117 sh[i++] = 108;
118 sh[i++] = 70;
119 sh[i++] = 64;
120 sh[i++] = 76;
121 sh[i++] = 67;
122 sh[i++] = 116;
123 sh[i++] = 74;
124 sh[i++] = 68;
125 sh[i++] = 87;
126 sh[i++] = 111;
127 sh[i++] = 52;
128 sh[i++] = 75;
129 sh[i++] = 119;
130 sh[i++] = 49;
131 sh[i++] = 34;
132 sh[i++] = 82;
133 sh[i++] = 81;
134 sh[i++] = 95;
135 sh[i++] = 65;
136 sh[i++] = 112;
137 sh[i++] = 86;
138 sh[i++] = 118;
139 sh[i++] = 110;
140 sh[i++] = 122;
141 sh[i++] = 105;
142 sh[i++] = 41;
143 sh[i++] = 57;
144 sh[i++] = 83;
145 sh[i++] = 43;
146 sh[i++] = 46;
147 sh[i++] = 102;
148 sh[i++] = 40;
149 sh[i++] = 89;
150 sh[i++] = 38;
151 sh[i++] = 103;
152 sh[i++] = 45;
153 sh[i++] = 50;
154 sh[i++] = 42;
155 sh[i++] = 123;
156 sh[i++] = 91;
157 sh[i++] = 35;
158 sh[i++] = 125;
159 sh[i++] = 55;
160 sh[i++] = 54;
161 sh[i++] = 66;
162 sh[i++] = 124;
163 sh[i++] = 126;
164 sh[i++] = 59;
165 sh[i++] = 47;
166 sh[i++] = 92;
167 sh[i++] = 71;
168 sh[i++] = 115;
169 sh[i++] = 78;
170 sh[i++] = 88;
171 sh[i++] = 107;
172 sh[i++] = 106;
173 sh[i++] = 56;
174 sh[i++] = 36;
175 sh[i++] = 121;
176 sh[i++] = 117;
177 sh[i++] = 104;
178 sh[i++] = 101;
179 sh[i++] = 100;
180 sh[i++] = 69;
181 sh[i++] = 73;
182 sh[i++] = 99;
183 sh[i++] = 63;
184 sh[i++] = 94;
185 sh[i++] = 93;
186 sh[i++] = 39;
187 sh[i++] = 37;
188 sh[i++] = 61;
189 sh[i++] = 48;
190 sh[i++] = 58;
191 sh[i++] = 113;
192 sh[i++] = 32;
193 sh[i++] = 90;
194 sh[i++] = 44;
195 sh[i++] = 98;
196 sh[i++] = 60;
197 sh[i++] = 51;
198 sh[i++] = 33;
199 sh[i++] = 97;
200 sh[i++] = 62;
201 sh[i++] = 77;
202 sh[i++] = 84;
203 sh[i++] = 80;
204 sh[i++] = 85;
205 sh[i++] = 223;
206 sh[i++] = 225;
207 sh[i++] = 216;
208 sh[i++] = 187;
209 sh[i++] = 166;
210 sh[i++] = 229;
211 sh[i++] = 189;
212 sh[i++] = 222;
213 sh[i++] = 188;
214 sh[i++] = 141;
215 sh[i++] = 249;
216 sh[i++] = 148;
217 sh[i++] = 200;
218 sh[i++] = 184;
219 sh[i++] = 136;
220 sh[i++] = 248;
221 sh[i++] = 190;
222 sh[i++] = 199;
223 sh[i++] = 170;
224 sh[i++] = 181;
225 sh[i++] = 204;
226 sh[i++] = 138;
227 sh[i++] = 232;
228 sh[i++] = 218;
229 sh[i++] = 183;
230 sh[i++] = 255;
231 sh[i++] = 234;
232 sh[i++] = 220;
233 sh[i++] = 247;
234 sh[i++] = 213;
235 sh[i++] = 203;
236 sh[i++] = 226;
237 sh[i++] = 193;
238 sh[i++] = 174;
239 sh[i++] = 172;
240 sh[i++] = 228;
241 sh[i++] = 252;
242 sh[i++] = 217;
243 sh[i++] = 201;
244 sh[i++] = 131;
245 sh[i++] = 230;
246 sh[i++] = 197;
247 sh[i++] = 211;
248 sh[i++] = 145;
249 sh[i++] = 238;
250 sh[i++] = 161;
251 sh[i++] = 179;
252 sh[i++] = 160;
253 sh[i++] = 212;
254 sh[i++] = 207;
255 sh[i++] = 221;
256 sh[i++] = 254;
257 sh[i++] = 173;
258 sh[i++] = 202;
259 sh[i++] = 146;
260 sh[i++] = 224;
261 sh[i++] = 151;
262 sh[i++] = 140;
263 sh[i++] = 196;
264 sh[i++] = 205;
265 sh[i++] = 130;
266 sh[i++] = 135;
267 sh[i++] = 133;
268 sh[i++] = 143;
269 sh[i++] = 246;
270 sh[i++] = 192;
271 sh[i++] = 159;
272 sh[i++] = 244;
273 sh[i++] = 239;
274 sh[i++] = 185;
275 sh[i++] = 168;
276 sh[i++] = 215;
277 sh[i++] = 144;
278 sh[i++] = 139;
279 sh[i++] = 165;
280 sh[i++] = 180;
281 sh[i++] = 157;
282 sh[i++] = 147;
283 sh[i++] = 186;
284 sh[i++] = 214;
285 sh[i++] = 176;
286 sh[i++] = 227;
287 sh[i++] = 231;
288 sh[i++] = 219;
289 sh[i++] = 169;
290 sh[i++] = 175;
291 sh[i++] = 156;
292 sh[i++] = 206;
293 sh[i++] = 198;
294 sh[i++] = 129;
295 sh[i++] = 164;
296 sh[i++] = 150;
297 sh[i++] = 210;
298 sh[i++] = 154;
299 sh[i++] = 177;
300 sh[i++] = 134;
301 sh[i++] = 127;
302 sh[i++] = 182;
303 sh[i++] = 128;
304 sh[i++] = 158;
305 sh[i++] = 208;
306 sh[i++] = 162;
307 sh[i++] = 132;
308 sh[i++] = 167;
309 sh[i++] = 209;
310 sh[i++] = 149;
311 sh[i++] = 241;
312 sh[i++] = 153;
313 sh[i++] = 251;
314 sh[i++] = 237;
315 sh[i++] = 236;
316 sh[i++] = 171;
317 sh[i++] = 195;
318 sh[i++] = 243;
319 sh[i++] = 233;
320 sh[i++] = 253;
321 sh[i++] = 240;
322 sh[i++] = 194;
323 sh[i++] = 250;
324 sh[i++] = 191;
325 sh[i++] = 155;
326 sh[i++] = 142;
327 sh[i++] = 137;
328 sh[i++] = 245;
329 sh[i++] = 235;
330 sh[i++] = 163;
331 sh[i++] = 242;
332 sh[i++] = 178;
333 sh[i++] = 152;
334
335 sShiftMap = sh;
336 }
337
338 /***
339 * Closes the connection
340 */
341 public void close() {
342 if (mSocket != null) {
343 try {
344 mSocket.close();
345 } catch (IOException e) {
346 e.printStackTrace();
347 return;
348 }
349 mSocket = null;
350 }
351 }
352
353 /***
354 * Gets the input stream from which the client should read server responses from
355 * @return InputStream
356 */
357 public InputStream getInputStream() {
358 return mIn;
359 }
360
361 /***
362 * Gets the output stream over which the client should send its requests to the server
363 * @return OutputStream
364 */
365 public OutputStream getOutputStream() {
366 return mOut;
367 }
368
369 /***
370 * Returns the port of the PServer
371 * @return int port number
372 */
373 public int getPort() {
374 return mPort;
375 }
376
377 /*** Receive a line of text from the server. */
378
379 private String receiveLine() throws IOException {
380 mReceiveBuffer.setLength(0);
381 int n;
382
383 while (true) {
384 n = mIn.read();
385
386 if (n == '\n' || n == -1) {
387 break;
388 }
389
390 mReceiveBuffer.append((char) n);
391 }
392
393 String s = mReceiveBuffer.toString();
394 return s;
395 }
396
397 /***
398 * Scrambles a password for a pserver connection.
399 * @param aPassword plaintext to be scrambled
400 * @return String scrambled password
401 */
402 private String scramblePassword(String aPassword) {
403 StringBuffer buf = new StringBuffer("A");
404 for (int i = 0; i < aPassword.length(); ++i) {
405 char ch = aPassword.charAt(i);
406
407 byte newCh = (byte) (sShiftMap[((int) ch & 255)] & 255);
408
409 buf.append((char) newCh);
410 }
411
412 return buf.toString();
413 }
414
415 private void sendLine(String aText) throws IOException {
416 sendText(aText + "\n");
417 pause(300);
418 }
419
420 private void pause(int aMillis) {
421 synchronized(this) {
422 try {
423 wait(aMillis);
424 } catch (InterruptedException e) {
425 e.printStackTrace();
426 }
427 }
428 }
429
430 private void sendText(String aText) throws IOException {
431
432 byte[] bytes = null;
433 try {
434 bytes = aText.getBytes("ASCII");
435 } catch (UnsupportedEncodingException e) {
436 throw new RuntimeException("ASCII is not supported");
437 }
438 mOut.write(bytes);
439 mOut.flush();
440 }
441 private static final int PSERVER_PORT = 2401;
442
443 private static int[] sShiftMap;
444 private InputStream mIn = null;
445 private OutputStream mOut = null;
446 private int mPort;
447 private StringBuffer mReceiveBuffer = new StringBuffer(100);
448 private Socket mSocket = null;
449
450 }
This page was automatically generated by Maven