Merge "Factor out proxy properties from OpenIdServiceImpl"

This commit is contained in:
Shawn Pearce
2014-08-08 01:35:24 +00:00
committed by Gerrit Code Review
4 changed files with 110 additions and 23 deletions

View File

@@ -0,0 +1,23 @@
// Copyright (C) 2014 The Android Open Source Project
//
// 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 com.google.gerrit.httpd;
import java.net.URL;
public interface ProxyProperties {
URL getProxyUrl();
String getUsername();
String getPassword();
}

View File

@@ -0,0 +1,73 @@
// Copyright (C) 2014 The Android Open Source Project
//
// 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 com.google.gerrit.httpd;
import com.google.common.base.Strings;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import org.eclipse.jgit.lib.Config;
import java.net.MalformedURLException;
import java.net.URL;
@Singleton
class ProxyPropertiesProvider implements Provider<ProxyProperties> {
private URL proxyUrl;
private String proxyUser;
private String proxyPassword;
@Inject
ProxyPropertiesProvider(@GerritServerConfig Config config)
throws MalformedURLException {
String proxyUrlStr = config.getString("http", null, "proxy");
if (!Strings.isNullOrEmpty(proxyUrlStr)) {
proxyUrl = new URL(proxyUrlStr);
proxyUser = config.getString("http", null, "proxyUsername");
proxyPassword = config.getString("http", null, "proxyPassword");
String userInfo = proxyUrl.getUserInfo();
if (userInfo != null) {
int c = userInfo.indexOf(':');
if (0 < c) {
proxyUser = userInfo.substring(0, c);
proxyPassword = userInfo.substring(c + 1);
} else {
proxyUser = userInfo;
}
}
}
}
@Override
public ProxyProperties get() {
return new ProxyProperties() {
@Override
public URL getProxyUrl() {
return proxyUrl;
}
@Override
public String getUsername() {
return proxyUser;
}
@Override
public String getPassword() {
return proxyPassword;
}
};
}
}

View File

@@ -131,6 +131,8 @@ public class WebModule extends LifecycleModule {
bind(SocketAddress.class).annotatedWith(RemotePeer.class).toProvider( bind(SocketAddress.class).annotatedWith(RemotePeer.class).toProvider(
HttpRemotePeerProvider.class).in(RequestScoped.class); HttpRemotePeerProvider.class).in(RequestScoped.class);
bind(ProxyProperties.class).toProvider(ProxyPropertiesProvider.class);
listener().toInstance(registerInParentInjectors()); listener().toInstance(registerInParentInjectors());
} }
} }

View File

@@ -19,6 +19,7 @@ import com.google.gerrit.common.auth.openid.OpenIdUrls;
import com.google.gerrit.extensions.registration.DynamicItem; import com.google.gerrit.extensions.registration.DynamicItem;
import com.google.gerrit.extensions.restapi.Url; import com.google.gerrit.extensions.restapi.Url;
import com.google.gerrit.httpd.CanonicalWebUrl; import com.google.gerrit.httpd.CanonicalWebUrl;
import com.google.gerrit.httpd.ProxyProperties;
import com.google.gerrit.httpd.WebSession; import com.google.gerrit.httpd.WebSession;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.IdentifiedUser; import com.google.gerrit.server.IdentifiedUser;
@@ -55,7 +56,6 @@ import org.openid4java.message.sreg.SRegMessage;
import org.openid4java.message.sreg.SRegRequest; import org.openid4java.message.sreg.SRegRequest;
import org.openid4java.message.sreg.SRegResponse; import org.openid4java.message.sreg.SRegResponse;
import org.openid4java.util.HttpClientFactory; import org.openid4java.util.HttpClientFactory;
import org.openid4java.util.ProxyProperties;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -108,29 +108,18 @@ class OpenIdServiceImpl {
final Provider<IdentifiedUser> iu, final Provider<IdentifiedUser> iu,
CanonicalWebUrl up, CanonicalWebUrl up,
@GerritServerConfig final Config config, final AuthConfig ac, @GerritServerConfig final Config config, final AuthConfig ac,
final AccountManager am) throws ConsumerException, MalformedURLException { final AccountManager am,
ProxyProperties proxyProperties)
throws ConsumerException, MalformedURLException {
if (config.getString("http", null, "proxy") != null) { if (proxyProperties.getProxyUrl() != null) {
final URL proxyUrl = new URL(config.getString("http", null, "proxy")); final org.openid4java.util.ProxyProperties proxy =
String username = config.getString("http", null, "proxyUsername"); new org.openid4java.util.ProxyProperties();
String password = config.getString("http", null, "proxyPassword"); URL url = proxyProperties.getProxyUrl();
proxy.setProxyHostName(url.getHost());
final String userInfo = proxyUrl.getUserInfo(); proxy.setProxyPort(url.getPort());
if (userInfo != null) { proxy.setUserName(proxyProperties.getUsername());
int c = userInfo.indexOf(':'); proxy.setPassword(proxyProperties.getPassword());
if (0 < c) {
username = userInfo.substring(0, c);
password = userInfo.substring(c + 1);
} else {
username = userInfo;
}
}
final ProxyProperties proxy = new ProxyProperties();
proxy.setProxyHostName(proxyUrl.getHost());
proxy.setProxyPort(proxyUrl.getPort());
proxy.setUserName(username);
proxy.setPassword(password);
HttpClientFactory.setProxyProperties(proxy); HttpClientFactory.setProxyProperties(proxy);
} }