Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OSJC 61-66-71] fixing where timeouts are read from #127

Merged
merged 1 commit into from Mar 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/openshift/client/IHttpClient.java
Expand Up @@ -24,6 +24,7 @@
/**
* @author André Dietisheim
* @author Nicolas Spano
* @author Corey Daley
*/
public interface IHttpClient {

Expand Down Expand Up @@ -65,9 +66,8 @@ public interface IHttpClient {
public static final String SYSPROP_DEFAULT_CONNECT_TIMEOUT = "sun.net.client.defaultConnectTimeout";
public static final String SYSPROP_DEFAULT_READ_TIMEOUT = "sun.net.client.defaultReadTimeout";

public static final int DEFAULT_CONNECT_TIMEOUT = 10 * 1000;
public static final int DEFAULT_READ_TIMEOUT = 2 * 60 * 1000;
public static final int NO_TIMEOUT = 0;
public static final int NO_TIMEOUT = -1;

public String get(URL url, int timeout) throws HttpClientException, SocketTimeoutException;

Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/openshift/client/OpenShiftConnectionFactory.java
Expand Up @@ -29,10 +29,11 @@
*
* @author Xavier Coulon
* @author Andre Dietisheim
* @author Corey Daley
*
*/
public class OpenShiftConnectionFactory extends AbstractOpenShiftConnectionFactory {

private IOpenShiftConfiguration configuration = null;
/**
* Establish a connection with the clientId along with user's password.
* User's login and Server URL are retrieved from the local configuration
Expand All @@ -48,7 +49,6 @@ public class OpenShiftConnectionFactory extends AbstractOpenShiftConnectionFacto
* @throws OpenShiftException
*/
public IOpenShiftConnection getConnection(final String clientId, final String password) throws OpenShiftException {
IOpenShiftConfiguration configuration = null;
try {
configuration = new OpenShiftConfiguration();
} catch (IOException e) {
Expand All @@ -75,7 +75,6 @@ public IOpenShiftConnection getConnection(final String clientId, final String pa
*/
public IOpenShiftConnection getConnection(final String clientId, final String username, final String password)
throws OpenShiftException {
IOpenShiftConfiguration configuration;
try {
configuration = new OpenShiftConfiguration();
} catch (IOException e) {
Expand Down Expand Up @@ -134,8 +133,16 @@ public IOpenShiftConnection getConnection(final String clientId, final String us
* @throws OpenShiftException
*/
public IOpenShiftConnection getConnection(final String clientId, final String username, final String password,
final String authKey, final String authIV, final String serverUrl,
final ISSLCertificateCallback sslCertificateCallback) throws OpenShiftException {
final String authKey, final String authIV, final String serverUrl,
final ISSLCertificateCallback sslCertificateCallback) throws OpenShiftException {
if (configuration == null) {
try {
configuration = new OpenShiftConfiguration();
} catch (IOException e) {
throw new OpenShiftException(e, "Failed to load OpenShift configuration file.");
}
}

Assert.notNull(clientId);
Assert.notNull(username);
Assert.notNull(password);
Expand All @@ -146,6 +153,7 @@ public IOpenShiftConnection getConnection(final String clientId, final String us
new UrlConnectionHttpClientBuilder()
.setCredentials(username, password, authKey, authIV)
.setSSLCertificateCallback(sslCertificateCallback)
.setConfigTimeout(configuration.getTimeout())
.client();
return getConnection(clientId, username, password, serverUrl, httpClient);
} catch (IOException e) {
Expand Down
Expand Up @@ -25,16 +25,20 @@

/**
* @author André Dietisheim
* @author Corey Daley
*/
public abstract class AbstractOpenshiftConfiguration implements IOpenShiftConfiguration {

protected static final String KEY_RHLOGIN = "default_rhlogin";
protected static final String KEY_LIBRA_SERVER = "libra_server";
protected static final String KEY_LIBRA_DOMAIN = "libra_domain";


protected static final String KEY_PASSWORD = "rhpassword";
protected static final String KEY_CLIENT_ID = "client_id";


protected static final String KEY_TIMEOUT = "timeout";

private static final Pattern QUOTED_REGEX = Pattern.compile("['\"]*([^'\"]+)['\"]*");
private static final char SINGLEQUOTE = '\'';

Expand Down Expand Up @@ -139,6 +143,10 @@ public String getLibraDomain() {
return removeQuotes(properties.getProperty(KEY_LIBRA_DOMAIN));
}

public Integer getTimeout() {
return Integer.parseInt(properties.getProperty(KEY_TIMEOUT));
}

protected String ensureIsSingleQuoted(String value) {
return SINGLEQUOTE + removeQuotes(value) + SINGLEQUOTE;
}
Expand Down
Expand Up @@ -14,10 +14,12 @@
import java.io.IOException;
import java.util.Properties;

import com.openshift.client.IHttpClient;
import com.openshift.client.OpenShiftException;

/**
* @author André Dietisheim
* @author Corey Daley
*/
public class DefaultConfiguration extends AbstractOpenshiftConfiguration {

Expand All @@ -33,6 +35,7 @@ protected Properties getProperties(File file, Properties defaultProperties) {
Properties properties = new Properties();
properties.put(KEY_LIBRA_SERVER, LIBRA_SERVER);
properties.put(KEY_LIBRA_DOMAIN, LIBRA_DOMAIN);
properties.put(KEY_TIMEOUT, "180000");
return properties;
}
}
Expand Up @@ -14,6 +14,7 @@

/**
* @author André Dietisheim
* @author Corey Daley
*/
public interface IOpenShiftConfiguration {

Expand All @@ -27,6 +28,8 @@ public interface IOpenShiftConfiguration {

public abstract String getLibraDomain();

public Integer getTimeout();

public abstract void setLibraDomain(String libraDomain);

public Properties getProperties();
Expand Down
Expand Up @@ -18,9 +18,12 @@

/**
* @author André Dietisheim
* @author Corey Daley
*/
public class SystemProperties extends AbstractOpenshiftConfiguration {

protected static final String KEY_OPENSHIFT_TIMEOUT = "express.timeout";

public SystemProperties(IOpenShiftConfiguration parentConfiguration) throws OpenShiftException, IOException {
super(parentConfiguration);
}
Expand All @@ -33,12 +36,15 @@ protected Properties getProperties(File file, Properties defaultProperties) {
copySystemProperty(KEY_RHLOGIN, properties);
copySystemProperty(KEY_PASSWORD, properties);
copySystemProperty(KEY_CLIENT_ID, properties);
copySystemProperty(KEY_OPENSHIFT_TIMEOUT, properties);
return properties;
}

private void copySystemProperty(String key, Properties properties) {
Object value = System.getProperties().get(key);
if (value != null) {
if (key.equals(KEY_OPENSHIFT_TIMEOUT) && value != null) {
properties.put(KEY_TIMEOUT, value);
} else if (value != null) {
properties.put(key, value);
}
}
Expand Down
Expand Up @@ -84,7 +84,6 @@ public RestResponse request(Link link, int timeout, IMediaType mediaType, List<P
* @see IHttpClient#SYSPROP_DEFAULT_CONNECT_TIMEOUT
* @see IHttpClient#SYSPROP_DEFAULT_READ_TIMEOUT
* @see IHttpClient#SYSPROP_OPENSHIFT_CONNECT_TIMEOUT
* @see IHttpClient#DEFAULT_CONNECT_TIMEOUT
* @see IHttpClient#DEFAULT_READ_TIMEOUT
*/
public RestResponse request(Link link, int timeout, IMediaType mediaType, List<Parameter> urlPathParameters,
Expand Down
Expand Up @@ -32,6 +32,7 @@
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import com.openshift.client.configuration.OpenShiftConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -47,6 +48,7 @@
/**
* @author Andre Dietisheim
* @author Nicolas Spano
* @author Corey Daley
*/
public class UrlConnectionHttpClient implements IHttpClient {

Expand All @@ -60,6 +62,7 @@ public class UrlConnectionHttpClient implements IHttpClient {
protected String acceptedMediaType;
protected String acceptedVersion;
protected ISSLCertificateCallback sslAuthorizationCallback;
protected Integer configTimeout;

public UrlConnectionHttpClient(
String username, String password, String userAgent, String acceptedMediaType, String version) {
Expand All @@ -68,11 +71,11 @@ public UrlConnectionHttpClient(

public UrlConnectionHttpClient(
String username, String password, String userAgent, String acceptedMediaType, String version, String authKey, String authIV) {
this(username, password, userAgent, acceptedMediaType, version, authKey, authIV, null);
this(username, password, userAgent, acceptedMediaType, version, authKey, authIV, null,null);
}

public UrlConnectionHttpClient(String username, String password, String userAgent, String acceptedMediaType,
String version, String authKey, String authIV, ISSLCertificateCallback callback) {
String version, String authKey, String authIV, ISSLCertificateCallback callback, Integer configTimeout) {
this.username = username;
this.password = password;
this.userAgent = userAgent;
Expand All @@ -81,6 +84,7 @@ public UrlConnectionHttpClient(String username, String password, String userAgen
this.authKey = authKey;
this.authIV = authIV;
this.sslAuthorizationCallback = callback;
this.configTimeout = configTimeout;
}

@Override
Expand Down Expand Up @@ -297,33 +301,24 @@ private void setupTrustManagerCallback(HttpsURLConnection connection) {
}

private void setConnectTimeout(int timeout, URLConnection connection) {
connection.setConnectTimeout(
getTimeout(
timeout,
getSystemPropertyInteger(SYSPROP_OPENSHIFT_CONNECT_TIMEOUT),
getSystemPropertyInteger(SYSPROP_DEFAULT_CONNECT_TIMEOUT),
DEFAULT_CONNECT_TIMEOUT));
if (getTimeout(timeout) != NO_TIMEOUT) {
connection.setConnectTimeout(getTimeout(timeout));
}
}

private void setReadTimeout(int timeout, URLConnection connection) {
connection.setReadTimeout(
getTimeout(
timeout,
getSystemPropertyInteger(SYSPROP_OPENSHIFT_READ_TIMEOUT),
getSystemPropertyInteger(SYSPROP_DEFAULT_READ_TIMEOUT),
DEFAULT_READ_TIMEOUT));
if (getTimeout(timeout) != NO_TIMEOUT) {
connection.setReadTimeout(getTimeout(timeout));
}

}

private int getTimeout(int timeout, int openShiftTimeout, int systemPropertyTimeout, int defaultTimeout) {
if (timeout == NO_TIMEOUT) {
timeout = openShiftTimeout;
private int getTimeout(int timeout) {
if (timeout == NO_TIMEOUT) {
timeout = systemPropertyTimeout;
if (timeout == NO_TIMEOUT) {
timeout = defaultTimeout;
if (configTimeout != null) {
timeout = this.configTimeout;
}
}
}
return timeout;
}

Expand Down
Expand Up @@ -15,6 +15,7 @@

/**
* @author André Dietisheim
* @author Corey Daley
*/
public class UrlConnectionHttpClientBuilder {

Expand All @@ -25,6 +26,7 @@ public class UrlConnectionHttpClientBuilder {
private String authIV;
private String acceptedMediaType;
private String version;
private Integer configTimeout;
private ISSLCertificateCallback callback;

public UrlConnectionHttpClientBuilder setUserAgent(String userAgent) {
Expand All @@ -43,6 +45,10 @@ public UrlConnectionHttpClientBuilder setCredentials(String username, String pas
this.authIV = authIV;
return this;
}
public UrlConnectionHttpClientBuilder setConfigTimeout (Integer configTimeout) {
this.configTimeout = configTimeout;
return this;
}

public UrlConnectionHttpClientBuilder setAcceptMediaType(String mediaType) {
this.acceptedMediaType = mediaType;
Expand All @@ -61,6 +67,6 @@ public UrlConnectionHttpClientBuilder setVersion(String version) {

public IHttpClient client() {
return new UrlConnectionHttpClient(
username, password, userAgent, acceptedMediaType, version, authKey, authIV, callback);
username, password, userAgent, acceptedMediaType, version, authKey, authIV, callback, configTimeout);
}
}
@@ -0,0 +1,46 @@
package com.openshift.client.fakes;

import com.openshift.client.OpenShiftException;
import com.openshift.client.configuration.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;

/**
* @author Corey Daley
*/
public class OpenShiftConfigurationFake extends AbstractOpenshiftConfiguration {
public OpenShiftConfigurationFake(final String systemConfigurationTimeout, final String userConfigurationTimeout, final String systemPropertiesTimeout) throws FileNotFoundException, IOException, OpenShiftException {
super(new SystemPropertiesFake(
new UserConfigurationFake(
new SystemConfigurationFake(
new DefaultConfiguration()){
//SystemConfigurationFake
protected void init(Properties properties) {
if (systemConfigurationTimeout != null) {
properties.put(KEY_TIMEOUT, systemConfigurationTimeout);
}
}
}){
//UserConfigurationFake
protected void initFile(Writer writer) throws IOException {
if (userConfigurationTimeout != null) {
writer.append(KEY_TIMEOUT).append('=').append(userConfigurationTimeout).append('\n');
}
}
}){
//SystemPropertiesFake
@Override
protected Properties getProperties(File file, Properties defaultProperties) {
Properties properties = new Properties(defaultProperties);
if (systemPropertiesTimeout != null) {
properties.setProperty(KEY_TIMEOUT,systemPropertiesTimeout);
}
return properties;
}
});
}
}
Expand Up @@ -44,6 +44,7 @@ protected PayLoadReturningHttpClientFake(OpenShiftTestConfiguration configuratio
version,
null,
null,
null,
null);
}

Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/openshift/client/fakes/SystemPropertiesFake.java
@@ -0,0 +1,16 @@
package com.openshift.client.fakes;

import com.openshift.client.OpenShiftException;
import com.openshift.client.configuration.IOpenShiftConfiguration;
import com.openshift.client.configuration.SystemProperties;

import java.io.IOException;

/**
* @author Corey Daley
*/
public class SystemPropertiesFake extends SystemProperties {
public SystemPropertiesFake(IOpenShiftConfiguration parentConfiguration) throws OpenShiftException, IOException {
super(parentConfiguration);
}
}