/* * Copyright 2000-2020 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.teamcity; import java.net.MalformedURLException; import java.net.URL; import java.text.MessageFormat; import com.intellij.openapi.diagnostic.Logger; import jetbrains.buildServer.IdeSettings; import jetbrains.buildServer.serverProxy.ProxySettings; import jetbrains.teamcity.Activator.IConnectionParams; import jetbrains.teamcity.core.Debug; import jetbrains.teamcity.core.compatibility.net.IProxyService; import jetbrains.teamcity.core.util.Pair; import org.eclipse.core.net.proxy.IProxyData; import org.jetbrains.annotations.NotNull; public class EclipseIDESettings implements IdeSettings { public static final boolean RESTORE_CONNECTION = true; public static final int DEFAULT_REFRESH_INTERVAL = 10000; public static final EclipseIDESettings DEFAULT = new EclipseIDESettings( System.getProperty("user.name"), Constants.EMPTY_STRING, Constants.DEFAULT_TEAMCITY_URL, Activator.NULL_PROXY_SERVICE); //$NON-NLS-1$ private final Logger LOG = Debug.Category.DEBUG_PLATFORM.getLogger(EclipseIDESettings.class); private String myURL; private String myUserName; private char[] myPassword; private ProxySettings myProxySetting = new ProxySettingAdapter(Activator.NULL_PROXY_SERVICE); private IConnectionParams myConnectionParams = new IConnectionParams() { public int getUpdateInterval() { return DEFAULT_REFRESH_INTERVAL; } public boolean isRestoreConnection() { return RESTORE_CONNECTION; } }; private EclipseIDESettings(final String userName, final String password, final String url, final jetbrains.teamcity.core.compatibility.net.IProxyService proxyService) { myUserName = userName; myPassword = password != null ? password.toCharArray() : new char[0]; myURL = url; setProxyService(proxyService); } public EclipseIDESettings(final String userName, final String password, final String url, final IConnectionParams params, final jetbrains.teamcity.core.compatibility.net.IProxyService proxyService) { this(userName, password, url, proxyService); myConnectionParams = params; } public void apply(final @NotNull EclipseIDESettings other) { myUserName = other.getLogin(); myPassword = other.getPassword(); myURL = other.getServerUrl(); myProxySetting = other.myProxySetting; myConnectionParams = other.myConnectionParams; } private void setProxyService(IProxyService proxyService) { myProxySetting = new ProxySettingAdapter(proxyService != null ? proxyService : Activator.NULL_PROXY_SERVICE); //check host: TW-7507: "Host name may not be null" warn and infinite login when I try to use system proxy final String host = myProxySetting.getHost(); if (host == null) { myProxySetting = new ProxySettingAdapter(Activator.NULL_PROXY_SERVICE); LOG.debug("Undefined proxy host. Discard proxy for connection"); //$NON-NLS-1$ } else { LOG.debug(MessageFormat.format("Proxy defined for \"{0}:{1}\"", host, String.valueOf(myProxySetting.getPort()))); //$NON-NLS-1$ } } public String getLogin() { return myUserName; } public boolean isFilterSummaryByCurrentProject() { return Activator.getDefault().getIDE().isFilterSummaryByCurrentProject();//have to delegate as far IdeSettings does not exist till login.... } public void setFilterSummaryByCurrentProject(final boolean filterIt) { Activator.getDefault().getIDE().setFilterSummaryByCurrentProject(filterIt); } public char[] getPassword() { return myPassword; } public String getServerUrl() { return myURL; } public ProxySettings getProxySettings() { return myProxySetting; } private class ProxySettingAdapter implements ProxySettings { private final jetbrains.teamcity.core.compatibility.net.IProxyService myProxyService; private Pair data; public ProxySettingAdapter(@NotNull final jetbrains.teamcity.core.compatibility.net.IProxyService proxyService) { myProxyService = proxyService; } public String getHost() { try { final IProxyData data = getProxyData(getServerUrl()); if (data != null) { return data.getHost(); } } catch (MalformedURLException e) { throw new RuntimeException(e); } return null; } public int getPort() { try { final IProxyData data = getProxyData(getServerUrl()); if (data != null) { return data.getPort(); } } catch (MalformedURLException e) { throw new RuntimeException(e); } return -1; } public String getUser() { try { final IProxyData data = getProxyData(getServerUrl()); if (data != null) { return data.getUserId(); } } catch (MalformedURLException e) { throw new RuntimeException(e); } return null; } public String getPassword() { try { final IProxyData data = getProxyData(getServerUrl()); if (data != null) { return data.getPassword(); } } catch (MalformedURLException e) { throw new RuntimeException(e); } return null; } public boolean useAuthentication() { try { final IProxyData data = getProxyData(getServerUrl()); if (data != null) { return data.isRequiresAuthentication(); } } catch (MalformedURLException e) { throw new RuntimeException(e); } return false; } public boolean useProxy() { return myProxyService.isProxiesEnabled() || (myProxyService.isSystemProxiesEnabled() && myProxyService.hasSystemProxies()); } private IProxyData getProxyData(@NotNull final String urlStr) throws MalformedURLException { if (data != null) { if (data.first.equals(urlStr)) { return data.second; } } final URL url = new URL(urlStr); final String protocol = url.getProtocol(); final String proxyType; //normalize if (IProxyData.HTTP_PROXY_TYPE.equalsIgnoreCase(String.valueOf(protocol))) { proxyType = IProxyData.HTTP_PROXY_TYPE; } else if (IProxyData.HTTPS_PROXY_TYPE.equalsIgnoreCase(String.valueOf(protocol))) { proxyType = IProxyData.HTTPS_PROXY_TYPE; } else if (IProxyData.SOCKS_PROXY_TYPE.equalsIgnoreCase(String.valueOf(protocol))) { proxyType = IProxyData.SOCKS_PROXY_TYPE; } else { proxyType = ""; //pseudo value. proxyData should not be found } final IProxyData proxyData = myProxyService.getProxyDataForHost(url.toString(), proxyType); data = Pair.create(urlStr, proxyData); return proxyData; } } public IConnectionParams getConnectionParams() { return myConnectionParams; } }