update copyright
[fedora-idea.git] / plugins / cvs / cvs-core / src / com / intellij / cvsSupport2 / connections / ssh / SocksAuthenticatorManager.java
blobba8927aeea8c19908b3c6f825563370acc1663ed
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.cvsSupport2.connections.ssh;
18 import com.intellij.openapi.components.ServiceManager;
19 import com.intellij.openapi.util.Pair;
20 import com.intellij.openapi.util.text.StringUtil;
21 import org.netbeans.lib.cvsclient.connection.ConnectionSettings;
23 import java.net.Authenticator;
24 import java.net.PasswordAuthentication;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Map;
29 public class SocksAuthenticatorManager {
30 private final static String SOCKS_REQUESTING_PROTOCOL = "SOCKS";
31 private final MyAuthenticator myAuthenticator;
33 private volatile boolean myIsRegistered;
34 private final Object myLock;
36 public static SocksAuthenticatorManager getInstance() {
37 return ServiceManager.getService(SocksAuthenticatorManager.class);
40 private SocksAuthenticatorManager() {
41 myAuthenticator = new MyAuthenticator();
42 myLock = new Object();
45 public void register(final ConnectionSettings connectionSettings) {
46 SshLogger.debug("register in authenticator");
47 ensureRegistered();
48 myAuthenticator.register(connectionSettings.getProxyHostName(), connectionSettings.getProxyPort(), connectionSettings.getProxyLogin(),
49 connectionSettings.getProxyPassword());
52 public void unregister(final ConnectionSettings connectionSettings) {
53 SshLogger.debug("unregister in authenticator");
54 myAuthenticator.unregister(connectionSettings.getProxyHostName(), connectionSettings.getProxyPort());
57 private void ensureRegistered() {
58 // safe double check
59 if (! myIsRegistered) {
60 synchronized (myLock) {
61 if (! myIsRegistered) {
62 myIsRegistered = true;
63 Authenticator.setDefault(myAuthenticator);
69 private static class MyAuthenticator extends Authenticator {
70 private final Map<Pair<String, Integer>, Pair<String, String>> myKnown;
72 private MyAuthenticator() {
73 myKnown = Collections.synchronizedMap(new HashMap<Pair<String, Integer>, Pair<String, String>>());
76 public void register(final String host, final int port, final String login, final String password) {
77 myKnown.put(new Pair<String, Integer>(host, port), new Pair<String, String>(login, password));
80 public void unregister(final String host, final int port) {
81 myKnown.remove(new Pair<String, Integer>(host, port));
84 @Override
85 protected PasswordAuthentication getPasswordAuthentication() {
86 SshLogger.debug("proxy authenticator asked");
87 final String protocol = getRequestingProtocol();
88 if ((protocol == null) || (! StringUtil.containsIgnoreCase(protocol, SOCKS_REQUESTING_PROTOCOL))) {
89 return super.getPasswordAuthentication();
91 final RequestorType type = getRequestorType();
92 /*if ((type == null) || (! RequestorType.PROXY.equals(type))) {
93 return super.getPasswordAuthentication();
94 }*/
95 final String host = getRequestingHost();
96 final int port = getRequestingPort();
97 final Pair<String, String> result = myKnown.get(new Pair<String, Integer>(host, port));
98 if (result != null) {
99 SshLogger.debug("proxy authenticator found what to answer");
100 return new PasswordAuthentication(result.getFirst(), result.getSecond().toCharArray());
102 return super.getPasswordAuthentication();