1 22 package org.netbeans.lib.cvsclient.connection; 23 24 import java.io.*; 25 import java.net.*; 26 import java.text.*; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 30 import org.netbeans.lib.cvsclient.CVSRoot; 31 import org.netbeans.lib.cvsclient.request.*; 32 import org.netbeans.lib.cvsclient.command.CommandAbortedException; 33 import org.netbeans.lib.cvsclient.command.CommandException; 34 import org.netbeans.lib.cvsclient.util.*; 35 36 import javax.net.SocketFactory; 37 38 46 public class PServerConnection extends AbstractConnection { 47 51 protected static final String OPEN_PREAMBLE = "BEGIN AUTH REQUEST\n"; 53 56 protected static final String OPEN_POSTAMBLE = "END AUTH REQUEST\n"; 58 59 64 protected static final String VERIFY_PREAMBLE = 65 "BEGIN VERIFICATION REQUEST\n"; 67 70 protected static final String VERIFY_POSTAMBLE = 71 "END VERIFICATION REQUEST\n"; 73 76 protected static final String AUTHENTICATION_SUCCEEDED_RESPONSE = 77 "I LOVE YOU"; 79 private static final String AUTHENTICATION_SUCCEEDED_RESPONSE_RAW = 80 "I LOVE YOU\n"; 82 85 protected static final String AUTHENTICATION_FAILED_RESPONSE = 86 "I HATE YOU"; 88 private static final String AUTHENTICATION_FAILED_RESPONSE_RAW = 89 "I HATE YOU\n"; 91 92 93 96 protected String userName; 97 98 101 protected String encodedPassword; 102 103 106 public static final int DEFAULT_PORT = 2401; 107 108 111 protected int port = DEFAULT_PORT; 112 113 116 protected String hostName; 117 118 121 protected Socket socket; 122 123 126 protected SocketFactory socketFactory; 127 128 132 public PServerConnection() { 133 } 134 135 141 public PServerConnection(CVSRoot cvsRoot) { 142 this(cvsRoot, null); 143 } 144 145 151 public PServerConnection(CVSRoot cvsRoot, SocketFactory factory) { 152 if (!CVSRoot.METHOD_PSERVER.equals(cvsRoot.getMethod())) { 153 throw new IllegalArgumentException ("CVS Root '"+cvsRoot+"' does not represent :pserver: connection type."); 154 } 155 socketFactory = factory; 156 String userName = cvsRoot.getUserName(); 157 if (userName == null) { 158 userName = System.getProperty("user.name"); 159 } 160 setUserName(userName); 161 String password = cvsRoot.getPassword(); 162 if (password != null) { 163 setEncodedPassword(StandardScrambler.getInstance().scramble(password)); 164 } 165 setHostName(cvsRoot.getHostName()); 166 setRepository(cvsRoot.getRepository()); 167 int port = cvsRoot.getPort(); 168 if (port == 0) { 169 port = 2401; } 171 setPort(port); 172 } 173 174 185 private void openConnection(String preamble, String postamble) 186 throws AuthenticationException, CommandAbortedException { 187 if (hostName == null) { 188 String locMessage = AuthenticationException.getBundleString( 189 "AuthenticationException.HostIsNull"); throw new AuthenticationException("HostIsNull", locMessage); } 192 193 try { 194 SocketFactory sf = (socketFactory != null) ? socketFactory : SocketFactory.getDefault(); 195 socket = sf.createSocket(hostName, port); 196 197 BufferedOutputStream bos = 198 new BufferedOutputStream(socket.getOutputStream(), 32768); 199 LoggedDataOutputStream outputStream = new LoggedDataOutputStream(bos); 200 setOutputStream(outputStream); 201 202 BufferedInputStream bis = 203 new BufferedInputStream(socket.getInputStream(), 32768); 204 LoggedDataInputStream inputStream = new LoggedDataInputStream(bis); 205 setInputStream(inputStream); 206 207 outputStream.writeBytes(preamble, "US-ASCII"); 208 outputStream.writeBytes(getRepository() + "\n"); outputStream.writeBytes(userName + "\n"); outputStream.writeBytes(getEncodedPasswordNotNull() + "\n", "US-ASCII"); outputStream.writeBytes(postamble, "US-ASCII"); 212 outputStream.flush(); 213 214 if (Thread.interrupted()) { 215 reset(); 216 String localMsg = CommandException.getLocalMessage("Client.connectionAborted", null); throw new CommandAbortedException("Aborted during connecting to the server.", localMsg); } 219 220 byte rawResponse[] = inputStream.readBytes(AUTHENTICATION_SUCCEEDED_RESPONSE_RAW.length()); 224 String response = new String (rawResponse, "utf8"); 226 if (Thread.interrupted()) { 227 reset(); 228 String localMsg = CommandException.getLocalMessage("Client.connectionAborted", null); throw new CommandAbortedException("Aborted during connecting to the server.", localMsg); } 231 232 if (AUTHENTICATION_SUCCEEDED_RESPONSE_RAW.equals(response)) { 233 return; 234 } 235 236 if (AUTHENTICATION_FAILED_RESPONSE_RAW.equals(response)) { 237 String localizedMsg = getLocalMessage("AuthenticationException.badPassword", 238 null); 239 throw new AuthenticationException("AuthenticationFailed", localizedMsg); 241 } 242 243 if (response == null) response = ""; String locMessage = getLocalMessage("AuthenticationException.AuthenticationFailed", new Object []{ response }); 246 throw new AuthenticationException("AuthenticationFailed", locMessage); 248 } 249 catch (AuthenticationException ex) { 250 reset(); 251 throw ex; 252 } 253 catch (ConnectException ex) { 254 reset(); 255 String locMessage = 256 getLocalMessage("AuthenticationException.ConnectException", new Object []{hostName, Integer.toString(port)}); 258 throw new AuthenticationException("ConnectException", ex, locMessage); 260 } 261 catch (NoRouteToHostException ex) { 262 reset(); 263 String locMessage = 264 getLocalMessage("AuthenticationException.NoRouteToHostException", new Object []{hostName}); 266 throw new AuthenticationException("NoRouteToHostException", ex, locMessage); 268 } 269 catch (IOException ex) { 270 reset(); 271 String locMessage = 272 getLocalMessage("AuthenticationException.IOException", new Object []{hostName}); 274 throw new AuthenticationException("IOException", ex, locMessage); } 276 283 } 284 285 private void reset() { 286 socket = null; 287 setInputStream(null); 288 setOutputStream(null); 289 } 290 291 300 public void verify() throws AuthenticationException { 301 try { 302 openConnection(VERIFY_PREAMBLE, VERIFY_POSTAMBLE); 303 } catch (CommandAbortedException caex) { 304 } 306 if (socket == null) { 307 return; 308 } 309 310 try { 311 socket.close(); 312 } 313 catch (IOException exc) { 314 String locMessage = AuthenticationException.getBundleString( 315 "AuthenticationException.Throwable"); throw new AuthenticationException("General error", exc, locMessage); } 318 finally { 319 reset(); 320 } 321 } 322 323 333 public void open() throws AuthenticationException, CommandAbortedException { 334 openConnection(OPEN_PREAMBLE, OPEN_POSTAMBLE); 335 } 336 337 340 public String getUserName() { 341 return userName; 342 } 343 344 348 public void setUserName(String userName) { 349 this.userName = userName; 350 } 351 352 356 public String getEncodedPassword() { 357 return encodedPassword; 358 } 359 360 private String getEncodedPasswordNotNull() { 361 if (encodedPassword == null) { 362 return StandardScrambler.getInstance().scramble(""); 363 } 364 return encodedPassword; 365 } 366 367 371 public void setEncodedPassword(String encodedPassword) { 372 this.encodedPassword = encodedPassword; 373 } 374 375 379 public int getPort() { 380 return port; 381 } 382 383 388 public void setPort(int port) { 389 this.port = port; 390 } 391 392 397 public String getHostName() { 398 return hostName; 399 } 400 401 406 public void setHostName(String hostName) { 407 this.hostName = hostName; 408 } 409 410 413 public void close() throws IOException { 414 if (!isOpen()) { 415 return; 416 } 417 418 try { 419 socket.close(); 420 } 421 finally { 422 reset(); 423 } 424 } 425 426 431 public void modifyInputStream(ConnectionModifier modifier) 432 throws IOException { 433 modifier.modifyInputStream(getInputStream()); 434 } 435 436 441 public void modifyOutputStream(ConnectionModifier modifier) 442 throws IOException { 443 modifier.modifyOutputStream(getOutputStream()); 444 } 445 446 private String getLocalMessage(String key, Object [] arguments) { 447 String locMessage = AuthenticationException.getBundleString(key); 448 if (locMessage == null) { 449 return null; 450 } 451 locMessage = MessageFormat.format(locMessage, arguments); 452 return locMessage; 453 } 454 455 458 public boolean isOpen() { 459 return socket != null; 460 } 461 462 } 463 | Popular Tags |