Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// ------------------------------------------------------------------------------
// Copyright (c) 2017 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ------------------------------------------------------------------------------

package com.microsoft.graph.core;

public class DefaultConnectionConfig implements IConnectionConfig{

/**
* Default connect timeout
*/
private static int DEFAULT_CONNECT_TIMEOUT_MS = 30_000;

/**
* Default connection read timeout
*/
private static int DEFAULT_READ_TIMEOUT_MS = 30_000;

/**
* Gets the connect timeout
*
* @return the timeout in milliseconds
*/
@Override
public int getConnectTimeout() {
return DEFAULT_CONNECT_TIMEOUT_MS;
}

/**
* Sets the connect timeout
*
* @param connectTimeoutValue Connect timeout in milliseconds to be set to.
*/
@Override
public void setConnectTimeout(int connectTimeoutValue) {
DEFAULT_CONNECT_TIMEOUT_MS = connectTimeoutValue;
}

/**
* Gets the read timeout
*
* @return the timeout in milliseconds
*/
@Override
public int getReadTimeout() {
return DEFAULT_READ_TIMEOUT_MS;
}

/**
* Sets the connect timeout
*
* @param readTimeoutValue Read timeout in milliseconds to be set to.
*/
@Override
public void setReadTimeout(int readTimeoutValue) {
DEFAULT_READ_TIMEOUT_MS = readTimeoutValue;
}

}
54 changes: 54 additions & 0 deletions src/main/java/com/microsoft/graph/core/IConnectionConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// ------------------------------------------------------------------------------
// Copyright (c) 2017 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ------------------------------------------------------------------------------

package com.microsoft.graph.core;

public interface IConnectionConfig {

/**
* Gets the connect timeout
*
* @return the timeout in milliseconds
*/
int getConnectTimeout();

/**
* Sets the connect timeout
*
* @param connectTimeoutValue Connect timeout in milliseconds to be set to.
*/
void setConnectTimeout(int connectTimeoutValue);

/**
* Gets the read timeout
*
* @return the timeout in milliseconds
*/
int getReadTimeout();

/**
* Sets the connect timeout
*
* @param readTimeoutValue Read timeout in milliseconds to be set to.
*/
void setReadTimeout(int readTimeoutValue);
}
34 changes: 34 additions & 0 deletions src/main/java/com/microsoft/graph/http/DefaultHttpProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.microsoft.graph.concurrency.IExecutors;
import com.microsoft.graph.concurrency.IProgressCallback;
import com.microsoft.graph.core.ClientException;
import com.microsoft.graph.core.DefaultConnectionConfig;
import com.microsoft.graph.core.IConnectionConfig;
import com.microsoft.graph.logger.ILogger;
import com.microsoft.graph.logger.LoggerLevel;
import com.microsoft.graph.options.HeaderOption;
Expand Down Expand Up @@ -89,6 +91,11 @@ public class DefaultHttpProvider implements IHttpProvider {
* The connection factory
*/
private IConnectionFactory connectionFactory;

/**
* The connection config
*/
private IConnectionConfig connectionConfig;

/**
* Creates the DefaultHttpProvider
Expand Down Expand Up @@ -232,6 +239,11 @@ private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRe
final URL requestUrl = request.getRequestUrl();
logger.logDebug("Starting to send request, URL " + requestUrl.toString());
final IConnection connection = connectionFactory.createFromRequest(request);
if(this.connectionConfig == null) {
this.connectionConfig = new DefaultConnectionConfig();
}
connection.setConnectTimeout(connectionConfig.getConnectTimeout());
connection.setReadTimeout(connectionConfig.getReadTimeout());

try {
logger.logDebug("Request Method " + request.getHttpMethod().toString());
Expand Down Expand Up @@ -479,4 +491,26 @@ public IExecutors getExecutors() {
public IAuthenticationProvider getAuthenticationProvider() {
return authenticationProvider;
}


/**
* Get connection config for read and connect timeout in requests
*
* @return Connection configuration to be used for timeout values
*/
public IConnectionConfig getConnectionConfig() {
if(this.connectionConfig == null) {
this.connectionConfig = new DefaultConnectionConfig();
}
return connectionConfig;
}

/**
* Set connection config for read and connect timeout in requests
*
* @param connectionConfig Connection configuration to be used for timeout values
*/
public void setConnectionConfig(IConnectionConfig connectionConfig) {
this.connectionConfig = connectionConfig;
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/microsoft/graph/http/IConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,18 @@ public interface IConnection {
* @param length the length of content
*/
void setContentLength(final int length);

/**
* Set the connect timeout on the connection
*
* @param connectTimeoutMilliseconds the connection timeout in milliseconds
*/
void setConnectTimeout(final int connectTimeoutMilliseconds);

/**
* Set the read timeout on the connection
*
* @param readTimeoutMilliseconds the read timeout in milliseconds
*/
void setReadTimeout(final int readTimeoutMilliseconds);
}
17 changes: 17 additions & 0 deletions src/main/java/com/microsoft/graph/http/IHttpProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.microsoft.graph.concurrency.ICallback;
import com.microsoft.graph.core.ClientException;
import com.microsoft.graph.core.IConnectionConfig;
import com.microsoft.graph.serializer.ISerializer;

/**
Expand All @@ -37,6 +38,22 @@ public interface IHttpProvider {
* @return the serializer for this provider
*/
ISerializer getSerializer();

/**
* Get connection config for read and connect timeout in requests
*
* @return Connection configuration to be used for timeout values
*
*/
public IConnectionConfig getConnectionConfig();

/**
* Set connection config for read and connect timeout in requests
*
* @param connectionConfig Connection configuration to be used for timeout values
*
*/
public void setConnectionConfig(IConnectionConfig connectionConfig);

/**
* Sends the HTTP request asynchronously
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/microsoft/graph/http/UrlConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@
*/
public class UrlConnection implements IConnection {

/**
* Default connection read timeout
*/
private static final int DEFAULT_CONNECTION_READ_TIMEOUT_MS = 30_000;
/**
* Default connect timeout
*/
private static final int DEFAULT_CONNECT_TIMEOUT_MS = 30_000;
/**
* The backing HTTP URL connection instance
*/
Expand All @@ -71,8 +63,6 @@ public UrlConnection(final IHttpRequest request) throws IOException {
}

connection.setUseCaches(request.getUseCaches());
connection.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MS);
connection.setReadTimeout(DEFAULT_CONNECTION_READ_TIMEOUT_MS);

try {
connection.setRequestMethod(request.getHttpMethod().toString());
Expand Down Expand Up @@ -164,6 +154,16 @@ public String getRequestMethod() {
public void setContentLength(final int length) {
connection.setFixedLengthStreamingMode(length);
}

@Override
public void setReadTimeout(final int readTimeoutMilliseconds) {
connection.setReadTimeout(readTimeoutMilliseconds);
}

@Override
public void setConnectTimeout(final int connectTimeoutMilliseconds) {
connection.setConnectTimeout(connectTimeoutMilliseconds);
}

/**
* Gets the response headers from an HTTP URL connection
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/microsoft/graph/http/MockConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public int getContentLength() {
public void setContentLength(int length) {
// noop
}

@Override
public void setConnectTimeout(int connectionTimeoutMilliseconds) {
// noop
}

@Override
public void setReadTimeout(int readTimeoutMilliseconds) {
// noop
}

@Override
public Map<String, List<String>> getResponseHeaders() {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/microsoft/graph/http/MockHttpProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.microsoft.graph.concurrency.IExecutors;
import com.microsoft.graph.concurrency.MockExecutors;
import com.microsoft.graph.core.ClientException;
import com.microsoft.graph.core.DefaultConnectionConfig;
import com.microsoft.graph.core.IConnectionConfig;
import com.microsoft.graph.logger.ILogger;
import com.microsoft.graph.logger.MockLogger;
import com.microsoft.graph.serializer.ISerializer;
Expand All @@ -25,6 +27,7 @@ public class MockHttpProvider implements IHttpProvider {
private final IExecutors mExecutors;
private final ILogger mLogger;
private IConnectionFactory mConnectionFactory;
private IConnectionConfig connectionConfig;

@Override
public ISerializer getSerializer() {
Expand Down Expand Up @@ -109,4 +112,15 @@ public <Result, BodyType, DeserializeType> Result send(IHttpRequest request, Cla
void setConnectionFactory(final IConnectionFactory factory) {
mConnectionFactory = factory;
}

public IConnectionConfig getConnectionConfig() {
if(this.connectionConfig == null) {
this.connectionConfig = new DefaultConnectionConfig();
}
return this.connectionConfig;
}

public void setConnectionConfig(IConnectionConfig connectionConfig) {
this.connectionConfig = connectionConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.microsoft.graph.concurrency.IProgressCallback;
import com.microsoft.graph.core.ClientException;
import com.microsoft.graph.core.DefaultClientConfig;
import com.microsoft.graph.core.DefaultConnectionConfig;
import com.microsoft.graph.core.IConnectionConfig;
import com.microsoft.graph.http.DefaultHttpProvider;
import com.microsoft.graph.http.IHttpProvider;
import com.microsoft.graph.http.IHttpRequest;
Expand Down Expand Up @@ -190,6 +192,16 @@ public <Result, BodyType, DeserializeType> Result send(IHttpRequest request,
throws ClientException {
return null;
}

@Override
public IConnectionConfig getConnectionConfig() {
return null;
}

@Override
public void setConnectionConfig(IConnectionConfig connectionConfig) {
// do nothing
}
};
IGraphServiceClient client = GraphServiceClient //
.builder() //
Expand Down Expand Up @@ -222,6 +234,39 @@ public void testExecutorsCannotBeNull() {
public void testLoggerCannotBeNull() {
GraphServiceClient.builder().authenticationProvider(auth).logger(null);
}

@Test
public void connectionConfigTest() {
IAuthenticationProvider ap = new IAuthenticationProvider() {
@Override
public void authenticateRequest(IHttpRequest request) {
// do nothing
}
};
IGraphServiceClient client = GraphServiceClient.builder()
.authenticationProvider(ap)
.buildClient();
client.getHttpProvider().setConnectionConfig(new DefaultConnectionConfig());
assertEquals(30_000, client.getHttpProvider().getConnectionConfig().getConnectTimeout());
assertEquals(30_000, client.getHttpProvider().getConnectionConfig().getReadTimeout());
}

@Test
public void connectionConfigValuesChangeTest() {
IAuthenticationProvider ap = new IAuthenticationProvider() {
@Override
public void authenticateRequest(IHttpRequest request) {
// do nothing
}
};
IGraphServiceClient client = GraphServiceClient.builder()
.authenticationProvider(ap)
.buildClient();
client.getHttpProvider().getConnectionConfig().setConnectTimeout(20_000);
client.getHttpProvider().getConnectionConfig().setReadTimeout(10_000);
assertEquals(20_000, client.getHttpProvider().getConnectionConfig().getConnectTimeout());
assertEquals(10_000, client.getHttpProvider().getConnectionConfig().getReadTimeout());
}

private static ILogger createLogger() {
return new ILogger() {
Expand Down